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 = "xxxxxx";
- const char* password = "xxxxxx";
- 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);
- // Parse JSON
- int size = result.length() + 1;
- char json[size];
- result.toCharArray(json, size);
- const size_t capacity = 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;
- }
- Serial.println("closing connection");
- delay(3000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement