Data structure lost

Viewing 3 reply threads
  • Author
    Posts
    • #18368
      Javi

        Hello, I am using four E22-900T22D, three as transmitters and one as receiver, each transmitter sends a data structure every 2 seconds:

           struct Message {
             chartype[5] = "1";
             byte amp1[5];
             byte volt1[6];
           } message;
        
        struct Message {
             chartype[5] = "2";
             byte amp2[5];
             byte volt2[6];
           } message;
        
        struct Message {
             chartype[5] = "3";
             byte amp3[5];
             byte volt3[6];
           } message;

        The receiver receives the data from the 3 senders well, but there are times that I lose messages.

        I want to use the function, ResponseContainer rs = e32ttl.receiveMessageUntil(); to read each structure individually.

        Can I use the function, ResponseContainer rs = e32ttl.receiveMessageUntil(); to receive a structure or only be used to receive a string?

        Thank you.

      • #18372
        Renzo Mischianti
        Keymaster

          Hi Javi,
          You can use the specified method, check the article of e32 module as reference
          Ebyte LoRa E32 device for Arduino, esp32 or esp8266: power saving and sending structured data – Part 5

          receiveMessageUntil is only for string.

          But attach your code, we try to check better.

          Bye Renzo

        • #18373
          Javi

            Thank you very much Renzo, I send you the code of the emitters ( 2 arduino nano) and receiver, in the emitters I use arduino nano and in the receiver a Mega:

            Emitter 1 code (Nano):

            #include "Arduino.h"
            #include "LoRa_E22.h"
            
            #include <SoftwareSerial.h>
            SoftwareSerial mySerial(4, 5); // Arduino RX <-- e22 TX, Arduino TX --> e22 RX
            LoRa_E22 e22ttl(&mySerial, 3, 7, 6); // AUX M0 M1
            
            void setup() {
              
              Serial.begin(9600);
              delay(500);
            
              // Startup all pins and UART
              e22ttl.begin();
            
              Serial.println("Hi, I'm going to send message!");
            
              struct Message {
                char type[5] = "1";
                byte amp1[5];
                byte volt1[6];    
              } message;
            
               *(int*)(message.amp1)=100;
               *(int*)(message.volt1)=10;
            
              // Send message
              ResponseStatus rs = e22ttl.sendMessage(&message, sizeof(Message));
              // Check If there is some problem of succesfully send
              Serial.println(rs.getResponseDescription());
            }
            
            void loop() {
            
              delay(2000);
                struct Message {
                char type[5] = "1";
                byte amp1[5];
                byte volt1[6];    
              } message;
              
              if (*(int*)(message.amp1)>999){
              *(int*)(message.amp1)=10;
              }
              (*(int*)(message.amp1))++;
              
              if (*(int*)(message.volt1)>99){
              *(int*)(message.volt1)=10;
            }
              (*(int*)(message.volt1))++;
            
              // Send message
              ResponseStatus rs = e22ttl.sendMessage(&message, sizeof(Message));
              // Check If there is some problem of succesfully send
              Serial.println(rs.getResponseDescription());
            
            }

            //——————————————————————–

            Emitter 2 code (Nano):

            #include "Arduino.h"
            #include "LoRa_E22.h"
            
            #include <SoftwareSerial.h>
            SoftwareSerial mySerial(4, 5); // Arduino RX <-- e22 TX, Arduino TX --> e22 RX
            LoRa_E22 e22ttl(&mySerial, 3, 7, 6); // AUX M0 M1
            
            void setup() {
              
              Serial.begin(9600);
              delay(500);
            
              // Startup all pins and UART
              e22ttl.begin();
            
              Serial.println("Hi, I'm going to send message!");
            
              struct Message {
                char type[5] = "2";
                byte amp2[5];
                byte volt2[6];    
              } message;
            
               *(int*)(message.amp2)=100;
               *(int*)(message.volt2)=10;
            
              // Send message
              ResponseStatus rs = e22ttl.sendMessage(&message, sizeof(Message));
              // Check If there is some problem of succesfully send
              Serial.println(rs.getResponseDescription());
            }
            
            void loop() {
            
              delay(1900);
                struct Message {
                char type[5] = "2";
                byte amp2[5];
                byte volt2[6];    
              } message;
              
              if (*(int*)(message.amp2)>999){
              *(int*)(message.amp2)=10;
              }
              (*(int*)(message.amp2))++;
              
              if (*(int*)(message.volt2)>99){
              *(int*)(message.volt2)=10;
            }
              (*(int*)(message.volt2))++;
            
              // Send message
              ResponseStatus rs = e22ttl.sendMessage(&message, sizeof(Message));
              // Check If there is some problem of succesfully send
              Serial.println(rs.getResponseDescription());
            
            }
            
            //-----------------------------------------------------------------------

            Receiver code (Mega):

            #include "Arduino.h"
            #include "LoRa_E22.h"
            
            LoRa_E22 e22ttl(&Serial3, 3, 7, 6); // AUX M0 M1
            
            void setup() {
              
              Serial.begin(9600);
              delay(1000);
              // Startup all pins and UART
              e22ttl.begin();
            
            struct Message1 {
                byte amp1[5];
                byte volt1[6];
            };
            struct Message2 {
                byte amp2[5];
                byte volt2[6];
            };
            
            }
            //-------------------------------------------loop--------------------------------
            void loop() {
            
              struct Message1 {
                byte amp1[5];
                byte volt1[6];
            };
            struct Message2 {
                byte amp2[5];
                byte volt2[6];
            };
            
              // If something available
              if (e22ttl.available()>1) {
            
                char type[5];
                
                ResponseContainer rs = e22ttl.receiveInitialMessage(sizeof(type));
                // Put string in a char array (not needed)
                //    memcpy ( type, rs.data.c_str(), sizeof(type) );
                String typeStr = rs.data;
            
                Serial.println(typeStr);
                   
               if (typeStr == "1"){
                 ResponseStructContainer rsc = e22ttl.receiveMessage(sizeof(Message1));
                 struct Message1 message = *(Message1*) rsc.data;
            
                  Serial.println(*(int*)(message.amp1));
                  Serial.println(*(int*)(message.volt1));
            
                  rsc.close();
                  
                }else if (typeStr == "2"){
                  ResponseStructContainer rsc = e22ttl.receiveMessage(sizeof(Message2));
                  struct Message2 message = *(Message2*) rsc.data;
            
                  Serial.println(*(int*)(message.amp2));
                  Serial.println(*(int*)(message.volt2));
                 
                  rsc.close();
            
                }else{
                  Serial.println("Something goes wrong!!");
                }
              }
            }
          • #18377
            Renzo Mischianti
            Keymaster

              Hi Javi,
              It seems all ok.
              But change the size of int value to 2 byte.
              And type to 1 char

              struct Message {
                  char type = '1';
                  byte amp1[2];
                  byte volt1[2];    
              } message;

              And reduce the packet size to 64bytes so It’s more responsive

              configuration.OPTION.subPacketSetting = SPS_064_10;

              and enable the LBT to prevent network problem

              configuration.TRANSMISSION_MODE.enableLBT = LBT_ENABLED;

              Bye Renzo

          Viewing 3 reply threads
          • You must be logged in to reply to this topic.
          Exit mobile version