I've been trying to create SMS alarm using Twilio, Firebase and Arduino IDE. Using sensors I am measuring the temperature. If the temperature is higher than a specified value (ESP reads that value from Firebase Realtime Database), a SMS should be sent using twilio. I am also storing the measures in Firebase Realtime Database. The code worked well, until I tried to add the part where it also pushed measurements (temperatures) to Firebase Realtime Database. It even prints in serial that it is connecting to Twilio api, however, I never receive the message.
Here's my code:
#/*
* Twilio SMS and MMS on ESP8266 Example.
*/
#include "FirebaseArduino.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <sstream>
#include <iostream>
#include "twilio.hpp"
// Use software serial for debugging?
#define USE_SOFTWARE_SERIAL 0
// Print debug messages over serial?
#define USE_SERIAL 1
// Your network SSID and password
const char* ssid="SSID";
const char* password = "PASSWORD";
// Find the api.twilio.com SHA1 fingerprint, this one was valid as
// of July 2020. This will change, please see
// https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-esp8266-cpp
// to see how to update the fingerprint.
const char fingerprint[] = "BC B0 1A 32 80 5D E6 E4 A2 29 66 2B 08 C8 E0 4C 45 29 3F D0";
// Twilio account specific details, from https://twilio.com/console
// Please see the article:
// https://www.twilio.com/docs/guides/receive-and-reply-sms-and-mms-messages-esp8266-c-and-ngrok
// If this device is deployed in the field you should only deploy a revocable
// key. This code is only suitable for prototyping or if you retain physical
// control of the installation.
const char* account_sid = "SID";
const char* auth_token = "TOKEN";
// Details for the SMS we'll send with Twilio. Should be a number you own
// (check the console, link above).
String to_number = "+55555";
String from_number = "+66666";
String message_body = "Hello from Twilio and the ESP8266!";
// The 'authorized number' to text the ESP8266 for our example
String master_number = "+55555";
// Optional - a url to an image. See 'MediaUrl' here:
// https://www.twilio.com/docs/api/rest/sending-messages
String media_url = "";
// Global twilio objects
Twilio *twilio;
ESP8266WebServer twilio_server(8000);
// Optional software serial debugging
#if USE_SOFTWARE_SERIAL == 1
#include <SoftwareSerial.h>
// You'll need to set pin numbers to match your setup if you
// do use Software Serial
extern SoftwareSerial swSer(14, 4, false, 256);
#else
#define swSer Serial
#endif
String b;
int c;
#define FIREBASE_HOST ".firebaseio.com"
#define FIREBASE_AUTH ""
#define ONE_WIRE_BUS 4
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
float temp1 = 0;
/*
* Setup function for ESP8266 Twilio Example.
*
* Here we connect to a friendly wireless network, set the time, instantiate
* our twilio object, optionally set up software serial, then send a SMS
* or MMS message.
*/
void setup() {
WiFi.begin(ssid, password);
twilio = new Twilio(account_sid, auth_token, fingerprint);
#if USE_SERIAL == 1
swSer.begin(115200);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
swSer.print(".");
}
swSer.println("");
swSer.println("Connected to WiFi, IP address: ");
swSer.println(WiFi.localIP());
#else
while (WiFi.status() != WL_CONNECTED) delay(1000);
#endif
// Response will be filled with connection info and Twilio API responses
// from this initial SMS send.
twilio_server.begin();
// Use LED_BUILTIN to find the LED pin and set the GPIO to output
pinMode(LED_BUILTIN, OUTPUT);
sensors.begin();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
/*
* In our main loop, we listen for connections from Twilio in handleClient().
*/
void loop() {
b = Firebase.getString("002/alarm");
c = b.toInt();
Serial.print(c);
sensors.requestTemperatures();
temp1 = sensors.getTempCByIndex(0);
StaticJsonBuffer<256> jsonBuffer;
JsonObject& timeStampObject = jsonBuffer.createObject();
timeStampObject[".sv"] = "timestamp";
JsonObject& root = jsonBuffer.createObject();
JsonArray& timestamp = root.createNestedArray("timestamp");
timestamp.add(timeStampObject);
JsonArray& temperature = root.createNestedArray("temperature");
temperature.add(temp1);
Firebase.push("Sensors/001", root);
if (temp1 > c) {
String response;
bool success = twilio->send_message(
to_number,
from_number,
message_body,
response,
media_url
);
}
delay(20000);
}