Arduino HTTP and Relay

#include <ArduinoJson.h>
#include <ArduinoJson.hpp>

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_Sensor.h>


static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;

#define ChamberA_WATER_MOTOR_PIN D1
#define ChamberB_WATER_MOTOR_PIN D2

#define ChamberA_LED_PIN D3
#define ChamberB_LED_PIN D4
#define ChamberA_FAN_PIN D5
#define ChamberB_FAN_PIN D6

//WIFI Valid
uint32_t delayMS;
const char* ssid = "glasswort";
const char* password = "glasswort2116";
String host = "http://202.31.246.171:10021/";
String controlhost = "http://202.31.246.171:10021/control/control/last/";
const long interval = 1000;
unsigned long previousMillis = 0;
WiFiServer server(80);
WiFiClient client;
HTTPClient http;
const char* PLANT = "ChamberA";


void setup() {
  Serial.begin(115200);


  pinMode(ChamberA_FAN_PIN, OUTPUT);
  pinMode(ChamberA_WATER_MOTOR_PIN, OUTPUT);  
  pinMode(ChamberB_WATER_MOTOR_PIN, OUTPUT);  
  pinMode(ChamberA_LED_PIN, OUTPUT);



  //WiFi Settings
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  Serial.println("Server started");
}
void loop() {
  delay(delayMS);

  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    // Control 제어값 받기
    http.begin(client, controlhost);
    http.setTimeout(1000);
    int httpCode = http.GET();
    if (httpCode > 0) {
      Serial.printf("GET code : %d\n\n", httpCode);
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);

        StaticJsonBuffer<200> JSONBuffer;  //Memory pool
        JsonArray& root = JSONBuffer.parseArray(payload);
        for (int i = 0; i < root.size(); i++) {
          JsonObject& doc = root[i];

          const String plant = (String)doc["plant"];
          const int water = (int)doc["water_pump"];
          const int mist = (int)doc["mist_pump"];
          const int fan = (int)doc["fan"];
          const int led = (int)doc["led"];
          Serial.println(plant=="ChamberA");
          
          if(plant=="ChamberA"){
            if (water == 0) {
              digitalWrite(ChamberA_WATER_MOTOR_PIN, HIGH);
            } else {
              digitalWrite(ChamberA_WATER_MOTOR_PIN, LOW);
            }

            if (led == 0) {

              digitalWrite(ChamberA_LED_PIN, HIGH);
            } else {
              digitalWrite(ChamberA_LED_PIN, LOW);
            }
            if (fan == 0) {

              digitalWrite(ChamberA_FAN_PIN, HIGH);
            } else {
              digitalWrite(ChamberA_FAN_PIN, LOW);
            }
            if (mist == 0) {


            } else {
            }
          }


          if(plant=="ChamberB"){
            if (water == 0) {
              digitalWrite(ChamberB_WATER_MOTOR_PIN, HIGH);
            } else {
              digitalWrite(ChamberB_WATER_MOTOR_PIN, LOW);
            }

            if (led == 0) {

              digitalWrite(ChamberB_LED_PIN, HIGH);
            } else {
              digitalWrite(ChamberB_LED_PIN, LOW);
            }
            if (fan == 0) {

              digitalWrite(ChamberB_FAN_PIN, HIGH);
            } else {
              digitalWrite(ChamberB_FAN_PIN, LOW);
            }
            if (mist == 0) {


            } else {
            }
          }
          

        }
      }
    } else {
      Serial.println(controlhost);
      Serial.printf("GET failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
  }
}


#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_Sensor.h>


#include <DHT.h>
#include <DHT_U.h>
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
//DHT Valid
DHT_Unified dht(D1, DHT22);

//토양수분
#define makePCB_S335_PIN D2


//WIFI Valid
uint32_t delayMS;
const char* ssid = "glasswort";
const char* password = "glasswort2116";
String host = "http://202.31.246.171:10021/";

const long interval = 5000;
unsigned long previousMillis = 0;
WiFiServer server(80);
WiFiClient client;
HTTPClient http;
const char* PLANT = "ChamberA";


void setup() {
  Serial.begin(115200);

  //DHT Settings
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  delayMS = sensor.min_delay / 1000;


  //WiFi Settings
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  Serial.println("Server started");
}
void loop() {
  delay(delayMS);

  sensors_event_t event;
  dht.temperature().getEvent(&event);
  float humidity = event.relative_humidity;
  dht.humidity().getEvent(&event);
  float temp = event.temperature;
  //makePCB S335

  float moisture = analogRead(makePCB_S335_PIN);

  String strHumi = "";
  String strTemp = "";
  String strMoisture = "";

  strHumi.concat(humidity);
  strTemp.concat(temp);
  strMoisture.concat(moisture);

  Serial.println(strHumi + "\t" + strTemp + "\t" + strMoisture);
  //  analogWrite(waterMotorPIN,0.5);
  // 센서값 DB 전송 부분
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    String server = host + "control/sensor/insert/?plant=" + PLANT + "&temperature=" + String(temp) + "&humidity=" + String(humidity) + "&soil_moisture=" + String(moisture) + "&co2=" + String(0);
    Serial.print("Connect to ");
    Serial.println(server);
    http.begin(client, server);
    http.setTimeout(1000);
    int httpCode = http.GET();
    if (httpCode > 0) {
      Serial.printf("GET code : %d\n\n", httpCode);
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        // Serial.println(payload);
      }
    } else {
      // Serial.println(server);
      // Serial.printf("GET failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();

        
      
    } else {
      Serial.println(server);
      Serial.printf("GET failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
  }
}

Leave a Comment