Skip to content

Commit 330c58b

Browse files
Merge pull request #50 from OpenEVSE/rapi_5.0.0_support
RAPI 5.0.0 support
2 parents 4162164 + 68dadaa commit 330c58b

File tree

9 files changed

+336
-112
lines changed

9 files changed

+336
-112
lines changed

src/RapiSender.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ RapiSender::_waitForResult(unsigned long timeout) {
299299
} else if (!strcmp(_tokens[0], "$NK")) {
300300
return RAPI_RESPONSE_NK;
301301
} else if (!strcmp(_tokens[0],"$WF") ||
302-
!strcmp(_tokens[0],"$ST"))
302+
!strcmp(_tokens[0],"$ST") ||
303+
!strncmp(_tokens[0],"$A",2))
303304
{
304305
return RAPI_RESPONSE_ASYNC_EVENT;
305306
} else { // not OK or NK

src/input.cpp

Lines changed: 56 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined(ENABLE_DEBUG) && !defined(ENABLE_DEBUG_RAPI)
1+
#if defined(ENABLE_DEBUG) && !defined(ENABLE_DEBUG_INPUT)
22
#undef ENABLE_DEBUG
33
#endif
44

@@ -17,10 +17,6 @@
1717

1818
#include "RapiSender.h"
1919

20-
#define OPENEVSE_WIFI_MODE_AP 0
21-
#define OPENEVSE_WIFI_MODE_CLIENT 1
22-
#define OPENEVSE_WIFI_MODE_AP_DEFAULT 2
23-
2420
int espflash = 0;
2521
int espfree = 0;
2622

@@ -131,65 +127,56 @@ update_rapi_values() {
131127
});
132128
break;
133129
case 2:
134-
rapiSender.sendCmd("$GS", [](int ret)
130+
OpenEVSE.getStatus([](int ret, uint8_t evse_state, uint32_t session_time, uint8_t pilot_state, uint32_t vflags)
135131
{
136132
if(RAPI_RESPONSE_OK == ret)
137133
{
138-
if(rapiSender.getTokenCnt() >= 3)
139-
{
140-
const char *val = rapiSender.getToken(1);
141-
DBUGVAR(val);
142-
state = strtol(val, NULL, 10);
143-
DBUGVAR(state);
144-
val = rapiSender.getToken(2);
145-
DBUGVAR(val);
146-
elapsed = strtol(val, NULL, 10);
147-
DBUGVAR(elapsed);
148-
149-
#ifdef ENABLE_LEGACY_API
150-
switch (state) {
151-
case 1:
152-
estate = "Not Connected";
153-
break;
154-
case 2:
155-
estate = "EV Connected";
156-
break;
157-
case 3:
158-
estate = "Charging";
159-
break;
160-
case 4:
161-
estate = "Vent Required";
162-
break;
163-
case 5:
164-
estate = "Diode Check Failed";
165-
break;
166-
case 6:
167-
estate = "GFCI Fault";
168-
break;
169-
case 7:
170-
estate = "No Earth Ground";
171-
break;
172-
case 8:
173-
estate = "Stuck Relay";
174-
break;
175-
case 9:
176-
estate = "GFCI Self Test Failed";
177-
break;
178-
case 10:
179-
estate = "Over Temperature";
180-
break;
181-
case 254:
182-
estate = "Sleeping";
183-
break;
184-
case 255:
185-
estate = "Disabled";
186-
break;
187-
default:
188-
estate = "Invalid";
189-
break;
190-
}
191-
#endif
134+
state = evse_state;
135+
elapsed = session_time;
136+
137+
#ifdef ENABLE_LEGACY_API
138+
switch (state) {
139+
case 1:
140+
estate = "Not Connected";
141+
break;
142+
case 2:
143+
estate = "EV Connected";
144+
break;
145+
case 3:
146+
estate = "Charging";
147+
break;
148+
case 4:
149+
estate = "Vent Required";
150+
break;
151+
case 5:
152+
estate = "Diode Check Failed";
153+
break;
154+
case 6:
155+
estate = "GFCI Fault";
156+
break;
157+
case 7:
158+
estate = "No Earth Ground";
159+
break;
160+
case 8:
161+
estate = "Stuck Relay";
162+
break;
163+
case 9:
164+
estate = "GFCI Self Test Failed";
165+
break;
166+
case 10:
167+
estate = "Over Temperature";
168+
break;
169+
case 254:
170+
estate = "Sleeping";
171+
break;
172+
case 255:
173+
estate = "Disabled";
174+
break;
175+
default:
176+
estate = "Invalid";
177+
break;
192178
}
179+
#endif
193180
}
194181
});
195182
break;
@@ -380,16 +367,13 @@ handleRapiRead()
380367
Profile_End(handleRapiRead, 10);
381368
}
382369

383-
void on_rapi_event()
370+
void input_setup()
384371
{
385-
if(!strcmp(rapiSender.getToken(0), "$ST"))
372+
OpenEVSE.onState([](uint8_t evse_state, uint8_t pilot_state, uint32_t current_capacity, uint32_t vflags)
386373
{
387-
const char *val = rapiSender.getToken(1);
388-
DBUGVAR(val);
389-
390-
// Update our local state
391-
state = strtol(val, NULL, 16);
392-
DBUGVAR(state);
374+
// Update our global state
375+
DBUGVAR(evse_state);
376+
state = evse_state;
393377

394378
// Send to all clients
395379
String event = F("{\"state\":");
@@ -402,13 +386,10 @@ void on_rapi_event()
402386
event += String(state);
403387
mqtt_publish(event);
404388
}
405-
}
406-
else if(!strcmp(rapiSender.getToken(0), "$WF"))
407-
{
408-
const char *val = rapiSender.getToken(1);
409-
DBUGVAR(val);
389+
});
410390

411-
long wifiMode = strtol(val, NULL, 10);
391+
OpenEVSE.onWiFi([](uint8_t wifiMode)
392+
{
412393
DBUGVAR(wifiMode);
413394
switch(wifiMode)
414395
{
@@ -420,5 +401,5 @@ void on_rapi_event()
420401
net_wifi_turn_off_ap();
421402
break;
422403
}
423-
}
404+
});
424405
}

src/input.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extern String ohm_hour;
6767
extern void handleRapiRead();
6868
extern void update_rapi_values();
6969
extern void create_rapi_json(String &data);
70-
extern void on_rapi_event();
7170

71+
extern void input_setup();
7272

7373
#endif // _EMONESP_INPUT_H

src/main.cpp

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@ void setup()
9090
DBUGF("After ota_setup: %d", HAL.getFreeHeap());
9191
#endif
9292

93+
input_setup();
94+
9395
lcd_display(F("OpenEVSE WiFI"), 0, 0, 0, LCD_CLEAR_LINE);
9496
lcd_display(currentfirmware, 0, 1, 5 * 1000, LCD_CLEAR_LINE);
9597

96-
rapiSender.setOnEvent(on_rapi_event);
97-
rapiSender.enableSequenceId(0);
98-
9998
start_mem = last_mem = HAL.getFreeHeap();
10099
} // end setup
101100

@@ -119,30 +118,32 @@ loop() {
119118
rapiSender.loop();
120119
divert_current_loop();
121120

122-
if(OPENEVSE_STATE_STARTING != state &&
123-
OPENEVSE_STATE_INVALID != state)
121+
if(OpenEVSE.isConnected())
124122
{
125-
126-
// Read initial state from OpenEVSE
127-
if (rapi_read == 0)
123+
if(OPENEVSE_STATE_STARTING != state &&
124+
OPENEVSE_STATE_INVALID != state)
128125
{
129-
DBUGLN("first read RAPI values");
130-
handleRapiRead(); //Read all RAPI values
131-
rapi_read=1;
132-
}
126+
// Read initial state from OpenEVSE
127+
if (rapi_read == 0)
128+
{
129+
DBUGLN("first read RAPI values");
130+
handleRapiRead(); //Read all RAPI values
131+
rapi_read=1;
132+
}
133133

134-
// -------------------------------------------------------------------
135-
// Do these things once every 2s
136-
// -------------------------------------------------------------------
137-
if ((millis() - Timer3) >= 2000) {
138-
uint32_t current = HAL.getFreeHeap();
139-
int32_t diff = (int32_t)(last_mem - current);
140-
if(diff != 0) {
141-
DEBUG.printf("Free memory %u - diff %d %d\n", current, diff, start_mem - current);
142-
last_mem = current;
134+
// -------------------------------------------------------------------
135+
// Do these things once every 2s
136+
// -------------------------------------------------------------------
137+
if ((millis() - Timer3) >= 2000) {
138+
uint32_t current = HAL.getFreeHeap();
139+
int32_t diff = (int32_t)(last_mem - current);
140+
if(diff != 0) {
141+
DEBUG.printf("Free memory %u - diff %d %d\n", current, diff, start_mem - current);
142+
last_mem = current;
143+
}
144+
update_rapi_values();
145+
Timer3 = millis();
143146
}
144-
update_rapi_values();
145-
Timer3 = millis();
146147
}
147148
}
148149
else
@@ -151,16 +152,13 @@ loop() {
151152
if ((millis() - Timer3) >= 1000)
152153
{
153154
// Check state the OpenEVSE is in.
154-
rapiSender.sendCmd("$GS", [](int ret) {
155-
if (RAPI_RESPONSE_OK == ret)
155+
OpenEVSE.begin(rapiSender, [](bool connected)
156+
{
157+
if(connected)
156158
{
157-
if(rapiSender.getTokenCnt() >= 3)
158-
{
159-
const char *val = rapiSender.getToken(1);
160-
DBUGVAR(val);
161-
state = strtol(val, NULL, 10);
162-
DBUGVAR(state);
163-
}
159+
OpenEVSE.getStatus([](int ret, uint8_t evse_state, uint32_t session_time, uint8_t pilot_state, uint32_t vflags) {
160+
state = evse_state;
161+
});
164162
} else {
165163
DBUGLN("OpenEVSE not responding or not connected");
166164
}

0 commit comments

Comments
 (0)