Skip to content

Commit 7eb0e94

Browse files
committed
Add Sensorbox-2 WiFi menu option. Only visible when Sensorbox 2 with suitable firmware is connected over RS485.
1 parent 78771fa commit 7eb0e94

File tree

6 files changed

+93
-5
lines changed

6 files changed

+93
-5
lines changed

SmartEVSE-3/include/evse.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ extern RemoteDebug Debug;
255255
#define SOLARSTARTTIME 40 // Seconds to keep chargecurrent at 6A
256256
#define OCPP_MODE 0
257257
#define AUTOUPDATE 0 // default for Automatic Firmware Update: 0 = disabled, 1 = enabled
258+
#define SB2_WIFI_MODE 0
258259

259260
// Mode settings
260261
#define MODE_NORMAL 0
@@ -385,7 +386,7 @@ extern RemoteDebug Debug;
385386
#define MENU_MODE 12 // 0x0200: EVSE mode
386387
#define MENU_CIRCUIT 13 // 0x0201: EVSE Circuit max Current
387388
#define MENU_GRID 14 // 0x0202: Grid type to which the Sensorbox is connected
388-
#define MENU_UNUSED 15 // 0x0203: Unused
389+
#define MENU_SB2_WIFI 15 // 0x0203: WiFi mode of the Sensorbox 2
389390
#define MENU_MAINS 16 // 0x0204: Max Mains Current
390391
#define MENU_START 17 // 0x0205: Surplus energy start Current
391392
#define MENU_STOP 18 // 0x0206: Stop solar charging at 6A after this time
@@ -465,6 +466,7 @@ extern struct tm timeinfo;
465466
extern uint8_t Mode; // EVSE mode
466467
extern uint8_t LoadBl; // Load Balance Setting (Disable, Master or Node)
467468
extern uint8_t Grid;
469+
extern uint8_t SB2_WIFImode;
468470
#if FAKE_RFID
469471
extern uint8_t Show_RFID;
470472
#endif
@@ -531,7 +533,7 @@ const struct {
531533
{"MODE", "Normal, Smart or Solar EVSE mode", 0, 2, MODE},
532534
{"CIRCUIT", "EVSE Circuit max Current", 10, 160, MAX_CIRCUIT},
533535
{"GRID", "Grid type to which the Sensorbox is connected", 0, 1, GRID},
534-
{"Unused", "Unused", 0, 1, 0},
536+
{"SB2 WIFI","Connect Sensorbox-2 to WiFi", 0, 2, SB2_WIFI_MODE},
535537
{"MAINS", "Max MAINS Current (per phase)", 10, 200, MAX_MAINS},
536538
{"START", "Surplus energy start Current (sum of phases)", 0, 48, START_CURRENT},
537539
{"STOP", "Stop solar charging at 6A after this time", 0, 60, STOP_TIME},
@@ -550,7 +552,7 @@ const struct {
550552
{"ENE REGI","Register for Energy (kWh) of custom electric meter", 0, 65534, EMCUSTOM_EREGISTER},
551553
{"ENE DIVI","Divisor for Energy (kWh) of custom electric meter", 0, 7, EMCUSTOM_EDIVISOR},
552554
{"READ MAX","Max register read at once of custom electric meter", 3, 255, 3},
553-
{"WIFI", "Use ESPTouch APP on your phone", 0, 2, WIFI_MODE},
555+
{"WIFI", "Connect SmartEVSE to WiFi", 0, 2, WIFI_MODE},
554556
{"AUTOUPDAT","Automatic Firmware Update", 0, 1, AUTOUPDATE},
555557
{"CONTACT 2","Contactor2 (C2) behaviour", 0, sizeof(StrEnableC2) / sizeof(StrEnableC2[0])-1, ENABLE_C2},
556558
{"MAX TEMP","Maximum temperature for the EVSE module", 40, 75, MAX_TEMPERATURE},
@@ -588,6 +590,15 @@ struct DelayedTimeStruct {
588590
int32_t diff; // StartTime minus current time in seconds
589591
};
590592

593+
struct Sensorbox {
594+
uint8_t SoftwareVer; // Sensorbox 2 software version
595+
uint8_t WiFiConnected; // 0:not connected / 1:connected to WiFi
596+
uint8_t WiFiAPSTA; // 0:no portal / 1: portal active
597+
uint8_t WIFImode; // 0:Wifi Off / 1:WiFi On / 2: Portal Start
598+
uint8_t IP[4];
599+
uint8_t APpassword[8];
600+
};
601+
591602
#define EPOCH2_OFFSET 1672531200
592603

593604
extern struct DelayedTimeStruct DelayedStartTime;

SmartEVSE-3/include/meter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
extern struct EMstruct EMConfig[EM_CUSTOM + 1];
3333
extern struct ModBus MB;
34+
extern struct Sensorbox SB2;
3435

3536
class Meter {
3637
public:

SmartEVSE-3/src/evse.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ struct DelayedTimeStruct DelayedStartTime;
146146
struct DelayedTimeStruct DelayedStopTime;
147147
uint8_t DelayedRepeat; // 0 = no repeat, 1 = daily repeat
148148
uint8_t Grid = GRID; // Type of Grid connected to Sensorbox (0:4Wire / 1:3Wire )
149+
uint8_t SB2_WIFImode = SB2_WIFI_MODE; // Sensorbox-2 WiFi Mode (0:Disabled / 1:Enabled / 2:Start Portal)
149150
uint8_t RFIDReader = RFID_READER; // RFID Reader (0:Disabled / 1:Enabled / 2:Enable One / 3:Learn / 4:Delete / 5:Delete All / 6: Remote via OCPP)
150151
#if FAKE_RFID
151152
uint8_t Show_RFID = 0;
@@ -1387,7 +1388,7 @@ Regis Access Description Unit Values
13871388
0x0200 R/W EVSE mode 0:Normal / 1:Smart / 2:Solar
13881389
0x0201 R/W EVSE Circuit max Current A 10 - 160
13891390
0x0202 R/W Grid type to which the Sensorbox is connected 0:4Wire / 1:3Wire
1390-
0x0203 R/W CT calibration value 0.01 Multiplier
1391+
0x0203 R/W Sensorbox 2 WiFi Mode 0:Disabled / 1:Enabled / 2:Portal
13911392
0x0204 R/W Max Mains Current A 10 - 200
13921393
0x0205 R/W Surplus energy start Current A 1 - 16
13931394
0x0206 R/W Stop solar charging at 6A after this time min 0:Disable / 1 - 60
@@ -1728,6 +1729,9 @@ uint8_t setItemValue(uint8_t nav, uint16_t val) {
17281729
case MENU_GRID:
17291730
Grid = val;
17301731
break;
1732+
case MENU_SB2_WIFI:
1733+
SB2_WIFImode = val;
1734+
break;
17311735
case MENU_MAINSMETER:
17321736
MainsMeter.Type = val;
17331737
break;
@@ -1866,6 +1870,8 @@ uint16_t getItemValue(uint8_t nav) {
18661870
return RCmon;
18671871
case MENU_GRID:
18681872
return Grid;
1873+
case MENU_SB2_WIFI:
1874+
return SB2_WIFImode;
18691875
case MENU_MAINSMETER:
18701876
return MainsMeter.Type;
18711877
case MENU_MAINSMETERADDRESS:
@@ -3328,6 +3334,7 @@ void Timer1S(void * parameter) {
33283334
// but in Normal mode we just want to charge ChargeCurrent, irrespective of communication problems.
33293335
ErrorFlags |= CT_NOCOMM;
33303336
setStatePowerUnavailable();
3337+
SB2.SoftwareVer = 0;
33313338
_LOG_W("Error, MainsMeter communication error!\n");
33323339
} else {
33333340
if (MainsMeter.Timeout) MainsMeter.Timeout--;
@@ -3774,6 +3781,7 @@ void read_settings() {
37743781
StopTime = preferences.getUShort("StopTime", STOP_TIME);
37753782
ImportCurrent = preferences.getUShort("ImportCurrent",IMPORT_CURRENT);
37763783
Grid = preferences.getUChar("Grid",GRID);
3784+
SB2_WIFImode = preferences.getUChar("SB2WIFImode",SB2_WIFI_MODE);
37773785
RFIDReader = preferences.getUChar("RFIDReader",RFID_READER);
37783786

37793787
MainsMeter.Type = preferences.getUChar("MainsMeter", MAINS_METER);
@@ -3852,6 +3860,7 @@ void write_settings(void) {
38523860
preferences.putUShort("StopTime", StopTime);
38533861
preferences.putUShort("ImportCurrent", ImportCurrent);
38543862
preferences.putUChar("Grid", Grid);
3863+
preferences.putUChar("SB2WIFImode", SB2_WIFImode);
38553864
preferences.putUChar("RFIDReader", RFIDReader);
38563865

38573866
preferences.putUChar("MainsMeter", MainsMeter.Type);

SmartEVSE-3/src/glcd.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,34 @@ void GLCD(void) {
428428
else if (RFIDstatus == 6) GLCD_print_buf(0, (const char*) "Card storage full!");
429429
else glcd_clrln(0, 0x00); // Clear line
430430
LCDTimer = 0; // reset timer, so it will not exit the menu when learning/deleting cards
431+
// Sensorbox 2 WiFi settings
432+
} else if (LCDNav == MENU_SB2_WIFI) {
433+
// wait for sensorbox to switch to selected mode
434+
if ( (SB2.WIFImode != SB2_WIFImode) && SubMenu && SB2_WIFImode !=2) {
435+
GLCD_write_buf_str(0,0, "one moment please...", GLCD_ALIGN_LEFT);
436+
437+
} else if (SB2_WIFImode == 1) { // Enable WiFi on Sensorbox 2
438+
439+
if (SB2.WiFiConnected) {
440+
sprintf(Str, "SB2: %u.%u.%u.%u",SB2.IP[0],SB2.IP[1],SB2.IP[2],SB2.IP[3]);
441+
GLCD_write_buf_str(0,0, Str, GLCD_ALIGN_LEFT);
442+
} else GLCD_write_buf_str(0,0, "Not connected to WiFi", GLCD_ALIGN_LEFT);
443+
444+
} else if (SB2_WIFImode == 2) { // Enable Portal on Sensorbox 2
445+
if (SubMenu) {
446+
sprintf(Str, "O button starts config");
447+
GLCD_write_buf_str(0,0, Str, GLCD_ALIGN_LEFT);
448+
} else {
449+
// Show Portal Password
450+
sprintf(Str, "Password: %s", SB2.APpassword);
451+
GLCD_write_buf_str(0, 0, Str, GLCD_ALIGN_LEFT);
452+
GLCD_sendbuf(7, 1);
453+
GLCD_buffer_clr();
454+
sprintf(Str, "Now connect to portal");
455+
GLCD_write_buf_str(0, 0, Str, GLCD_ALIGN_LEFT);
456+
}
457+
}
458+
431459
} else {
432460
// When connected to Wifi, display IP and time in top row
433461
uint8_t WIFImode = getItemValue(MENU_WIFI);
@@ -990,6 +1018,7 @@ const char * getMenuItemOption(uint8_t nav) {
9901018
case MENU_RFIDREADER:
9911019
return StrRFIDReader[value];
9921020
case MENU_WIFI:
1021+
case MENU_SB2_WIFI:
9931022
return StrWiFi[value];
9941023
case MENU_EXIT:
9951024
return StrExitMenu;
@@ -1019,6 +1048,9 @@ uint8_t getMenuItems (void) {
10191048
MenuItems[m++] = MENU_MAINSMETER; // - - Type of Mains electric meter (0: Disabled / Constants EM_*)
10201049
if (MainsMeter.Type == EM_SENSORBOX) { // - - ? Sensorbox?
10211050
if (GridActive == 1) MenuItems[m++] = MENU_GRID;
1051+
if (SB2.SoftwareVer >= 0x01) {
1052+
MenuItems[m++] = MENU_SB2_WIFI; // Sensorbox-2 Wifi 0:Disabled / 1:Enabled / 2:Portal
1053+
}
10221054
} else if (MainsMeter.Type && MainsMeter.Type != EM_API) { // - - ? Other?
10231055
MenuItems[m++] = MENU_MAINSMETERADDRESS; // - - - Address of Mains electric meter (9 - 247)
10241056
}

SmartEVSE-3/src/meter.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct EMstruct EMConfig[EM_CUSTOM + 1] = {
3333
{"Custom", ENDIANESS_LBF_LWF, 4, MB_DATATYPE_INT32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // Last entry!
3434
};
3535

36+
struct Sensorbox SB2;
3637

3738
Meter::Meter(uint8_t type, uint8_t address, uint8_t timeout) {
3839
for (int x = 1; x < 3; x++) {
@@ -180,6 +181,38 @@ uint8_t Meter::receiveCurrentMeasurement(uint8_t *buf) {
180181
if ((var[x] > -1) && (var[x] < 1)) var[x] = 0;
181182
}
182183
}
184+
// Store Sensorbox software version
185+
SB2.SoftwareVer = buf[0];
186+
// Make sure the version and datalength are correct before processing the data.
187+
// the version alone does not indicate that we have read the extended registers.
188+
if (SB2.SoftwareVer >= 1 && MB.DataLength == 64) {
189+
// Read Status, IP, AP Password from Sensorbox
190+
SB2.WiFiConnected = buf[40]>>1 & 1;
191+
SB2.WiFiAPSTA = buf[40]>>2 & 1;
192+
SB2.WIFImode = buf[41];
193+
_LOG_I("SB2 WiFiMode:%u\n",SB2.WIFImode);
194+
SB2.IP[0] = buf[48];
195+
SB2.IP[1] = buf[49];
196+
SB2.IP[2] = buf[50];
197+
SB2.IP[3] = buf[51];
198+
for (x = 0; x < 8; x++) {
199+
SB2.APpassword[7-x] = buf[56 + x];
200+
}
201+
202+
if (SB2_WIFImode == 2 && SB2.WiFiConnected && !SubMenu) {
203+
SB2_WIFImode = 1; // Portal active and connected? Switch back to Enabled.
204+
write_settings();
205+
LCDNav = 0;
206+
}
207+
208+
// Send new mode to Sensorbox 2 when mode differs from local SB2_WiFimode
209+
// for mode "SetupWifi" only send mode when -not- in Submenu.
210+
if ( (SB2.WIFImode != SB2_WIFImode) && (SB2_WIFImode != 2 || !SubMenu) ) {
211+
_LOG_I("New SB2 mode:%u\n", SB2_WIFImode);
212+
ModbusWriteSingleRequest(0x0A, 0x801, SB2_WIFImode); // Send new WiFi mode to Sensorbox
213+
}
214+
}
215+
183216
// Set Sensorbox 2 to 3/4 Wire configuration (and phase Rotation) (v2.16)
184217
if (buf[1] >= 0x10 && offset == 7) {
185218
GridActive = 1; // Enable the GRID menu option

SmartEVSE-3/src/modbus.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ void requestCurrentMeasurement(uint8_t Meter, uint8_t Address) {
335335
case EM_API:
336336
break;
337337
case EM_SENSORBOX:
338-
ModbusReadInputRequest(Address, 4, 0, 20);
338+
if (SB2.SoftwareVer >= 1) {
339+
ModbusReadInputRequest(Address, 4, 0, 32);
340+
} else ModbusReadInputRequest(Address, 4, 0, 20);
339341
break;
340342
case EM_EASTRON1P:
341343
case EM_EASTRON3P:

0 commit comments

Comments
 (0)