Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <ArduinoJson.h>
- #include <HttpClient.h>
- #include <Servo.h>
- const char * ssid = "xxxxx";
- const char * password = "xxxxx";
- Servo servo;
- int pos = 0;
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print("Connecting..");
- }
- servo.attach(D8); //D8
- }
- void loop() {
- if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
- HTTPClient http; //Declare an object of class HTTPClient
- WiFiClient client;
- http.begin("http://3.1.222.209/getdata.php"); //Specify request destination
- int httpCode = http.GET(); //Send the request
- if (httpCode > 0) { //Check the returning code
- // String section="header";
- //String line = client.readStringUntil('\r');
- // String result = line.substring(1);
- String payload = http.getString(); //Get the request response payload
- Serial.println(payload); //Print the response payload
- // Parse JSON
- // int size = result.length() + 1;
- // char json[size];
- // result.toCharArray(json, size);
- StaticJsonBuffer < 500 > jsonBuffer;
- JsonObject & json_parsed = jsonBuffer.parseObject(payload);
- if (!json_parsed.success()) {
- Serial.println("parseObject() failed");
- Serial.println("No data");
- closeDoor();
- return;
- }
- // Fetch values.
- //
- // Most of the time, you can rely on the implicit casts.
- // In other case, you can do root["time"].as<long>();
- int status = json_parsed["status"];
- Serial.print("Status = ");
- Serial.println(status);
- http.end();
- int value = LOW;
- if (status == 0) {
- Serial.println("No");
- //http request to send Telegram alert to phone
- 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
- 08 4B 00 8C");
- int httpCode = http.GET();
- http.end();
- //closeDoor();
- value = HIGH;
- delay(3000);
- } else if (status == 1) {
- Serial.println("Yes");
- openDoor();
- delay(3000);
- }
- } else {
- Serial.println("Error on HTTP request");
- }
- //http.end();
- }
- delay(10000);
- }
- void openDoor() {
- for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
- // in steps of 1 degree
- servo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- }
- void closeDoor() {
- for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
- servo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement