diff --git a/WeatherStation.ino b/WeatherStation.ino index f098fe5..d59338b 100644 --- a/WeatherStation.ino +++ b/WeatherStation.ino @@ -72,9 +72,19 @@ parser.startDelay = 0; } } -/* if ((parser.lastMillis + 60000) < millis()) { + if (Serial.available() > 0) { + int in = Serial.read(); + if (in == 's') { + Serial.print("{\"usbTaskState\": \""); + Serial.print(Usb.getUsbTaskState(), HEX); + Serial.println("\"}\""); + } + } + if ((parser.lastMillis + 60000) < millis()) { + parser.SetReadingZero(); parser.lastMillis = millis(); - startUsb(); - } */ + parser.startDelay = millis(); +// startUsb(); + } } diff --git a/hidwh1080rptparser.cpp b/hidwh1080rptparser.cpp index 77629d8..4754860 100644 --- a/hidwh1080rptparser.cpp +++ b/hidwh1080rptparser.cpp @@ -1,5 +1,34 @@ #include "hidwh1080rptparser.h" +WH1080ReportParser::WH1080ReportParser() { + chunkStart = 0; + readingBlockZero = true; + for (uint8_t i = 0; i < sizeof(WH1080_REPORT); i++) { + report[i] = pgm_read_byte(&WH1080_REPORT[i]); + } + memset(lastChunk, 0, WH1080_BUFFER_CHUNK); + lastMillis = millis(); + startDelay = 1000 + millis(); +} + +void WH1080ReportParser::SetReadingBlock(uint8_t high, uint8_t low) { + report[1] = high; + report[2] = low; + report[5] = high; + report[6] = low; + readingBlockZero = false; + chunkStart = 0; +} + +void WH1080ReportParser::SetReadingZero() { + report[1] = 0; + report[2] = 0; + report[5] = 0; + report[6] = 0; + readingBlockZero = true; + chunkStart = 0; +} + void WH1080ReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) { // See http://www.jim-easterbrook.me.uk/weather/mm/ @@ -7,12 +36,7 @@ memcpy(chunk + chunkStart, buf, WH1080_BUFFER_CHUNK - chunkStart); chunkStart = 0; if (readingBlockZero) { - unsigned short currentPos = chunk[WH1080_CURRENT_POS] + (chunk[WH1080_CURRENT_POS + 1] << 8); - report[1] = (uint8_t)(currentPos / 256); - report[2] = (uint8_t)(currentPos & 255); - report[5] = (uint8_t)(currentPos / 256); - report[6] = (uint8_t)(currentPos & 255); - readingBlockZero = false; + SetReadingBlock(chunk[WH1080_CURRENT_POS + 1], chunk[WH1080_CURRENT_POS]); startDelay = millis() + 250; } else { if (memcmp(chunk, lastChunk, WH1080_BUFFER_CHUNK) != 0) { @@ -25,35 +49,49 @@ lastMillis = millis(); Serial.print(",\"delay\":"); Serial.print(chunk[WH1080_DELAY]); - Serial.print(",\"in_humidity\":"); - Serial.print(chunk[WH1080_HUMIDITY_IN]); - Serial.print(",\"in_temp\":"); - Serial.print((((chunk[WH1080_TEMPERATURE_IN+1] & 0x7f) << 8) + chunk[WH1080_TEMPERATURE_IN]) * ((chunk[WH1080_TEMPERATURE_IN+1] & 0x80) ? -0.1 : 0.1)); - Serial.print(",\"out_humidity\":"); - Serial.print(chunk[WH1080_HUMIDITY_OUT]); - Serial.print(",\"out_temp\":"); - Serial.print((((chunk[WH1080_TEMPERATURE_OUT+1] & 0x7f) << 8) + chunk[WH1080_TEMPERATURE_OUT]) * ((chunk[WH1080_TEMPERATURE_OUT+1] & 0x80) ? -0.1 : 0.1)); - Serial.print(",\"pressure\":"); - Serial.print(((chunk[WH1080_ABS_PRESSURE+1] << 8) + chunk[WH1080_ABS_PRESSURE]) * 0.1); - Serial.print(",\"wind_speed\":"); - Serial.print((chunk[WH1080_WIND_AVE] + ((chunk[WH1080_WIND_GUST+1] & 0x0f) << 8)) * 0.1); - Serial.print(",\"wind_gust\":"); - Serial.print((chunk[WH1080_WIND_GUST] + ((chunk[WH1080_WIND_GUST+1] & 0xf0) << 4)) * 0.1); - Serial.print(",\"wind_dir\":"); - Serial.print(chunk[WH1080_WIND_DIR] * 22.5); - Serial.print(",\"rain\":"); - Serial.print(((chunk[WH1080_RAIN+1] << 8) + chunk[WH1080_RAIN]) * 0.3); // TODO: check overflow bit + if (chunk[WH1080_HUMIDITY_IN] != 0xFF) { + Serial.print(",\"in_humidity\":"); + Serial.print(chunk[WH1080_HUMIDITY_IN]); + } + if (!((chunk[WH1080_TEMPERATURE_IN] == 0xFF) && (chunk[WH1080_TEMPERATURE_IN + 1] == 0xFF))) { + Serial.print(",\"in_temp\":"); + Serial.print((((chunk[WH1080_TEMPERATURE_IN+1] & 0x7f) << 8) + chunk[WH1080_TEMPERATURE_IN]) * ((chunk[WH1080_TEMPERATURE_IN+1] & 0x80) ? -0.1 : 0.1)); + } + if (chunk[WH1080_HUMIDITY_OUT] != 0xFF) { + Serial.print(",\"out_humidity\":"); + Serial.print(chunk[WH1080_HUMIDITY_OUT]); + } + if (!((chunk[WH1080_TEMPERATURE_OUT] == 0xFF) && (chunk[WH1080_TEMPERATURE_OUT + 1] == 0xFF))) { + Serial.print(",\"out_temp\":"); + Serial.print((((chunk[WH1080_TEMPERATURE_OUT+1] & 0x7f) << 8) + chunk[WH1080_TEMPERATURE_OUT]) * ((chunk[WH1080_TEMPERATURE_OUT+1] & 0x80) ? -0.1 : 0.1)); + } + if (!((chunk[WH1080_ABS_PRESSURE] == 0xFF) && (chunk[WH1080_ABS_PRESSURE + 1] == 0xFF))) { + Serial.print(",\"pressure\":"); + Serial.print(((chunk[WH1080_ABS_PRESSURE+1] << 8) + chunk[WH1080_ABS_PRESSURE]) * 0.1); + } + if (!((chunk[WH1080_WIND_AVE] == 0xFF) && ((chunk[WH1080_WIND_GUST + 1] & 0x0F) == 0x0F))) { + Serial.print(",\"wind_speed\":"); + Serial.print((chunk[WH1080_WIND_AVE] + ((chunk[WH1080_WIND_GUST+1] & 0x0f) << 8)) * 0.1); + } + if (!((chunk[WH1080_WIND_GUST] == 0xFF) && ((chunk[WH1080_WIND_GUST + 1] & 0xF0) == 0xF0))) { + Serial.print(",\"wind_gust\":"); + Serial.print((chunk[WH1080_WIND_GUST] + ((chunk[WH1080_WIND_GUST+1] & 0xf0) << 4)) * 0.1); + } + if (chunk[WH1080_WIND_DIR] != 0xFF) { + Serial.print(",\"wind_dir\":"); + Serial.print(chunk[WH1080_WIND_DIR] * 22.5); + } + if (!((chunk[WH1080_RAIN] == 0xFF) && (chunk[WH1080_RAIN + 1] == 0xFF))) { + Serial.print(",\"rain\":"); + Serial.print(((chunk[WH1080_RAIN+1] << 8) + chunk[WH1080_RAIN]) * 0.3); // TODO: check overflow bit + } Serial.print(",\"status\":"); - Serial.print(chunk[WH1080_STATUS], HEX); + Serial.print(chunk[WH1080_STATUS]); Serial.println("}"); /* delay(5000); digitalWrite(2, HIGH); // put XBee to sleep */ } - readingBlockZero = true; - report[1] = 0; - report[2] = 0; - report[5] = 0; - report[6] = 0; + SetReadingZero(); startDelay = millis() + 5000; } } else { diff --git a/hidwh1080rptparser.h b/hidwh1080rptparser.h index 26c686c..a3386f6 100644 --- a/hidwh1080rptparser.h +++ b/hidwh1080rptparser.h @@ -66,18 +66,13 @@ unsigned long lastMillis; uint8_t report[8]; - WH1080ReportParser() { - chunkStart = 0; - readingBlockZero = true; - for (uint8_t i = 0; i < sizeof(WH1080_REPORT); i++) { - report[i] = pgm_read_byte(&WH1080_REPORT[i]); - } - memset(lastChunk, 0, WH1080_BUFFER_CHUNK); - lastMillis = millis(); - startDelay = 1000 + millis(); - } + WH1080ReportParser(); virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); + + void SetReadingBlock(uint8_t high, uint8_t low); + void SetReadingZero(); + }; #endif // __HIDWH1080RPTPARSER_H__