diff options
author | Steven <steven.vasilogianis@gmail.com> | 2021-09-30 16:40:35 -0400 |
---|---|---|
committer | Steven <steven.vasilogianis@gmail.com> | 2021-09-30 16:40:35 -0400 |
commit | 38ee8efec07aba1766fa100a54f241df7734beb1 (patch) | |
tree | ba269a6c3cdd790ac1d669b520a769063c5144e3 | |
parent | b931713a134dc10a81a5a93f0139daa306535bea (diff) |
Shorter output + OTA
-rw-r--r-- | src/main/main.ino | 88 | ||||
-rw-r--r-- | src/main/ota.h | 98 | ||||
-rw-r--r-- | src/main/wifiinfo.h | 5 |
3 files changed, 129 insertions, 62 deletions
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 @@ | |||
1 | #define TEMP_SENSOR_PIN 14 | 1 | #define TEMP_SENSOR_PIN 12 |
2 | #define HUMIDITY_SENSOR_PIN 14 | 2 | #define HUMIDITY_SENSOR_PIN 12 |
3 | #define FAN_PIN 15 | 3 | #define FAN_PIN 15 |
4 | #define CO2_PIN 16 | 4 | #define CO2_PIN 16 |
5 | #define ULTRASONIC_PIN 17 | 5 | #define ULTRASONIC_PIN 17 |
6 | 6 | ||
7 | #define CHECK_FREQUENCY 1 // check sensors every second | 7 | #define CHECK_FREQUENCY 2 // check sensors every 2 seconds |
8 | #define HUMIDITY_DESIRED 75 | 8 | #define HUMIDITY_DESIRED 75 |
9 | #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off | 9 | #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off |
10 | // at (75 + 3 = 78) | 10 | // at (75 + 3 = 78) |
11 | 11 | ||
12 | #include "ota.h" | ||
12 | 13 | ||
13 | // Tempature + Humidity Sensor | 14 | // Tempature + Humidity Sensor |
14 | #include <Adafruit_Sensor.h> | ||
15 | #include <DHT.h> | 15 | #include <DHT.h> |
16 | #include <DHT_U.h> | 16 | #define DHTPIN 12 |
17 | #define DHTTYPE DHT11 | 17 | #define DHTTYPE DHT11 |
18 | #define DHTPIN TEMP_SENSOR_PIN | 18 | DHT dht(DHTPIN, DHTTYPE); |
19 | DHT_Unified dht(DHTPIN, DHTTYPE); | ||
20 | 19 | ||
21 | int get_humidity () { | 20 | void setup () { |
22 | // Get temperature event and print its value. | 21 | setupWifi("mush"); |
23 | sensors_event_t event; | 22 | setupOTA("mush"); |
24 | dht.temperature().getEvent(&event); | ||
25 | if (isnan(event.temperature)) { | ||
26 | Serial.println(F("Error reading temperature!")); | ||
27 | } | ||
28 | else { | ||
29 | return event.temperature; | ||
30 | /* Serial.print(F("Temperature: ")); */ | ||
31 | /* Serial.print(event.temperature); */ | ||
32 | /* Serial.println(F("°C")); */ | ||
33 | } | ||
34 | } | ||
35 | |||
36 | int get_tempature () { | ||
37 | sensors_event_t event; | ||
38 | // Get humidity event and print its value. | ||
39 | dht.humidity().getEvent(&event); | ||
40 | if (isnan(event.relative_humidity)) { | ||
41 | Serial.println(F("Error reading humidity!")); | ||
42 | } | ||
43 | else { | ||
44 | return event.relative_humidity; | ||
45 | /* Serial.print(F("Humidity: ")); */ | ||
46 | /* Serial.print(event.relative_humidity); */ | ||
47 | /* Serial.println(F("%")); */ | ||
48 | } | ||
49 | } | ||
50 | |||
51 | int get_co2 () { ; } | ||
52 | bool start_ultrasonic () { ; } | ||
53 | bool stop_ultrasonic () { ; } | ||
54 | bool start_fan () { ; } | ||
55 | bool stop_fan () { ; } | ||
56 | 23 | ||
24 | Serial.begin(115200); | ||
25 | Serial.println(F("Mushing...")); | ||
57 | 26 | ||
58 | bool log ( int humidity, int temp, int co2 ) { | 27 | dht.begin(); // temp+humidity sensor |
59 | Serial.printf("Tempature: %d\tHumidity: %d\tCO2: %d\n", | ||
60 | humidity, temp, co2); | ||
61 | } | 28 | } |
62 | 29 | ||
63 | void setup () { | 30 | void log_reading ( float temp_c, float humidity, float heat_index_c ) { |
64 | Serial.begin(115200); | 31 | Serial.printf("T: %.2f H: %.2f HI: %.2f\r\n", temp_c, humidity, heat_index_c); |
65 | dht.begin(); /* temp/humidity sensor */ | ||
66 | } | 32 | } |
67 | 33 | ||
68 | void loop() { | 34 | void loop() { |
69 | delay(CHECK_FREQUENCY * 1000); | 35 | ArduinoOTA.handle(); |
70 | int humidity = get_humidity(); | 36 | // Wait a few seconds between measurements. |
71 | int temp = get_tempature(); | 37 | delay(2000); |
72 | int co2 = get_co2(); | 38 | |
73 | 39 | float h = dht.readHumidity(); | |
74 | if ( humidity < (HUMIDITY_DESIRED - HUMIDITY_VARIATION) ) { | 40 | float t = dht.readTemperature(); |
75 | start_ultrasonic(); | 41 | // Check if any reads failed and exit early (to try again). |
76 | stop_fan(); | 42 | if (isnan(h) || isnan(t) ) { |
77 | } else if ( humidity > (HUMIDITY_DESIRED + HUMIDITY_VARIATION) ) { | 43 | Serial.println(F("Failed to read from DHT sensor!")); |
78 | start_fan(); | 44 | return; |
79 | stop_ultrasonic(); | ||
80 | } else { | ||
81 | ; // keep doing the same | ||
82 | } | 45 | } |
46 | float hic = dht.computeHeatIndex(t, h, false); | ||
83 | 47 | ||
84 | log(humidity, temp, co2); | 48 | log_reading(t, h, hic); |
85 | } | 49 | } |
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 @@ | |||
1 | // Include what is needed to implement Over The Air Update | ||
2 | // The WiFi connection is not done on this include file, so must be done | ||
3 | // in the main program file | ||
4 | // | ||
5 | // This include file provides the functions | ||
6 | // setupWifi(hostname) | ||
7 | // hostname can be an empty string; this function will | ||
8 | // establish the wifi connections, WiFi credentials are supplied | ||
9 | // in the file "wifiinfo.h", see "wifiinfo.h.sample" | ||
10 | // setupOTA(hostname) | ||
11 | // hostname can be an empty string; will be used to retrieve the | ||
12 | // correct board to upgrade Over The Air. | ||
13 | // If an empty string the default name is esp8266-[ChipID] | ||
14 | // The above 2 functions must be called in the setup() function | ||
15 | // | ||
16 | // in the loop function the function | ||
17 | // ArduinoOTA.handle() | ||
18 | // must be called | ||
19 | |||
20 | #ifndef ota_h | ||
21 | #define ota_h | ||
22 | // needed include files | ||
23 | #include <ESP8266WiFi.h> | ||
24 | #include <ESP8266mDNS.h> | ||
25 | #include <WiFiUdp.h> | ||
26 | #include <ArduinoOTA.h> | ||
27 | |||
28 | // include our Network SSID and PASSWORD | ||
29 | #include "wifiinfo.h" | ||
30 | |||
31 | // ================================================================== | ||
32 | // Establish WiFi connection | ||
33 | // ================================================================== | ||
34 | void setupWifi(char hostname[]) { | ||
35 | int i = 0; | ||
36 | WiFi.mode(WIFI_STA); | ||
37 | if (sizeof(hostname) > 0) { | ||
38 | WiFi.hostname(hostname); | ||
39 | } | ||
40 | if (Serial) {Serial.println("Connecting ");} | ||
41 | WiFi.begin(ssid, password); | ||
42 | while (WiFi.status() != WL_CONNECTED) { | ||
43 | if (Serial) {Serial.print(".");} | ||
44 | delay(500); | ||
45 | } | ||
46 | if (Serial) { | ||
47 | Serial.println("."); | ||
48 | Serial.print("Connected! IP: "); | ||
49 | Serial.println(WiFi.localIP()); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | |||
54 | // ================================================================== | ||
55 | // Setup for OTA update | ||
56 | // ================================================================== | ||
57 | |||
58 | void setupOTA(char hostname[]) { | ||
59 | // Port defaults to 8266 | ||
60 | // ArduinoOTA.setPort(8266); | ||
61 | |||
62 | // Hostname defaults to esp8266-[ChipID] | ||
63 | if (sizeof(hostname) > 0) { | ||
64 | ArduinoOTA.setHostname(hostname); | ||
65 | } | ||
66 | |||
67 | // No authentication by default | ||
68 | // ArduinoOTA.setPassword((const char *)"123"); | ||
69 | |||
70 | ArduinoOTA.onStart([]() { | ||
71 | if (Serial) {Serial.println("Start");} | ||
72 | }); | ||
73 | ArduinoOTA.onEnd([]() { | ||
74 | if (Serial) {Serial.println("\nEnd");} | ||
75 | }); | ||
76 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | ||
77 | if (Serial) {Serial.printf("Progress: %u%%\r", (progress / (total / 100)));} | ||
78 | }); | ||
79 | ArduinoOTA.onError([](ota_error_t error) { | ||
80 | if (Serial) { | ||
81 | Serial.printf("Error[%u]: ", error); | ||
82 | if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | ||
83 | else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | ||
84 | else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | ||
85 | else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | ||
86 | else if (error == OTA_END_ERROR) Serial.println("End Failed"); | ||
87 | } | ||
88 | }); | ||
89 | ArduinoOTA.begin(); | ||
90 | if (Serial) { | ||
91 | Serial.println("Ready"); | ||
92 | Serial.print("IP address: "); | ||
93 | Serial.println(WiFi.localIP()); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | |||
98 | #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 @@ | |||
1 | #ifndef wifiinfo_h | ||
2 | #define wifiinfo_h | ||
3 | const char* ssid = "omni"; | ||
4 | const char* password = "everywhere"; | ||
5 | #endif | ||