-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiltuino.ino
246 lines (221 loc) · 7.82 KB
/
tiltuino.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
242
243
244
245
246
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
#include <ArduinoBLE.h>
char asciiBanner[] = " ▄▄▄▄▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄▄▄▄▄ ▄▄ ▄▄ ▄▄▄ ▄▄ ▄ ▄▄▄▄▄▄▄ \n█ █ █ █ █ █ █ █ █ █ █ █ █ █\n█▄ ▄█ █ █ █▄ ▄█ █ █ █ █ █▄█ █ ▄ █\n █ █ █ █ █ █ █ █ █▄█ █ █ █ █ █ █\n █ █ █ █ █▄▄▄ █ █ █ █ █ ▄ █ █▄█ █\n █ █ █ █ █ █ █ █ █ █ █ █ █ █\n █▄▄▄█ █▄▄▄█▄▄▄▄▄▄▄█ █▄▄▄█ █▄▄▄▄▄▄▄█▄▄▄█▄█ █▄▄█▄▄▄▄▄▄▄█";
char wifiSsid[] = "abc";
char wifiPassword[] = "abc";
char tiltBluetoothAddress[] = "00:00:00:00:00:00";
String customDeviceName = "Tiltuino";
char hostname[] = "tiltuino";
char brewfatherRootAddress[] = "log.brewfather.net";
char brewfatherCustomStreamPath[] = "/stream?id=abc";
long sendInterval = 10000;
bool useCelsius = true;
bool useCustomDns = false;
IPAddress customDns(1, 1, 1, 1);
bool debugMode = true;
bool sendDataToBrewfather = true;
WiFiClient wifiClient;
HttpClient httpClient = HttpClient(wifiClient, brewfatherRootAddress, 80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
if (debugMode) {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println(asciiBanner);
}
}
bool startBluetooth() {
if (!BLE.begin()) {
if (debugMode) {
Serial.println("starting BLE failed!");
}
return false;
}
if (debugMode) {
Serial.println("===BLUETOOTH START=======================================");
}
return true;
}
bool stopBluetooth() {
BLE.end();
if (debugMode) {
Serial.println("===BLUETOOTH STOP========================================");
}
}
int getNextValueFromTiltHydrometer(float reading[]) {
int scanningSuccessful = BLE.scan();
if (debugMode) {
Serial.println("Scanning " + scanningSuccessful ? "successful" : "failed");
}
if (scanningSuccessful = 0){
// Reset reading and return
reading[0] = 0;
reading[1] = 0;
return 0;
}
BLEDevice peripheral = BLE.available();
if(debugMode){
Serial.println("---------");
Serial.println("Found device");
Serial.println("Address " + peripheral.address());
Serial.println("HasManufacturerData " + peripheral.hasManufacturerData() ? "yes" : "no");
}
if (peripheral.address() != tiltBluetoothAddress || peripheral.hasManufacturerData() == 0){
// Reset reading and return
reading[0] = 0;
reading[1] = 0;
return 0;
}
if (debugMode) {
Serial.println("===READING START=========================================");
Serial.print("Address: ");
Serial.println(peripheral.address());
Serial.print("RSSI: ");
Serial.println(peripheral.rssi());
}
String manufacturerData = peripheral.manufacturerData();
if (debugMode) {
Serial.print("Manufacturer data: ");
Serial.println(manufacturerData);
}
String temperatureHex = manufacturerData.substring(40, 44);
String gravityHex = manufacturerData.substring(44, 48);
char tempHexAsChar[5];
temperatureHex.toCharArray(tempHexAsChar, 5);
float tempFarenheit = strtol(tempHexAsChar, NULL, 16);
float tempCelsius = (tempFarenheit - 32) * .5556;
if (debugMode) {
Serial.print("Temp: ");
Serial.println(useCelsius ? tempCelsius : tempFarenheit);
}
char gravityAsChar[5];
gravityHex.toCharArray(gravityAsChar, 5);
long gravityAsDecimalTimesThousand = strtol(gravityAsChar, NULL, 16);
float gravity = gravityAsDecimalTimesThousand / 1000.0f;
if (debugMode) {
Serial.print("Gravity: ");
Serial.println(gravity, 3);
Serial.println("===READING END===========================================");
}
reading[0] = useCelsius ? tempCelsius : tempFarenheit;
reading[1] = gravity;
return 1;
}
bool startWifi() {
if (WiFi.status() == WL_NO_MODULE) {
if (debugMode) {
Serial.println("Communication with the wifi module failed, stalling..");
}
return false;
}
if (debugMode) {
Serial.println("===WIFI START============================================");
}
connectToAP();
if (debugMode) {
printWifiwifiStatus();
}
return true;
}
void stopWifi() {
WiFi.end();
if (debugMode) {
Serial.println("===WIFI STOP=============================================");
}
}
void sendReadingToBrewfather(float reading[]) {
if (WiFi.status() == WL_CONNECTED) {
httpClient = HttpClient(wifiClient, brewfatherRootAddress, 80);
String requestBody = "{\n \"name\": \"" + customDeviceName + "\",\n \"temp\": \"" + String(reading[0]) + "\",\n \"temp_unit\": \"" + (useCelsius ? "C" : "F") + "\",\n \"gravity\": \"" + String(reading[1]) + "\",\n \"gravity_unit\": \"G\"\n}";
String request = "POST " + String(brewfatherCustomStreamPath) + " HTTP/1.1\nContent-Type: application/json\nAccept: */*\nHost: " + brewfatherRootAddress + "\nAccept-Encoding: gzip, deflate, br\nConnection: keep-alive\nContent-Length: " + requestBody.length() + "\n\n" + requestBody;
if (debugMode) {
Serial.print("Attempting to log to: ");
Serial.print(brewfatherRootAddress);
Serial.println(brewfatherCustomStreamPath);
Serial.println("===REQUEST START=======================================");
Serial.println(request);
Serial.println("===REQUEST END===========================================");
}
int statusCode = 0;
if (sendDataToBrewfather) {
httpClient.post(brewfatherCustomStreamPath, "application/json", requestBody);
statusCode = httpClient.responseStatusCode();
}
if (debugMode && sendDataToBrewfather) {
Serial.println("===RESPONSE START========================================");
Serial.print("Status code: ");
Serial.println(statusCode);
String response = httpClient.responseBody();
Serial.print("Body: ");
Serial.println(response);
Serial.println("===RESPONSE END==========================================");
}
}
}
void printWifiwifiStatus() {
if (debugMode) {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP(); // Device IP address
Serial.print("IP Address: ");
Serial.println(ip);
}
}
void connectToAP() {
while (WiFi.status() != WL_CONNECTED) {
if (debugMode) {
Serial.print("Attempting to connect to wifi network: ");
Serial.print(wifiSsid);
Serial.println("...");
}
WiFi.setHostname(hostname);
WiFi.begin(wifiSsid, wifiPassword);
}
delay(1000);
if (debugMode) {
Serial.println("Success!");
}
if (useCustomDns) {
WiFi.setDNS(customDns);
if (debugMode) {
Serial.print("DNS Configured: ");
Serial.println(IpAddressToString(customDns));
}
}
wifiClient = WiFiClient();
}
String IpAddressToString(const IPAddress& ipAddress)
{
return String(ipAddress[0]) + String(".") + \
String(ipAddress[1]) + String(".") + \
String(ipAddress[2]) + String(".") + \
String(ipAddress[3]) ;
}
void loop() {
if (debugMode) {
Serial.println("===FETCHING NEW VALUE START================================");
}
float reading[2] = {0, 0};
if (startBluetooth()) {
int successfulReading = 0;
while (successfulReading == 0) {
successfulReading = getNextValueFromTiltHydrometer(reading);
if (successfulReading == 0){
delay(1000);
}
}
stopBluetooth();
}
if (startWifi()) {
sendReadingToBrewfather(reading);
stopWifi();
}
if (debugMode) {
Serial.println("===FETCHING NEW VALUE END================================");
Serial.println(String("===WAITING ") + String(sendInterval) + String("MS======================================"));
}
delay(sendInterval);
}