#include <LiquidCrystal_I2C.h>;
#include <WiFi.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include <DS18B20.h>
//pin numbers are 32 (MQ5), 33 (MQ138), 15 (MQ2) and 4 (DS18b20)
#define MQ5 32
#define MQ138 33
#define MQ2 35
#define DSPIN 34
float mq5, mq138, mq2, temperature;
long thingspeakDelay = 20000;
long thingspeakMillis;
int wifiTries;
bool wifiState;
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = 2091247;
const char * myWriteAPIKey = "9GAVS6FG4O3AXQNE";
// initialize the LCD library with I2C address and LCD size
LiquidCrystal_I2C lcd (0x27, 20,4);
//ds initialisation
DS18B20 ds(DSPIN);
uint8_t address[] = {40, 250, 31, 218, 4, 0, 0, 52};
uint8_t selected;
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void connectToWifi();
void writeToThingSpeak();
void getDSTemperature();
void setup() {
Serial.begin(115200);
Serial.println ("Smart Nyalavu");
Serial.println ("ADC Test");
// Initialize the LCD connected
lcd. init ();
// Turn on the backlight on LCD.
lcd. backlight ();
lcd.print ("Smart Nyalavu");
lcd.write((byte)0); // print the custom char
lcd. setCursor (0, 1);
lcd.print ("ADC Thingspeak TEST");
delay(3000);
connectToWifi();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
thingspeakMillis = millis();
delay(1000);
//lcd.clear();
}
void loop() {
mq5 = analogRead(MQ5);
mq138 = analogRead(MQ138);
mq2 = analogRead(MQ2);
//temperature = analogRead(DS18B20);
getDSTemperature();
Serial.println("pin 32 (MQ5): " + String(mq5));
//Serial.println(" I = " + String ((current/4096)*3.3));
Serial.println("pin 33 (MQ138): " + String(mq138));
Serial.println("pin 35 (MQ2): " + String(mq2));
Serial.println("pin 34 (DS18b20): " + String(temperature));
lcd.setCursor(0,2);
lcd.print("P32=" + String(mq5));
lcd.print(" P33=" + String(mq138));
lcd.setCursor(0,3);
lcd.print("P35=" + String(mq2));
//lcd.print(" P34=" + String(temperature));
// Serial.print("pin 39: " + String(voltage));
// Serial.println(" V = " + String ((voltage/4096)*3.3));
lcd.setCursor(17,1);
lcd.print(" ");
lcd.setCursor(17,1);
lcd.print(int(millis() - thingspeakMillis)/1000);
if ((millis() - thingspeakMillis) > thingspeakDelay){
writeToThingSpeak();
thingspeakMillis = millis();
}
delay(500);
}
void getDSTemperature(){
if (selected) {
Serial.print("(DS18b20): " + String(ds.getTempC()));
lcd.print(" P34=" + String(temperature));
//Serial.print(ds.getTempC());
if (ds.hasAlarm()) {
Serial.print(" Warning!");
//Serial.print(ds.getTempC());
//Serial.println(" C");
lcd.print("!");
}
Serial.println();
} else {
Serial.println("Device not found!");
}
}
void connectToWifi(){
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
lcd.clear();
lcd.setCursor(0,1);
lcd.print ("Connecting");
//lcd.setCursor(0,1);
//lcd.print(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin("Wokwi-GUEST", "", 6); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(1000);
spinner();
lcd.print ("("+String(wifiTries)+")");
//delay(5000);
wifiTries ++;
if (wifiTries>10){
lcd.clear();
lcd.print("No Wifi");
wifiState = 0;
break;
}
}
//TODO: break out of this as well if not successfully connected
Serial.println("\nConnected.");
lcd.clear();
lcd.setCursor(0,1);
//lcd.print("Connected!");
delay(1500);
lcd.clear();
wifiState = 1;
}
}
void writeToThingSpeak(){
if (wifiState == 0){
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" No WIFI!");
lcd.setCursor(0,2);
lcd.print("Reset Device");
}
else {
ThingSpeak.setField(1, mq5);
ThingSpeak.setField(2, mq138);
ThingSpeak.setField(3, mq2);
ThingSpeak.setField(4, temperature);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
lcd.setCursor(0,2);
lcd.print("Update Successful ");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
lcd.setCursor(0,2);
lcd.print("Update Fail: ");
lcd.setCursor(14,2);
lcd.print(x);
}
}
}