Arduino Nano + SIM800L EVB // Working code, BUT - How to make code more effective?

Hi,
It´s a long time that i was trying to send data to Ubidots by SIM800L EVB module with example codes in Ubidots_arduino_GPRS library. But it didn´t work. I am not a programmer (that is a big problem :slight_smile: ) but i think that the issue is that library is for SIM900 module. So after that i found a code for sending data to Ubidots without library, just with AT commands, and it works! Problem is that to send data of 7 variables takes about 90 seconds, and it is really bad for my low-energy project. Can anyone please help with shorter code ? Or anyone know how to make example code MultiVariables working with SIM800L EVB?

There is code that i am using:

#include <SoftwareSerial.h>
#include <String.h>

SoftwareSerial mySerial(7, 8);

int value1;
int value2;
int value3;
int value4;
int value5;
int value6;
int value7;

String token = “// PLACE FOR YOUR TOKEN //”;

String id_value1 = “// PLACE FOR YOUR ID //”;
String id_value1 = “// PLACE FOR YOUR ID //”;
String id_value1 = “// PLACE FOR YOUR ID //”;
String id_value1 = “// PLACE FOR YOUR ID //”;
String id_value1 = “// PLACE FOR YOUR ID //”;
String id_value1 = “// PLACE FOR YOUR ID //”;
String id_value1 = “// PLACE FOR YOUR ID //”;

void setup()
{

mySerial.begin(19200);
Serial.begin(19200);
delay(10000);
}

void loop()
{

value1 = random(36, 39);
value2 = random(24, 30);
value3 = random(65, 75);
value4 = random(50, 60);
value5 = random(12, 13);
value6 = random(45, 48);
value7 = random(85, 87);

comunication_start();

send_value(value1, idvariable1);
send_value(value2, idvariable2);
send_value(value3, idvariable3);
send_value(value4, idvariable4);
send_value(value5, idvariable5);
send_value(value6, idvariable6);
send_value(value7, idvariable7);

comunication_end();

if (mySerial.available())
Serial.write(mySerial.read());

}

void comunication_start()
{

mySerial.println(“AT+CGATT?”);
delay(2000);

ShowSerialData();
mySerial.println(“AT+CGATT?”);
delay(2000);

ShowSerialData();
mySerial.println(“AT+CGATT?”);
delay(2000);

ShowSerialData();
mySerial.println(“AT+CGATT?”);
delay(2000);

ShowSerialData();
mySerial.println(“AT+CGATT?”);
delay(2000);

ShowSerialData();
mySerial.println(“AT+CGATT?”);
delay(2000);

ShowSerialData();

mySerial.println(“AT+CSTT=”//PLACE FOR YOUR APN//""); // Replace with your APN
delay(1000);

ShowSerialData();

mySerial.println(“AT+CIICR”); // Bring up the wireless connection
delay(3000);

ShowSerialData();

mySerial.println(“AT+CIFSR”); // Get IP adress
delay(2000);

ShowSerialData();

mySerial.println(“AT+CIPSPRT=0”);
delay(3000);

ShowSerialData();

mySerial.println("AT+CIPSTART=“tcp”,“things.ubidots.com”,“80"”); // Start the connection to Ubidots
delay(3000);

ShowSerialData();

}

void send_value(int value, String idvariable)
{
int num;
String le;
String var;
var="{“value”:"+ String(value) + “}”;
num=var.length();
le=String(num);

mySerial.println(“AT+CIPSEND”); // Start to send data to remote server
delay(3000);
ShowSerialData();
mySerial.print(“POST /api/v1.6/variables/”+idvariable);
delay(100);
ShowSerialData();
mySerial.println("/values HTTP/1.1");
delay(100);
ShowSerialData();
mySerial.println(“Content-Type: application/json”);
delay(100);
ShowSerialData();
mySerial.println("Content-Length: "+le);
delay(100);
ShowSerialData();
mySerial.print("X-Auth-Token: ");
delay(100);
ShowSerialData();
mySerial.println(token);
delay(100);
ShowSerialData();
mySerial.println(“Host: things.ubidots.com”);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println(var);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println((char)26); // Sending the HTTP request
delay(7000); // Waiting for a reply. Important! tweak this time depending on the latency of your             
mobile Internet connection
mySerial.println();
ShowSerialData();

}

void comunication_end()
{
mySerial.println(“AT+CIPCLOSE”); // Close the connection
delay(1000);
ShowSerialData();
}

// This function is to show the response in your serial terminal

void ShowSerialData()
{
while(mySerial.available()!=0)

```
Serial.write(mySerial.read());
```

}

Thank you for your time and help! :slight_smile:

Hi!
I found next solution for sending multiplne variables on this forum, but it didnt work very well.
I have problem like guy in this post: https://ubidots.com/community/t/solved-sending-mutiple-variable-to-ubidots/2473, bud his solution is not working for me.

I can see on ubidots just first 3 variables. Next 3 variables are not updating

I changed broker for industrial.api.ubidots.com like in the post, and it didnt work.
I also tryed to change the maximum packet size supported by the PubSubClient, but when i rewrite it to 512, program just stopped working, and i was not able to send first 3 variables.

Then i tryed to send only variables 4-6, and it worked, so maybe problem is that i am sending to much data in one time, but i dont know how to make it work.

code:

#define TINY_GSM_MODEM_SIM800

#include <TinyGsmClient.h>
#include <PubSubClient.h>

#define SerialMon Serial

#include <SoftwareSerial.h>
SoftwareSerial SerialAT(7, 8); // TX, RX

const char apn[]  = "internet";
const char user[] = "";
const char pass[] = "";

const char* broker = "industrial.api.ubidots.com";
const char* token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 

const char * API_1 = "1"; // Assing the variable label
const char * API_2 = "2"; // Assing the variable label
const char * API_3 = "3"; // Assing the variable label
const char * API_4 = "4"; // Assing the variable label
const char * API_5 = "5"; // Assing the variable label
const char * API_6 = "6"; // Assing the variable label

const char * device_label = "s1"; // Assig the device label

char payload[150];
char topic[300];

char str_var_1[10];
char str_var_2[10];
char str_var_3[10];

char str_var_4[10];
char str_var_5[10];
char str_var_6[10];


TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);

long lastReconnectAttempt = 0;

void setup() {
  SerialMon.begin(115200);
  delay(10);

  SerialAT.begin(4800);
  delay(3000);

  SerialMon.println("Initializing modem...");
  modem.restart();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem: ");
  SerialMon.println(modemInfo);

  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    while (true);
  }
  SerialMon.println(" OK");

  SerialMon.print("Connecting to ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, user, pass)) {
    SerialMon.println(" fail");
    while (true);
  }
  SerialMon.println(" OK");

  mqtt.setServer(server, 1883);
  mqtt.setCallback(mqttCallback);
}

boolean mqttConnect() {
  SerialMon.print("Connecting to ");
  SerialMon.print(server);

  boolean status = mqtt.connect("DTIDOSK", token, token);

  if (status == false) {
    SerialMon.println(" fail");
    return false;
  }
  SerialMon.println(" OK");
  return mqtt.connected();
}

void loop() {
  if (!mqtt.connected()) {
    SerialMon.println("=== MQTT NOT CONNECTED ===");
    // Reconnect every 10 seconds
    unsigned long t = millis();
    if (t - lastReconnectAttempt > 10000L) {
      lastReconnectAttempt = t;
      if (mqttConnect()) {
        lastReconnectAttempt = 0;
      }
    }
    return;
  }

  /* Reading temperature and humidity */
  float var1 = random(36, 39);
  float var2 = random(65, 75);
  float var3 = random(12, 13);
  float var4 = random(12, 18);
  float var5 = random(12, 18);
  float var6 = random(12, 18);


  /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
  dtostrf(var1, 3, 0, str_var_1);
  dtostrf(var2, 4, 2, str_var_2);
  dtostrf(var3, 3, 0, str_var_3);
  dtostrf(var4, 3, 0, str_var_4);
  dtostrf(var5, 3, 0, str_var_5);
  dtostrf(var6, 3, 0, str_var_6);


  /* Building the Ubidots request */
  sprintf(topic, "%s%s", "/v1.6/devices/", device_label);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\": %s,", API_1, str_var_1); // Adds the variable label
  sprintf(payload, "%s\"%s\": %s,", payload, API_2, str_var_2); // Adds the variable label
  sprintf(payload, "%s\"%s\": %s}", payload, API_3, str_var_3); // Adds the variable label

  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  mqtt.publish(topic, payload);

  sprintf(payload, "%s", ""); // Cleans the payload 
  sprintf(payload, "{\"%s\": %s,", API_4, str_var_4); // Adds the variable label
  sprintf(payload, "%s\"%s\": %s,", payload, API_5, str_var_5); // Adds the variable label
  sprintf(payload, "%s\"%s\": %s}", payload, API_6, str_var_6); // Adds the variable label

  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  mqtt.publish(topic, payload);
  //mqtt.loop();

  delay(15000);
}

void mqttCallback(char* topic, byte* payload, unsigned int len) {
  SerialMon.print("Message arrived [");
  SerialMon.print(topic);
  SerialMon.print("]: ");
  SerialMon.write(payload, len);
  SerialMon.println();
}

Thank you

hey there, you are sending 2 variable in a single payload. Just add another payload for str_var_6 to upload to ubidots everytime.

try this

#define TINY_GSM_MODEM_SIM800

#include <TinyGsmClient.h>
#include <PubSubClient.h>

#define SerialMon Serial

#include <SoftwareSerial.h>
SoftwareSerial SerialAT(7, 8); // TX, RX

const char apn[] = “internet”;
const char user[] = “”;
const char pass[] = “”;

const char* broker = “industrial.api.ubidots.com”;
const char* token = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;

const char * API_1 = “1”; // Assing the variable label
const char * API_2 = “2”; // Assing the variable label
const char * API_3 = “3”; // Assing the variable label
const char * API_4 = “4”; // Assing the variable label
const char * API_5 = “5”; // Assing the variable label
const char * API_6 = “6”; // Assing the variable label

const char * device_label = “s1”; // Assig the device label

char payload[150];
char topic[300];
char topic2[300];
char topic3[300];
char topic4[300];
char topic5[300];
char topic6[300];

char str_var_1[10];
char str_var_2[10];
char str_var_3[10];

char str_var_4[10];
char str_var_5[10];
char str_var_6[10];

TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);

long lastReconnectAttempt = 0;

void setup() {
SerialMon.begin(115200);
delay(10);

SerialAT.begin(4800);
delay(3000);

SerialMon.println(“Initializing modem…”);
modem.restart();

String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);

SerialMon.print(“Waiting for network…”);
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
while (true);
}
SerialMon.println(" OK");

SerialMon.print(“Connecting to “);
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(” fail”);
while (true);
}
SerialMon.println(" OK");

mqtt.setServer(server, 1883);
mqtt.setCallback(mqttCallback);
}

boolean mqttConnect() {
SerialMon.print("Connecting to ");
SerialMon.print(server);

boolean status = mqtt.connect(“DTIDOSK”, token, token);

if (status == false) {
SerialMon.println(" fail");
return false;
}
SerialMon.println(" OK");
return mqtt.connected();
}

void loop() {
if (!mqtt.connected()) {
SerialMon.println("=== MQTT NOT CONNECTED ===");
// Reconnect every 10 seconds
unsigned long t = millis();
if (t - lastReconnectAttempt > 10000L) {
lastReconnectAttempt = t;
if (mqttConnect()) {
lastReconnectAttempt = 0;
}
}
return;
}

/* Reading temperature and humidity */
float var1 = random(36, 39);
float var2 = random(65, 75);
float var3 = random(12, 13);
float var4 = random(12, 18);
float var5 = random(12, 18);
float var6 = random(12, 18);

/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(var1, 3, 0, str_var_1);
dtostrf(var2, 4, 2, str_var_2);
dtostrf(var3, 3, 0, str_var_3);
dtostrf(var4, 3, 0, str_var_4);
dtostrf(var5, 3, 0, str_var_5);
dtostrf(var6, 3, 0, str_var_6);

/* Building the Ubidots request */
sprintf(topic, “%s%s”, “/v1.6/devices/”, device_label);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s": %s,", API_1, str_var_1); // Adds the variable label
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
mqtt.publish(topic, payload);

sprintf(topic2, “%s%s”, “/v1.6/devices/”, device_label);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s": %s,", API_2, str_var_2); // Adds the variable label
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
mqtt.publish(topic2, payload);

sprintf(topic3, “%s%s”, “/v1.6/devices/”, device_label);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s": %s,", API_3, str_var_3); // Adds the variable label
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
mqtt.publish(topic3, payload);

sprintf(topic4, “%s%s”, “/v1.6/devices/”, device_label);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s": %s,", API_4, str_var_4); // Adds the variable label
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
mqtt.publish(topic4, payload);

sprintf(topic5, “%s%s”, “/v1.6/devices/”, device_label);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s": %s,", API_5, str_var_5); // Adds the variable label
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
mqtt.publish(topic5, payload);

sprintf(topic6, “%s%s”, “/v1.6/devices/”, device_label);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s": %s,", API_6, str_var_6); // Adds the variable label
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
mqtt.publish(topic6, payload);

//mqtt.loop();

delay(15000);
}

void mqttCallback(char* topic, byte* payload, unsigned int len) {
SerialMon.print(“Message arrived [”);
SerialMon.print(topic);
SerialMon.print("]: ");
SerialMon.write(payload, len);
SerialMon.println();
}