Advertisement
nedfire

sentcodeserv2

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