masahen

IOT Notifikasi dengan Pushingbox

Dec 24th, 2019
2,701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2.  
  3. // PushingBox scenario DeviceId code and API
  4. String deviceId = "**********"; //Masukkan Device ID PushingBox mu
  5. const char* logServer = "api.pushingbox.com";
  6.  
  7. const char* ssid = "******"; // nama wifi
  8. const char* password = "*******"; // password wifi
  9.  
  10. int btn = 2;
  11. int baca;
  12. void setup() {
  13.   Serial.begin(9600);
  14.   // Sending a notification to your mobile phone
  15.   // function takes the message as a parameter
  16.  pinMode(btn, LOW);
  17. }
  18.  
  19. void sendNotification(String message){
  20.  
  21.   Serial.println("- connecting to Home Router SID: " + String(ssid));
  22.  
  23.   WiFi.begin(ssid, password);
  24.   while (WiFi.status() != WL_CONNECTED) {
  25.     delay(500);
  26.     Serial.print(".");
  27.   }
  28.  
  29.   Serial.println();
  30.   Serial.println("- succesfully connected");
  31.   Serial.println("- starting client");
  32.  
  33.   WiFiClient client;
  34.  
  35.   Serial.println("- connecting to pushing server: " + String(logServer));
  36.   if (client.connect(logServer, 80)) {
  37.     Serial.println("- succesfully connected");
  38.    
  39.     String postStr = "devid=";
  40.     postStr += String(deviceId);
  41.     postStr += "&message_parameter=";
  42.     postStr += String(message);
  43.     postStr += "\r\n\r\n";
  44.    
  45.     Serial.println("- sending data...");
  46.    
  47.     client.print("POST /pushingbox HTTP/1.1\n");
  48.     client.print("Host: api.pushingbox.com\n");
  49.     client.print("Connection: close\n");
  50.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  51.     client.print("Content-Length: ");
  52.     client.print(postStr.length());
  53.     client.print("\n\n");
  54.     client.print(postStr);
  55.   }
  56.   client.stop();
  57.   Serial.println("- stopping the client");
  58. }
  59.  
  60. void loop() {
  61.   baca = digitalRead(btn);
  62.   if(baca == HIGH){
  63.     sendNotification("Rumah Anda Kemalingan !!!!"); // pesan yang dikirmkan ke Android ($message_parameter$)
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment