1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <SoftwareSerial.h>
// Configure software serial port
SoftwareSerial SIM900(4, 5);
// Variable to store text message
String textMessage;
// Create a variable to store Lamp state
String lampState = "HIGH";
// Relay connected to pin 6
const int relay = 6;
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, LOW);
// Initializing serial commmunication
Serial.begin(9600);
SIM900.begin(9600);
// Give time to your GSM shield log on to network
delay(10000);
Serial.print("SIM900 ready");
// AT command Configure le mode texte SMS
//SIM900.print("AT+CMGF=1\r");
SIM900.print("AT+CMGF=1\r");
delay(100);
// Définir le module pour envoyer les données SMS à la sortie série lors de la réception
SIM900.print("AT+CNMI=1,2,0,0,0\r");
delay(100);
Serial.print("ici commence void loop");
}
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, HIGH);
lampState = "on";
Serial.println("Relay set to ON");
textMessage = "";
}
if(textMessage.indexOf("OFF")>=0){
// Turn off relay and save current state
digitalWrite(relay, LOW);
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 = "";
Serial.print("fin void loop ON");
}
}
// Function that sends SMS
void sendSMS(String message){
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER
// USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS
SIM900.println("AT + CMGS = \"+XXX\"\r"); // c'est mon numero de gsm
delay(1000);
// Send the SMS
SIM900.println(message);
delay(1000);
// End AT command with a ^Z, ASCII code 26
SIM900.println((char)26);
delay(1000);
SIM900.println();
// Give module time to send SMS
delay(5000);
} |
Partager