//motion_sensor_and_relay_with_timer_whatsapp.ino #include String router_SSID="Your router SSID"; String router_password="Your router password"; String whatsapp_number="yourphonenumber"; String whatsapp_apikey="your api key"; int sensor_pin=0; int sensor_value; int sensor_state; int relay_pin=2; int relay_state=1; //set mininum time light (relay) stays on to 60 sec; //Don't go below 60000 (60 sec) because whatsapp will block you //for sending too many messages to quickly; long min_light_time=60000; long light_start_time; int text_message_active=0; //-------------------- void setup() { //-------------------- pinMode(sensor_pin, INPUT); pinMode(relay_pin,OUTPUT); Serial.begin(115200); Serial.println(); connect_to_router(router_SSID,router_password); } //-------------------- void loop() { //-------------------- read_sensor(); } //-------------------- void read_sensor() { //-------------------- sensor_state=digitalRead(sensor_pin); if(sensor_state==1){ //turn the light on digitalWrite(relay_pin,0); //start the light timer from the beginning light_start_time=millis(); if(text_message_active==0){ //Only send message when sensor is triggered but //only when the light first turns on send_whatsapp_message(whatsapp_number,whatsapp_apikey,"Intruder Alert on Sensor # 3."); Serial.println("Sending alert to WhasApp"); } text_message_active=1; } if(sensor_state==0){ //turn the light off if(millis()>light_start_time+min_light_time){ digitalWrite(relay_pin,1); text_message_active=0; } } delay(50); } //-------------------- void connect_to_router(String ssid,String password){ //-------------------- Serial.print("Connecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(" connected"); } //-------------------- String www(String html_method,String host,String url,int port,String post_data){ //-------------------- host.replace(" ","%20"); url.replace(" ","%20"); url.replace("\n","%0A"); url.replace("#","%23"); String head; String response; WiFiClient client; if (!client.connect(host, port)) { return "Could not connect with "+host; } if(html_method=="get"){ client.println("GET "+url+" HTTP/1.1"); client.println("Accept: text/*"); client.println("Host: "+host); client.println("Connection: close"); client.println(""); } if(html_method=="post"){ client.println("POST "+url+" HTTP/1.1"); client.println("Host: "+host); client.println("Content-Type: application/x-www-form-urlencoded"); client.println("Content-Length: "+String(post_data.length()) ); client.println(""); client.println(post_data); } int timeout = millis() + 5000; while (client.available() == 0) { if (timeout-millis()<0){ client.stop(); return "Could not connect with "+host; } } String str; while (client.available() > 0) { String line = client.readStringUntil('\n'); head+=line+"\n"; str+=line; } int n=str.indexOf("\r\r"); //Serial.println("---------- header -----"); //Serial.println(head.substring(0,n+1)); //Serial.println("---------- end header ----"); String webpage=str.substring(n); return webpage; } //-------------------- String send_whatsapp_message(String num,String apikey,String msg){ //-------------------- String url="/whatsapp.php?phone="+whatsapp_number+"&text="+msg+"&apikey="+whatsapp_apikey; String whatsapp_reply=www("get","api.callmebot.com",url, 80,""); return whatsapp_reply; }