#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define TEMP_SENSOR 4
#define PH_SENSOR 35
#define WATER_LEVEL 34
//----------------------------------------
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "Holuwaferanmi"
#define AIO_KEY "aio_oHqM34c3GcbwZMoaNRfIRoCKsgyU"
WiFiClient client;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
// oneWire instance to communicate with DS18B20 sensor
OneWire oneWire(TEMP_SENSOR);
DallasTemperature tempSensor(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
// 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_KEY);
Adafruit_MQTT_Publish ph_feed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/ph-level");
Adafruit_MQTT_Publish temp_feed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature");
Adafruit_MQTT_Publish waterL_feed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/water-level");
float temp;
float pH_val;
const char *water_status[] = {"LOW", "MID", "OK"};
uint8_t water_level = 0;
//icon for termometer
byte thermometer_icon[8] =
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};
byte water_icon[8] = //icon for water droplet
{
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110,
};
void monitorParams(){
// Read temperature from DS18B20 sensor
tempSensor.requestTemperatures();
temp = tempSensor.getTempCByIndex(0);
// Read pH Value
int pH_adc = analogRead(PH_SENSOR);
pH_val = (float)pH_adc * 14.0 / 4095;
// pH_val = map(pH_adc, 0, 4095, 0.0, 14.0);
// Read Water level value
int wLevel_adc = analogRead(WATER_LEVEL);
water_level = map(wLevel_adc, 0, 4095, 100, 0);
}
void display(){
// refresh only if value changes
static float temp_d = temp;
static float pH_val_d = pH_val;
static int water_level_d = water_level;
if (temp_d != temp || pH_val_d != pH_val || water_level_d != water_level){
// LCD.clear();
temp_d = temp;
pH_val_d = pH_val;
water_level_d = water_level;
}
LCD.home();
// LCD.print("pH:"+String(pH_val, 1)+" Temp:"+String(temp, 1)+"C ");
LCD.print("pH: ");
LCD.print(pH_val, 1);
LCD.print(" ");
LCD.setCursor(0, 1);
LCD.write(1);
LCD.print(" " + String(temp, 1) + "C ");
LCD.write(2);
LCD.print(" " + String(water_level)+"% ");
}
void cloudUpdate() {
if (mqtt.connected()) {
// publish all parameters to clou
ph_feed.publish(ph_val);
temp_feed.publish(temp);
waterL_feed.publish(water_level);
} else {
// reconnect to the MQTT server
int8_t status;
while (!mqtt.connected() && WiFi.status() == WL_CONNECTED) {
Serial.print("Connecting to MQTT... ");
if ((status = mqtt.connect()) == 0) {
Serial.println("connected");
} else {
Serial.print("failed, status code =");
Serial.print(mqtt.connectErrorString(status));
Serial.println(" try again in 5 seconds");
// wait 5 seconds before retrying
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
// }
}
void setup() {
// put you setup code here, to run once:
// Start the DS18B20 sensor
tempSensor.begin();
LCD.init();
LCD.backlight();
LCD.createChar(1, thermometer_icon);
LCD.createChar(2, water_icon);
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi...");
WiFi.begin("Wokwi-GUEST", "", 6);
// Wait 10s max for WiFi fo connect
uint32_t time_start = millis();
while (millis() - time_start <= 10000) {
if (WiFi.status() == WL_CONNECTED){
break;
}
delay(10);
}
LCD.clear();
if (WiFi.status() == WL_CONNECTED){
LCD.print("WiFi Connected!");
} else {
LCD.print("WiFi connection");
LCD.setCursor(0, 1);
LCD.print("Failed !");
}
delay(2000);
LCD.clear();
analogReadResolution(12); // 12-bit resolution
analogSetAttenuation(ADC_11db); // Extend input range to ~3.9V
}
void loop() {
// put your main code here, to run repeatedly:
monitorParams();
display();
// delay(10); // this speeds up the simulation
}
Water Level simulator
pH sensor simulator