#include <WiFi.h>
#include "DHTesp.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
/************************* WiFi Access Point ***************************/
#define WLAN_SSID "Wokwi-GUEST"
#define WLAN_PASS ""
/************************* Adafruit.io Setup ***************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // port 8883 untuk SSL
#define AIO_USERNAME "ikmalmughni"
#define AIO_KEY "aio_tLpl917zP737Y66R9WQRS4Pzu5PJ"
#define AIO_FEED_1 "/feeds/relay-lampu"
#define AIO_FEED_2 "/feeds/kondisi-kelembapan-ruangan"
#define AIO_FEED_3 "/feeds/kondisi-kelembapan-tanah"
#define AIO_FEED_4 "/feeds/kondisi-suhu-ruangan"
#define AIO_FEED_5 "/feeds/relay-pompa"
/************ Global State (you don't need to change this!) ************/
#define DHT_PIN 15
#define RELAYLAMP 4
#define RELAYPUMP 5
#define soilsensor 34
DHTesp dhtSensor;
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup feeds 'temp' and 'hmdt' for publishing.
Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME AIO_FEED_4);
Adafruit_MQTT_Publish hmdt = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME AIO_FEED_2);
Adafruit_MQTT_Publish soil = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME AIO_FEED_3);
// Setup a feed called 'onoff' for subscribing to changes to the ON/OFF button
Adafruit_MQTT_Subscribe onoffbtnLAMP = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME AIO_FEED_1);
Adafruit_MQTT_Subscribe onoffbtnPUMP = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME AIO_FEED_5);
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 10 seconds...");
mqtt.disconnect();
delay(10000); // wait 10 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
// Function called back when an onoffbutton feed message arrives
void onoffcallbackLAMP(char *datalamp, uint16_t len) {
Serial.print("Hey we're in an onoff callback, the button value is: ");
Serial.println(datalamp);
if (strcmp(datalamp, "ON") == 0) {
Serial.println("Turning LAMP ON");
digitalWrite(RELAYLAMP, HIGH);
} else {
Serial.println("Turning LAMP OFF");
digitalWrite(RELAYLAMP, LOW);
}
}
void onoffcallbackPUMP(char *datapump, uint16_t len) {
Serial.print("Hey we're in an onoff callback, the button value is: ");
Serial.println(datapump);
if (strcmp(datapump, "ON") == 0) {
Serial.println("Turning PUMP ON");
digitalWrite(RELAYPUMP, HIGH);
} else {
Serial.println("Turning PUMP OFF");
digitalWrite(RELAYPUMP, LOW);
}
}
void setup() {
Serial.begin(115200);
pinMode(RELAYLAMP, OUTPUT);
pinMode(RELAYPUMP, OUTPUT);
pinMode(soilsensor, INPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(); Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Set the callback function to be called when feed's message arrives
onoffbtnLAMP.setCallback(onoffcallbackLAMP);
onoffbtnPUMP.setCallback(onoffcallbackPUMP);
// Setup MQTT subscription for time feed.
mqtt.subscribe(&onoffbtnLAMP);
mqtt.subscribe(&onoffbtnPUMP);
}
unsigned long lastMsg = 0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets and callback em' busy subloop
// try to spend your time here:
mqtt.processPackets(10000);
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
if(! mqtt.ping()) {
mqtt.disconnect();
}
float nkelembapantanah = analogRead(soilsensor);
float kelembapantanah = map(nkelembapantanah, 0, 4095, 0, 100);
Serial.println("Kelembapan tanah: " + String(kelembapantanah, 0) + "%");
char strSoil[20];
sprintf(strSoil, "%.0f", kelembapantanah);
soil.publish(strSoil);
// Publish feeds
unsigned long now = millis();
if (now - lastMsg > 10000) {
lastMsg = now;
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temperature: " + String(data.temperature, 1) + "°C");
Serial.println("Humidity...: " + String(data.humidity, 0) + "%");
Serial.println("---");
char strTemperature[20], strHumidity[20];
sprintf(strTemperature, "%.1f", data.temperature);
sprintf(strHumidity, "%.0f", data.humidity);
temp.publish(strTemperature);
hmdt.publish(strHumidity);
}
}