Skip to content

Commit 4f858be

Browse files
Merge pull request #22 from BernhardSchlegel/feature/calibration
Feature/calibration
2 parents 57da728 + 8a612b5 commit 4f858be

File tree

1 file changed

+138
-116
lines changed

1 file changed

+138
-116
lines changed

ESP8266/src/main.cpp

+138-116
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ uint8_t global_warning = 0;
3636
String global_error_text = "";
3737
String global_warning_text = "";
3838
float celsius, fahrenheit;
39+
String sensor_id = "";
3940
String chipid = "";
4041
uint32_t main_interval_ms = 1000; // 1s default intervall for first iteration
4142
uint8_t global_relais_state = 0;
42-
String global_version = "0.9.2";
43+
String global_version = "0.9.4";
4344

4445
void writeStringToEEPROM(int addrOffset, const String &strToWrite)
4546
{
@@ -155,6 +156,138 @@ void short_flash_500ms(uint8_t count)
155156
}
156157
}
157158

159+
void arrayToString(byte array[], unsigned int len, char buffer[])
160+
{
161+
// source https://stackoverflow.com/questions/44748740/
162+
for (unsigned int i = 0; i < len; i++)
163+
{
164+
byte nib1 = (array[i] >> 4) & 0x0F;
165+
byte nib2 = (array[i] >> 0) & 0x0F;
166+
buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
167+
buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
168+
}
169+
buffer[len * 2] = '\0';
170+
}
171+
172+
void readTemperature()
173+
{
174+
// put your main code here, to run repeatedly:
175+
byte i;
176+
byte present = 0;
177+
byte type_s;
178+
byte data[12];
179+
byte addr[8];
180+
181+
if (!ds.search(addr))
182+
{
183+
Serial.println("No more addresses.");
184+
Serial.println();
185+
ds.reset_search();
186+
delay(250);
187+
return;
188+
}
189+
190+
Serial.print("ROM =");
191+
for (i = 0; i < 8; i++)
192+
{
193+
Serial.write(' ');
194+
Serial.print(addr[i], HEX);
195+
}
196+
197+
if (OneWire::crc8(addr, 7) != addr[7])
198+
{
199+
Serial.println("CRC is not valid!");
200+
return;
201+
}
202+
Serial.println();
203+
204+
// the first ROM byte indicates which chip
205+
switch (addr[0])
206+
{
207+
case 0x10:
208+
Serial.println(" Chip = DS18S20"); // or old DS1820
209+
type_s = 1;
210+
break;
211+
case 0x28:
212+
Serial.println(" Chip = DS18B20");
213+
type_s = 0;
214+
break;
215+
case 0x22:
216+
Serial.println(" Chip = DS1822");
217+
type_s = 0;
218+
break;
219+
default:
220+
Serial.println("Device is not a DS18x20 family device.");
221+
return;
222+
}
223+
224+
ds.reset();
225+
ds.select(addr);
226+
ds.write(0x44, 1); // start conversion, with parasite power on at the end
227+
228+
delay(1000); // maybe 750ms is enough, maybe not
229+
// we might do a ds.depower() here, but the reset will take care of it.
230+
231+
present = ds.reset();
232+
ds.select(addr);
233+
ds.write(0xBE); // Read Scratchpad
234+
235+
// store sensor id => 8 bytes, one byte needs 2 char + Termination
236+
char buffer[17] = "";
237+
arrayToString(addr, 8, buffer);
238+
sensor_id = String(buffer);
239+
240+
Serial.print(" Data = ");
241+
Serial.print(present, HEX);
242+
Serial.print(" ");
243+
for (i = 0; i < 9; i++)
244+
{ // we need 9 bytes
245+
data[i] = ds.read();
246+
Serial.print(data[i], HEX);
247+
Serial.print(" ");
248+
}
249+
Serial.print(" CRC=");
250+
Serial.print(OneWire::crc8(data, 8), HEX);
251+
Serial.println();
252+
253+
// Convert the data to actual temperature
254+
// because the result is a 16 bit signed integer, it should
255+
// be stored to an "int16_t" type, which is always 16 bits
256+
// even when compiled on a 32 bit processor.
257+
int16_t raw = (data[1] << 8) | data[0];
258+
if (type_s)
259+
{
260+
raw = raw << 3; // 9 bit resolution default
261+
if (data[7] == 0x10)
262+
{
263+
// "count remain" gives full 12 bit resolution
264+
raw = (raw & 0xFFF0) + 12 - data[6];
265+
}
266+
}
267+
else
268+
{
269+
byte cfg = (data[4] & 0x60);
270+
// at lower res, the low bits are undefined, so let's zero them
271+
if (cfg == 0x00)
272+
raw = raw & ~7; // 9 bit resolution, 93.75 ms
273+
else if (cfg == 0x20)
274+
raw = raw & ~3; // 10 bit res, 187.5 ms
275+
else if (cfg == 0x40)
276+
raw = raw & ~1; // 11 bit res, 375 ms
277+
//// default is 12 bit resolution, 750 ms conversion time
278+
}
279+
celsius = (float)raw / 16.0;
280+
fahrenheit = celsius * 1.8 + 32.0;
281+
Serial.print(" Temperature = ");
282+
Serial.print(celsius);
283+
Serial.println(" Celsius.");
284+
//Serial.print(fahrenheit);
285+
//Serial.println(" Fahrenheit");
286+
287+
String apikey_restored = readStringFromEEPROM(EEPROM_ADDRESS_APIKEY);
288+
Serial.println("PARAM apikey from EEPROM = " + apikey_restored);
289+
}
290+
158291
void setup()
159292
{
160293
Serial.println("BierBot Brick 101 starting");
@@ -187,6 +320,9 @@ void setup()
187320

188321
Serial.println("\n Starting");
189322

323+
// read temperature once
324+
readTemperature();
325+
190326
// guid, len 36 + terminator
191327
// e.g. 550e8400-e29b-11d4-a716-446655440000
192328
// test custom html(radio)
@@ -333,128 +469,14 @@ void checkRequest()
333469
}
334470
}
335471

336-
void readTemperature()
337-
{
338-
// put your main code here, to run repeatedly:
339-
byte i;
340-
byte present = 0;
341-
byte type_s;
342-
byte data[12];
343-
byte addr[8];
344-
345-
if (!ds.search(addr))
346-
{
347-
Serial.println("No more addresses.");
348-
Serial.println();
349-
ds.reset_search();
350-
delay(250);
351-
return;
352-
}
353-
354-
Serial.print("ROM =");
355-
for (i = 0; i < 8; i++)
356-
{
357-
Serial.write(' ');
358-
Serial.print(addr[i], HEX);
359-
}
360-
361-
if (OneWire::crc8(addr, 7) != addr[7])
362-
{
363-
Serial.println("CRC is not valid!");
364-
return;
365-
}
366-
Serial.println();
367-
368-
// the first ROM byte indicates which chip
369-
switch (addr[0])
370-
{
371-
case 0x10:
372-
Serial.println(" Chip = DS18S20"); // or old DS1820
373-
type_s = 1;
374-
break;
375-
case 0x28:
376-
Serial.println(" Chip = DS18B20");
377-
type_s = 0;
378-
break;
379-
case 0x22:
380-
Serial.println(" Chip = DS1822");
381-
type_s = 0;
382-
break;
383-
default:
384-
Serial.println("Device is not a DS18x20 family device.");
385-
return;
386-
}
387-
388-
ds.reset();
389-
ds.select(addr);
390-
ds.write(0x44, 1); // start conversion, with parasite power on at the end
391-
392-
delay(1000); // maybe 750ms is enough, maybe not
393-
// we might do a ds.depower() here, but the reset will take care of it.
394-
395-
present = ds.reset();
396-
ds.select(addr);
397-
ds.write(0xBE); // Read Scratchpad
398-
399-
Serial.print(" Data = ");
400-
Serial.print(present, HEX);
401-
Serial.print(" ");
402-
for (i = 0; i < 9; i++)
403-
{ // we need 9 bytes
404-
data[i] = ds.read();
405-
Serial.print(data[i], HEX);
406-
Serial.print(" ");
407-
}
408-
Serial.print(" CRC=");
409-
Serial.print(OneWire::crc8(data, 8), HEX);
410-
Serial.println();
411-
412-
// Convert the data to actual temperature
413-
// because the result is a 16 bit signed integer, it should
414-
// be stored to an "int16_t" type, which is always 16 bits
415-
// even when compiled on a 32 bit processor.
416-
int16_t raw = (data[1] << 8) | data[0];
417-
if (type_s)
418-
{
419-
raw = raw << 3; // 9 bit resolution default
420-
if (data[7] == 0x10)
421-
{
422-
// "count remain" gives full 12 bit resolution
423-
raw = (raw & 0xFFF0) + 12 - data[6];
424-
}
425-
}
426-
else
427-
{
428-
byte cfg = (data[4] & 0x60);
429-
// at lower res, the low bits are undefined, so let's zero them
430-
if (cfg == 0x00)
431-
raw = raw & ~7; // 9 bit resolution, 93.75 ms
432-
else if (cfg == 0x20)
433-
raw = raw & ~3; // 10 bit res, 187.5 ms
434-
else if (cfg == 0x40)
435-
raw = raw & ~1; // 11 bit res, 375 ms
436-
//// default is 12 bit resolution, 750 ms conversion time
437-
}
438-
celsius = (float)raw / 16.0;
439-
fahrenheit = celsius * 1.8 + 32.0;
440-
Serial.print(" Temperature = ");
441-
Serial.print(celsius);
442-
Serial.println(" Celsius.");
443-
//Serial.print(fahrenheit);
444-
//Serial.println(" Fahrenheit");
445-
446-
String apikey_restored = readStringFromEEPROM(EEPROM_ADDRESS_APIKEY);
447-
Serial.println("PARAM apikey from EEPROM = " + apikey_restored);
448-
}
449-
450472
void contactBackend()
451473
{
452474
if (1 == 1)
453475
{ // WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
454476
String apikey_restored = readStringFromEEPROM(EEPROM_ADDRESS_APIKEY);
455477
String temp = String(celsius);
456478
String relais = String(global_relais_state);
457-
String url = "https://bricks.bierbot.com/api/iot/v1?apikey=" + apikey_restored + "&type=" + "sonoff_th16" + "&brand=" + "bierbot" + "&version=" + global_version + "&s_number_temp_0=" + temp + "&a_bool_epower_0=" + relais + "&chipid=" + chipid; // + "&temp=" + temp + "&type=" + "sonoff_th16" + "&version=" + global_version
479+
String url = "https://bricks.bierbot.com/api/iot/v1?apikey=" + apikey_restored + "&type=" + "sonoff_th16" + "&brand=" + "bierbot" + "&version=" + global_version + "&s_number_temp_0=" + temp + "&a_bool_epower_0=" + relais + "&chipid=" + chipid + "&s_number_temp_id_0=" + sensor_id;
458480

459481
Serial.print("s_number_temp_0=" + temp);
460482
Serial.println(", a_bool_epower_0=" + relais);

0 commit comments

Comments
 (0)