/*This code is written by AS on 14-10-2023 for Food Tester*/
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <HTTPClient.h>
// OLED Display
// Screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C // See datasheet for actual Address; usually 0x3D for 128x64, 0x3C for 128x32
// Initialize the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DHT11 Sensor
#define DHTPIN 15
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Air Quality Sensor
const int airQualityPin = 35; // Use pin 35 for air quality sensor
// pH Sensor
const int pHSensorPin = 34; // Use pin 34 for pH sensor
const int numReadings = 10; // Number of readings for averaging
int pHReadings[numReadings]; // Array to store sensor readings
int pHIndex = 0; // Index for the readings array
int pHReadingsTotal = 0; // Running total of readings
String URL = "http://192.168.1.3/sensor_data_FT/test_data.php"; // Change with your PC_IP, name of mySQL data table, and name of PHP script file
const char* ssid = "C8-H11-NO2"; // Add your WIFI SSID
const char* password = "Dopamine"; // Add your WIFI password
// Variables to hold sensor readings
float temperature = 0.0;
float humidity = 0.0;
int airQuality = 0;
float pHValue = 0.0;
// Relay for Heating Element
const int relayPin = 4;
// Constants
const int AIR_QUALITY_THRESHOLD = 500; // Replace magic number with desired threshold to turn on and off heating element
void setup() {
Serial.begin(115200); // ESP32 uses a higher baud rate
// OLED Display setup
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
//connectWiFi();
// DHT Sensor setup
dht.begin();
// Relay setup
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Initially, turn off the heating element
// Initialize the pH readings array
for (int i = 0; i < numReadings; i++) {
pHReadings[i] = 0;
}
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
//connectWiFi();
}
// Read sensor data
temperature = dht.readTemperature();
humidity = dht.readHumidity();
airQuality = analogRead(airQualityPin);
// pH Sensor Readings
int rawpH = analogRead(pHSensorPin);
// Smooth the pH readings
for (int i = 0; i < numReadings; i++) {
pHReadings[i] = analogRead(pHSensorPin);
delay(10);
}
// Sort the analog values from small to large
for (int i = 0; i < numReadings - 1; i++) {
for (int j = i + 1; j < numReadings; j++) {
if (pHReadings[i] > pHReadings[j]) {
int temp = pHReadings[i];
pHReadings[i] = pHReadings[j];
pHReadings[j] = temp;
}
}
}
// Take the average value of 6 center samples
pHReadingsTotal = 0;
for (int i = 2; i < 8; i++) {
pHReadingsTotal += pHReadings[i];
}
// Average the pH readings
float averagepHValue = pHReadingsTotal / 6.0;
// Convert the analog value into millivolt (ESP32 is 12-bit ADC)
float phMilliVolt = (averagepHValue / 4095.0) * 5000.0;
// Convert the millivolt into pH value
pHValue = 2.8 * (phMilliVolt / 1000.0);
// Display sensor data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.println(" C");
display.print("Humidity: ");
display.print(humidity);
display.println("%");
display.print("Air Quality: ");
display.println(airQuality);
display.print("pH Value: ");
display.println(pHValue);
display.display();
// Control heating element based on sensor data
if (airQuality > AIR_QUALITY_THRESHOLD) {
digitalWrite(relayPin, HIGH); // Turn on heating element
} else {
digitalWrite(relayPin, LOW); // Turn off heating element
}
// Data logging to serial
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print(" C, Humidity: ");
Serial.print(humidity);
Serial.print("%, Air Quality: ");
Serial.print(airQuality);
Serial.print(", pH Value: ");
Serial.println(pHValue);
sendDataToServer();
delay(5000);
}
void connectWiFi() {
WiFi.mode(WIFI_OFF);
delay(1000);
// This line hides the viewing of ESP as a wifi hotspot
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void sendDataToServer() {
String postData = "temperature=" + String(temperature) + "&humidity=" + String(humidity) + "&airQuality=" + String(airQuality) + "&pHValue=" + String(pHValue);
HTTPClient http;
http.begin(URL);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(postData);
String payload = "";
if (httpCode > 0) {
// file found at server
if (httpCode == HTTP_CODE_OK) {
payload = http.getString();
Serial.println(payload);
} else {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end(); // Close connection
Serial.print("URL : ");
Serial.println(URL);
Serial.print("Data: ");
Serial.println(postData);
Serial.print("httpCode: ");
Serial.println(httpCode);
Serial.print("payload : ");
Serial.println(payload);
Serial.println("--------------------------------------------------");
}