Ho realizzato il progetto e il codice sotto riportato per l’invio e la ricezione di SMS, utilizzando:
SIM800L (blu), alimentata con 5V 4a;
Arduino Nano, alimentato con 5V 2a al pin +5V;
alimentazioni separate, con massa in comune.
Sia l’invio, sia la ricezione di SMS funzionano perfettamente se Arduino è collegato al serial monitor.
Se invece Arduino no è collegato al serial monitor, funziona unicamente l’invio.
La ricezione funziona solo se, dopo l’accensione del sistema, eseguo il reset hardware con il tastino. Così facendo, continua a funzionare regolarmente, fino allo spegnimento.
Al Nano ho messo il bootloader dell’Arduino Uno.
Ho provato anche ad inserire un condensatore elettrolitico tra reset e gnd e, in effetti, sembra faccia un reset all’accensione, ma il risultato non cambia.
Pensavo di usare watchdog per il reset, ma non ho ancora capito come fare a farlo una sola volta all’accensione.
[code]
#include <SoftwareSerial.h>
#define CTRL_Z 26
#define intervallo 180000//Intervallo di tempo tra due letture(millisecondi)= 3 min
long previousMillis = 0; //Memorizza l'ultima volta che la sonda è stata letta
SoftwareSerial SIM800(2, 3); //SIM800L Tx & Rx is connected to Arduino #2 & #3
void setup() {
pinMode(13, OUTPUT);
pinMode(4, INPUT);
Serial.begin(9600);
SIM800.begin(9600);
Serial.println("Initializing...");
delay(1000);
SIM800.println("AT");
updateSerial();
SIM800.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
updateSerial();
SIM800.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
updateSerial();
SIM800.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
SIM800.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
}
int n = 0;
String ret = "";
bool LOOP = true;
int bt, prevbt;
void loop() {
if (SIM800.available()) {
char c = SIM800.read();
if (c == '\n') {
Serial.println("RX:" + ret + "#");
if (ret == "Led on") {
digitalWrite(13, HIGH);
Serial.println("on");
} else if (ret == "Led off") {
digitalWrite(13, LOW);
Serial.println("off");
}
ret = "";
} else if (c == '\r') {
} else {
ret += c;
}
}
if (millis() - previousMillis > intervallo)
{
bt = digitalRead(4);
if (!prevbt && bt)
{
sendSMS();
Serial.println("NO Energia Elettrica");
}
// prevbt = bt;
delay(100);
if (prevbt && !bt)
{
sendSMSok();
Serial.println("Energia Elettrica OK");
}
delay(100);
prevbt = bt;
previousMillis = millis();
}
}
void sendSMS()
{
// AT command to set SIM800 to SMS mode
SIM800.print("AT+CMGF=1\r");
delay(100);
updateSerial();
SIM800.println("AT+CMGS=\"+39XXXXXXXXXX\"");
delay(100);
updateSerial();
SIM800.println("NO Energia Elettrica");
delay(100);
// End AT command with a ^Z, ASCII code 26
SIM800.println((char)26);
delay(100);
SIM800.println();
// Give module time to send SMS
delay(5000);
}
void sendSMSok()
{
// AT command to set SIM800 to SMS mode
SIM800.print("AT+CMGF=1\r");
delay(100);
SIM800.println("AT+CMGS=\"+39XXXXXXXXXX\"");
delay(100);
SIM800.println("Energia Elettrica OK");
delay(100);
// End AT command with a ^Z, ASCII code 26
SIM800.println((char)26);
delay(100);
SIM800.println();
// Give module time to send SMS
delay(5000);
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
SIM800.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (SIM800.available())
{
Serial.write(SIM800.read());//Forward what Software Serial received to Serial Port
}
}
[/code]
Si accettano consigli, critiche e suggerimenti.
Grazie
Marco