From 38ee8efec07aba1766fa100a54f241df7734beb1 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 30 Sep 2021 16:40:35 -0400 Subject: Shorter output + OTA --- src/main/main.ino | 88 ++++++++++++++--------------------------------- src/main/ota.h | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/wifiinfo.h | 5 +++ 3 files changed, 129 insertions(+), 62 deletions(-) create mode 100644 src/main/ota.h create mode 100644 src/main/wifiinfo.h diff --git a/src/main/main.ino b/src/main/main.ino index bdab190..d6f41d5 100644 --- a/src/main/main.ino +++ b/src/main/main.ino @@ -1,85 +1,49 @@ -#define TEMP_SENSOR_PIN 14 -#define HUMIDITY_SENSOR_PIN 14 +#define TEMP_SENSOR_PIN 12 +#define HUMIDITY_SENSOR_PIN 12 #define FAN_PIN 15 #define CO2_PIN 16 #define ULTRASONIC_PIN 17 -#define CHECK_FREQUENCY 1 // check sensors every second +#define CHECK_FREQUENCY 2 // check sensors every 2 seconds #define HUMIDITY_DESIRED 75 #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off // at (75 + 3 = 78) +#include "ota.h" // Tempature + Humidity Sensor -#include #include -#include +#define DHTPIN 12 #define DHTTYPE DHT11 -#define DHTPIN TEMP_SENSOR_PIN -DHT_Unified dht(DHTPIN, DHTTYPE); +DHT dht(DHTPIN, DHTTYPE); -int get_humidity () { - // Get temperature event and print its value. - sensors_event_t event; - dht.temperature().getEvent(&event); - if (isnan(event.temperature)) { - Serial.println(F("Error reading temperature!")); - } - else { - return event.temperature; - /* Serial.print(F("Temperature: ")); */ - /* Serial.print(event.temperature); */ - /* Serial.println(F("°C")); */ - } -} - -int get_tempature () { - sensors_event_t event; - // Get humidity event and print its value. - dht.humidity().getEvent(&event); - if (isnan(event.relative_humidity)) { - Serial.println(F("Error reading humidity!")); - } - else { - return event.relative_humidity; - /* Serial.print(F("Humidity: ")); */ - /* Serial.print(event.relative_humidity); */ - /* Serial.println(F("%")); */ - } -} - -int get_co2 () { ; } -bool start_ultrasonic () { ; } -bool stop_ultrasonic () { ; } -bool start_fan () { ; } -bool stop_fan () { ; } +void setup () { + setupWifi("mush"); + setupOTA("mush"); + Serial.begin(115200); + Serial.println(F("Mushing...")); -bool log ( int humidity, int temp, int co2 ) { - Serial.printf("Tempature: %d\tHumidity: %d\tCO2: %d\n", - humidity, temp, co2); + dht.begin(); // temp+humidity sensor } -void setup () { - Serial.begin(115200); - dht.begin(); /* temp/humidity sensor */ +void log_reading ( float temp_c, float humidity, float heat_index_c ) { + Serial.printf("T: %.2f H: %.2f HI: %.2f\r\n", temp_c, humidity, heat_index_c); } void loop() { - delay(CHECK_FREQUENCY * 1000); - int humidity = get_humidity(); - int temp = get_tempature(); - int co2 = get_co2(); - - if ( humidity < (HUMIDITY_DESIRED - HUMIDITY_VARIATION) ) { - start_ultrasonic(); - stop_fan(); - } else if ( humidity > (HUMIDITY_DESIRED + HUMIDITY_VARIATION) ) { - start_fan(); - stop_ultrasonic(); - } else { - ; // keep doing the same + ArduinoOTA.handle(); + // Wait a few seconds between measurements. + delay(2000); + + float h = dht.readHumidity(); + float t = dht.readTemperature(); + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t) ) { + Serial.println(F("Failed to read from DHT sensor!")); + return; } + float hic = dht.computeHeatIndex(t, h, false); - log(humidity, temp, co2); + log_reading(t, h, hic); } diff --git a/src/main/ota.h b/src/main/ota.h new file mode 100644 index 0000000..28fb37b --- /dev/null +++ b/src/main/ota.h @@ -0,0 +1,98 @@ +// Include what is needed to implement Over The Air Update +// The WiFi connection is not done on this include file, so must be done +// in the main program file +// +// This include file provides the functions +// setupWifi(hostname) +// hostname can be an empty string; this function will +// establish the wifi connections, WiFi credentials are supplied +// in the file "wifiinfo.h", see "wifiinfo.h.sample" +// setupOTA(hostname) +// hostname can be an empty string; will be used to retrieve the +// correct board to upgrade Over The Air. +// If an empty string the default name is esp8266-[ChipID] +// The above 2 functions must be called in the setup() function +// +// in the loop function the function +// ArduinoOTA.handle() +// must be called + +#ifndef ota_h +#define ota_h +// needed include files +#include +#include +#include +#include + +// include our Network SSID and PASSWORD +#include "wifiinfo.h" + +// ================================================================== +// Establish WiFi connection +// ================================================================== +void setupWifi(char hostname[]) { + int i = 0; + WiFi.mode(WIFI_STA); + if (sizeof(hostname) > 0) { + WiFi.hostname(hostname); + } + if (Serial) {Serial.println("Connecting ");} + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + if (Serial) {Serial.print(".");} + delay(500); + } + if (Serial) { + Serial.println("."); + Serial.print("Connected! IP: "); + Serial.println(WiFi.localIP()); + } +} + + +// ================================================================== +// Setup for OTA update +// ================================================================== + +void setupOTA(char hostname[]) { + // Port defaults to 8266 + // ArduinoOTA.setPort(8266); + + // Hostname defaults to esp8266-[ChipID] + if (sizeof(hostname) > 0) { + ArduinoOTA.setHostname(hostname); + } + + // No authentication by default + // ArduinoOTA.setPassword((const char *)"123"); + + ArduinoOTA.onStart([]() { + if (Serial) {Serial.println("Start");} + }); + ArduinoOTA.onEnd([]() { + if (Serial) {Serial.println("\nEnd");} + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + if (Serial) {Serial.printf("Progress: %u%%\r", (progress / (total / 100)));} + }); + ArduinoOTA.onError([](ota_error_t error) { + if (Serial) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); + else if (error == OTA_END_ERROR) Serial.println("End Failed"); + } + }); + ArduinoOTA.begin(); + if (Serial) { + Serial.println("Ready"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + } +} + + +#endif diff --git a/src/main/wifiinfo.h b/src/main/wifiinfo.h new file mode 100644 index 0000000..9a5f315 --- /dev/null +++ b/src/main/wifiinfo.h @@ -0,0 +1,5 @@ +#ifndef wifiinfo_h +#define wifiinfo_h +const char* ssid = "omni"; +const char* password = "everywhere"; +#endif -- cgit v1.2.3