Advertisement
nedfire

NodeMCU original lock program

May 26th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ArduinoJson.h>
  3. #include <Servo.h>
  4. #include <HttpClient.h>
  5.  
  6. Servo servo;
  7.  
  8. const char* ssid = "xxxxxx";
  9. const char* password = "xxxxxx";
  10.  
  11.  
  12. const char* host = "fully-grown-apprent.000webhostapp.com"; //replace it with your webhost url
  13. String url;
  14. int count = 0;
  15. void setup() {
  16. Serial.begin(115200);
  17. delay(100);
  18. //pinMode(BUILTIN_LED, OUTPUT);
  19.  
  20. servo.attach(D8); //D8
  21. servo.write(0);
  22.  
  23. Serial.println();
  24. Serial.println();
  25. Serial.print("Connecting to ");
  26. Serial.println(ssid);
  27.  
  28. WiFi.begin(ssid, password);
  29. while (WiFi.status() != WL_CONNECTED) {
  30. delay(500);
  31. Serial.print(".");
  32. }
  33.  
  34. Serial.println("");
  35. Serial.println("WiFi connected");
  36. Serial.println("IP address: ");
  37. Serial.println(WiFi.localIP());
  38. Serial.print("Netmask: ");
  39. Serial.println(WiFi.subnetMask());
  40. Serial.print("Gateway: ");
  41. Serial.println(WiFi.gatewayIP());
  42.  
  43.  
  44. // digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  45. // but actually the LED is on; this is because
  46. // it is acive low on the ESP-01)
  47.  
  48. //delay(100); // Wait for a second
  49. // digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  50.  
  51. // delay(100);
  52. }
  53.  
  54. void loop() {
  55.  
  56. Serial.print("connecting to ");
  57. Serial.println(host);
  58.  
  59. WiFiClient client;
  60. const int httpPort = 80;
  61. if (!client.connect(host, httpPort)) {
  62. Serial.println("connection failed");
  63. return;
  64. }
  65.  
  66. //url = "/read_all.php?id=1";
  67. url = "/getdata.php";
  68.  
  69. Serial.println("URl Set..");
  70.  
  71. Serial.print("Requesting URL: ");
  72. Serial.println(url);
  73.  
  74. client.print(String("GET ") + url + " HTTP/1.0\r\n" + //changed fron http1.1 to http1.0
  75. "Host: " + host + "\r\n" +
  76. "Connection: close\r\n\r\n");
  77. delay(500);
  78. String section="header";
  79. while(client.available()){
  80.  
  81.  
  82.  
  83. String line = client.readStringUntil('\r');
  84. Serial.print(line);
  85. if(line=="{\"status\":\"1\"}")
  86. {
  87. Serial.println("Success");
  88. }
  89. // we’ll parse the HTML body here
  90. if (section=="header") { // headers..
  91. // Serial.println("header");
  92. if (line=="\n") { // skips the empty space at the beginning
  93. section="json";
  94. }
  95. }
  96. else if (section=="json") { // print the good stuff
  97. Serial.println("json");
  98. section="ignore";
  99. String result = line.substring(1);
  100.  
  101. // Parse JSON
  102. int size = result.length() + 1;
  103. char json[size];
  104. result.toCharArray(json, size);
  105. const size_t capacity = JSON_OBJECT_SIZE(1) + 10;
  106. DynamicJsonDocument doc(capacity);
  107. deserializeJson(doc, json);
  108. auto error = deserializeJson(doc, json);
  109. // Serial.println(json);
  110. const char* status = doc["status"];
  111. if (error) {
  112. Serial.print(F("deserializeJson() failed with code "));
  113. Serial.println(error.c_str());
  114. return;
  115. }
  116. if (!error) {
  117. Serial.print(F("deserializeJson() succeed "));
  118. return;
  119. }
  120.  
  121. Serial.println("closing connection");
  122. delay(3000);
  123.  
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement