Advertisement
nedfire

NEW_espmotor3

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