Advertisement
nedfire

NodeMCU telegram servo

Jun 5th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 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. const char * ssid = "maidiee1234@unifi";
  11. const char * password = "napsiah1";
  12.  
  13. const char * host = "maker.ifttt.com";
  14. const int httpsPort = 443;
  15. Servo servo;
  16. int pos = 0;
  17.  
  18. void setup() {
  19.  
  20. Serial.begin(115200);
  21.  
  22. Serial.print("Connecting to wifi");
  23. WiFi.begin(ssid, password);
  24. while (WiFi.status() != WL_CONNECTED) {
  25. delay(500);
  26. Serial.print(".");
  27. }
  28.  
  29. Serial.println("\r\nWiFi connected.");
  30. Serial.println("access point:");
  31. Serial.println(WiFi.SSID());
  32. Serial.println("ip address:");
  33. Serial.println(WiFi.localIP());
  34.  
  35. servo.attach(D8); //D8
  36.  
  37. // wait for WiFi connection
  38.  
  39. }
  40. void loop() {
  41. if (WiFi.status() == WL_CONNECTED) {
  42.  
  43. HTTPClient http;
  44. WiFiClientSecure client;
  45.  
  46. http.begin("https://fully-grown-apprent.000webhostapp.com/getdata.php", "42 E9 F5 F9 30 21 14 CD 75 A1 41 EF 39 33 E8:D4 C7 97 B9 7C");
  47.  
  48.  
  49. int httpCode = http.GET();
  50. if (httpCode > 0) { //Check for the returning code
  51.  
  52. String payload = http.getString();
  53. Serial.println(httpCode);
  54. Serial.println(payload);
  55.  
  56. // Parse JSON
  57.  
  58.  
  59. StaticJsonBuffer < 500 > jsonBuffer;
  60. JsonObject & json_parsed = jsonBuffer.parseObject(payload);
  61. if (!json_parsed.success()) {
  62. Serial.println("parseObject() failed");
  63. Serial.println("No data");
  64. return;
  65. }
  66.  
  67. // Fetch values.
  68.  
  69. // Most of the time, you can rely on the implicit casts.
  70. // In other case, you can do root["time"].as<long>();
  71.  
  72. int status = json_parsed["status"];
  73.  
  74. Serial.print("Status = ");
  75. Serial.println(status);
  76. http.end();
  77. int value = LOW;
  78. if (status == 0) {
  79. Serial.println("No");
  80. //http request to send Telegram alert to phone
  81. http.begin("https://maker.ifttt.com/trigger/passwrong/with/key/xxxxxxxxxxxxxxxxx", "AA 75 CB 41 2E D5
  82. F9 97 FF 5D A0 8B 7D AC 12 21 08 4B 00 8C");
  83. int httpCode = http.GET();
  84. http.end();
  85.  
  86. closeDoor();
  87. value = HIGH;
  88. delay(3000);
  89.  
  90. } else if (status == 1) {
  91. Serial.println("Yes");
  92. openDoor();
  93. delay(3000);
  94. }
  95.  
  96. } else {
  97. Serial.println("Error on HTTP request");
  98. }
  99. //http.end();
  100. }
  101.  
  102. delay(10000);
  103. }
  104. void openDoor() {
  105. for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
  106. // in steps of 1 degree
  107. servo.write(pos); // tell servo to go to position in variable 'pos'
  108. delay(15); // waits 15ms for the servo to reach the position
  109. }
  110.  
  111. }
  112.  
  113. void closeDoor() {
  114. for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
  115. servo.write(pos); // tell servo to go to position in variable 'pos'
  116. delay(15); // waits 15ms for the servo to reach the position
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement