Good Morning all;
I have been working on a piece of code and was looking for some assistance as I am not 100% there yet.
I am using a MKR NB1500 and a MKR Relay Proto board. I am looking 2 things
-
If I get rid of the delays under the void alarms and use milli's would this mean that all 4 of the alarms would be looking for a change of state at the same time?
-
Change the SMS response time for void loop alarms. Lets say that after the first notification that there is an alarm state change that someone can either text it to bring it to a non responsive state or to change the response time to a longer duration?
Thank you
#include <MKRNB.h>
#include "arduino_secrets.h"
NB nbAccess;
NB_SMS sms;
char PINNUMBER [] = SECRET_PINNUMBER;
String sender = SECRET_YOUR_NUMBER;
char senderNumber[20];
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) == NB_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(13,OUTPUT);
pinMode(14,OUTPUT);
pinMode(16,INPUT_PULLUP);
pinMode(17,INPUT_PULLUP);
pinMode(18,INPUT_PULLUP);
pinMode(19,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 witch the respective relays on the shielkd
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;
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(){
int sensorVal = digitalRead(16);
Serial.println(sensorVal);
if (sensorVal ==HIGH){
digitalWrite(11,LOW);
String texmsg = "Alarm";
sms.beginSMS(senderNumber); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
}
else {
digitalWrite(11, HIGH);
}
}
void loop_alarm2(){
int sensorVal = digitalRead(17);
Serial.println(sensorVal);
if (sensorVal ==HIGH){
digitalWrite(12,LOW);
String texmsg = "Alarm2";
sms.beginSMS(senderNumber); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
}
else {
digitalWrite(12, HIGH);
}
}
void loop_alarm3(){
int sensorVal = digitalRead(18);
Serial.println(sensorVal);
if (sensorVal ==HIGH){
digitalWrite(13,LOW);
String texmsg = "Alarm3";
sms.beginSMS(senderNumber); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
}
else {
digitalWrite(13, HIGH);
}
}
void loop_alarm4(){
int sensorVal = digitalRead(19);
Serial.println(sensorVal);
if (sensorVal ==HIGH){
digitalWrite(14,LOW);
String texmsg = "Alarm4";
sms.beginSMS(senderNumber); //resposnse to the remote sender with the relay number and the status of the relays
sms.print(texmsg);
sms.endSMS();
delay(10000);
}
else {
digitalWrite(14, HIGH);
}
}
void loop(){
loop_relay();
loop_alarm();
loop_alarm2();
loop_alarm3();
loop_alarm4();
}