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);
}