//======================================================================================================================================= // SMS_BT_IOT // This code implements a BT receiver on an Arduino Mega board fitted with a HC06 BT shield set to 115200 baud // Seria1 is usd to communicate to the BT shield // when it receives a message from the BT, it parses its contents and in the password is correct, actuates the commands in the message // at commands completion it sends back a message with status of the controlled actuators // every two seconds it sends a "alive" character to the gateway to retrigger its watchdog. if not sent the Gateway retries // the BT synchronization. // // Author : Ugo Schiara // Ver_01 : 02/01/2021 first release WARNING though I have written a more complex code, based on the same structure, and it works // correctly, this is ONLY the structure: you have to complete it with YOUR code //======================================================================================================================================== #include #define DEBUG 1 #define TRUE 1 #define FALSE 0 #define LIMITE 128 #define SECTIONS 6 //Globals unsigned long parte; char comando[LIMITE]; String password= ""; String Str_Command[SECTIONS]; int puntabuffer = 0; //============================================================================================================== void setup() { char chk = ' '; // empty char Serial.begin(115200); // init serial toward PC Serial1.begin(115200); // init serial toward BT (HC05 or HC06 shall be set accordingly) chk =' '; while (chk != '!') // wait for trigger character { if(Serial1.available()) // anything from BT ? { chk = Serial1.read(); // yes, get it #ifdef DEBUG Serial.println(chk); // echo to PC #endif } } // end while parte = millis(); // start timer for the "alive character" to the app } //============================================================================================================= void loop() { bool ce = FALSE; ce = Wait_for_SMS(); // anything from BT ? if (ce == TRUE) // yes, analyze the message { Parse_Command(); // creates an array of strings str[6] whose first is the password if (password == "Your_Write_Password") // now fixed in the code: better to be retrieved from the EEPROM { Actuate(); // write password correct, do the actuation and send message to BT } else if (password == "Your_Read_Password") // now fixed in the code: better to be retrieved from the EEPROM { Read_Actuate(); // read password correct, collect status and send message to BT } } if ((millis() - parte) >= (unsigned long)2000) // two seconds elapsed ? { parte = millis(); // retrigger the timer Simcmd("$"); // send "alive" character to the Gateway } } //================================================================================================================ // Write command received, put here the actuation commands, then send the feedback. //================================================================================================================ void Actuate() { // put here your actuation code Send_Config(); } //================================================================================================================ // Read Status and send it on BT to the gateway //================================================================================================================ void Read_Actuate() { // Collect_Status(); // Read the status of commands Send_Config(); // send using BT to the Gateway to send then via SMS } //================================================================================================================ // If a valid command has been received, parse its contents e creates the # SECTIONS command strings //================================================================================================================ void Parse_Command() { password = "Extracted from the SMS received"; Str_Command[0] = "Extracted from the SMS received"; // the same applies for all SECTIONS } //======================================================================================================================================= // Verify if any SMS from BT // If none, return FALSE //======================================================================================================================================= bool Wait_for_SMS() { byte i; byte ritch; char scarto; if(Serial1.available() == 0 ) // any characters from BT ? { return FALSE; // no characters, nothing to do and returns false } for(i = 0; i