Created
November 14, 2022 18:13
-
-
Save rurubell/27117b0a152cea41cfa440bc6aaec34f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal_I2C.h> | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <ESP8266WiFi.h> | |
#include "DHT.h" | |
#include <ESP8266WebServer.h> | |
#define DHTPIN D7 | |
#define DHTTYPE DHT11 | |
OneWire oneWire_1(0); | |
OneWire oneWire_2(2); | |
OneWire oneWire_3(14); | |
OneWire oneWire_4(12); | |
DallasTemperature sensors_1(&oneWire_1); | |
DallasTemperature sensors_2(&oneWire_2); | |
DallasTemperature sensors_3(&oneWire_3); | |
DallasTemperature sensors_4(&oneWire_4); | |
DHT dht( DHTPIN, DHTTYPE ); | |
const char* ssid = "my_sensor"; | |
const char* password = "12345678"; | |
float f_dht_temp = 0.0; | |
float f_dht_humid = 0.0; | |
float f_pump_sensor1_temp = 0.0; | |
float f_pump_sensor2_temp = 0.0; | |
float f_pump_sensor3_temp = 0.0; | |
float f_pump_sensor4_temp = 0.0; | |
LiquidCrystal_I2C LCD( 0x27, 16, 2 ); | |
IPAddress apIP( 192, 168, 0, 1 ); | |
ESP8266WebServer server(80); | |
String gedSensorsData() | |
{ | |
String s_res = | |
"<ts1>" + String( sensors_1.getTempCByIndex(0) ) + "</ts1>\n" + | |
"<ts2>" + String( sensors_2.getTempCByIndex(0) ) + "</ts2>\n" + | |
"<ts3>" + String( sensors_3.getTempCByIndex(0) ) + "</ts3>\n" + | |
"<ts4>" + String( sensors_4.getTempCByIndex(0) ) + "</ts4>\n" + | |
"<piezo>" + String( getPiezoData( 0, 1024 ) ) + "</piezo>" + | |
"<dht_temp>" + String( f_dht_temp ) + "</dht_temp>" + | |
"<dht_humid>" + String( f_dht_humid ) + "</dht_humid>"; | |
return s_res; | |
} | |
void wifiInit() | |
{ | |
WiFi.mode(WIFI_AP_STA); | |
WiFi.softAPConfig( apIP, apIP, IPAddress(255, 255, 255, 0) ); | |
WiFi.softAP( ssid, password ); | |
} | |
void setup() | |
{ | |
pinMode( DHTPIN, INPUT ); | |
dht.begin(); | |
wifiInit(); | |
LCD.init(); | |
LCD.backlight(); | |
sensors_1.begin(); | |
sensors_2.begin(); | |
sensors_3.begin(); | |
sensors_4.begin(); | |
server.begin(); | |
server.on( "/", []() { server.send ( 200, "text/plain", gedSensorsData() ); } ); | |
IPAddress myIP = WiFi.softAPIP(); | |
LCD.print( myIP ); | |
delay( 5000 ); | |
} | |
uint32_t n_global_ds18b20_time = 0; | |
uint32_t n_global_dht11_time = 0; | |
uint32_t n_global_lcd_time = 0; | |
uint32_t n_ds18b20_period = 1000; | |
uint32_t n_dht11_period = 4000; | |
uint32_t n_lcd_period = 5000; | |
int n_lcd_flag = 0; | |
void loop() | |
{ | |
server.handleClient(); | |
if( WiFi.status() != 0 ) { wifiInit(); } | |
if( millis() < n_global_ds18b20_time ) { n_global_ds18b20_time = millis(); } | |
if( millis() < n_global_dht11_time ) { n_global_dht11_time = millis(); } | |
if( millis() < n_global_lcd_time ) { n_global_lcd_time = millis(); } | |
if( ( millis() - n_global_ds18b20_time ) >= n_ds18b20_period ) | |
{ | |
sensors_1.requestTemperatures(); | |
sensors_2.requestTemperatures(); | |
sensors_3.requestTemperatures(); | |
sensors_4.requestTemperatures(); | |
n_global_ds18b20_time = millis(); | |
} | |
if( ( millis() - n_global_dht11_time ) >= n_dht11_period ) | |
{ | |
f_dht_temp = dht.readTemperature(); | |
f_dht_humid = dht.readHumidity(); | |
n_global_dht11_time = millis(); | |
} | |
if( ( millis() - n_global_lcd_time ) >= n_lcd_period ) | |
{ | |
switch( n_lcd_flag ) | |
{ | |
case 0: | |
printPumpTempSensorsValuesOnLCD(); | |
n_lcd_flag++; | |
break; | |
case 1: | |
printPumpVibrationValuesOnLCD(); | |
n_lcd_flag++; | |
break; | |
case 2: | |
printDHT11ValuesOnLCD(); | |
n_lcd_flag++; | |
break; | |
case 3: | |
printWifiStatusOnLCD(); | |
n_lcd_flag++; | |
break; | |
case 4: | |
printUptimeOnLCD(); | |
n_lcd_flag = 0; | |
break; | |
default: | |
n_lcd_flag = 0; | |
} | |
n_global_lcd_time = millis(); | |
} | |
} | |
void printPumpTempSensorsValuesOnLCD() | |
{ | |
LCD.clear(); | |
LCD.setCursor( 0, 0 ); | |
LCD.print( "t1=" + String( sensors_1.getTempCByIndex(0), 1 ) ); | |
LCD.setCursor( 9, 0 ); | |
LCD.print( "t2=" + String( sensors_2.getTempCByIndex(0), 1 ) ); | |
LCD.setCursor( 0, 1 ); | |
LCD.print( "t3=" + String( sensors_3.getTempCByIndex(0), 1 ) ); | |
LCD.setCursor( 9, 1 ); | |
LCD.print( "t4=" + String( sensors_4.getTempCByIndex(0), 1 ) ); | |
} | |
void printPumpVibrationValuesOnLCD() | |
{ | |
LCD.clear(); | |
LCD.setCursor( 0, 0 ); | |
LCD.print( "Vib=" + String( getPiezoData( 0, 1024 ) ) ); | |
} | |
void printDHT11ValuesOnLCD() | |
{ | |
LCD.clear(); | |
LCD.setCursor( 0, 0 ); | |
LCD.print( "Temp=" + String( f_dht_temp, 1 ) ); | |
LCD.setCursor( 0, 1 ); | |
LCD.print( "Humid=" + String( f_dht_humid, 1 ) ); | |
} | |
void printWifiStatusOnLCD() | |
{ | |
LCD.clear(); | |
LCD.setCursor( 0, 0 ); | |
LCD.print( "Wifi errcode:" ); | |
LCD.setCursor( 0, 1 ); | |
LCD.print( WiFi.status() ); | |
} | |
void printUptimeOnLCD() | |
{ | |
LCD.clear(); | |
LCD.setCursor( 0, 0 ); | |
LCD.print( "Uptime:" ); | |
LCD.setCursor( 0, 1 ); | |
LCD.print( uptime() ); | |
} | |
String uptime() | |
{ | |
char c_buf[20]; | |
int days = 0; | |
int hours = 0; | |
int minutes = 0; | |
int seconds = 0; | |
unsigned long sec2minutes = 60; | |
unsigned long sec2hours = (sec2minutes * 60); | |
unsigned long sec2days = (sec2hours * 24); | |
unsigned long time_delta = millis() / 1000UL; | |
days = (int)(time_delta / sec2days); | |
hours = (int)((time_delta - days * sec2days) / sec2hours); | |
minutes = (int)((time_delta - days * sec2days - hours * sec2hours) / sec2minutes); | |
seconds = (int)(time_delta - days * sec2days - hours * sec2hours - minutes * sec2minutes); | |
sprintf( c_buf, "%01dd %02dh:%02dm", days, hours, minutes ); | |
return String( c_buf ); | |
} | |
unsigned long getPiezoData( int n_a_pin, int n_samples ) | |
{ | |
unsigned long n_res = 0; | |
for( int i = 0; i < n_samples; i++ ) | |
{ | |
n_res += analogRead( n_a_pin ); | |
} | |
return n_res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment