Hello;
I am wondering if someone might be able to assist me with an issue that I am having with my code.
The code seems to run well than at apx the 35 hour mark the device just sends out Alarm and Alarm off about 5 times a minute until I power off the device.
#include <MKRGSM.h>
#include "arduino_secrets.h"
GSM nbAccess;
GSM_SMS sms;
char PINNUMBER [] = SECRET_PINNUMBER;
String sender = SECRET_YOUR_NUMBER;
char senderNumber[20];
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("SMS Messages Receiver");
// connection state
bool connected = false;
// Start LTE connection
while (!connected) {
if (nbAccess.begin(PINNUMBER) == GSM_READY) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("LTE initialized");
Serial.println("Waiting for messages");
pinMode(1,OUTPUT);
pinMode(0,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(16,INPUT_PULLUP);
pinMode(17,INPUT_PULLUP);
}
void loop_relay()
{
int c;
String texmsg = "";
// If there are any SMSs available()
if (sms.available()) {
Serial.println("Message received from: ");
// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
if (String(senderNumber) == sender) {
// An example of message disposal
// Any messages starting with # should be discarded
if (sms.peek() == '#') {
Serial.println("Discarded SMS");
sms.flush();
}
//read the first char of the message text to which the respective relays on the shield
c = sms.read();
switch (c) {
case 49:
digitalWrite(1, !digitalRead(1));
texmsg = "Relay 1, state: " + String(digitalRead(1));
sms.beginSMS(senderNumber); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
break;
case 50:
digitalWrite(2, !digitalRead(2));
texmsg = "Relay 2, state: " + String(digitalRead(2));
sms.beginSMS(senderNumber); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
break;
case 51:
digitalWrite(3, !digitalRead(3));
texmsg = " Device online" + (digitalRead(3));
sms.beginSMS(senderNumber); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
break;
default:
break;
}
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
} else {
sms.flush();
Serial.println("MESSAGE DELETED");
}
}
delay(1000);
}
void loop_alarm(){
buttonState1 = digitalRead(16);
if (buttonState1 != lastButtonState1){
if (buttonState1 ==HIGH){
// Serial.println("Alarm"); //Enable for testing
digitalWrite(11,LOW);
String texmsg = "Alarm, Alarm, Alarm";
sms.beginSMS(SECRET_YOUR_NUMBER); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
}
else {
// Serial.println("Alarm off, Alarm off, Alarm off"); //Enable for testing
String texmsg = "Alarm off, Alarm off, Alarm off";
sms.beginSMS(SECRET_YOUR_NUMBER); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
digitalWrite(11, HIGH);
}
delay (50);
}
lastButtonState1 = buttonState1;
}
void loop_alarm2(){
buttonState2 = digitalRead(17);
if (buttonState2 != lastButtonState2){
if (buttonState2 ==HIGH){
// Serial.println("Alarm2"); //Enable for testing
digitalWrite(12,LOW);
String texmsg = "Alarm2, Alarm2, Alarm2";
sms.beginSMS(SECRET_YOUR_NUMBER); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
}
else {
// Serial.println("Alarm2 off, Alarm2 off, Alarm2 off"); //Enable for testing
String texmsg = "Alarm2 off, Alarm2 off, Alarm2 off";
sms.beginSMS(SECRET_YOUR_NUMBER); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
digitalWrite(12, HIGH);
}
delay (50);
}
lastButtonState2 = buttonState2;
}
void loop(){
loop_relay();
loop_alarm();
loop_alarm2();
}