Skip to content

Commit 7db1947

Browse files
committed
Implemented start and stop command jsphuebner/libopeninv#25
1 parent 4fa06c0 commit 7db1947

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

esp32-web-interface.ino

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,29 @@ static void handleCommand() {
517517
server.send(200, "text/plain", "No reply to save command");
518518
}
519519
}
520+
else if (cmd == "stop") {
521+
if (OICan::StartStop(0)) {
522+
server.send(200, "text/plain", "Inverter halted");
523+
}
524+
else {
525+
server.send(200, "text/plain", "Stop command failed");
526+
}
527+
}
528+
else if (cmd.startsWith("start")) {
529+
String str(cmd);
530+
int modeStart = str.indexOf(' ');
531+
int opmode = str.substring(modeStart, str.indexOf('\r')).toInt();
532+
533+
if (OICan::StartStop(opmode)) {
534+
server.send(200, "text/plain", "Inverter started");
535+
}
536+
else {
537+
server.send(200, "text/plain", "Start command failed");
538+
}
539+
}
540+
else {
541+
server.send(200, "text/plain", "Unknown command");
542+
}
520543

521544
digitalWrite(LED_BUILTIN, LOW);
522545
}

src/oi_can.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
#define SDO_CMD_SAVE 0
5555
#define SDO_CMD_LOAD 1
5656
#define SDO_CMD_RESET 2
57-
57+
#define SDO_CMD_DEFAULTS 3
58+
#define SDO_CMD_START 4
59+
#define SDO_CMD_STOP 5
5860

5961
namespace OICan {
6062

@@ -577,7 +579,24 @@ bool SaveToFlash() {
577579

578580
setValueSdo(SDO_INDEX_COMMANDS, SDO_CMD_SAVE, 0U);
579581

580-
if (twai_receive(&rxframe, pdMS_TO_TICKS(200)) == ESP_OK) {
582+
if (twai_receive(&rxframe, pdMS_TO_TICKS(200)) == ESP_OK && rxframe.data[0] == SDO_WRITE_REPLY) {
583+
return true;
584+
}
585+
else {
586+
return false;
587+
}
588+
}
589+
590+
bool StartStop(int opmode)
591+
{
592+
if (state != IDLE) return false;
593+
594+
twai_message_t rxframe;
595+
uint8_t subIdx = opmode == 0 ? SDO_CMD_STOP : SDO_CMD_START;
596+
597+
setValueSdo(SDO_INDEX_COMMANDS, subIdx, opmode);
598+
599+
if (twai_receive(&rxframe, pdMS_TO_TICKS(200)) == ESP_OK && rxframe.data[0] == SDO_WRITE_REPLY) {
581600
return true;
582601
}
583602
else {

src/oi_can.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ SetResult AddCanMapping(String json);
3333
SetResult RemoveCanMapping(String json);
3434
SetResult SetValue(String name, double value);
3535
double GetValue(String name);
36+
bool StartStop(int opmode);
3637
bool SaveToFlash();
3738
String StreamValues(String names, int samples);
3839
int StartUpdate(String fileName);

0 commit comments

Comments
 (0)