Advertisement
nedfire

NEW_espmotor4

Jul 13th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. #include <ESP8266HTTPClient.h>
  4.  
  5. #include<ArduinoJson.h>
  6.  
  7. #include <Servo.h>
  8.  
  9.  
  10.  
  11. const char * ssid = "xxxxxxx";
  12. const char * password = "xxxxxx";
  13. const char * host = "maker.ifttt.com";
  14. const int httpsPort = 443;
  15. Servo servo;
  16. int pos = 0;
  17.  
  18. void setup() {
  19. pinMode(D0, OUTPUT); //red
  20. pinMode(D1, OUTPUT); //yellow
  21. pinMode(D2, OUTPUT); //green
  22. Serial.begin(115200);
  23.  
  24. Serial.print("Connecting to wifi");
  25. WiFi.begin(ssid, password);
  26. while (WiFi.status() != WL_CONNECTED) {
  27. delay(500);
  28. Serial.print(".");
  29. }
  30.  
  31. Serial.println("\r\nWiFi connected.");
  32. Serial.println("access point:");
  33. Serial.println(WiFi.SSID());
  34. Serial.println("ip address:");
  35. Serial.println(WiFi.localIP());
  36.  
  37. servo.attach(D8); //D8
  38.  
  39. digitalWrite(D0, HIGH); //red
  40. digitalWrite(D1, HIGH);
  41. digitalWrite(D2, HIGH);
  42. delay(2000);
  43. digitalWrite(D0, LOW); //red
  44. digitalWrite(D1, LOW);
  45. digitalWrite(D2, LOW);
  46.  
  47. // wait for WiFi connection
  48.  
  49. }
  50. void loop() {
  51. if (WiFi.status() == WL_CONNECTED) {
  52.  
  53. HTTPClient http;
  54. WiFiClientSecure client;
  55.  
  56. http.begin("https://fully-grown-apprent.000webhostapp.com/getdata.php", "5B:FB:D1:D4:49:D3:0F:A9:C6:40:03:34:BA:E0:24:05:AA:D2:E2:01");
  57.  
  58. int httpCode = http.GET();
  59. if (httpCode > 0) { //Check for the returning code
  60.  
  61. String payload = http.getString();
  62. Serial.println(httpCode);
  63. Serial.println(payload);
  64.  
  65. // Parse JSON
  66.  
  67. StaticJsonBuffer < 500 > jsonBuffer;
  68. JsonObject & json_parsed = jsonBuffer.parseObject(payload);
  69. if (!json_parsed.success()) {
  70. Serial.println("parseObject() failed");
  71. Serial.println("No data");
  72.  
  73. digitalWrite(D0, LOW);
  74. digitalWrite(D1, HIGH); //yellow
  75. digitalWrite(D2, LOW);
  76. servo.write(0);
  77. //closeDoor(); //for the door to close after open
  78. return;
  79. }
  80.  
  81. // Fetch values.
  82.  
  83. // Most of the time, you can rely on the implicit casts.
  84. // In other case, you can do root["time"].as<long>();
  85.  
  86. int status = json_parsed["status"];
  87.  
  88. Serial.print("Status = ");
  89. Serial.println(status);
  90. http.end();
  91. //int value = LOW;
  92. if (status == 0) {
  93. Serial.println("No");
  94. digitalWrite(D0, HIGH); //red
  95. digitalWrite(D1, LOW);
  96. digitalWrite(D2, LOW);
  97.  
  98. //http request to send Telegram alert to phone
  99. http.begin("https://maker.ifttt.com/trigger/passwrong/with/key/i1PfqgGnzPAkrGKLeS1ZVubUqgPxe5oQ1WgAbVaNQJ9", "AA 75 CB 41 2E D5 F9 97 FF 5D A0 8B 7D AC 12 21 08 4B 00 8C");
  100. int httpCode = http.GET();
  101. http.end();
  102.  
  103. //closeDoor();
  104. //value = HIGH;
  105. delay(3000);
  106.  
  107. } else if (status == 1) {
  108. Serial.println("Yes");
  109.  
  110. digitalWrite(D0, LOW);
  111. digitalWrite(D1, LOW);
  112. digitalWrite(D2, HIGH); //green
  113.  
  114. servo.write(180);
  115. //openDoor();
  116. delay(3000);
  117.  
  118. }
  119.  
  120. } else {
  121. Serial.println("Error on HTTP request");
  122. }
  123. //http.end();
  124. } //wifi comm
  125.  
  126. delay(10000);
  127. } //end void loop
  128. void openDoor() {
  129. for (pos = 0; pos <= 180; pos += 2) { // goes from 0 degrees to 180 degrees
  130. // in steps of 1 degree
  131. servo.write(pos); // tell servo to go to position in variable 'pos'
  132. delay(15); // waits 15ms for the servo to reach the position
  133. }
  134.  
  135. }
  136.  
  137. void closeDoor() {
  138. for (pos = 180; pos >= 0; pos -= 2) { // goes from 180 degrees to 0 degrees
  139. servo.write(pos); // tell servo to go to position in variable 'pos'
  140. delay(15); // waits 15ms for the servo to reach the position
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement