-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wireless stuff on its separate task, separate modules for general wif…
…i+OTA, webserver and MQTT, some messing around with the settings
- Loading branch information
Showing
11 changed files
with
1,328 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
#include <Arduino.h> | ||
#include "USER_SETTINGS.h" | ||
|
||
#ifdef WEBSERVER | ||
#define ENABLE_AP //Comment out this line to turn off the broadcasted AP | ||
const char* ssid = "REPLACE_WITH_YOUR_SSID"; // maximum of 63 characters; | ||
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // minimum of 8 characters; | ||
#ifdef USE_WIFI | ||
|
||
// MQTT | ||
// For more detailed settings, see mqtt.h | ||
#ifdef USE_MQTT | ||
const char* mqtt_user = "REDACTED"; | ||
const char* mqtt_password = "REDACTED"; | ||
#endif // USE_MQTT | ||
|
||
const char* ssid = "REDACTED"; // maximum of 63 characters; | ||
const char* password = "REDACTED"; // minimum of 8 characters; | ||
const char* ssidAP = "Battery Emulator"; // maximum of 63 characters; | ||
const char* passwordAP = "123456789"; // minimum of 8 characters; set to NULL if you want the access point to be open | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "mqtt.h" | ||
#include <Arduino.h> | ||
#include <WiFi.h> | ||
#include <freertos/FreeRTOS.h> | ||
#include "../../../USER_SETTINGS.h" | ||
#include "../../lib/knolleary-pubsubclient/PubSubClient.h" | ||
#include "../wifi/wifi.h" | ||
|
||
const char* mqtt_subscriptions[] = MQTT_SUBSCRIPTIONS; | ||
const size_t mqtt_nof_subscriptions = sizeof(mqtt_subscriptions) / sizeof(mqtt_subscriptions[0]); | ||
|
||
WiFiClient espClient; | ||
PubSubClient client(espClient); | ||
char msg[MSG_BUFFER_SIZE]; | ||
int value = 0; | ||
static unsigned long previousMillisUpdateVal; | ||
|
||
#ifdef USE_MQTT | ||
/** Publish global values and call callbacks for specific modules */ | ||
static void publish_values(void) { | ||
|
||
snprintf(msg, sizeof(msg), | ||
"{\n" | ||
" \"SOC\": %.3f,\n" | ||
" \"StateOfHealth\": %.3f,\n" | ||
" \"temperature_min\": %.3f,\n" | ||
" \"temperature_max\": %.3f,\n" | ||
" \"cell_max_voltage\": %d,\n" | ||
" \"cell_min_voltage\": %d\n" | ||
"}\n", | ||
((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0, | ||
((float)((int16_t)temperature_max)) / 10.0, cell_max_voltage, cell_min_voltage); | ||
client.publish("battery/info", msg); | ||
// Serial.println(msg); // Uncomment to print the payload on serial | ||
} | ||
|
||
/* This is called whenever a subscribed topic changes (hopefully) */ | ||
static void callback(char* topic, byte* payload, unsigned int length) { | ||
Serial.print("Message arrived ["); | ||
Serial.print(topic); | ||
Serial.print("] "); | ||
for (unsigned int i = 0; i < length; i++) { | ||
Serial.print((char)payload[i]); | ||
} | ||
Serial.println(); | ||
} | ||
|
||
/* If we lose the connection, get it back and re-sub */ | ||
static void reconnect() { | ||
// attempt one reconnection | ||
Serial.print("Attempting MQTT connection... "); | ||
// Create a random client ID | ||
String clientId = "LilyGoClient-"; | ||
clientId += String(random(0xffff), HEX); | ||
// Attempt to connect | ||
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) { | ||
Serial.println("connected"); | ||
|
||
for (int i = 0; i < mqtt_nof_subscriptions; i++) { | ||
client.subscribe(mqtt_subscriptions[i]); | ||
Serial.print("Subscribed to: "); | ||
Serial.println(mqtt_subscriptions[i]); | ||
} | ||
} else { | ||
Serial.print("failed, rc="); | ||
Serial.print(client.state()); | ||
Serial.println(" try again in 5 seconds"); | ||
// Wait 5 seconds before retrying | ||
} | ||
} | ||
#endif // | ||
|
||
void init_mqtt(void) { | ||
client.setServer(MQTT_SERVER, MQTT_PORT); | ||
client.setCallback(callback); | ||
|
||
previousMillisUpdateVal = millis(); | ||
reconnect(); | ||
Serial.println("MQTT initialized"); | ||
} | ||
|
||
void mqtt_loop(void) { | ||
if (client.connected()) { | ||
client.loop(); | ||
if (millis() - previousMillisUpdateVal >= 5000) // Every 5s | ||
{ | ||
previousMillisUpdateVal = millis(); | ||
publish_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus. | ||
} | ||
} else { | ||
if (millis() - previousMillisUpdateVal >= 5000) // Every 5s | ||
{ | ||
previousMillisUpdateVal = millis(); | ||
reconnect(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* MQTT add-on for the battery emulator | ||
* | ||
* Usage: | ||
* | ||
* Subscription - Add topics to MQTT_SUBSCRIPTIONS in USER_SETTINGS.h and handle the messages in mqtt.cpp:callback() | ||
* | ||
* Publishing - See example in mqtt.cpp:publish_values() for constructing the payload | ||
* | ||
* Home assistant - See below for an example, and the official documentation is quite good (https://www.home-assistant.io/integrations/sensor.mqtt/) | ||
* in configuration.yaml: | ||
* mqtt: !include mqtt.yaml | ||
* | ||
* in mqtt.yaml: | ||
* sensor: | ||
* - name: "Cell max" | ||
* state_topic: "battery/info" | ||
* unit_of_measurement: "mV" | ||
* value_template: "{{ value_json.cell_max_voltage | int }}" | ||
* - name: "Cell min" | ||
* state_topic: "battery/info" | ||
* unit_of_measurement: "mV" | ||
* value_template: "{{ value_json.cell_min_voltage | int }}" | ||
* - name: "Temperature max" | ||
* state_topic: "battery/info" | ||
* unit_of_measurement: "C" | ||
* value_template: "{{ value_json.temperature_max | float }}" | ||
* - name: "Temperature min" | ||
* state_topic: "battery/info" | ||
* unit_of_measurement: "C" | ||
* value_template: "{{ value_json.temperature_min | float }}" | ||
*/ | ||
|
||
#ifndef __MQTT_H__ | ||
#define __MQTT_H__ | ||
|
||
#include <Arduino.h> | ||
#include "../../../USER_SETTINGS.h" | ||
|
||
#define MSG_BUFFER_SIZE (256) | ||
|
||
extern uint16_t SOC; | ||
extern uint16_t StateOfHealth; | ||
extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) | ||
extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) | ||
extern uint16_t cell_max_voltage; //mV, 0-4350 | ||
extern uint16_t cell_min_voltage; //mV, 0-4350 | ||
|
||
extern const char* mqtt_user; | ||
extern const char* mqtt_password; | ||
|
||
void init_mqtt(void); | ||
void mqtt_loop(void); | ||
|
||
#endif |
Oops, something went wrong.