Hello there,
I am a newbie with arduino and zero knowledge with coding. Based on what i learned from online sources, I am trying to prepare a simple early warning system using arduino nano, jsn-sr04t and sim800L.
the setup is supposed to do:
- The jsn-sr04t will detect water level height, whenever it reach a certain range, it will send a sms message to list of phone numbers.
- phone number list can be changed through special sms messages. So, whenever people change their phone number, i can change the number list remotely.
I tried to write the coding seperately for each purpose, and it works for each purpose. Problem starts when i tried to merge the codes. The combined codes sometimes works, and sometime does not work. I believe because it is a multitasking job.
I am still trying to avoid using 2 arduino nano at the moment, and still looking for a way out with only using 1 arduino nani. The parameters (how many phone numbers in the list, how many times the jsn-sr04t detection before sending sms, etc) can be changed later when the code is functioning.
I read the possibility of using coroutine, but unable to finish it properly. I hope this is the right path.
I hope i use the forum correctly. Could someone please help me? I tried to find answers from forums, but could not find something that can solve my problem. Thanks in advance.
Here is what i wrote:
// TUNGGU 5 MENIT . JANGAN LUPA GANTI IMEI. power supply pakai laptop lama
#include <SoftwareSerial.h>
#include <NewPing.h>
SoftwareSerial mySerial(3,4);
String SMS;
//int distance=5000;
int distance;
//int jarak=5000;
int jarak;
#define TRIG 11 //Module pins
#define ECHO 12
#define MAX_DISTANCE 400
NewPing sonar = NewPing(TRIG, ECHO, MAX_DISTANCE);
int times1;
int times2;
int times3;
char kode;
String message;
String CellNumtemp;
String CellNum;
String f1001;
String f1002;
String f1003;
const int onTime=5000; // in ms
const int offTime=20000; // in ms
boolean currentlyOn=false;
unsigned long startTime;
void setup()
{
// pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input
// pinMode(ECHO, INPUT_PULLUP);
// pinMode(ECHO, INPUT);
//digitalWrite(TRIG, HIGH);
mySerial.begin(9600);
Serial.begin(9600);
Serial.println("cek...");
Serial.println("System Started...");
delay(1000);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
mySerial.println("AT+CNMI=1,2,0,0,0");
f1001 = "+6281260707654";
f1002 = "+2345678901234";
f1003 = "+1234567890123";
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
startTime=millis(); // Initialize
}
void loop() {
while (mySerial.available() > 0){
command();
}
if (currentlyOn && millis()>startTime+onTime){ // Switch resistor off
// digitalWrite(resistorPin,LOW);
currentlyOn=false;
startTime=millis(); // Reset timer
}
if (!currentlyOn && millis()>startTime+offTime){ // Switch resistor on
cek_jarak();
currentlyOn=true;
startTime=millis(); // Reset timer
}
}
void command(){
message =mySerial.readString();
mySerial.println (message);
CellNumtemp = message.substring(message.indexOf("+62"));
CellNum = CellNumtemp.substring(0,14);
// Serial.println("nomor pengirim:");
// Serial.println(CellNum);
if (message.indexOf("NEW f1001:") > -1) {
int idx = message.indexOf("f1001:");
f1001 = message.substring(idx+6, idx+6+f1001.length());
mySerial.println (f1001);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("New f1001:"); //text content
mySerial.print(f1001); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
if (message.indexOf("NEW f1002:") > -1) {
int idx = message.indexOf("f1002:");
f1002 = message.substring(idx+6, idx+6+f1002.length());
mySerial.println (f1002);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("New f1002:"); //text content
mySerial.print(f1002); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
if (message.indexOf("NEW f1003:") > -1) {
int idx = message.indexOf("f1003:");
f1003 = message.substring(idx+6, idx+6+f1003.length());
mySerial.println (f1003);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("New f1003:"); //text content
mySerial.print(f1003); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
if (message.indexOf("REPORT") > -1) {
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("f1001:"); //text content
mySerial.print(f1001); //text content
mySerial.print("\nf1002:"); //text content
mySerial.print(f1002); //text content
mySerial.print("\nf1003:"); //text content
mySerial.print(f1003); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
}
void cek_jarak(){
// digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS
// delayMicroseconds(5);
// digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging
// delayMicroseconds(20);
// digitalWrite(TRIG, LOW); // Send pin low again
// distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse
// distance = pulseIn(ECHO, HIGH); // Read in times pulse
// jarak= distance*0.034/2; //Convert the pulse duration to distance
//You can add other math functions to calibrate it well
jarak = distance;
// while (jarak!=0){
Serial.print("Distance ");
Serial.print(sonar.ping_cm());
// Serial.print(jarak);
Serial.println(" cm");
if ((jarak<=200)&& (jarak>=100)){
times1++;
times2 = 0;
times3 = 0;
Serial.println(times1);
delay(1000);
if (times1 == 20){
SMS = "air naik.";
sms();
times1 = 0;
delay(1000);
}}
else if ((jarak<100)&& (jarak>=50)){
times2++;
times1 = 0;
times3 = 0;
Serial.println(times2);
delay(1000);
if (times2 == 20){
SMS = "siap2 mengangkat barang ke tempat yang tinggi";
sms();
times2 = 0;
delay(1000);
}}
else if ((jarak<50)&& (jarak>=10)){
times3++;
times1 = 0;
times2 = 0;
Serial.println(times3);
delay(1000);
if (times3 == 20){
SMS = " sebentar lagi air masuk kerumah";
sms();
times3 = 0;
delay(1000);
}}
else {
times1 = 0;
times2 = 0;
times3 = 0;
}
delay(20);
//
}
//}
//-----------GSM Function ---------------------------------------------------------
void sms()
{
mySerial.println ("Sending Message f1001");
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.println("AT+CNMI=1,2,0,0,0");
delay(1000);
mySerial.println ("Set SMS Number");
mySerial.println("AT+CMGS=\"" + f1001 + "\"\r"); //Mobile phone number to send message
delay(1000);
//String SMS = "Sample SMS is sending....."; //sms content
//String SMS = " sebentar lagi air masuk kerumah "; //sms content
mySerial.println(SMS);
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
mySerial.println (SMS);
mySerial.println ("Sent");
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
//-------------------------
mySerial.println ("Sending Message f1002");
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.println("AT+CNMI=1,2,0,0,0");
delay(1000);
mySerial.println ("Set SMS Number");
mySerial.println("AT+CMGS=\"" + f1002 + "\"\r"); //Mobile phone number to send message
delay(1000);
//String SMS = "Sample SMS is sending....."; //sms content
//String SMS = " sebentar lagi air masuk kerumah "; //sms content
mySerial.println(SMS);
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
mySerial.println (SMS);
mySerial.println ("Sent");
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
//---------------------------------------
mySerial.println ("Sending Message f1003");
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.println("AT+CNMI=1,2,0,0,0");
delay(1000);
mySerial.println ("Set SMS Number");
mySerial.println("AT+CMGS=\"" + f1003 + "\"\r"); //Mobile phone number to send message
delay(1000);
//String SMS = "Sample SMS is sending....."; //sms content
//String SMS = " sebentar lagi air masuk kerumah "; //sms content
mySerial.println(SMS);
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
mySerial.println (SMS);
mySerial.println ("Sent");
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
And this one is when i try using coroutine:
// TUNGGU 5 MENIT . JANGAN LUPA GANTI IMEI. power supply pakai laptop lama
#include <SoftwareSerial.h>
#include <AceRoutine.h>
using namespace ace_routine;
SoftwareSerial mySerial(3, 4);
String SMS;
int distance=5000;
int jarak=5000;
#define TRIG 11 //Module pins
#define ECHO 12
int times1;
int times2;
int times3;
char kode;
String message;
String CellNumtemp;
String CellNum;
String f1001;
String f1002;
String f1003;
COROUTINE(cek_jarak) {
COROUTINE_BEGIN();
digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIG, LOW); // Send pin low again
distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse
jarak= distance/58; //Convert the pulse duration to distance
//You can add other math functions to calibrate it well
if (jarak!=0){
Serial.print("Distance ");
Serial.print(jarak);
Serial.println(" cm");
if ((jarak<=200)&& (jarak>=100)){
times1++;
times2 = 0;
times3 = 0;
Serial.println(times1);
if (times1 == 20){
SMS = "air naik.";
COROUTINE_YIELD_INTERNAL();
setJump(&& sms);
times1 = 0;
delay(100000);
}}
else if ((jarak<100)&& (jarak>=50)){
times2++;
times1 = 0;
times3 = 0;
Serial.println(times2);
if (times2 == 20){
SMS = "siap2 mengangkat barang ke tempat yang tinggi";
COROUTINE_YIELD_INTERNAL();
setJump(&& sms);
times2 = 0;
delay(100000);
}}
else if ((jarak<50)&& (jarak>=10)){
times3++;
times1 = 0;
times2 = 0;
Serial.println(times3);
if (times3 == 20){
SMS = " sebentar lagi air masuk kerumah";
COROUTINE_YIELD_INTERNAL();
setJump(&& sms);
times3 = 0;
delay(100000);
}}
}
COROUTINE_YIELD();
COROUTINE_END();
}
COROUTINE(command) {
COROUTINE_BEGIN();
message =mySerial.readString();
mySerial.println (message);
CellNumtemp = message.substring(message.indexOf("+62"));
CellNum = CellNumtemp.substring(0,14);
// Serial.println("nomor pengirim:");
// Serial.println(CellNum);
if (message.indexOf("NEW f1001:") > -1) {
int idx = message.indexOf("f1001:");
f1001 = message.substring(idx+6, idx+6+f1001.length());
Serial.println (f1001);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("New f1001:"); //text content
mySerial.print(f1001); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
if (message.indexOf("NEW f1002:") > -1) {
int idx = message.indexOf("f1002:");
f1002 = message.substring(idx+6, idx+6+f1002.length());
Serial.println (f1002);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("New f1002:"); //text content
mySerial.print(f1002); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
if (message.indexOf("NEW f1003:") > -1) {
int idx = message.indexOf("f1003:");
f1003 = message.substring(idx+6, idx+6+f1003.length());
Serial.println (f1003);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("New f1003:"); //text content
mySerial.print(f1003); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
if (message.indexOf("REPORT") > -1) {
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
delay(1000);
mySerial.print("f1001:"); //text content
mySerial.print(f1001); //text content
mySerial.print("\nf1002:"); //text content
mySerial.print(f1002); //text content
mySerial.print("\nf1003:"); //text content
mySerial.print(f1003); //text content
delay(1000);
mySerial.println((char)26);
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
COROUTINE_YIELD();
COROUTINE_END();
}
COROUTINE(sms) {
COROUTINE_BEGIN();
mySerial.println ("Sending Message f1001");
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.println("AT+CNMI=1,2,0,0,0");
delay(1000);
mySerial.println ("Set SMS Number");
mySerial.println("AT+CMGS=\"" + f1001 + "\"\r"); //Mobile phone number to send message
delay(1000);
//String SMS = "Sample SMS is sending....."; //sms content
//String SMS = " sebentar lagi air masuk kerumah "; //sms content
mySerial.println(SMS);
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
mySerial.println (SMS);
mySerial.println ("Sent");
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
//-------------------------
mySerial.println ("Sending Message f1002");
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.println("AT+CNMI=1,2,0,0,0");
delay(1000);
mySerial.println ("Set SMS Number");
mySerial.println("AT+CMGS=\"" + f1002 + "\"\r"); //Mobile phone number to send message
delay(1000);
//String SMS = "Sample SMS is sending....."; //sms content
//String SMS = " sebentar lagi air masuk kerumah "; //sms content
mySerial.println(SMS);
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
mySerial.println (SMS);
mySerial.println ("Sent");
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
//---------------------------------------
mySerial.println ("Sending Message f1003");
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.println("AT+CNMI=1,2,0,0,0");
delay(1000);
mySerial.println ("Set SMS Number");
mySerial.println("AT+CMGS=\"" + f1003 + "\"\r"); //Mobile phone number to send message
delay(1000);
//String SMS = "Sample SMS is sending....."; //sms content
//String SMS = " sebentar lagi air masuk kerumah "; //sms content
mySerial.println(SMS);
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
mySerial.println (SMS);
mySerial.println ("Sent");
delay(1000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
COROUTINE_YIELD();
COROUTINE_END();
}
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("cek...");
pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input
pinMode(ECHO, INPUT_PULLUP);
digitalWrite(TRIG, HIGH);
Serial.println("System Started...");
delay(1000);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
mySerial.println("AT+CNMI=1,2,0,0,0");
f1001 = "+6281260707654";
f1002 = "+6282306402836";
f1003 = "+1234567890123";
delay(2000);
mySerial.print("AT+CMGDA=\"");
mySerial.println("DEL ALL\"");
delay(1000);
}
void loop() {
//mySerial.read();
if (mySerial.available() > 0){
command.runCoroutine();}
//cek_jarak.runCoroutine();
}