Skip to content

Commit

Permalink
slightly better tasmota commands support
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Jul 30, 2022
1 parent a873e98 commit c35a357
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/cmnds/cmd_tasmota.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 75 additions & 2 deletions src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,17 +1342,90 @@ int http_fn_cfg_ha(http_request_t *request) {
// 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);


Expand Down
1 change: 1 addition & 0 deletions src/httpserver/rest_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c35a357

Please sign in to comment.