diff --git a/src/cmnds/cmd_tasmota.c b/src/cmnds/cmd_tasmota.c index b5d905a20..f4bf6501d 100644 --- a/src/cmnds/cmd_tasmota.c +++ b/src/cmnds/cmd_tasmota.c @@ -23,11 +23,25 @@ static int power(const void *context, const char *cmd, const char *args, int cmd //if (!wal_strnicmp(cmd, "POWER", 5)){ int channel = 0; int iVal = 0; + int i; ADDLOG_INFO(LOG_FEATURE_CMD, "tasCmnd POWER (%s) received with args %s",cmd,args); if (strlen(cmd) > 5) { channel = atoi(cmd+5); + } else { + // if new LED driver active + if(PIN_CountPinsWithRole(IOR_PWM) > 0) { + channel = SPECIAL_CHANNEL_LEDPOWER; + } else { + // find first active channel, because some people index with 0 and some with 1 + for(i = 0; i < CHANNEL_MAX; i++) { + if (h_isChannelRelay(i) || CHANNEL_GetType(i) == ChType_Toggle) { + channel = i; + break; + } + } + } } #if 0 // it seems that my Home Assistant expects RGB etc light bulbs to be turned off entirely diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 77c9a69ff..a7a65a9f1 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1342,17 +1342,90 @@ http:///cm?user=admin&password=joker&cmnd=Power%20Toggle // https://www.elektroda.com/rtvforum/viewtopic.php?p=19330027#19330027 // Web browser sends: GET /cm?cmnd=POWER1 // System responds with state +int http_tasmota_json_power(http_request_t *request) { + int numRelays = 0; + int numPWMs = 0; + int i; + int lastRelayState; + + // try to return status + numPWMs = PIN_CountPinsWithRole(IOR_PWM); + numRelays = PIN_CountPinsWithRole(IOR_Relay); + + // LED driver (if has PWMs) + if(numPWMs > 0){ + if(LED_GetEnableAll() == 0) { + poststr(request,"{\"POWER\":\"OFF\"}"); + } else { + poststr(request,"{\"POWER\":\"ON\"}"); + } + } else { + // relays driver + for(i = 0; i < CHANNEL_MAX; i++) { + if (h_isChannelRelay(i) || CHANNEL_GetType(i) == ChType_Toggle) { + numRelays++; + lastRelayState = CHANNEL_Get(i); + } + } + if(numRelays == 1) { + if(lastRelayState) { + poststr(request,"{\"POWER\":\"ON\"}"); + } else { + poststr(request,"{\"POWER\":\"OFF\"}"); + } + } else { + for(i = 0; i < CHANNEL_MAX; i++) { + if (h_isChannelRelay(i) || CHANNEL_GetType(i) == ChType_Toggle) { + lastRelayState = CHANNEL_Get(i); + if(lastRelayState) { + hprintf128(request,"{\"POWER%i\":\"ON\"}",i); + } else { + hprintf128(request,"{\"POWER%i\":\"OFF\"}",i); + } + } + } + } + + } + return 0; +} +/* +{"StatusSNS":{"Time":"2022-07-30T10:11:26","ENERGY":{"TotalStartTime":"2022-05-12T10:56:31","Total":0.003,"Yesterday":0.003,"Today":0.000,"Power": 0,"ApparentPower": 0,"ReactivePower": 0,"Factor":0.00,"Voltage":236,"Current":0.000}}} +*/ +int http_tasmota_json_status_SNS(http_request_t *request) { + float power, factor, voltage, current; + + factor = 0; // TODO + voltage = DRV_GetReading(OBK_VOLTAGE); + current = DRV_GetReading(OBK_CURRENT); + power = DRV_GetReading(OBK_POWER); + + hprintf128(request,"{\"StatusSNS\":{\"ENERGY\":{"); + hprintf128(request,"\"Power\": %f,", power); + hprintf128(request,"\"ApparentPower\": 0,\"ReactivePower\": 0,\"Factor\":%f,", factor); + hprintf128(request,"\"Voltage\":%f,", voltage); + hprintf128(request,"\"Current\":%f}}}", current); + +} int http_fn_cm(http_request_t *request) { char tmpA[128]; http_setup(request, httpMimeTypeJson); + // exec command if( http_getArg(request->url,"cmnd",tmpA,sizeof(tmpA))) { CMD_ExecuteCommand(tmpA,COMMAND_FLAG_SOURCE_HTTP); - + if(!wal_strnicmp(tmpA,"POWER",5)) { + http_tasmota_json_power(request); + } + else if(!wal_strnicmp(tmpA,"STATUS",6)) { + http_tasmota_json_status_SNS(request); + } else { + http_tasmota_json_status_SNS(request); + } } - poststr(request,"{\"POWER1\":\"OFF\"}"); + poststr(request, NULL); diff --git a/src/httpserver/rest_interface.c b/src/httpserver/rest_interface.c index 46bca90e2..a9ba09ccb 100644 --- a/src/httpserver/rest_interface.c +++ b/src/httpserver/rest_interface.c @@ -689,6 +689,7 @@ static int http_rest_get_info(http_request_t *request){ hprintf128(request, "\"mac\":\"%s\",", HAL_GetMACStr(macstr)); hprintf128(request, "\"mqtthost\":\"%s:%d\",", CFG_GetMQTTHost(), CFG_GetMQTTPort()); hprintf128(request, "\"mqtttopic\":\"%s\",", CFG_GetShortDeviceName()); + hprintf128(request, "\"chipset\":\"%s\",", PLATFORM_MCU_NAME); hprintf128(request, "\"webapp\":\"%s\"}", CFG_GetWebappRoot()); poststr(request, NULL);