resolved mdns conflicts and merged

master v0.0.6
Kenneth Barbour 2020-06-26 20:00:05 -04:00
commit 1aae56a0fe
8 changed files with 137 additions and 26 deletions

View File

@ -8,6 +8,7 @@
#include "config.h"
#include "time.h"
#include "mdns.h"
#include "mqtt.h"
#ifndef SERIAL_BAUD
#define SERIAL_BAUD 74880
@ -78,6 +79,7 @@ void setup()
uweather_time_init();
uweather_update_init();
uweather_mqtt_init();
}
@ -87,5 +89,6 @@ void loop()
uweather_update_handle();
uweather_time_handle();
uweather_mdns_handle();
uweather_mqtt_handle();
delay(10);
}

73
src/mqtt.cpp 100644
View File

@ -0,0 +1,73 @@
#include "mqtt.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "config.h"
#include <Buffer.h>
#include "sensors.h"
#define MQTT_BUFFER_SIZE 200
#define MQTT_REPORT_INTERVAL 60000
bool enabled = false;
WiFiClient wfclient;
PubSubClient mqtt(wfclient);
char buffer[MQTT_BUFFER_SIZE] = {};
unsigned long last_millis = 0;
void uweather_mqtt_init()
{
last_millis = 0;
// should we be running?
if (Config._data.mqtt_host[0] == '\0') {
enabled = false;
Serial.println("MQTT disabled, no host set");
return;
}
//mqtt.setServer(Config._data.mqtt_host, 1883); //TODO: configurable port
// server is an ip addr
IPAddress addr;
if (addr.fromString((const char*) Config._data.mqtt_host)) {
mqtt.setServer(addr, 1883); //TODO: configurable port
} else {
mqtt.setServer((const char*) Config._data.mqtt_host, 1883);
}
Serial.printf("MQTT Host set to %s\n", Config._data.mqtt_host);
enabled = true;
}
void uweather_mqtt_handle()
{
if (!enabled) return;
unsigned long now = millis();
if (now - last_millis < MQTT_REPORT_INTERVAL) return;
last_millis = now;
if (!mqtt.connected()) {
String client_id = String(random(now), HEX);
Serial.printf("Connecting to MQTT Host %s...", Config._data.mqtt_host);
if (mqtt.connect(client_id.c_str())) {
Serial.println(" connected");
} else {
Serial.println(" unable to connect");
return;
}
}
const char* topic = "/";
if (Config._data.mqtt_prefix[0] != '\0') {
topic = (const char*) Config._data.mqtt_prefix;
}
int result = sensors_print_json(buffer, MQTT_BUFFER_SIZE);
if (result > MQTT_BUFFER_SIZE || result < 0) {
Serial.printf("ERROR: MQTT_BUFFER_SIZE too small; needed %d bytes, but only had %d\n", result, MQTT_BUFFER_SIZE);
return;
}
Serial.printf("Publishing data to mqtt://%s topic: %s\n", Config._data.mqtt_host, topic);
mqtt.publish(topic, buffer);
}

7
src/mqtt.h 100644
View File

@ -0,0 +1,7 @@
#ifndef _UWEATHER_MQTT_H_
#define _UWEATHER_MQTT_H_
void uweather_mqtt_init();
void uweather_mqtt_handle();
#endif // _UWEATHER_MQTT_H_ include guard

28
src/sensors.cpp 100644
View File

@ -0,0 +1,28 @@
#include "sensors.h"
#include "time.h"
#include "bme280.h"
#include "battery.h"
int sensors_print_json(char* buffer, size_t len) {
uweather_handle_battery();
unsigned long now = current_timestamp();
char buff[5][10] = {};
dtostrf(BME.readTemperature(), 2, 1, buff[0]);
dtostrf(BME.readPressure(), 2, 1, buff[1]);
dtostrf(BME.readHumidity(), 2, 1, buff[2]);
dtostrf(battery_get_voltage(),2, 1, buff[3]);
if (now) {
sprintf(buff[4], "%d", now);
} else {
strcpy(buff[4], "null");
}
return snprintf(buffer, len, "{\"time\": %s, \"temperature\": %s, \"pressure\": %s, \"humidity\": %s, \"battery\": { \"voltage\": %s, \"is_charging\": %s, \"percent\": %d}}",
buff[4],
buff[0],
buff[1],
buff[2],
buff[3],
(Battery.is_charging ? "true":"false"),
battery_get_percent()
);
}

8
src/sensors.h 100644
View File

@ -0,0 +1,8 @@
#ifndef _UWEATHER_SENSORS_H_
#define _UWEATHER_SENSORS_H_
#include <stddef.h>
int sensors_print_json(char*, size_t);
#endif // _UWEATHER_SENSORS_H_ include guard

View File

@ -77,17 +77,19 @@ void uweather_time_handle()
unsigned long high = word(packet_buffer[40], packet_buffer[41]);
unsigned long low = word(packet_buffer[42], packet_buffer[43]);
unsigned long secs = (high << 16 | low) - 2208988800UL; // subtract 70 years from time
time_offset = millis();
if (has_timestamp()) {
Serial.printf("Time Drift: %lu\n", current_timestamp() - secs);
}
time_offset = millis()/1000;
ntp_timestamp = secs;
ntp_state = state::IDLE;
Serial.printf("Received NTP response, current timestamp is %lu\n", secs);
retry_at = millis() + 6 * 60 * 60 * 1000;
}
case state::IDLE:
break;
case state::NO_HOST:
if (millis() > retry_at) {
state::INIT;
ntp_state = state::INIT;
}
break;
default:

View File

@ -6,5 +6,6 @@ void uweather_time_handle();
unsigned long timestamp(unsigned long);
unsigned long current_timestamp();
bool has_timestamp();
#endif /** End _UWEATHER_NTP_H_ include guard */

View File

@ -6,8 +6,9 @@
#include <QueryString.h>
#include "config.h"
#include "version.h"
#include "battery.h"
#include "time.h"
#include "mqtt.h"
#include "sensors.h"
/**
* How long to wait after the OK to shutdown before no longer
@ -82,27 +83,15 @@ void handle_firmware(HttpRequest& req, HttpResponse& res)
void handle_weather_current(HttpRequest& req, HttpResponse& res)
{
unsigned long now = current_timestamp();
char buff[5][10] = {};
dtostrf(BME.readTemperature(), 2, 1, buff[0]);
dtostrf(BME.readPressure(), 2, 1, buff[1]);
dtostrf(BME.readHumidity(), 2, 1, buff[2]);
dtostrf(battery_get_voltage(),2, 1, buff[3]);
if (now) {
sprintf(buff[4], "%d", now);
} else {
strcpy(buff[4], "null");
}
res.headers.set("Content-Type", "application/json");
content.printf("{\"time\": %s, \"temperature\": %s, \"pressure\": %s, \"humidity\": %s, \"battery\": { \"voltage\": %s, \"is_charging\": %s, \"percent\": %d}}",
buff[4],
buff[0],
buff[1],
buff[2],
buff[3],
(Battery.is_charging ? "true":"false"),
battery_get_percent()
);
char buff[250];
int printed = sensors_print_json(buff, 250);
if (printed > 250 || printed < 0) {
res.code = 500;
client_error("Sensor result data too large", res);
return;
}
content.print(buff);
}
// Handle WiFi Scan
@ -199,6 +188,7 @@ void handle_config_set(HttpRequest& req, HttpResponse& res)
result = Config.setNTPHost(value);
} else if (strcmp("mqtt_host", key) == 0) {
result = Config.setMQTTHost(value);
uweather_mqtt_init();
} else if (strcmp("mqtt_prefix", key) == 0) {
result = Config.setMQTTPrefix(value);
} else if (strcmp("enable_http", key) == 0) {
@ -301,7 +291,6 @@ void uweather_web_init(void)
void uweather_web_handle(void)
{
if (web_is_shutdown) return;
uweather_handle_battery();
if (web_shutdown_at) {
unsigned long now = millis();
if (now > web_shutdown_at) {