ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By Christos Karras
#86794 Hello, I hope everyone is doing fine during this carantine time :D
So, I need some help with an IoT project im trying to build. I've successfully created a home automation system with esp8266 and a webserver. I'm using python and django for the webserver and the esp is connect with 8 channel relay. My issue is to be able to control a device with alexa. I did the example from sinric pro for a light and it turns on successfully. But when trying to merge the code from example and my code webserver + sinric pro doesnt not work together. Any ideas??

I'm ataching my code for this. The esp is connecting to the webserver under /home/get_relay_group_status and it's getting the json variables (r_status = false or true). I can also send merged code with sinric.

Code: Select all
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "wifiname";
const char* password = "wifipass";

const int relay1 = 05; // GPIO05
const int relay2 = 04; // GPIO04
const int relay3 = 00; // GPIO00
const int relay4 = 02; // GPIO02
const int relay5 = 14; // GPIO14
const int relay6 = 12; // GPIO12
const int relay7 = 13; // GPIO13
const int relay8 = 15; // GPIO15


void setup() {

  Serial.begin(115200);
  delay(4000);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);

}

void loop() {

  if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status

    HTTPClient http;

    http.begin("http://35.237.179.138//home/get_relay_group_status/"); //Specify the URL
    int httpCode = http.GET();                                        //Make the request

    if (httpCode > 0) { //Check for the returning code

      String payload = http.getString();

      const size_t capacity = JSON_ARRAY_SIZE(8) + 8 * JSON_OBJECT_SIZE(4) + 620;
      DynamicJsonDocument doc(capacity);

      char charBuf[capacity];

      payload.toCharArray(charBuf, capacity);

      deserializeJson(doc, charBuf);

      JsonObject root_0 = doc[0];
      bool root_0_r_status = root_0["r_status"];

      JsonObject root_1 = doc[1];
      bool root_1_r_status = root_1["r_status"];

      JsonObject root_2 = doc[2];
      bool root_2_r_status = root_2["r_status"];

      JsonObject root_3 = doc[3];
      bool root_3_r_status = root_3["r_status"];

      JsonObject root_4 = doc[4];
      bool root_4_r_status = root_4["r_status"];

      JsonObject root_5 = doc[5];
      bool root_5_r_status = root_5["r_status"];

      JsonObject root_6 = doc[6];
      bool root_6_r_status = root_6["r_status"];

      JsonObject root_7 = doc[7];
      bool root_7_r_status = root_7["r_status"];



      if (root_0_r_status) {
        Serial.println("Relay 1 is on ");
        digitalWrite(relay1, LOW);
      } else {
        Serial.println("Relay 1 is off ");
        digitalWrite(relay1, HIGH);
      }

      if (root_1_r_status) {
        Serial.println("Relay 2 is on ");
        digitalWrite(relay2, LOW);
      } else {
        Serial.println("Relay 2 is off ");
        digitalWrite(relay2, HIGH);
      }


      if (root_2_r_status) {
        Serial.println("Relay 3 is on ");
        digitalWrite(relay3, LOW);
      } else {
        Serial.println("Relay 3 is off ");
        digitalWrite(relay3, HIGH);
      }

      if (root_3_r_status) {
        Serial.println("Relay 4 is on ");
        digitalWrite(relay4, LOW);
      } else {
        Serial.println("Relay 4 is off ");
        digitalWrite(relay4, HIGH);
      }

      if (root_4_r_status) {
        Serial.println("Relay 5 is on ");
        digitalWrite(relay5, LOW);
      } else {
        Serial.println("Relay 5 is off ");
        digitalWrite(relay5, HIGH);
      }

      if (root_5_r_status) {
        Serial.println("Relay 6 is on ");
        digitalWrite(relay6, LOW);
      } else {
        Serial.println("Relay 6 is off ");
        digitalWrite(relay6, HIGH);
      }

      if (root_6_r_status) {
        Serial.println("Relay 7 is on ");
        digitalWrite(relay7, LOW);
      } else {
        Serial.println("Relay 7 is off ");
        digitalWrite(relay7, HIGH);
      }


      if (root_7_r_status) {
        Serial.println("Relay 8 is on ");
        digitalWrite(relay8, LOW);
      } else {
        Serial.println("Relay 8 is off ");
        digitalWrite(relay8, HIGH);
      }


    }

    else {
      Serial.println("Error on HTTP request");
    }

    http.end(); //Free the resources
  }

  delay(10000);

}