-
-
Save sarasantos/23c0a883d0302b5f344e72f7406504b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include Software Serial library to communicate with SIM900 | |
#include <SoftwareSerial.h> | |
// Configure software serial port | |
SoftwareSerial SIM900(7, 8); | |
// Variable to store text message | |
String textMessage; | |
// Create a variable to store Lamp state | |
String lampState = "HIGH"; | |
// Relay connected to pin 12 | |
const int relay = 12; | |
void setup() { | |
// Automatically turn on the shield | |
digitalWrite(9, HIGH); | |
delay(1000); | |
digitalWrite(9, LOW); | |
delay(5000); | |
// Set relay as OUTPUT | |
pinMode(relay, OUTPUT); | |
// By default the relay is off | |
digitalWrite(relay, HIGH); | |
// Initializing serial commmunication | |
Serial.begin(9600); | |
SIM900.begin(9600); | |
// Give time to your GSM shield log on to network | |
delay(20000); | |
Serial.print("SIM900 ready…"); | |
// AT command to set SIM900 to SMS mode | |
SIM900.print("AT+CMGF=1\r"); | |
delay(100); | |
// Set module to send SMS data to serial out upon receipt | |
SIM900.print("AT+CNMI=2,2,0,0,0\r"); | |
delay(100); | |
} | |
void loop(){ | |
if(SIM900.available()>0){ | |
textMessage = SIM900.readString(); | |
Serial.print(textMessage); | |
delay(10); | |
} | |
if(textMessage.indexOf("ON")>=0){ | |
// Turn on relay and save current state | |
digitalWrite(relay, LOW); | |
lampState = "on"; | |
Serial.println("Relay set to ON"); | |
textMessage = ""; | |
} | |
if(textMessage.indexOf("OFF")>=0){ | |
// Turn off relay and save current state | |
digitalWrite(relay, HIGH); | |
lampState = "off"; | |
Serial.println("Relay set to OFF"); | |
textMessage = ""; | |
} | |
if(textMessage.indexOf("STATE")>=0){ | |
String message = "Lamp is " + lampState; | |
sendSMS(message); | |
Serial.println("Lamp state resquest"); | |
textMessage = ""; | |
} | |
} | |
// Function that sends SMS | |
void sendSMS(String message){ // AT command to set SIM900 to SMS mode | |
SIM900.println("AT+CMGF=1"); | |
delay(100); | |
// REPLACE THE X’s WITH THE RECIPIENT’S MOBILE NUMBER | |
// USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS | |
// Send the SMS | |
SIM900.println("AT+CMGS=\"+33616234297\""); | |
delay(100); | |
//Send theSMS | |
SIM900.println(message); | |
// End AT command with a ^Z, ASCII code 26 | |
SIM900.write(26); | |
delay(100); | |
SIM900.println(); | |
// Give module time to send SMS | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment