esp32-warm-water
esp32 based project for the control of a heating element based on temperature
main.c
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2021 wolffshots
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * A note on the copyright line: https://www.copyright.gov/title17/92chap4.html#408
25  */
26 
42 /*
43  ESP_LOGE - error (lowest)
44  ESP_LOGW - warning
45  ESP_LOGI - info
46  ESP_LOGD - debug
47  ESP_LOGV - verbose (highest)
48 */
49 
50 #include <stdio.h> // esp packaged standard io library
51 #include <stdbool.h> // booleans
52 #include "sdkconfig.h" // the generated configuration variables
53 #include "led.h" // led module
54 char ip_address[16];
55 #ifdef CONFIG_ESP_ENABLE_WIFI // check if wifi is enabled in sdkconfig
56 #ifdef CONFIG_ESP_ENABLE_WIFI_STA // check if station mode is enabled
57 #include "wifi_sta.h" // include station header if so
58 #endif // CONFIG_ESP_ENABLE_WIFI_STA
59 #ifdef CONFIG_ESP_ENABLE_WIFI_SOFTAP // check if soft ap mode is enabled
60 #include "wifi_sap.h" // include soft ap header if so
61 #endif // CONFIG_ESP_ENABLE_WIFI_SOFTAP
62 #endif // CONFIG_ESP_ENABLE_WIFI
63 #include "utility.h" // used for printing chip info
64 #include "ds18b20_wrapper.h" // for setting up and interfacing with the temp sensor via owb
65 #include "timer.h" // wrappers for setting up high resolution timers
66 #include "ssd1306.h"
67 #include "symbols.h"
68 #include "esp_log.h"
69 #include "control.h"
70 #include "spiffs.h"
71 volatile float goal = CONFIG_INITIAL_GOAL_TEMP;
72 volatile float temp = 0.0f;
73 volatile float under = 0.5f;
74 volatile float over = 0.5f;
75 volatile bool heating = false;
76 #include "webserver.h" // webserver import must come after volatiles because it interacts with them
77 
78 int num_sensors = 0;
79 esp_timer_handle_t periodic_check_timer;
80 
82 // char lineChar[20];
83 
84 static const char *TAG = "esp32-warm-water";
85 
86 void update_display(void)
87 {
88  char line[20];
89  char position = 0;
90  sprintf(line, " goal: %0.1f", goal);
91  position = strlen(line);
92  sprintf(line, " goal: %0.1f C", goal);
94  ssd1306_display_image(&dev, 2, position * 8, degree_symbol, 8);
95  sprintf(line, " %1.1f %1.1f ", under, over);
97  ssd1306_display_image(&dev, 3, 6 * 8, down_arrow, 8);
98  ssd1306_display_image(&dev, 3, 9 * 8, up_arrow, 8);
99  sprintf(line, " temp: %0.3f C", temp);
101  sprintf(line, " temp: %0.3f", goal);
102  position = strlen(line);
103  ssd1306_display_image(&dev, 5, position * 8, degree_symbol, 8);
104  sprintf(line, " %s", ip_address);
106  ssd1306_wrapped_display_text(&dev, 7, " mode:");
107  position = strlen(" mode: ");
108  ssd1306_display_image(&dev, 7, position * 8, heating ? fire_symbol : snowflake_symbol, 8);
109 }
110 
112 void relay_on(void)
113 {
114  gpio_set_level(CONFIG_RELAY_ONE_PIN, 0);
115  gpio_set_level(CONFIG_RELAY_TWO_PIN, 0);
116  ESP_LOGI(TAG, "relay on: setting to %d", 0);
117 }
119 void relay_off(void)
120 {
121  gpio_set_level(CONFIG_RELAY_ONE_PIN, 1);
122  gpio_set_level(CONFIG_RELAY_TWO_PIN, 1);
123  ESP_LOGI(TAG, "relay off: setting to %d", 1);
124 }
125 
134 void check_system_handler(void *arg)
135 {
136  float results[sizeof(float) * num_sensors];
137  ds18b20_wrapped_capture(results, num_sensors); // pass float array through to be captured to
138  check_relay(results[0]);
139  for (int i = 0; i < num_sensors; ++i) // for each sensor
140  { //
141  ESP_LOGI(TAG, "%d: %.3f - %lld us", i, results[i], esp_timer_get_time()); // log result out
142  } // this will probably be phased out once relay control is implemented
143  temp = results[0]; // this should be the average
144  update_display();
145 }
149 void check_relay(float average_temp)
150 {
152 
153  if ((average_temp > goal + over) && heating) // check status and stuff here
154  {
155  // turn off relay
156  heating = false;
157  relay_off();
158  led_off(CONFIG_STATUS_ONE_PIN);
159  }
160  else if ((average_temp < goal - under) && !heating)
161  {
162  // turn on relay
163  heating = true;
164  relay_on();
165  led_on(CONFIG_STATUS_ONE_PIN);
166  }
167  else
168  {
169  // continue what it's currently doing
170  // heating = heating
171  }
172 }
173 
177 void app_deinit(void)
178 {
180  relay_deinit();
182  ssd1306_deinit(&dev);
184  spiffs_deinit();
185  fflush(stdout);
186 }
187 
191 void app_main(void)
192 {
193 #if defined(CONFIG_ESP_ENABLE_WIFI) && defined(CONFIG_ESP_ENABLE_WIFI_STA) // check if station mode
194  wifi_init_sta(); // start wifi in station mode and connect to ap
195 #endif // CONFIG_ESP_ENABLE_WIFI && CONFIG_ESP_ENABLE_WIFI_STA
196 #if defined(CONFIG_ESP_ENABLE_WIFI) && defined(CONFIG_ESP_ENABLE_WIFI_SOFTAP) // check if softap mode
197  wifi_init_sap(); // start wifi in soft ap mode and start broadcasting ap
198 #endif // CONFIG_ESP_ENABLE_WIFI && CONFIG_ESP_ENABLE_WIFI_SOFTAP
199  print_chip_info(); // print the chip features and details
200 
201  relay_off();
202  gpio_output(CONFIG_RELAY_ONE_PIN);
203  gpio_output(CONFIG_RELAY_TWO_PIN);
204  led_init();
205 
206  num_sensors = ds18b20_wrapped_init(); // capture num sensors and init
207 
208  general_timer_init(periodic_check_timer, check_system_handler, true, CONFIG_CONTROL_INTERVAL, "check_system_handler");
209 
210  spiffs_init();
211 
212  ssd1306_init(&dev);
213 
214  ssd1306_clear_screen(&dev, false);
215  ssd1306_wrapped_display_text(&dev, 0, " warm water :)");
216 
217  ESP_ERROR_CHECK(start_file_server("/spiffs"));
218 }
void relay_deinit(void)
void ds18b20_wrapped_deinit(void)
deinit the sensor cleans up and frees all of the devices and the onewire bus
void ds18b20_wrapped_capture(float *results, int size)
capture temps to results this function runs conversion on all the owb devices, waits for conversion t...
int ds18b20_wrapped_init(void)
init the sensor intitialises the onewire bus and finds and intialises ds18b20 sensors along the pin
definitions for wrapper component to help setup and interface with temp sensor
void gpio_output(gpio_num_t pin_number)
Definition: gpio_wrapper.c:41
void led_init(void)
Definition: led.c:4
void led_on(int pin)
Definition: led.c:10
void led_off(int pin)
Definition: led.c:15
void app_deinit(void)
Definition: main.c:177
void check_relay(float average_temp)
Definition: main.c:149
SSD1306_t dev
device for oled
Definition: main.c:81
volatile float temp
current temp
Definition: main.c:72
int num_sensors
the number of sensors ds18b20 init has found (initially 0)
Definition: main.c:78
volatile float under
margin below goal temp at which to turn relay on
Definition: main.c:73
void app_main(void)
Definition: main.c:191
void relay_on(void)
Definition: main.c:112
esp_timer_handle_t periodic_check_timer
variable to control the timer associated with running the temperature polling to check system
Definition: main.c:79
volatile float goal
goal temp for the system to aim for
Definition: main.c:71
volatile float over
margin above goal temp at which to turn relay off
Definition: main.c:74
void update_display(void)
Definition: main.c:86
char ip_address[16]
Definition: main.c:54
void relay_off(void)
Definition: main.c:119
void check_system_handler(void *arg)
Definition: main.c:134
volatile bool heating
whether or not the system should be outputing a signal to enable the relay (note: could be used to co...
Definition: main.c:75
defininitions for spiffs
void spiffs_deinit(void)
Definition: spiffs.c:159
void spiffs_init(void)
Definition: spiffs.c:111
defininitions for setting up, interacting with and sending commands to an ssd1306 driven screen via i...
void ssd1306_deinit(SSD1306_t *dev)
Definition: ssd1306.c:100
void ssd1306_wrapped_display_text(SSD1306_t *dev, int line, char *text)
Definition: ssd1306.c:146
void ssd1306_display_image(SSD1306_t *dev, int page, int seg, uint8_t *images, int width)
Definition: ssd1306.c:159
void ssd1306_init(SSD1306_t *dev)
Definition: ssd1306.c:53
void ssd1306_clear_screen(SSD1306_t *dev, bool invert)
Definition: ssd1306.c:175
defininitions for wrapper component which assists in setting up timers
void general_timer_deinit(esp_timer_handle_t timer)
Definition: timer.c:71
void general_timer_init(esp_timer_handle_t timer, void(timer_callback)(void *arg), bool periodic, int period_in_seconds, char *timer_name)
Definition: timer.c:51
defininitions for simple utility functions
void print_chip_info(void)
Definition: utility.c:38
defininitions for webserver
esp_err_t start_file_server(const char *base_path)
Definition: webserver.c:429
defininitions for wrapper component to set up wifi in softap mode
void wifi_init_sap(void)
Definition: wifi_sap.c:90
void wifi_shared_deinit(void)
Definition: wifi_shared.c:4
const char * TAG
Definition: wifi_sta.c:42
defininitions for wrapper component to set up wifi in station mode
void wifi_init_sta(void)
Definition: wifi_sta.c:166