#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Arduino_JSON.h>
//Sensor
#define PH_PIN 16
#define TDS_PIN 17
#define MQ_PIN 18
#define TURBIDITY_PIN 8
//Aktuator
#define Relay1PIN 40
#define Relay2PIN 39
#define Relay3PIN 38
#define BUZZER 37
#define LedRED 36
#define LedGREEN 35
//variabel value sensor
int16_t analogTurbidity, analogPH, analogTDS, analogMQ;
float PHValue, TemperatureValue;
int TurbidityValue, TDSValue, MQValue;
//variabel value aktuator
float solenoid, pump, relay, lamp, alarmStatus;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* urlAPI = " https://c312-2001-448a-5020-fbc1-eda8-2e7b-4b54-ea0f.ngrok-free.app/api/data";
WiFiClient client;
OneWire oneWire(5);
DallasTemperature TempSensor(&oneWire);
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32-S3!");
SetupWifi();
TempSensor.begin();
pinMode(Relay1PIN, OUTPUT);
pinMode(Relay2PIN, OUTPUT);
pinMode(Relay3PIN, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(LedRED, OUTPUT);
pinMode(LedGREEN, OUTPUT);
}
void loop() {
readSensor();
postData();
delay(1000); // this speeds up the simulation
}
void readSensor() {
//PH Meter
analogPH = analogRead(PH_PIN);
PHValue = ((float) analogPH / 4095.0) * 14;
PHValue = roundf(PHValue * 100) / 100;
//turbidity meter
analogTurbidity = analogRead(TURBIDITY_PIN);
TurbidityValue = (analogTurbidity / 4.095);
//temperature
TempSensor.requestTemperatures();
TemperatureValue = TempSensor.getTempCByIndex(0);
//TDS meter
analogTDS = analogRead(TDS_PIN);
TDSValue = (analogTDS / 4.095);
//MQ-135
analogMQ = analogRead(MQ_PIN);
MQValue = (analogMQ / 4.095);
}
void aktuator() {
if (PHValue == 8) {
digitalWrite(LedRED, LOW);
digitalWrite(LedGREEN, HIGH);
} else {
digitalWrite(LedRED, HIGH);
digitalWrite(LedGREEN, LOW);
}
if (PHValue == 8) {
digitalWrite(Relay1PIN, HIGH);
} else {
digitalWrite(Relay1PIN, LOW);
}
}
void SetupWifi() {
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.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void postData() {
HTTPClient http;
String response;
int responseCode;
StaticJsonDocument<200> buff;
String jsonParams;
buff["pH"] = PHValue;
buff["temperature"] = TemperatureValue;
buff["mq135"] = MQValue;
buff["TDS"] = TDSValue;
buff["turbidity"] = TurbidityValue;
serializeJson(buff, jsonParams);
Serial.println(jsonParams);
delay(2000);
http.begin(urlAPI);
http.addHeader("Content-Type", "application/json");
responseCode = http.POST(jsonParams);
response = http.getString();
Serial.print("Response : ");
Serial.print(responseCode);
if (responseCode > 0) {
Serial.print(" - ");
Serial.println(response);
} else {
Serial.print(" - ");
Serial.println(http.errorToString(responseCode).c_str());
}
http.end();
}
void getDataConfig() {
HTTPClient http;
String response;
JSONVar jsonResponse;
int responseCode;
http.begin(urlAPI);
responseCode = http.GET();
response = http.getString();
Serial.print("Response : ");
Serial.print(responseCode);
if (responseCode > 0) {
Serial.print(" - ");
Serial.println(response);
jsonResponse = JSON.parse(response);
relay = JSON.stringify(jsonResponse["data"]["Relay"]).toInt();
solenoid = JSON.stringify(jsonResponse["data"]["solenoid"]).toInt();
lamp = JSON.stringify(jsonResponse["data"]["buzzer"]).toInt();
pump = JSON.stringify(jsonResponse["data"]["pump"]).toInt();
alarmStatus = JSON.stringify(jsonResponse["data"]["alarm"]).toInt();
} else {
Serial.print(" - ");
Serial.println(http.errorToString(responseCode).c_str());
}
}
Loading
ds18b20
ds18b20