SIM800l GSM with UBIDOTS

My SIM800l GSM Module is working fine alone, but when I try to combine it with my Ubidots code, it doesn’t work, it does not send an SMS. What seems to be the problem? I got the flood level variable on ubidots using HTTP GET, then I want to use that variable level to make a notification SMS that will be sent on my mobile number. I hope someone can help me please. Thankyou

Here is my code:

/*****************************************
* Include Libraries
****************************************/
#include <ESP8266WiFi.h>
#include <ConfigManager.h>
#include <PubSubClient.h>
#include<SoftwareSerial.h>
#include "Ubidots.h"

#define ECHO D2
#define TRIG D4

SoftwareSerial sms(D7, D8);
/****************************************
* Define Constants
****************************************/
namespace{
const char * AP_NAME = "JESVINKER Device"; // Assigns your Access Point name
const char * MQTT_SERVER = "industrial.api.ubidots.com"; 
const char * TOKEN = "**********************"; // Assigns your Ubidots 
TOKEN
const char * DEVICE_LABEL = "FLOOD-MONITORING"; // Assigns your Device Label
const char * VARIABLE_LABEL = "vertical-distance"; // Assigns your Variable Label
const char * VARIABLE_LABEL2 = "sensor-calibration"; // Assigns your Variable Label
const char * VARIABLE_LABEL3 = "flood-water-level"; // Assigns your Variable Label  
}

int flag = 0;
float value;
float distance;
float dis;
float level;
char topic[150];
char payload[50];
char str_sensor[10];
char str_sensor2[10];
String clientMac = "";
String textSMS;
String f1001 = "+639***********";
unsigned char mac[6];

unsigned long previousMillis_one = 0; // store the last time updated
unsigned long previousMillis_two = 0; // store the last time updated
long three_seconds_interval = 3*1000UL; // interval desired (millisenconds)

struct Config {
 char name[20];
 bool enabled;
 int8 hour;
} config;

/****************************************
* Initialize a global instance
****************************************/
WiFiClient espClient;
PubSubClient client(espClient);
ConfigManager configManager;

/****************************************
* Auxiliar Functions
****************************************/

void callback(char* topic, byte* payload, unsigned int length){

}

void reconnect() {
while (!client.connected()) {
//Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientMac.c_str(), TOKEN, NULL)) {
  //Serial.println("connected");
  break;
  } else {
    configManager.reset();
    //Serial.print("failed, rc=");
    //Serial.print(client.state());
    //Serial.println(" try again in 3 seconds");
    for(uint8_t Blink=0; Blink<=3; Blink++){
      digitalWrite(LED2, LOW);
      delay(1000);
      digitalWrite(LED2, HIGH);
      delay(1000);
    }
  }
}
}

String macToStr(const uint8_t* mac) {
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)result += ':';
}
return result;
}

Ubidots ubidots(TOKEN, UBI_HTTP);

/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
sms.begin(115200);
/* Declare PINs as input/output*/
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT_PULLUP);
pinMode(PIN_RESET, INPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED2, LOW);

/* Assign WiFi MAC address as MQTT client name */
WiFi.macAddress(mac);
clientMac += macToStr(mac);

/* Access Point configuration */
configManager.setAPName(AP_NAME);
configManager.addParameter("name", config.name, 20);
configManager.addParameter("enabled", &config.enabled);
configManager.addParameter("hour", &config.hour);
configManager.begin(config);

/* Set Sets the server details */
client.setServer(MQTT_SERVER, 1883);
client.setCallback(callback);

/* Build the topic request */
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
flag = 1;
}

void loop() {  
configManager.reset();
configManager.loop();   

/* MQTT client reconnection */
if (!client.connected()) {
  reconnect();
}

if (client.connect(clientMac.c_str(), TOKEN, NULL))
{
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
}
/* Sensor Reading */
digitalWrite(TRIG, LOW);
delayMicroseconds(2);

digitalWrite(TRIG, HIGH);
delayMicroseconds(20);

digitalWrite(TRIG, LOW);
distance = pulseIn(ECHO, HIGH, 26000);
distance = distance/58;
dis = distance*0.0328084;
value = ubidots.get(DEVICE_LABEL, VARIABLE_LABEL2);
level = value - dis;

dtostrf(dis, 4, 2, str_sensor);
dtostrf(level, 4, 2, str_sensor2);
/* Build the payload request */
unsigned long currentMillis = millis();
if (currentMillis - previousMillis_one > three_seconds_interval) 
{
previousMillis_one = currentMillis;
//Serial.println("three seconds interval");

/* Building the Ubidots request */
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\": %s}", VARIABLE_LABEL, str_sensor); // Adds the variable label

//Serial.println("Publishing values to Cloud");
//Serial.print("Distance = ");
//Serial.println(dis);
client.publish(topic, payload);
client.loop(); 
}

if (currentMillis - previousMillis_two > three_seconds_interval) 
{
previousMillis_two = currentMillis;
//Serial.println("three seconds interval");

/* Building the Ubidots request */
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\": %s}", VARIABLE_LABEL3, str_sensor2); // Adds the variable label

//Serial.println("Publishing values to Cloud");
//Serial.print("Level = ");
//Serial.println(value);
client.publish(topic, payload);
client.loop(); 
}

// Evaluates the results obtained
 if (value != ERROR_VALUE) {
//Serial.print("Value:");
//Serial.println(value);
}

if (level >= 1) 
{
if(flag == 1)
{
textSMS = "Flood waters are over the ankle level";
sendSMS(textSMS, f1001);
Serial.println(textSMS);
Serial.println("Message sent...");
flag = 2;
}
}
//Serial.println("looping..");
  delay(1000);     
}

void sendSMS(String message, String number)
{
String mnumber = "AT+CMGS=\""+number+"\"";
sms.println("AT+CMGF=1\r");
delay(500);
sms.println(mnumber);
delay(500);
sms.println(message);
delay(500);
sms.println((char)26);
delay(500);
sms.println();
delay(100);
}

Hello @itschristianjay,

From what I see in the code, there are many things that could be the cause for it not working properly. For example, using MQTT and HTTP in the same code or managing of WiFi connection via ConfigManager.

I have a couple of questions that will help to give more information and context so I can assist you better:

  1. Why did you choose to send SMS via your GSM module instead of the Events module of Ubidots?
  2. Can you confirm that the value obtained through the GET is correct and corresponds to the one shown in Ubidots and not the library’s ERROR_VALUE
  3. In the loop, when is the variable flag returned to 1 so in case value is greater than 1 it will send an SMS again?
  4. What is the purpose of mixing HTTP and MQTT? This may cause conflicts with the network. Why not send and receive data with just one protocol? I recommend using just one protocol for sending and receiving data to/from Ubidots. Also, please try out the HTTP examples from our Library for the esp8266.
  5. I understand your code will work while using the GSM module alone, now, could you try your code without using the GSM functionality and see if it works?
  6. Could you please place a debug line in the setup? The line is the following:
    ubidots.setDebug(true);

Best regards,
-Sebastián

  1. I choose to send SMS via GSM because it’s my teacher’s requirement.

  2. Yes. the GET value is the same or corresponds to my dashboard when I check it in my Serial Monitor. If my dashboard value says -5.3, it will also be printed in my Serial Monitor.

  3. Yes, I’m planning to send multiple alerts, like (if level >= 1) , (if level >=2), etc. That’s why it has a flag variable greater than 1.

  4. Because I want to get the value of the flood level then send it using my GSM via SMS. I don’t know if MQTT has a GET value also…

and I can’t use HTTP Protocol only because Ubidots WifiManager only uses MQTT as stated in this link:

Unless there’s way that this Ubidots WifiManager can be changed to HTTP…

  1. Yes, my main code works fine even without GSM, it will only send all of my data through the Ubidots… But now I want to get the value from my dashboard then send it using my GSM.

  2. What does [ubidots.setDebug(true)] is used for? But sure no problem, I’ll add it to my code.

Thankyou so much for your quick response. Hoping my problem get resolved. :frowning:

Does this function, by itselft, work?

void sendSMS(String message, String number)
{
String mnumber = "AT+CMGS=\""+number+"\"";
sms.println("AT+CMGF=1\r");
delay(500);
sms.println(mnumber);
delay(500);
sms.println(message);
delay(500);
sms.println((char)26);
delay(500);
sms.println();
delay(100);
}

Your issue seems related with your hardware rather than with the firmware