[solved] Air202 GSM how to publish data?

Hello!

I have such a GSM Air202 module.

Przechwytywanie
I connect to the panel for 1 second, but I can not publish anything.

What is the correct format for sending data?

This?
virtualWrite(2, 25.2, "temp", "c")

Thank you for the information.

Can you share the entire code you are using?

Yes, I can. All scripts are here.
https://github.com/openLuat/Luat_2G_RDA_8955

Here is the basic script with mqtt functions.
https://github.com/openLuat/Luat_2G_RDA_8955/blob/master/script_LuaTask/lib/mqtt.lua

what is the topic you are publishing to? have a look at this http://mydevices.com/cayenne/docs/cayenne-mqtt-api/

@vanihada Have a look at these two posts, I use a generic MQTT client in both and describe how it works.

1 Like

Thank you for the links from which I learned a lot. Of course, the topic will be continued.
If you are interested in the GSM Air202 module, I recommend visiting this site. https://www.elektroda.pl/rtvforum/viewtopic.php?t=3523979&highlight= I described there how to install softwares in Air202.

Thanks @vanihada for the link. were you able to add the device to cayenne?

Air202 is connected to cayenne, but has not published any data. I am at the stage of learning the chapter “MQTT Messaging Topics”, because I have a problem with formatting the data being sent.

this code might help you a bit.

#define dbg
// no need to add the below 3. 
char aux_str[100];
const char * __APN      = "";
const char * __usrnm    = "";
const char * __password = "";
int data1;

int counter = 0;
unsigned int Counter = 0;
unsigned long datalength, checksum, rLength;
unsigned short topiclength;
unsigned short topiclength2;
unsigned char topic[200];
char str[250];
unsigned char encodedByte;
int X;

unsigned short MQTTProtocolNameLength;
unsigned short MQTTClientIDLength;
unsigned short MQTTUsernameLength;
unsigned short MQTTPasswordLength;
unsigned short MQTTTopicLength;

//const char * MQTTHost = "m10.cloudmqtt.com";
const char * MQTTHost = "mqtt.mydevices.com";
//const char * MQTTPort = "15180";
const char * MQTTPort = "1883";
const char * MQTTClientID = "xxxx";
unsigned char MQTTTopic[200] = "v1/username/things/clientID/data/2";
//const char * MQTTTopic = "v1/eb58f900-4d71-11e8-bf56-db14f0c2b326/things/90536080-7eaf-11e8-9c05-61f9e9bc1eea/data/2";
const char * MQTTTopic2 = "SampleTopic2";
//const char * MQTTProtocolName = "MQIsdp";
const char * MQTTProtocolName = "MQTT";
const char MQTTLVL = 0x03;
const char MQTTFlags = 0xC2;
const unsigned int MQTTKeepAlive = 60;
//const char * MQTTUsername = "ejknrdnn";
//const char * MQTTPassword = "L1TRGif6EnCd";
const char * MQTTUsername = "xxxx;
const char * MQTTPassword = "yyyyy";
const char MQTTQOS = 0x00;
const char MQTTPacketID = 0x0001;

char sim800lreset = 4;

void setup()
{
  Serial.begin(115200);
  Serial2.begin(115200);

  pinMode(sim800lreset, OUTPUT);
  delay(3000);
  MQTTProtocolNameLength = strlen(MQTTProtocolName);



}

void loop()
{
  if (initTCP()) {
    MQTTConnect();
    readServerResponse("AT+CIPRXGET=2,1024", "OK", "ERROR", 10000);//receive data from n/w

    //MQTTsubscribe();
    //readServerResponse("AT+CIPRXGET=2,1024", "OK", "ERROR", 10000);//receive data from n/w



    while (1) {
      MQTTpublish();
      readServerResponse("AT+CIPRXGET=2,1024", "OK", "ERROR", 10000);//receive data from n/w
      Serial.println("Sending");
      Counter++;
      delay(5000);
    }

  }
}

int  MQTTConnect() {

  if (sendATcommand2("AT+CIPSEND", ">", "ERROR", 1000)) {
    Serial2.write(0x10);
    MQTTProtocolNameLength = strlen(MQTTProtocolName);
    MQTTClientIDLength = strlen(MQTTClientID);
    MQTTUsernameLength = strlen(MQTTUsername);
    MQTTPasswordLength = strlen(MQTTPassword);
    datalength = MQTTProtocolNameLength + 2 + 4 + MQTTClientIDLength + 2 + MQTTUsernameLength + 2 + MQTTPasswordLength + 2;
    X = datalength;
    do
    {
      encodedByte = X % 128;
      X = X / 128;
      // if there are more data to encode, set the top bit of this byte
      if ( X > 0 ) {
        encodedByte |= 128;
      }

      Serial2.write(encodedByte);
    }
    while ( X > 0 );
    Serial2.write(MQTTProtocolNameLength >> 8);
    Serial2.write(MQTTProtocolNameLength & 0xFF);
    Serial2.write(MQTTProtocolName);

    Serial2.write(MQTTLVL); // LVL
    Serial2.write(MQTTFlags); // Flags
    Serial2.write(MQTTKeepAlive >> 8);
    Serial2.write(MQTTKeepAlive & 0xFF);


    Serial2.write(MQTTClientIDLength >> 8);
    Serial2.write(MQTTClientIDLength & 0xFF);
    Serial2.print(MQTTClientID);


    Serial2.write(MQTTUsernameLength >> 8);
    Serial2.write(MQTTUsernameLength & 0xFF);
    Serial2.print(MQTTUsername);


    Serial2.write(MQTTPasswordLength >> 8);
    Serial2.write(MQTTPasswordLength & 0xFF);
    Serial2.print(MQTTPassword);

    Serial2.write(0x1A);
    if (sendATcommand2("", "SEND OK", "SEND FAIL", 5000)) {
      Serial.println(F("CONNECT PACKET SUCCESS"));
      return 1;
    }
    else return 0;
  }
}


int  MQTTpublish() {
  if (sendATcommand2("AT+CIPSEND", ">", "ERROR", 1000)) {

    memset(str, 0, sizeof(str));
    //counter = counter + 1;
    data1 = 1;
    //int mesasge = "temp,f=" + counter;
    topiclength = sprintf((char*)topic, MQTTTopic);
    //  Serial.println(topic);
    //datalength = sprintf((char*)str,"%s%u",topic,Counter);
    //datalength = sprintf((char*)str, "%s%s", topic, mesasge);
    datalength = sprintf((char*)str, "%s%s%d", topic, "digital_aactuator,d=", data1);
    Serial.println(str);
    delay(1000);
    Serial2.write(0x30);
    X = datalength + 2;
    do
    {
      encodedByte = X % 16384;
      X = X / 16384;
      // if there are more data to encode, set the top bit of this byte
      if ( X > 0 ) {
        encodedByte |= 16384;
      }
      Serial2.write(encodedByte);
    }
    while ( X > 0 );
    Serial.println(str);
    Serial2.write(topiclength >> 8);
    Serial2.write(topiclength & 0xFF);
    Serial2.print(str);
    Serial2.write(0x1A);
    if (sendATcommand2("", "SEND OK", "SEND FAIL", 5000)) {
      Serial.println(F("PUBLISH PACKET SENT"));
      return 1;
    }
    else return 0;
  }
}

void MQTTsubscribe() {

  if (sendATcommand2("AT+CIPSEND", ">", "ERROR", 1000)) {

    memset(str, 0, 250);
    topiclength2 = strlen(MQTTTopic2);
    datalength = 2 + 2 + topiclength2 + 1;
    delay(1000);

    Serial2.write(0x82);
    X = datalength;
    do
    {
      encodedByte = X % 128;
      X = X / 128;
      // if there are more data to encode, set the top bit of this byte
      if ( X > 0 ) {
        encodedByte |= 128;
      }
      Serial2.write(encodedByte);
    }
    while ( X > 0 );
    Serial2.write(MQTTPacketID >> 8);
    Serial2.write(MQTTPacketID & 0xFF);
    Serial2.write(topiclength2 >> 8);
    Serial2.write(topiclength2 & 0xFF);
    Serial2.print(MQTTTopic2);
    Serial2.write(MQTTQOS);

    Serial2.write(0x1A);
    if (sendATcommand2("", "SEND OK", "SEND FAIL", 5000)) {
      Serial.println(F("SUBSCRIBE PACKET SENT"));
      return 1;
    }
    else return 0;
  }

}
1 Like

I solved the problem.
I gave an example of how to publish data.

Script example.

Preview program debugging.

1 Like

awesome. great job. share the code with community so anyone who wants to work with this device can look at your code.
Add the widget by pressing the + on top right corner of the widget.

Of course, the code will be available (after testing).

I added. I have such a table.

2 Likes

Hello!

I’m from Poland. Air202 I bought on Aliexpress. I give an example of a script that sends the battery voltage and temperature to the Cayenne cloud.

  1. Download the scripts -> https://github.com/openLuat/Luat_2G_RDA_8955/tree/master/script_LuaTask
  2. In the file:
  • link.lua,
  • main.lua,
  • mqttInMsg.lua,
  • mqttOutMsg.lua,
  • mqttTask.lua enter the visible changes below.
  1. Install the changed scripts in this way -> https://www.elektroda.pl/rtvforum/viewtopic.php?t=3523979&highlight=

Debugging the program.

2 Likes

Great, thanks for sharing such detailed notes!

thanks @vanihada for sharing your project.