#include "Adafruit_FONA.h"
#include "Adafruit_MAX31855.h"
#include <WiFi.h>
#define SIM800L_RX 27
#define SIM800L_TX 26
#define SIM800L_PWRKEY 4
#define SIM800L_RST 5
#define SIM800L_POWER 23
#define SIM800L_DTR 11
char replybuffer[150];
//per wi-fi
const char *SSID = "";
const char *PWD = "";
//per sim800
HardwareSerial *sim800lSerial = &Serial1;
Adafruit_FONA sim800l = Adafruit_FONA(SIM800L_PWRKEY);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
#define LED_BLUE 13
//definisco pin per temperatura
#define MAXDO 35
#define MAXCS 14
#define MAXCLK 19
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
// Your phone number to send SMS: + (plus sign) and country code
#define SMS_TARGET "+39xxxxxxxx"
//conversione in secondi
#define uS_TO_S_FACTOR 1000000UL
#define TIME_TO_SLEEP 60 //1 ora
int conteggio = 0;
int conteggioMax = 3;
String smsString = "";
char callerIDbuffer[32]; //we'll store the SMS sender number in here
double int_temp = thermocouple.readInternal();
double c = thermocouple.readCelsius();
String internalTemperature() {
if (isnan(int_temp)) {
Serial.println(F("Something wrong with internal temperature!"));
return "";
}
String Internal_temp = ("Internal Temperature : " + String(int_temp) + " °C");
delay(1000);
Serial.println(Internal_temp);
char tempInterna[50];
char informazioni[30] = "Internal Temperature -> ";
strcat(informazioni, itoa(int_temp, tempInterna, 10));
strcat(informazioni, " C\n");
return informazioni;
}
String thermocoupleTemperature() {
if (isnan(c)) {
Serial.println(F("Something wrong with thermocouple temperature!"));
return "";
}
String Thermocouple_temp = "Thermocouple Temperature " + String(c) + " °C";
delay(1000);
Serial.println(Thermocouple_temp);
char tempTermo[50];
char informazioni2[30] = "Thermocouple Temperature -> ";
strcat(informazioni2, itoa(c, tempTermo, 10));
strcat(informazioni2, " C\n");
return informazioni2;
}
//connessione al wi-fi
void connectToWiFi() {
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PWD);
Serial.println("Connecting...");
delay(20000);
conteggio = 0;
while (WiFi.status() != WL_CONNECTED && conteggio <= conteggioMax) {
Serial.println("Not connected to wifi");
delay(2000);
Serial.println("Reconnecting..");
WiFi.reconnect();
delay(3000);
conteggio++;
}
if (WiFi.status() != WL_CONNECTED || conteggio >= 3) {
sim800l.sendSMS(SMS_TARGET, "Not connected to wifi");
}
if (WiFi.status() == WL_CONNECTED) {
sim800l.sendSMS(SMS_TARGET, "Wi-Fi Available!");
Serial.println("Wi-Fi Available!");
Serial.print("Connected. IP: ");
Serial.println(WiFi.localIP());
delay(1000);
Serial.print("WiFi RRSI: ");
Serial.println("dBm");
Serial.println(WiFi.RSSI());
}
}
void setup() {
pinMode(LED_BLUE, OUTPUT);
pinMode(SIM800L_POWER, OUTPUT);
pinMode(SIM800L_RST, OUTPUT);
digitalWrite(SIM800L_RST, LOW);
delay(100);
digitalWrite(SIM800L_RST, HIGH);
digitalWrite(LED_BLUE, HIGH);
digitalWrite(SIM800L_POWER, HIGH);
Serial.begin(115200);
Serial.println(F("ESP32 with GSM SIM800L"));
Serial.println(F("Initializing....(May take more than 10 seconds)"));
delay(10000);
while (!Serial) delay(1);
delay(500);
Serial.print("Initializing sensor...");
if (!thermocouple.begin()) {
Serial.println("ERROR.");
while (1) delay(10);
}
Serial.println("DONE.");
// Make it slow so its easy to read!
sim800lSerial->begin(4800, SERIAL_8N1, SIM800L_TX, SIM800L_RX);
if (!sim800l.begin(*sim800lSerial)) {
Serial.println(F("Couldn't find GSM SIM800L"));
while (1)
;
}
Serial.println(F("GSM SIM800L is OK"));
char imei[16] = { 0 }; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = sim800l.getIMEI(imei);
if (imeiLen > 0) {
Serial.print("SIM card IMEI: ");
Serial.println(imei);
}
//get rssi gsm
uint8_t n = sim800l.getRSSI();
int8_t r;
Serial.print(F("GSM RSSI "));
Serial.print(n);
Serial.print(": ");
r = map(n, 2, 30, -110, -54);
Serial.print(r);
Serial.println(F(" dBm"));
// Set up the FONA to send a +CMTI notification
// when an SMS is received
sim800lSerial->print("AT+CNMI=2,1\r\n");
Serial.println("GSM SIM800L Ready");
connectToWiFi();
}
long prevMillis = 0;
int interval = 1000;
char sim800lNotificationBuffer[64]; //for notifications from the FONA
char smsBuffer[250];
boolean ledState = false;
int value = 0;
void loop() {
delay(10000);
if (millis() - prevMillis > interval) {
ledState = !ledState;
digitalWrite(LED_BLUE, ledState);
prevMillis = millis();
}
char *bufPtr = sim800lNotificationBuffer; //handy buffer pointer
if (sim800l.available()) {
int slot = 0; // this will be the slot number of the SMS
int charCount = 0;
// Read the notification into fonaInBuffer
do {
*bufPtr = sim800l.read();
Serial.write(*bufPtr);
delay(1);
} while ((*bufPtr++ != '\n') && (sim800l.available()) && (++charCount < (sizeof(sim800lNotificationBuffer) - 1)));
//Add a terminal NULL to the notification string
*bufPtr = 0;
//Scan the notification string for an SMS received notification.
// If it's an SMS message, we'll get the slot number in 'slot'
if (1 == sscanf(sim800lNotificationBuffer, "+CMTI: \"SM\",%d", &slot)) {
Serial.print("slot: ");
Serial.println(slot);
// Retrieve SMS sender address/phone number.
if (!sim800l.getSMSSender(slot, callerIDbuffer, 149)) {
Serial.println("Didn't find SMS message in slot!");
}
Serial.print(F("FROM: "));
Serial.println(callerIDbuffer);
// Retrieve SMS value.
uint16_t smslen;
// Pass in buffer and max len!
if (sim800l.readSMS(slot, smsBuffer, 250, &smslen)) {
smsString = String(smsBuffer);
Serial.println(smsString);
}
if (smsString == "State") {
Serial.println("Current Temperature:");
char invio[] = "Current Temperature:\n";
char appoggio[50];
char appoggio2[50];
internalTemperature().toCharArray(appoggio, 100);
strcat(invio, appoggio);
Serial.println(invio);
thermocoupleTemperature().toCharArray(appoggio2, 100);
strcat(invio, appoggio2);
Serial.println(invio);
sim800l.sendSMS(callerIDbuffer, invio);
internalTemperature();
thermocoupleTemperature();
delay(100);
}
if (smsString == "Connect") {
Serial.println("Attempted wifi connection...");
sim800l.sendSMS(callerIDbuffer, "Attempted wifi connection...");
WiFi.reconnect();
connectToWiFi();
}
while (1) {
if (sim800l.deleteSMS(slot)) {
Serial.println(F("OK!"));
break;
} else {
Serial.print(F("Couldn't delete SMS in slot "));
Serial.println(slot);
sim800l.print(F("AT+CMGD=?\r\n"));
delay(10000);
}
}
}
}
Serial.println("sleep for 1 minute..");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
i tried to implement the reset but the sim still doesn't work after deep sleep