Hi all,
I am working on a project with an ESP8266 that reads sensors and publishes to a HTML webpage. I also want to be able to read the data on a 16 x 2 I2C LCD. All of that stuff is set up, but I am having problems reading a pushbutton to switch between readings. I.e., Push button to display air temp, push again, display humidity, etc.
Here is the code that I have currently:
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
//Static IP setup
IPAddress staticIP (10, 0, 0, 50);
IPAddress gateway (10, 0, 0, 1);
IPAddress subnet (255, 255, 255, 0);
//wifi information
const char* ssid = "SSID";
const char* password = "PASSWORD";
//server
WiFiServer server(80);
//integers
int ledPin = 16;
int buttonState = 0;
int previousButtonState = 0;
const int menuButton = 10;
const int adcPin = A0;
static int presses = 0;
//definitions
#define DHTTYPE DHT11
#define DHTPIN 0
#define ONE_WIRE_BUS 2
Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd = LiquidCrystal_I2C (0x27, 16, 2);
//floats
float WaterC = 0;
float WaterF = 0;
const float m = -5.59701;
//Millis Timing
unsigned long previousMillis = 0;
const long interval = 500;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(menuButton, INPUT);
WiFi.hostname("Greenhouse Monitor");
WiFi.begin(ssid, password);
WiFi.disconnect();
WiFi.hostname ("Greenhouse Monitor");
WiFi.config(staticIP, subnet, gateway);
WiFi.begin(ssid, password);
dht.begin();
lcd.init();
lcd.backlight();
if (!bmp.begin()){
Serial.println("Check wiring...");
while(1);
}
server.begin();
}
void loop() {
//Barometric Pressure
float PressurePa = bmp.readPressure();
float PressureInHg = (PressurePa + 3966)/3386 ;
//Temperature
float TempC = bmp.readTemperature();
float TempF = (TempC *9/5) + 32;
//humidity
float humidity = dht.readHumidity();
//Dew Point
float dewPointF = TempF - ((100 - humidity) * 0.36);
float dewPointC = (dewPointF - 32) * 5/9;
//Water Temperature
sensors.requestTemperatures();
WaterC = sensors.getTempCByIndex(0);
WaterF = sensors.toFahrenheit(WaterC);
//pH Sensor
float Po = analogRead(adcPin) * 3.3 / 1024;
float phValue = 7 - (1.65 - Po) * m;
//Menu Button
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
buttonState = digitalRead(menuButton);
previousMillis = currentMillis;
if (buttonState == HIGH){
presses = presses + 1;
delay(10);
}
}
//LCD Menus
if (presses == 0){
lcd.setCursor(0, 0);
lcd.print("Air Temp");
lcd.setCursor(0, 1);
lcd.print(TempF);
}else
if (presses == 1){
lcd.setCursor(0, 0);
lcd.print("Humidity");
lcd.setCursor(0, 1);
lcd.print(humidity);
}else
if (presses == 2){
lcd.setCursor(0, 0);
lcd.print("Dew Point");
lcd.setCursor(0, 1);
lcd.print(dewPointF);
}else
if (presses == 3){
lcd.setCursor(0, 0);
lcd.print("Pressure");
lcd.setCursor(0, 1);
lcd.print(PressureInHg);
}else
if (presses == 4){
lcd.setCursor(0, 0);
lcd.print("Water Temp");
lcd.setCursor(0, 1);
lcd.print(WaterF);
}else
if (presses == 5){
lcd.setCursor(0, 0);
lcd.print("pH Level");
lcd.setCursor(0, 1);
lcd.print(phValue);
}else
if (presses == 6){
lcd.setCursor(0, 0);
lcd.print("Pump 1");
}else
if (presses == 7){
lcd.setCursor(0, 0);
lcd.print("Pump 2");
}else
if (presses >= 8){
presses = 0;
}else{}
WiFiClient client = server.available();
String request = client.readStringUntil('\r');
if (client){
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE> Greenhouse Monitor</TITLE>");
client.println("</HEAD>");
client.println("<BODY bgcolor=#000000>");
client.println("<H1 style=\"color:Green;\">Shenango Greenhouse Monitor</H1>");
client.println("<hr>");
client.println("
");
client.println("<H1 style=\"color:Green;\">Grow Lights:</H1>");
client.println("<H3><a href=\"/?GROWON\"\">Turn On Growlights</a>
</H2>");
client.println("<H3><a href=\"/?GROWOFF\"\">Turn Off Growlights</a>
</H2>");
client.println("<H1 style=\"color:Green;\">Air Temperature:</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(TempC);
client.println("C");
client.println("
");
client.println(TempF);
client.println("F");
client.println("</H3>");
client.println("<H1 style=\"color:Green;\">Relative Humidity:</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(humidity);
client.println("%");
client.println("</H3>");
client.println("<H1 style=\"color:Green;\">Dew Point</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(dewPointC);
client.println(" C");
client.println("
");
client.println(dewPointF);
client.println(" F");
client.println("</H3>");
client.println("<H1 style=\"color:Green;\">Barometric Pressure:</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(PressurePa);
client.println(" Pa");
client.println("
");
client.println(PressureInHg);
client.println(" InHg");
client.println("</H3>");
client.println("<H1 style=\"color:Green;\">Water Temperature:</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(WaterC);
client.println("C");
client.println("
");
client.println(WaterF);
client.println("F");
client.println("</H3>");
client.println("<H1 style=\"color:Green;\">Water Acidity:</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(phValue);
client.println(" pH");
client.println("</H3>");
client.println("<H1 style=\"color:Green;\">Flow Valve 1:</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(" L/Min");
client.println("</H3>");
client.println("<H1 style=\"color:Green;\">Flow Valve 2:</H1>");
client.println("<H3 style=\"color:White;\">");
client.println(" L/Min");
client.println("
");
client.println("</H3>");
client.println("</BODY>");
client.println("</HTML>");
}
if (request.indexOf("/?GROWON") != -1){
digitalWrite(ledPin, LOW);
}
if (request.indexOf("/?GROWOFF") != -1){
digitalWrite(ledPin, HIGH);
}
delay(10);
client.stop();
}
This works, but I have to hold down the button for about 5 seconds before it goes to the next reading. I feel as though I don't have the millis set up correctly in the loop:
//Menu Button
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
buttonState = digitalRead(menuButton);
previousMillis = currentMillis;
if (buttonState == HIGH){
presses = presses + 1;
delay(10);
}
}
Does anyone have any idea of what I did wrong here?