forked from NickB1/balboa-spa-mqtt-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalboa-spa-mqtt-controller.ino
241 lines (204 loc) · 6.46 KB
/
balboa-spa-mqtt-controller.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "ascii_to_7seg.h"
#include "balboa-interface.h"
//Balboa interface variables
extern s_status bbstatus;
extern s_bbdata bbdata_display, bbdata_controller;
extern volatile uint32_t balboa_controller_clock_cycle_cnt, balboa_controller_clock_cycle_cnt_measured;
extern volatile uint8_t balboa_controller_bit_skipped;
//Function declarations
extern void IRAM_ATTR __digitalWrite(uint8_t pin, uint8_t val);
extern int IRAM_ATTR __digitalRead(uint8_t pin);
extern uint8_t IRAM_ATTR write_byte_bit(uint8_t* write_byte, uint8_t write_bit, uint8_t bit_location);
void setup()
{
Serial.begin(115200);
delay(10);
balboa_interface_init();
mqtt_init();
mqtt_connect();
serial_print_header();
}
void loop()
{
serial_read();
balboa_display_poll();
balboa_controller_reset_data_pin();
balboa_button_press_sequencer(false, 0, 0);
balboa_data_process();
//mqtt_loop();
}
//Serial Debug
void serial_print_header(void)
{
Serial.println();
Serial.println("Balboa Spa - MQTT Controller by Nick Blommen");
Serial.println();
Serial.println("Button Presses:");
Serial.println();
Serial.println("0: Temperature Up");
Serial.println("1: Temperature Down");
Serial.println("2: Toggle Lighting");
Serial.println("3: Toggle Jets");
Serial.println("4: Toggle Blower");
Serial.println();
Serial.println("Options:");
Serial.println();
Serial.println("5: Set Temperature (e.g: 5 37.0;)");
Serial.println("6: Set Lighting (e.g: 6 2;)");
Serial.println("7: Set Jets (e.g: 7 2;)");
Serial.println("8: Set Blower (e.g: 8 1;)");
Serial.println("9: Print Status");
Serial.println("a: Debug");
Serial.println("b: Print Error Codes");
Serial.println();
}
void serial_read(void)
{
if (Serial.available())
{
int inByte = Serial.read();
switch (inByte)
{
case '0':
balboa_controller_send_button(Button_Temp_Up);
Serial.println("Button Press: Temperature Up");
break;
case '1':
balboa_controller_send_button(Button_Temp_Down);
Serial.println("Button Press: Temperature Down");
break;
case '2':
balboa_controller_send_button(Button_Lights);
Serial.println("Button Press: Toggle Lighting");
break;
case '3':
balboa_controller_send_button(Button_Jets);
Serial.println("Button Press: Toggle Jets");
break;
case '4':
balboa_controller_send_button(Button_Blower);
Serial.println("Button Press: Toggle Blower");
break;
case '5':
balboa_set_desired_temperature(Serial.parseFloat());
break;
case '6':
balboa_set_lights(Serial.parseInt());
break;
case '7':
balboa_set_jets(Serial.parseInt());
break;
case '8':
balboa_set_blower(Serial.parseInt());
break;
case '9':
Serial.print("Current Water Temperature: ");
Serial.print(bbstatus.temperature, 2);
Serial.println("°C");
Serial.print("Desired Water Temperature: ");
Serial.print(bbstatus.temperature_desired, 2);
Serial.println("°C");
Serial.print("Lights: ");
Serial.println(bbstatus.lights);
Serial.print("Jets: ");
Serial.println(bbstatus.jets);
Serial.print("Blower: ");
Serial.println(bbstatus.blower);
Serial.print("Last Code: ");
Serial.println(bbstatus.code);
break;
case 'a':
serial_print_debug();
break;
case 'b':
serial_print_error_codes(balboa_error_codes, (int) (sizeof(balboa_error_codes) / sizeof(balboa_error_codes[0])));
break;
default:
break;
}
Serial.println();
}
}
void serial_print_debug(void)
{
char DisplayString[20] = {0};
char ControllerString[20] = {0};
sprintf(DisplayString, "%02X:%02X:%02X:%02X:%02X:%02X", bbdata_display.disp[0], bbdata_display.disp[1], bbdata_display.disp[2], bbdata_display.disp[3], bbdata_display.disp[4], bbdata_display.disp[5]);
sprintf(ControllerString, "%02X:%02X:%02X:%02X:%02X:%02X", bbdata_controller.disp[0], bbdata_controller.disp[1], bbdata_controller.disp[2], bbdata_controller.disp[3], bbdata_controller.disp[4], bbdata_controller.disp[5]);
Serial.println("---- Debug Data ----");
Serial.println();
Serial.println("Controller:");
Serial.println();
Serial.print("Data: ");
Serial.println(ControllerString);
Serial.print("Button data: ");
Serial.println(bbdata_controller.buttons, HEX);
Serial.print("Clock cycle count calculated: ");
Serial.println(balboa_controller_clock_cycle_cnt);
Serial.print("Clock cycle count measured: ");
Serial.println(balboa_controller_clock_cycle_cnt_measured);
Serial.print("Bit skip detection: ");
Serial.println(balboa_controller_bit_skipped);
Serial.println();
Serial.println("Display:");
Serial.println();
Serial.print("Data: ");
Serial.println(DisplayString);
Serial.print("Button data: ");
Serial.println(bbdata_display.buttons, HEX);
}
void serial_print_error_codes(String string_array[][2], int array_length)
{
char char_buffer_1[5];
char char_buffer_2[5];
for (int i = 0; i < array_length; i++)
{
string_array[i][0].toCharArray(char_buffer_1, string_array[i][0].length() + 1);
Serial.print(string_array[i][0]);
Serial.print(" - ");
Serial.print(char_buffer_1[0], HEX);
Serial.print(" ");
Serial.print(char_buffer_1[1], HEX);
Serial.print(" ");
Serial.print(char_buffer_1[2], HEX);
Serial.print(" ");
Serial.print(char_buffer_1[3], HEX);
Serial.print(" ");
Serial.print(char_buffer_1[4], HEX);
string_array[i][1].toCharArray(char_buffer_2, string_array[i][1].length() + 1);
Serial.print(" ---- Converted ---- ");
Serial.print(char_buffer_2[0], HEX);
Serial.print(" ");
Serial.print(char_buffer_2[1], HEX);
Serial.print(" ");
Serial.print(char_buffer_2[2], HEX);
Serial.print(" ");
Serial.print(char_buffer_2[3], HEX);
Serial.print(" ");
Serial.println(char_buffer_2[4], HEX);
}
}
//Helper Functions
extern void IRAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
//pwm_stop_pin(pin);
if (pin < 16) {
if (val) GPOS = (1 << pin);
else GPOC = (1 << pin);
} else if (pin == 16) {
if (val) GP16O |= 1;
else GP16O &= ~1;
}
}
extern int IRAM_ATTR __digitalRead(uint8_t pin) {
//pwm_stop_pin(pin);
if (pin < 16) {
return GPIP(pin);
} else if (pin == 16) {
return GP16I & 0x01;
}
return 0;
}
uint8_t cpu_freq_mhz()
{
return ESP.getCpuFreqMHz();
}