Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.0 kB
1
Indexable
Never
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>

const char *ssid = "admin";
const char *password = "12345689";

const char *server = "https://iot-s3aq.onrender.com"; // Server URL

StaticJsonDocument<200> PINS;


WiFiClientSecure client;

void setup()
{
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  delay(100);

  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    // wait 1 second for re-trying
    delay(1000);
  }

  Serial.print("Connected to ");
  Serial.println(ssid);

  client.setInsecure();

  Serial.println("\nStarting connection to server...");

  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);

  PINS["D0"] = D0;
  PINS["D1"] = D1;
  PINS["D2"] = D2;
  PINS["D3"] = D3;
  PINS["D4"] = D4;
  PINS["D5"] = D5;
  PINS["D6"] = D6;
  PINS["D7"] = D7;
  PINS["D8"] = D8;
  PINS["A0"] = A0;


}

void loop()
{


  if (!client.connect(server, 443))
    Serial.println("Connection failed!");
  else
  {
    Serial.println("Connected to server!");
    // Make a HTTP request:
    client.println("GET https://iot-s3aq.onrender.com/api/v1/devices-adv HTTP/1.0");
    client.println("Host: iot-s3aq.onrender.com");
    client.println("Connection: close");
    client.println();

    while (client.connected())
    {
      String line = client.readStringUntil('\n');
      if (line == "\r")
      {
        Serial.println("headers received");
        break;
      }
    }




    String response = client.readString();
    //Serial.println(response);

    const size_t capacity = JSON_ARRAY_SIZE(100) + 3 * JSON_OBJECT_SIZE(3) + 120;
    DynamicJsonDocument doc(capacity);

    DeserializationError error = deserializeJson(doc, response);


    const int errorCode = doc["errorCode"];
    const char* message = doc["message"];
    const JsonArray data = doc["data"];



    Serial.println(errorCode);
    Serial.println(message);
    //Serial.println(data);

    Serial.println("Start check json");

    for (JsonObject obj : data) {
      const char* pin = obj["pin"];
      const int statusPin = obj["status"];
      const int  intPin = PINS[pin];

      Serial.println(pin);
      Serial.println(intPin);
      Serial.println(statusPin);


      if (statusPin == 0) {
        Serial.println("tat");

        digitalWrite(intPin, LOW);

      } else if (statusPin == 1) {
        Serial.println("bat");

        digitalWrite(intPin, HIGH);

      } else {
        Serial.println("Status out of range");
      }

      Serial.println("end");

    }
    Serial.println("End check json");
    delay(1000);
    client.stop();
  }
}