Skip to content

Commit

Permalink
synchronize network.cpp/network.h with SmartEVSEv3
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo35 committed Oct 20, 2024
1 parent cc18e8c commit fe3695e
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 394 deletions.
110 changes: 110 additions & 0 deletions Sensorbox2_ESP/include/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
; Project: Smart EVSE v3
;
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/

#ifndef __EVSE_DEBUG

#define __EVSE_DEBUG

#ifndef VERSION
//please note that this version will only be displayed with the correct time/date if the program is recompiled
//so the webserver will show correct version if evse.cpp is recompiled
//the lcd display will show correct version if glcd.cpp is recompiled
#define VERSION (__TIME__ " @" __DATE__)
#endif

#if DBG == 0
//used to steer RemoteDebug
#define DEBUG_DISABLED 1
#define _LOG_W( ... ) //dummy
#define _LOG_I( ... ) //dummy
#define _LOG_D( ... ) //dummy
#define _LOG_V( ... ) //dummy
#define _LOG_A( ... ) //dummy
#define _LOG_W_NO_FUNC( ... ) //dummy
#define _LOG_I_NO_FUNC( ... ) //dummy
#define _LOG_D_NO_FUNC( ... ) //dummy
#define _LOG_V_NO_FUNC( ... ) //dummy
#define _LOG_A_NO_FUNC( ... ) //dummy
#endif

#if DBG == 1
#define _LOG_A(fmt, ...) if (Debug.isActive(Debug.ANY)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Always = Errors!!!
#define _LOG_P(fmt, ...) if (Debug.isActive(Debug.PROFILER)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__)
#define _LOG_V(fmt, ...) if (Debug.isActive(Debug.VERBOSE)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Verbose
#define _LOG_D(fmt, ...) if (Debug.isActive(Debug.DEBUG)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Debug
#define _LOG_I(fmt, ...) if (Debug.isActive(Debug.INFO)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Info
#define _LOG_W(fmt, ...) if (Debug.isActive(Debug.WARNING)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Warning
#define _LOG_E(fmt, ...) if (Debug.isActive(Debug.ERROR)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Error not used!
#define _LOG_A_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.ANY)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_P_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.PROFILER)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_V_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.VERBOSE)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_D_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.DEBUG)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_I_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.INFO)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_W_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.WARNING)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_E_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.ERROR)) Debug.printf(fmt, ##__VA_ARGS__)
#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
extern RemoteDebug Debug;
#endif

#define EVSE_LOG_FORMAT(letter, format) "[%6u][" #letter "][%s:%u] %s(): " format , (uint32_t) (esp_timer_get_time() / 1000ULL), pathToFileName(__FILE__), __LINE__, __FUNCTION__

#if DBG == 2
#define DEBUG_DISABLED 1
#if LOG_LEVEL >= 1 // Errors
#define _LOG_A(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(E, fmt), ##__VA_ARGS__)
#define _LOG_A_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_A( ... )
#define _LOG_A_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 2 // Warnings
#define _LOG_W(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(W, fmt), ##__VA_ARGS__)
#define _LOG_W_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_W( ... )
#define _LOG_W_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 3 // Info
#define _LOG_I(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(I, fmt), ##__VA_ARGS__)
#define _LOG_I_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_I( ... )
#define _LOG_I_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 4 // Debug
#define _LOG_D(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(D, fmt), ##__VA_ARGS__)
#define _LOG_D_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_D( ... )
#define _LOG_D_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 5 // Verbose
#define _LOG_V(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(V, fmt), ##__VA_ARGS__)
#define _LOG_V_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_V( ... )
#define _LOG_V_NO_FUNC( ... )
#endif
#endif // if DBG == 2

#endif
4 changes: 2 additions & 2 deletions Sensorbox2_ESP/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#define WIFI_MODE 0 // WiFi disabled
#define AP_PASSWORD "00000000" // Will be overwritten by random 8 char password.

#include "debug.h"

// Toggle pins of the RS485 transceiver.
void ToggleRS485(bool level);

#define FREE(x) free(x); x = NULL;

116 changes: 18 additions & 98 deletions Sensorbox2_ESP/include/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
#include "mongoose.h"
#include <ArduinoJson.h>

#ifndef VERSION
//please note that this version will only be displayed with the correct time/date if the program is recompiled
//so the webserver will show correct version if evse.cpp is recompiled
//the lcd display will show correct version if glcd.cpp is recompiled
#define VERSION (__TIME__ " @" __DATE__)
#endif

#ifndef MQTT
#define MQTT 1 // Uncomment or set to 0 to disable MQTT support in code
#endif
Expand All @@ -52,7 +45,7 @@
#include "mqtt_client.h"
#endif

extern bool shouldReboot;
#define FREE(x) free(x); x = NULL;

#if MQTT
// MQTT connection info
Expand Down Expand Up @@ -90,6 +83,11 @@ class MQTTclient_t {
#endif
};

extern MQTTclient_t MQTTclient;
extern void SetupMQTTClient();
extern void mqtt_receive_callback(const String topic, const String payload);
#endif //MQTT

// wrapper so hasParam and getParam still work
class webServerRequest {
private:
Expand All @@ -104,95 +102,19 @@ class webServerRequest {
const String& value(); // Return the string value
};

extern MQTTclient_t MQTTclient;

#endif

#ifndef ENABLE_OCPP
#define ENABLE_OCPP 0
#endif

#if ENABLE_OCPP
#include <MicroOcpp/Model/ConnectorBase/Notification.h>
#endif

extern bool shouldReboot;
extern void network_loop(void);
extern String APhostname;
extern webServerRequest* request;
extern struct mg_mgr mgr;
extern String TZinfo;
extern uint8_t WIFImode;
extern char *downloadUrl;
extern uint32_t serialnr;
extern void RunFirmwareUpdate(void);
extern void WiFiSetup(void);
extern void handleWIFImode(HardwareSerial *s = &Serial);

#if DBG == 0
//used to steer RemoteDebug
#define DEBUG_DISABLED 1
#define _LOG_W( ... ) //dummy
#define _LOG_I( ... ) //dummy
#define _LOG_D( ... ) //dummy
#define _LOG_V( ... ) //dummy
#define _LOG_A( ... ) //dummy
#define _LOG_W_NO_FUNC( ... ) //dummy
#define _LOG_I_NO_FUNC( ... ) //dummy
#define _LOG_D_NO_FUNC( ... ) //dummy
#define _LOG_V_NO_FUNC( ... ) //dummy
#define _LOG_A_NO_FUNC( ... ) //dummy
#endif

#if DBG == 1
#define _LOG_A(fmt, ...) if (Debug.isActive(Debug.ANY)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Always = Errors!!!
#define _LOG_P(fmt, ...) if (Debug.isActive(Debug.PROFILER)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__)
#define _LOG_V(fmt, ...) if (Debug.isActive(Debug.VERBOSE)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Verbose
#define _LOG_D(fmt, ...) if (Debug.isActive(Debug.DEBUG)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Debug
#define _LOG_I(fmt, ...) if (Debug.isActive(Debug.INFO)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Info
#define _LOG_W(fmt, ...) if (Debug.isActive(Debug.WARNING)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Warning
#define _LOG_E(fmt, ...) if (Debug.isActive(Debug.ERROR)) Debug.printf("(%s)(C%d) " fmt, __func__, xPortGetCoreID(), ##__VA_ARGS__) //Error not used!
#define _LOG_A_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.ANY)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_P_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.PROFILER)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_V_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.VERBOSE)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_D_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.DEBUG)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_I_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.INFO)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_W_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.WARNING)) Debug.printf(fmt, ##__VA_ARGS__)
#define _LOG_E_NO_FUNC(fmt, ...) if (Debug.isActive(Debug.ERROR)) Debug.printf(fmt, ##__VA_ARGS__)
#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
extern RemoteDebug Debug;
#endif

#define EVSE_LOG_FORMAT(letter, format) "[%6u][" #letter "][%s:%u] %s(): " format , (uint32_t) (esp_timer_get_time() / 1000ULL), pathToFileName(__FILE__), __LINE__, __FUNCTION__

#if DBG == 2
#define DEBUG_DISABLED 1
#if LOG_LEVEL >= 1 // Errors
#define _LOG_A(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(E, fmt), ##__VA_ARGS__)
#define _LOG_A_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_A( ... )
#define _LOG_A_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 2 // Warnings
#define _LOG_W(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(W, fmt), ##__VA_ARGS__)
#define _LOG_W_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_W( ... )
#define _LOG_W_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 3 // Info
#define _LOG_I(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(I, fmt), ##__VA_ARGS__)
#define _LOG_I_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_I( ... )
#define _LOG_I_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 4 // Debug
#define _LOG_D(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(D, fmt), ##__VA_ARGS__)
#define _LOG_D_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_D( ... )
#define _LOG_D_NO_FUNC( ... )
#endif
#if LOG_LEVEL >= 5 // Verbose
#define _LOG_V(fmt, ... ) Serial.printf(EVSE_LOG_FORMAT(V, fmt), ##__VA_ARGS__)
#define _LOG_V_NO_FUNC( ... ) Serial.printf ( __VA_ARGS__ )
#else
#define _LOG_V( ... )
#define _LOG_V_NO_FUNC( ... )
#endif
#endif // if DBG == 2
extern void handleWIFImode(void *s = &Serial);
extern bool getLatestVersion(String owner_repo, String asset_name, char *version);

#define FW_DOWNLOAD_PATH "http://smartevse-3.s3.eu-west-2.amazonaws.com"

Expand All @@ -201,6 +123,4 @@ extern RemoteDebug Debug;
#define OWNER_COMM "dingo35"
#define REPO_COMM "SmartEVSE-3.5"

#define WIFI_MODE 0

#endif
File renamed without changes.
69 changes: 67 additions & 2 deletions Sensorbox2_ESP/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
#include "time.h"

#include "main.h"
#include "radix.h"
#include "utils.h"
#include "prg_pic.h"
#include <SPI.h>
#include <HTTPClient.h>
Expand Down Expand Up @@ -93,7 +93,6 @@ uint8_t P1data[2000];
uint16_t ModbusData[50]; // 50 registers

extern uint8_t WIFImode;
uint32_t serialnr;
String SmartEVSEHost = "";

unsigned long ModbusTimer=0;
Expand Down Expand Up @@ -896,6 +895,72 @@ void setup() {
}


#if MQTT
void mqtt_receive_callback(const String topic, const String payload) {
// Make sure MQTT updates directly to prevent debounces
lastMqttUpdate = 10;
}


void SetupMQTTClient() {
// Set up subscriptions
MQTTclient.subscribe(MQTTprefix + "/Set/#",1);
MQTTclient.publish(MQTTprefix+"/connected", "online", true, 0);

//publish MQTT discovery topics
//we need something to make all this JSON stuff readable, without doing all this assign and serialize stuff
#define jsn(x, y) String(R"(")") + x + R"(" : ")" + y + R"(")"
//jsn(device_class, current) expands to:
// R"("device_class" : "current")"

#define jsna(x, y) String(R"(, )") + jsn(x, y)
//json add expansion, same as above but now with a comma prepended

//first all device stuff:
const String device_payload = String(R"("device": {)") + jsn("model","SmartEVSE v3") + jsna("identifiers", MQTTprefix) + jsna("name", MQTTprefix) + jsna("manufacturer","Stegen") + jsna("configuration_url", "http://" + WiFi.localIP().toString().c_str()) + jsna("sw_version", String(VERSION)) + "}";
//a device SmartEVSE-1001 consists of multiple entities, and an entity can be in the domains sensor, number, select etc.
String entity_suffix, entity_name, optional_payload;

//some self-updating variables here:
#define entity_id String(MQTTprefix + "-" + entity_suffix)
#define entity_path String(MQTTprefix + "/" + entity_suffix)
#define entity_name(x) entity_name = x; entity_suffix = entity_name; entity_suffix.replace(" ", "");

//create template to announce an entity in it's own domain:
#define announce(x, entity_domain) entity_name(x); \
MQTTclient.publish("homeassistant/" + String(entity_domain) + "/" + entity_id + "/config", \
"{" \
+ jsn("name", entity_name) \
+ jsna("object_id", entity_id) \
+ jsna("unique_id", entity_id) \
+ jsna("state_topic", entity_path) \
+ jsna("availability_topic",String(MQTTprefix+"/connected")) \
+ ", " + device_payload + optional_payload \
+ "}", \
true, 0); // Retain + QoS 0

//set the parameters for and announce sensors with device class 'current':
optional_payload = jsna("device_class","current") + jsna("unit_of_measurement","A") + jsna("value_template", R"({{ value | int / 10 }})");
// if (MainsMeter.Type) {
announce("Mains Current L1", "sensor");
announce("Mains Current L2", "sensor");
announce("Mains Current L3", "sensor");
// }

//set the parameters for and announce diagnostic sensor entities:
optional_payload = jsna("entity_category","diagnostic");
announce("WiFi SSID", "sensor");
announce("WiFi BSSID", "sensor");
optional_payload = jsna("entity_category","diagnostic") + jsna("device_class","signal_strength") + jsna("unit_of_measurement","dBm");
announce("WiFi RSSI", "sensor");
optional_payload = jsna("entity_category","diagnostic") + jsna("device_class","duration") + jsna("unit_of_measurement","s") + jsna("entity_registry_enabled_default","False");
announce("ESP Uptime", "sensor");

MQTTclient.publish(MQTTprefix + "/WiFiSSID", String(WiFi.SSID()), true, 0);
MQTTclient.publish(MQTTprefix + "/WiFiBSSID", String(WiFi.BSSIDstr()), true, 0);
}
#endif //MQTT

//make mongoose 7.14 compatible with 7.13
#define mg_http_match_uri(X,Y) mg_match(X->uri, mg_str(Y), NULL)

Expand Down
Loading

0 comments on commit fe3695e

Please sign in to comment.