Advertisement
nedfire

NodeMCU lock program

May 25th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 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. const char* ssid = "TP-Link_DB6F";
  8. const char* password = "akupunya";
  9. //const char* ssid = "maidiee1234@unifi";
  10. //const char* password = "napsiah1";
  11.  
  12.  
  13. const char* host = "fully-grown-apprent.000webhostapp.com"; //replace it with your webhost url
  14. String url;
  15. int count = 0;
  16. void setup() {
  17. Serial.begin(115200);
  18. delay(100);
  19. //pinMode(BUILTIN_LED, OUTPUT);
  20.  
  21. servo.attach(D8); //D8
  22. servo.write(0);
  23.  
  24. Serial.println();
  25. Serial.println();
  26. Serial.print("Connecting to ");
  27. Serial.println(ssid);
  28.  
  29. WiFi.begin(ssid, password);
  30. while (WiFi.status() != WL_CONNECTED) {
  31. delay(500);
  32. Serial.print(".");
  33. }
  34.  
  35. Serial.println("");
  36. Serial.println("WiFi connected");
  37. Serial.println("IP address: ");
  38. Serial.println(WiFi.localIP());
  39. Serial.print("Netmask: ");
  40. Serial.println(WiFi.subnetMask());
  41. Serial.print("Gateway: ");
  42. Serial.println(WiFi.gatewayIP());
  43.  
  44.  
  45. // digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  46. // but actually the LED is on; this is because
  47. // it is acive low on the ESP-01)
  48.  
  49. //delay(100); // Wait for a second
  50. // digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  51.  
  52. // delay(100);
  53. }
  54.  
  55. void loop() {
  56.  
  57. Serial.print("connecting to ");
  58. Serial.println(host);
  59.  
  60. WiFiClient client;
  61. const int httpPort = 80;
  62. if (!client.connect(host, httpPort)) {
  63. Serial.println("connection failed");
  64. return;
  65. }
  66.  
  67. //url = "/read_all.php?id=1";
  68. url = "/getdata.php";
  69.  
  70. Serial.println("URl Set..");
  71.  
  72. Serial.print("Requesting URL: ");
  73. Serial.println(url);
  74.  
  75. client.print(String("GET ") + url + " HTTP/1.0\r\n" + //changed fron http1.1 to http1.0
  76. "Host: " + host + "\r\n" +
  77. "Connection: close\r\n\r\n");
  78. delay(500);
  79. String section="header";
  80. while(client.available()){
  81.  
  82.  
  83.  
  84. String line = client.readStringUntil('\r');
  85. Serial.print(line);
  86. if(line=="{\"status\":\"1\"}")
  87. {
  88. Serial.println("Success");
  89. }
  90. // we’ll parse the HTML body here
  91. if (section=="header") { // headers..
  92. // Serial.println("header");
  93. if (line=="\n") { // skips the empty space at the beginning
  94. section="json";
  95. }
  96. }
  97. else if (section=="json") { // print the good stuff
  98. Serial.println("json");
  99. section="ignore";
  100. String result = line.substring(1);
  101.  
  102. // HttpClient client;
  103. //client.print(String("GET ") + url + " HTTP/1.0\r\n" +
  104. // "Host: " + host + "\r\n" +
  105. // "Connection: close\r\n\r\n");
  106.  
  107. //int index = 0;
  108. //char uselessJSONCrap[120];
  109. //while(client.connected())
  110. //{
  111. // while(client.available() > 0 && index < 118)
  112. // {
  113. // uselessJSONCrap[index++] = client.read();
  114. // uselessJSONCrap[index] = '\0';
  115. // }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. // Parse JSON
  132. int size = result.length() + 1;
  133. char json[size];
  134. result.toCharArray(json, size);
  135. // Serial.println("aftertoarray");
  136.  
  137. //StaticJsonDocument<256u> doc;
  138. // DynamicJsonDocument doc(1024);
  139.  
  140. // const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(2) + 30;
  141. // const size_t capacity = JSON_OBJECT_SIZE(1) + 20;
  142. // DynamicJsonDocument doc(capacity);
  143. //DynamicJsonDocument doc(200);
  144. // const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 10;
  145. // DynamicJsonDocument doc(capacity);
  146. // StaticJsonBuffer<200> jsonBuffer;
  147. // JsonObject& json_parsed = jsonBuffer.parseObject(json);
  148.  
  149. // JsonObject& root = jsonBuffer.parseObject(json);
  150. const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 10;
  151. DynamicJsonDocument doc(capacity);
  152. deserializeJson(doc, json);
  153. auto error = deserializeJson(doc, json);
  154. Serial.println(json);
  155. //const char* status = doc["status"];
  156. if (error) {
  157. Serial.print(F("deserializeJson() failed with code "));
  158. Serial.println(error.c_str());
  159. return;
  160. }
  161. if (!error) {
  162. Serial.print(F("deserializeJson() succeed "));
  163. return;
  164. }
  165.  
  166.  
  167.  
  168. // String pwd = doc["status"];
  169. //const char* pwd = doc["status"];
  170.  
  171. //const char* pwd = doc[0]["status"]; // "1"
  172. // Serial.println(pwd);
  173. // int value = LOW;
  174. // if(pwd == "1"){
  175. //// openDoor();
  176. //// delay(3000);
  177. //Serial.println("Success door unlock");
  178. //
  179. // }
  180. // else if(pwd=="0"){
  181. //// closeDoor();
  182. //// value = HIGH;
  183. //// delay(3000);
  184. //Serial.println("Wrong door lock");
  185. // }
  186. // else{
  187. //// closeDoor();
  188. //// value = HIGH;
  189. //// delay(3000);
  190. //Serial.println("No data");
  191. // }
  192. //// int response = doc["Success:"];
  193. // //Serial.println(response);
  194. //
  195. // // if (!json_parsed.success())
  196. // //// {
  197. // // Serial.println("parseObject() failed");
  198. // // return;
  199. // // }
  200. //
  201. // // {"success":0,"password":[null]}json
  202. //// int obj.isNull();
  203. // // JsonObject obj = doc.as<JsonObject>();
  204. // int za = 0;
  205. // //String psswd_web;
  206. // // while(za == 0){
  207. // // if(data=="123"){
  208. // // Serial.println("Psswd Correct");
  209. // // za++;
  210. // // }
  211. //
  212. // // else{
  213. // // Serial.println("Psswd Wrong!");
  214. // // }
  215. // // }
  216. //
  217. ////}
  218.  
  219. }//else
  220. // if (stringComplete) {
  221. // if (inputString=="your_password")
  222. // {
  223. // servo.write(90);
  224. // delay(300);
  225. // servo.write(0);
  226. // Serial.println(inputString);
  227. // clear the string:
  228. // inputString = "";
  229. // stringComplete = false; }
  230.  
  231.  
  232.  
  233.  
  234. //Serial.println();
  235. Serial.println("closing connection");
  236. delay(3000);
  237.  
  238. }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement