void ICACHE_FLASH_ATTR mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) { char strTopic[topic_len + 1]; os_memcpy(strTopic, topic, topic_len); strTopic[topic_len] = '\0'; char strData[lengh + 1]; os_memcpy(strData, data, lengh); strData[lengh] = '\0'; char relayNum=strTopic[topic_len-1]; char strSubsTopic[strlen((char *)sysCfg.mqtt_relay_subs_topic)]; os_strcpy(strSubsTopic,(char *)sysCfg.mqtt_relay_subs_topic); strSubsTopic[(strlen((char *)sysCfg.mqtt_relay_subs_topic)-1)]=relayNum; char mqtt_temp_topic[strlen((char *)sysCfg.mqtt_temp_subs_topic)]; os_strcpy(mqtt_temp_topic,(char *)sysCfg.mqtt_temp_subs_topic); os_printf("MQTT strSubsTopic: %s, strTopic: %s \r\n", strSubsTopic, strTopic); if (os_strcmp(strSubsTopic,strTopic) == 0 ) { os_printf("Relay %d is now: %s \r\n", relayNum-'0', strData); if(relayNum=='1') { currGPIO5State=atoi(strData); ioGPIO(currGPIO5State,RELAY_GPIO); } if( sysCfg.relay_latching_enable) { sysCfg.relay_1_state=currGPIO5State; CFG_Save(); } }else if (os_strcmp(mqtt_temp_topic,strTopic) == 0 ) { MQTTreading=atoi(strData)*100; os_printf("MQTT temp: %d \r\n", MQTTreading); } os_printf("MQTT topic: %s, data: %s \r\n", strTopic, strData); }
//Cgi that turns the Relays on or off according to the 'relayX' param in the GET data int ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData) { int len; char buff[128]; int gotcmd=0; if (connData->conn==NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; } len=httpdFindArg(connData->getArgs, "relay1", buff, sizeof(buff)); if (len>0) { currGPIO12State=atoi(buff); ioGPIO(currGPIO12State,12); gotcmd=1; //Manually switching relays means switching the thermostat off if(sysCfg.thermostat1state!=0) { sysCfg.thermostat1state=0; } } if(gotcmd==1) { if( sysCfg.relay_latching_enable) { sysCfg.relay_1_state=currGPIO12State; CFG_Save(); } httpdRedirect(connData, "relay.tpl"); return HTTPD_CGI_DONE; } else { //with no parameters returns JSON with relay state httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "text/json"); httpdHeader(connData, "Access-Control-Allow-Origin", "*"); httpdEndHeaders(connData); len=os_sprintf(buff, "{\"relay1\": %d\n,\"relay1name\":\"%s\"}\n", currGPIO12State,(char *)sysCfg.relay1name ); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; } }