terça-feira, 30 de maio de 2023

COMUNICAÇÃO P2P COM LSM110A - RADIOLIB

        

O objetivo deste BLOG é demonstrar como é possível programar o módulo WISOL LSM110A via ARDUINO e assim utilizá-lo como OPENCPU.

BETA BETA BETA BETA

Será testado o exemplo LoRa_P2P de jgromes/RadioLib: Universal wireless communication library for embedded devices (github.com) que permite fazer a comunicação P2P entre 2 LSM110A, na frequência de 868000000.

A tecnologia LoRa® permite estabelecer comunicação diretamente entre dois dispositivos, dispensando o gateway. Com um pouco de esforço de desenvolvimento, é possível implementar uma rede de comunicação. Essa arquitetura é comumente chamada no mercado de peer-to-peer ou P2P.

LSM110A Starter KIT
Módulo

O LSM110A é um módulo de última geração que integra o STMicroelectronics STM32WL. É muito menos consumo atual para o dispositivo IoT para estender a vida útil da bateria. E, também suporta ambas as tecnologias – Sigfox e LoRa – com o próprio módulo LSM110A.

Você pode trocar Sigfox ou LoRa com ele para que você também possa reduzir o custo. É altamente otimizado para solução de IoT (Alto Consumo de Energia, Baixo Custo)

BREAKOUT para testes

Esquema Elétrico - últimas correções





PLACA MONTADA



CONEXÃO COM ST-LINK V2 E UART




ARDUINO

O que é Arduino? Se você sabe pouco sobre Arduino, por favor dê uma olhada abaixo:

Você conhece Arduino. Instale o IDE primeiro:
https://www.arduino.cc/en/Main/Software

LSM110A e Arduino (STM32DUINO)

LSM110A é baseado em STM32WL55.  No Arduino STM32 existe este core.

Como instalar Arduino STM32? adicionar em Arquivo-->Preferências-->URLs adicionais

https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json


BSP

2.4.0

Adicione ao Arduino IDE:


Pronto


Altere Placa para STM32WL series


Modelo Generic WL55JCLx



Baixe a LIB RadioLib e instale no Arduino


Deve-se gravar 2 LSM110A para que haja a comunicação P2P.  Um pacote "payload" será transmitido entre eles.

STM32 CUBE



Segue código - STM32WLx_Transmit_Interrupt.ino

/* RadioLib STM32WLx Transmit with Interrupts Example This example transmits LoRa packets with one second delays between them. Each packet contains up to 256 bytes of data, in the form of: - Arduino String - null-terminated char array (C-string) - arbitrary binary data (byte array) For default module settings, see the wiki page https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem For full API reference, see the GitHub Pages https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> // no need to configure pins, signals are routed to the radio internally STM32WLx radio = new STM32WLx_Module(); // set RF switch configuration for Nucleo WL55JC1 // NOTE: other boards may be different! static const uint32_t rfswitch_pins[] = {PC3, PC4, PC5}; static const Module::RfSwitchMode_t rfswitch_table[] = { {STM32WLx::MODE_IDLE, {LOW, LOW, LOW}}, {STM32WLx::MODE_RX, {HIGH, HIGH, LOW}}, {STM32WLx::MODE_TX_LP, {HIGH, HIGH, HIGH}}, {STM32WLx::MODE_TX_HP, {HIGH, LOW, HIGH}}, END_OF_MODE_TABLE, }; // save transmission state between loops int transmissionState = RADIOLIB_ERR_NONE; void setup() { Serial.begin(9600); // set RF switch control configuration // this has to be done prior to calling begin() radio.setRfSwitchTable(rfswitch_pins, rfswitch_table); // initialize STM32WL with default settings, except frequency Serial.print(F("[STM32WL] Initializing ... ")); int state = radio.begin(868.0); if (state == RADIOLIB_ERR_NONE) { Serial.println(F("success!")); } else { Serial.print(F("failed, code ")); Serial.println(state); while (true); } // set appropriate TCXO voltage for Nucleo WL55JC1 state = radio.setTCXO(1.7); if (state == RADIOLIB_ERR_NONE) { Serial.println(F("success!")); } else { Serial.print(F("failed, code ")); Serial.println(state); while (true); } // set the function that will be called // when packet transmission is finished radio.setDio1Action(setFlag); // start transmitting the first packet Serial.print(F("[STM32WL] Sending first packet ... ")); // you can transmit C-string or Arduino string up to // 256 characters long transmissionState = radio.startTransmit("Hello World!"); // you can also transmit byte array up to 256 bytes long /* byte byteArr[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; state = radio.startTransmit(byteArr, 8); */ } // flag to indicate that a packet was sent volatile bool transmittedFlag = false; // this function is called when a complete packet // is transmitted by the module // IMPORTANT: this function MUST be 'void' type // and MUST NOT have any arguments! void setFlag(void) { // we sent a packet, set the flag transmittedFlag = true; } void loop() { // check if the previous transmission finished if(transmittedFlag) { // reset flag transmittedFlag = false; if (transmissionState == RADIOLIB_ERR_NONE) { // packet was successfully sent Serial.println(F("transmission finished!")); // NOTE: when using interrupt-driven transmit method, // it is not possible to automatically measure // transmission data rate using getDataRate() } else { Serial.print(F("failed, code ")); Serial.println(transmissionState); } // clean up after transmission is finished // this will ensure transmitter is disabled, // RF switch is powered down etc. radio.finishTransmit(); // wait a second before transmitting again delay(1000); // send another one Serial.print(F("[STM32WL] Sending another packet ... ")); // you can transmit C-string or Arduino string up to // 256 characters long transmissionState = radio.startTransmit("Hello World!"); // you can also transmit byte array up to 256 bytes long /* byte byteArr[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; int state = radio.startTransmit(byteArr, 8); */ } }

Segue código final - STM32WLx_Receive_Interrupt.ino

/* RadioLib STM32WLx Receive with Interrupts Example This example listens for LoRa transmissions and tries to receive them. Once a packet is received, an interrupt is triggered. To successfully receive data, the following settings have to be the same on both transmitter and receiver: - carrier frequency - bandwidth - spreading factor - coding rate - sync word This example assumes Nucleo WL55JC1 is used. For other Nucleo boards or standalone STM32WL, some configuration such as TCXO voltage and RF switch control may have to be adjusted. For default module settings, see the wiki page https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem For full API reference, see the GitHub Pages https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> // no need to configure pins, signals are routed to the radio internally STM32WLx radio = new STM32WLx_Module();
// set RF switch configuration for Nucleo WL55JC1 // NOTE: other boards may be different! static const uint32_t rfswitch_pins[] = {PB12, PC13, PC5}; //PC5 NOT USED static const Module::RfSwitchMode_t rfswitch_table[] = { {STM32WLx::MODE_IDLE, {LOW, LOW, LOW}}, //THIRD COLUMM NOT USED {STM32WLx::MODE_RX, {HIGH, LOW, LOW}}, //THIRD COLUMM NOT USED {STM32WLx::MODE_TX_LP, {HIGH, HIGH, HIGH}}, //THIRD COLUMM NOT USED {STM32WLx::MODE_TX_HP, {HIGH, HIGH, HIGH}}, //THIRD COLUMM NOT USED END_OF_MODE_TABLE, };



void setup() { Serial.begin(9600); // set RF switch control configuration // this has to be done prior to calling begin() radio.setRfSwitchTable(rfswitch_pins, rfswitch_table); // initialize STM32WL with default settings, except frequency Serial.print(F("[STM32WL] Initializing ... ")); int state = radio.begin(868.0); if (state == RADIOLIB_ERR_NONE) { Serial.println(F("success!")); } else { Serial.print(F("failed, code ")); Serial.println(state); while (true); } // set appropriate TCXO voltage for Nucleo WL55JC1 state = radio.setTCXO(1.7); if (state == RADIOLIB_ERR_NONE) { Serial.println(F("success!")); } else { Serial.print(F("failed, code ")); Serial.println(state); while (true); } // set the function that will be called // when new packet is received radio.setDio1Action(setFlag); // start listening for LoRa packets Serial.print(F("[STM32WL] Starting to listen ... ")); state = radio.startReceive(); if (state == RADIOLIB_ERR_NONE) { Serial.println(F("success!")); } else { Serial.print(F("failed, code ")); Serial.println(state); while (true); } // if needed, 'listen' mode can be disabled by calling // any of the following methods: // // radio.standby() // radio.sleep() // radio.transmit(); // radio.receive(); // radio.readData(); // radio.scanChannel(); } // flag to indicate that a packet was received volatile bool receivedFlag = false; // this function is called when a complete packet // is received by the module // IMPORTANT: this function MUST be 'void' type // and MUST NOT have any arguments! void setFlag(void) { // we got a packet, set the flag receivedFlag = true; } void loop() { // check if the flag is set if(receivedFlag) { // reset flag receivedFlag = false; // you can read received data as an Arduino String String str; int state = radio.readData(str); // you can also read received data as byte array /* byte byteArr[8]; int state = radio.readData(byteArr, 8); */ if (state == RADIOLIB_ERR_NONE) { // packet was successfully received Serial.println(F("[STM32WL] Received packet!")); // print data of the packet Serial.print(F("[STM32WL] Data:\t\t")); Serial.println(str); // print RSSI (Received Signal Strength Indicator) Serial.print(F("[STM32WL] RSSI:\t\t")); Serial.print(radio.getRSSI()); Serial.println(F(" dBm")); // print SNR (Signal-to-Noise Ratio) Serial.print(F("[STM32WL] SNR:\t\t")); Serial.print(radio.getSNR()); Serial.println(F(" dB")); } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { // packet was received, but is malformed Serial.println(F("CRC error!")); } else { // some other error occurred Serial.print(F("failed, code ")); Serial.println(state); } // put module back to listen mode radio.startReceive(); } }

Execução - Receive


Execução - Transmit


Montagem


Fontes:

Dúvidas

Sobre a SMARTCORE

A SmartCore fornece módulos para comunicação wireless, biometria, conectividade, rastreamento e automação.
Nosso portfólio inclui modem 2G/3G/4G/NB-IoT/Cat.M, satelital, módulos WiFi, Bluetooth, GNSS / GPS, Sigfox, LoRa, leitor de cartão, leitor QR code, mecanismo de impressão, mini-board PC, antena, pigtail, LCD, bateria, repetidor GPS e sensores.

Mais detalhes em www.smartcore.com.br

sexta-feira, 26 de maio de 2023

PROGRAMANDO WISOL LSM110A ( LoRaWAN CLASS A - OTAA) COM ARDUINO E ACESSANDO LoRaWAN TTN - MODO ASSÍNCRONO

  

O objetivo deste BLOG é demonstrar como é possível programar o módulo WISOL LSM110A via Arduino Framework e assim utilizá-lo como OPENCPU.

BETA BETA BETA BETA

Será publicada uma sequência de bytes 
{0xde, 0xad, 0xbe, 0xef,0xde, 0xad, 0xbe, 0xef,0xde, 0xad, 0xbe} a cada 60 segundos na rede LoRaWAN TTN (CLASS A, OTAA), no modo ASSÍNCRONO, ou seja, é testado o exemplo SIMPLEASYNC.

Esta biblioteca fornece suporte para comunicação LoRaWAN usando o microcontrolador STM32WL55 (com rádio LoRa embutido) dentro do ambiente de desenvolvimento Arduino (juntamente com o núcleo stm32duino com versão 2.4.0 ou posterior).

A mesma foi modificada para funcionar no LSM110A.

BETA BETA BETA BETA

LSM110A Starter KIT
Módulo

O LSM110A é um módulo de última geração que integra o STMicroelectronics STM32WL. É muito menos consumo atual para o dispositivo IoT para estender a vida útil da bateria. E, também suporta ambas as tecnologias – Sigfox e LoRa – com o próprio módulo LSM110A.

Você pode trocar Sigfox ou LoRa com ele para que você também possa reduzir o custo. É altamente otimizado para solução de IoT (Alto Consumo de Energia, Baixo Custo)

BREAKOUT para testes


Esquema Elétrico - últimas correções





PLACA MONTADA



CONEXÃO COM ST-LINK V2 E UART


TTN
The Thing Network

A Rede de Coisas (TTN) é uma iniciativa iniciada pela sociedade civil holandesa. O objetivo é ter redes LoRaWAN instaladas em todas as cidades do mundo. Ao interconectar essas redes locais, a TTN quer construir uma infra-estrutura mundial para facilitar uma Internet das Coisas (IoT) pública.

A The Things Network (TTN) é o servidor IoT na nuvem utilizado nesse projeto. É um dos servidores gratuitos para LoRaWAN mais utilizados, com mais de 90 mil desenvolvedores, mais de 9 mil gateways de usuários conectados à rede ao redor do mundo e mais de 50 mil aplicações em funcionamento.

A TTN comercializa nós e gateways LoRa e provê treinamento individual e coletivo para empresas e desenvolvedores que desejam utilizar o LoRa. Possui uma comunidade bem ativa nos fóruns, sempre colaborando e ajudando a resolver problemas, além de prover diversos meios de integrar a TTN com a aplicação que se deseja usar. Possui integração nativa com diversas aplicações como: Cayenne, Hypertext Transfer Protocol (HTTP), permitindo ao usuário realizar uplink para um gateway e receber downlink por HTTP, OpenSensors e EVRYTHNG . Caso o usuário queira criar sua própria aplicação, a TTN disponibiliza Application Programming Interface (API) para uso com Message Queuing Telemetry Transport (MQTT) e diversos Software Developer Kits (SDK) para uso com as linguagens Python, Java , Node.Js , NODE-RED e Go

A rede TTN utiliza o protocolo LoRaWAN objetivando uma cobertura em longo alcance para os dispositivos da rede, caracterizando-a assim com uma Wide Area Network (WAN). Devido ao baixo consumo de energia e ao uso da tecnologia LoRa, é chamada de LPWAN (Low Power Wide Area Network). O grande diferencial da TTN é seu estabelecimento como uma rede aberta (open-source) e colaborativa (crowd-sourced), onde qualquer usuário pode contribuir instalando um gateway em sua residência.

Os elementos da TTN são classificados como: 

• Endpoints (nós): Os dispositivos responsáveis pela camada de sensoriamento da rede, o endpoint LoRaWAN. Podem coletar informações através de sensores e também acionar dispositivos/máquinas via atuadores. São configurados através de uma das três classes distintas do protocolo LaRaWAN; 

• Gateways: Elementos responsáveis por concentrar e processar as informações enviadas pelos endpoints. Os gateways em geral estão conectados a internet, seja por WiFi/Ethernet ou 3G/4G em locais remotos. Mesmo que uma mesma rede LoRaWAN tenha diferentes objetivos, baseados em aplicações distintas, os gateways possuem o objetivo comum de fornecer a maior área de cobertura possível; 

• Aplicações: Conectar e interligar os diferentes dispositivos da rede TTN para o fornecimento de informações gerais sobre a coleta de dados dos dispositivos.

ARDUINO

O que é Arduino? Se você sabe pouco sobre Arduino, por favor dê uma olhada abaixo:

Você conhece Arduino. Instale o IDE primeiro:
https://www.arduino.cc/en/Main/Software

LSM110A e Arduino (STM32DUINO)

LSM110A é baseado em STM32WL55.  No Arduino STM32 existe este core.

Como instalar Arduino STM32? adicionar em Arquivo-->Preferências-->URLs adicionais

https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json


BSP

2.4.0

LIB STM32LoRaWAN - EXCELENTE!

No github


Baixe em formato ZIP


Adicione ao Arduino IDE:


Pronto


Altere Placa para STM32WL series


Modelo Generic WL55JCLx


Ok, o ambiente de desenvolvimento está pronto. Abra o  Basic.ino  e configure 3 parâmetros para LoRaWAN_OTAA os quais deve ser obtidos no servidor TTN.

bool connected = modem.joinOTAA(/* AppEui */ "0000000000000099", /* AppKey */ "FF9F138B40180AA45D6846E0A0146954", /* DevEui */ "0080E115051FD80A");



Neste exemplo, será transmitido para LoRaWAN (TTN) os dados

Altere em radio_board_if.c 
em
C:\Users\Usuario\Documents\Arduino\libraries\STM32LoRaWAN-main\src\BSP

// Supported TX modes (LP/HP or both) #if !defined(LORAWAN_TX_CONFIG) #define LORAWAN_TX_CONFIG RBI_CONF_RFO_HP //MIGUEL#define LORAWAN_TX_CONFIG RBI_CONF_RFO_LP_HP #endif #if !defined(LORAWAN_RFSWITCH_PINS) #define LORAWAN_RFSWITCH_PINS PB12,PC13 #define LORAWAN_RFSWITCH_PIN_COUNT 2 #define LORAWAN_RFSWITCH_OFF_VALUES LOW,LOW #define LORAWAN_RFSWITCH_RX_VALUES HIGH,LOW #define LORAWAN_RFSWITCH_RFO_LP_VALUES HIGH,HIGH #define LORAWAN_RFSWITCH_RFO_HP_VALUES HIGH,HIGH #endif











Baseado no...


...altere máscara de canais (Join, Uplink, Downlink)

Ver RegionAU915.c
C:\Users\Usuario\Documents\Arduino\libraries\STM32LoRaWAN-main\src\STM32CubeWL\LoRaWAN\Mac\Region

// Initialize channels default mask #if ( HYBRID_ENABLED == 1 ) RegionNvmGroup2->ChannelsDefaultMask[0] = HYBRID_DEFAULT_MASK0; RegionNvmGroup2->ChannelsDefaultMask[1] = HYBRID_DEFAULT_MASK1; RegionNvmGroup2->ChannelsDefaultMask[2] = HYBRID_DEFAULT_MASK2; RegionNvmGroup2->ChannelsDefaultMask[3] = HYBRID_DEFAULT_MASK3; RegionNvmGroup2->ChannelsDefaultMask[4] = HYBRID_DEFAULT_MASK4; RegionNvmGroup2->ChannelsDefaultMask[5] = 0x0000; #else RegionNvmGroup2->ChannelsDefaultMask[0] = 0xFF00; RegionNvmGroup2->ChannelsDefaultMask[1] = 0x0000; RegionNvmGroup2->ChannelsDefaultMask[2] = 0x0000; RegionNvmGroup2->ChannelsDefaultMask[3] = 0x0000; RegionNvmGroup2->ChannelsDefaultMask[4] = 0x00FF; RegionNvmGroup2->ChannelsDefaultMask[5] = 0x0000; #endif /* HYBRID_ENABLED == 1 */

Segue código final (alterado)

/** * This is a an example of the asynchronous API. This sketch is not * completely asynchronous (see ScheduledAsync.ino for that) and its * flow still follows the LoRa flow, but this sketch shows how some * background work can be done while waiting for the LoRa library to * complete its work. * * Revised BSD License - https://spdx.org/licenses/BSD-3-Clause.html */ #include <STM32LoRaWAN.h> STM32LoRaWAN modem; static const unsigned long TX_INTERVAL = 60000; /* ms */ unsigned long last_tx = 0; static const unsigned long BLINK_INTERVAL = 250; /* ms */ unsigned long last_blink = 0; void background_work() { if (millis() - last_blink > BLINK_INTERVAL) { last_blink = millis(); digitalWrite(PA8, !digitalRead(PA8)); } } void wait_for_idle() { while (modem.busy()) { // Call maintain() so the lora library can do any // pending background work too. modem.maintain(); background_work(); } } void setup() { pinMode(PA8, OUTPUT); Serial.begin(115200); Serial.println("Start"); modem.begin(AU915);
modem.setAppEui("0000000000000099"); modem.setAppKey("FF9F138B40180AA45D6846E0A0146954"); modem.setDevEui("0XX0EXX50XXFXX0A"); while(1) { // Configure join method by (un)commenting the right method // calls, and fill in credentials in those method calls. modem.joinOTAAAsync(); //modem.setDevAddr("00000000"); //modem.setNwkSKey("00000000000000000000000000000000"); //modem.setAppSKey("00000000000000000000000000000000"); //modem.joinABPAsync(); wait_for_idle(); if (modem.connected()) { Serial.println("Joined"); return; } else { Serial.println("Join failed"); //while (true) /* infinite loop */; delay(2000); } } } void send_packet() { uint8_t payload[] = {0xde, 0xad, 0xbe, 0xef,0xde, 0xad, 0xbe, 0xef,0xde, 0xad, 0xbe}; modem.setPort(10); modem.beginPacket(); modem.write(payload, sizeof(payload));
//confirmed package = true if (modem.endPacketAsync(true) == sizeof(payload)) { Serial.println("Queued packet"); } else { Serial.println("Failed to send packet"); return; } wait_for_idle(); Serial.println("Sent packet"); if (modem.available()) { Serial.print("Received packet on port "); Serial.print(modem.getDownlinkPort()); Serial.print(":"); while (modem.available()) { uint8_t b = modem.read(); Serial.print(" "); Serial.print(b >> 4, HEX); Serial.print(b & 0xF, HEX); } Serial.println(); } } void loop() { background_work(); if (!last_tx || millis() - last_tx > TX_INTERVAL) { send_packet(); last_tx = millis(); } }














Montagem


Compilando - Modo Debug



Gravador

"Instale" o STM32 Cube Programmer, o Arduino (STM32 PACKAGE) irá reconhecê-lo e então utilizá-lo para programar o LSM110A. O Kit LSM110A possui um "gravador" ST-LINK embarcado.

Neste blog foi utilizado o BREAKOUT e um ST-LINK-V2.



Uma vez gravado o Software, o LSM110A fará o JOIN na LoRaWAN enviará o pacote a cada 60 segundos de forma assíncrona.



UPLINK message
PA2 - TXD / PA3- RXD

DOWNLINK message



Fontes:


Dúvidas

Sobre a SMARTCORE

A SmartCore fornece módulos para comunicação wireless, biometria, conectividade, rastreamento e automação.
Nosso portfólio inclui modem 2G/3G/4G/NB-IoT/Cat.M, satelital, módulos WiFi, Bluetooth, GNSS / GPS, Sigfox, LoRa, leitor de cartão, leitor QR code, mecanismo de impressão, mini-board PC, antena, pigtail, LCD, bateria, repetidor GPS e sensores.

Mais detalhes em www.smartcore.com.br