Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ArduinoJson.h>
- #include <Servo.h>
- #include <HttpClient.h>
- Servo servo;
- const char* ssid = "TP-Link_DB6F";
- const char* password = "akupunya";
- //const char* ssid = "maidiee1234@unifi";
- //const char* password = "napsiah1";
- const char* host = "fully-grown-apprent.000webhostapp.com"; //replace it with your webhost url
- String url;
- int count = 0;
- void setup() {
- Serial.begin(115200);
- delay(100);
- //pinMode(BUILTIN_LED, OUTPUT);
- servo.attach(D8); //D8
- servo.write(0);
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("Netmask: ");
- Serial.println(WiFi.subnetMask());
- Serial.print("Gateway: ");
- Serial.println(WiFi.gatewayIP());
- // digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
- // but actually the LED is on; this is because
- // it is acive low on the ESP-01)
- //delay(100); // Wait for a second
- // digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
- // delay(100);
- }
- void loop() {
- Serial.print("connecting to ");
- Serial.println(host);
- WiFiClient client;
- const int httpPort = 80;
- if (!client.connect(host, httpPort)) {
- Serial.println("connection failed");
- return;
- }
- //url = "/read_all.php?id=1";
- url = "/getdata.php";
- Serial.println("URl Set..");
- Serial.print("Requesting URL: ");
- Serial.println(url);
- client.print(String("GET ") + url + " HTTP/1.0\r\n" + //changed fron http1.1 to http1.0
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- delay(500);
- String section="header";
- while(client.available()){
- String line = client.readStringUntil('\r');
- Serial.print(line);
- if(line=="{\"status\":\"1\"}")
- {
- Serial.println("Success");
- }
- // we’ll parse the HTML body here
- if (section=="header") { // headers..
- // Serial.println("header");
- if (line=="\n") { // skips the empty space at the beginning
- section="json";
- }
- }
- else if (section=="json") { // print the good stuff
- Serial.println("json");
- section="ignore";
- String result = line.substring(1);
- // HttpClient client;
- //client.print(String("GET ") + url + " HTTP/1.0\r\n" +
- // "Host: " + host + "\r\n" +
- // "Connection: close\r\n\r\n");
- //int index = 0;
- //char uselessJSONCrap[120];
- //while(client.connected())
- //{
- // while(client.available() > 0 && index < 118)
- // {
- // uselessJSONCrap[index++] = client.read();
- // uselessJSONCrap[index] = '\0';
- // }
- // Parse JSON
- int size = result.length() + 1;
- char json[size];
- result.toCharArray(json, size);
- // Serial.println("aftertoarray");
- //StaticJsonDocument<256u> doc;
- // DynamicJsonDocument doc(1024);
- // const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(2) + 30;
- // const size_t capacity = JSON_OBJECT_SIZE(1) + 20;
- // DynamicJsonDocument doc(capacity);
- //DynamicJsonDocument doc(200);
- // const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 10;
- // DynamicJsonDocument doc(capacity);
- // StaticJsonBuffer<200> jsonBuffer;
- // JsonObject& json_parsed = jsonBuffer.parseObject(json);
- // JsonObject& root = jsonBuffer.parseObject(json);
- const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 10;
- DynamicJsonDocument doc(capacity);
- deserializeJson(doc, json);
- auto error = deserializeJson(doc, json);
- Serial.println(json);
- //const char* status = doc["status"];
- if (error) {
- Serial.print(F("deserializeJson() failed with code "));
- Serial.println(error.c_str());
- return;
- }
- if (!error) {
- Serial.print(F("deserializeJson() succeed "));
- return;
- }
- // String pwd = doc["status"];
- //const char* pwd = doc["status"];
- //const char* pwd = doc[0]["status"]; // "1"
- // Serial.println(pwd);
- // int value = LOW;
- // if(pwd == "1"){
- //// openDoor();
- //// delay(3000);
- //Serial.println("Success door unlock");
- //
- // }
- // else if(pwd=="0"){
- //// closeDoor();
- //// value = HIGH;
- //// delay(3000);
- //Serial.println("Wrong door lock");
- // }
- // else{
- //// closeDoor();
- //// value = HIGH;
- //// delay(3000);
- //Serial.println("No data");
- // }
- //// int response = doc["Success:"];
- // //Serial.println(response);
- //
- // // if (!json_parsed.success())
- // //// {
- // // Serial.println("parseObject() failed");
- // // return;
- // // }
- //
- // // {"success":0,"password":[null]}json
- //// int obj.isNull();
- // // JsonObject obj = doc.as<JsonObject>();
- // int za = 0;
- // //String psswd_web;
- // // while(za == 0){
- // // if(data=="123"){
- // // Serial.println("Psswd Correct");
- // // za++;
- // // }
- //
- // // else{
- // // Serial.println("Psswd Wrong!");
- // // }
- // // }
- //
- ////}
- }//else
- // if (stringComplete) {
- // if (inputString=="your_password")
- // {
- // servo.write(90);
- // delay(300);
- // servo.write(0);
- // Serial.println(inputString);
- // clear the string:
- // inputString = "";
- // stringComplete = false; }
- //Serial.println();
- Serial.println("closing connection");
- delay(3000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement