Exemple #1
0
static void ICACHE_FLASH_ATTR mqtt_app_timer_cb(void *arg){

	
	struct ip_info ipConfig;
	
	wifi_get_ip_info(STATION_IF, &ipConfig);	
	wifiStatus = wifi_station_get_connect_status();

	//check wifi
	if(wifiStatus != lastWifiStatus){

		lastWifiStatus = wifiStatus;
		
		if(wifiStatus==STATION_GOT_IP && ipConfig.ip.addr != 0){
        	MQTT_DBG("MQTT: Detected wifi network up");
        	MQTT_Connect(&mqtt_client);       
	    }
	    else{
	    	MQTT_DBG("MQTT: Detected wifi network down");
	    	MQTT_Disconnect(&mqtt_client);    
	    }

	}
	    
}
Exemple #2
0
static void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
	char *topicBuf = (char*)os_zalloc(topic_len+1),
			*dataBuf = (char*)os_zalloc(data_len+1);

	MQTT_Client* client = (MQTT_Client*)args;

	os_memcpy(topicBuf, topic, topic_len);
	topicBuf[topic_len] = 0;

	os_memcpy(dataBuf, data, data_len);
	dataBuf[data_len] = 0;

	MQTT_DBG("Receive topic: %s, data: %s ", topicBuf, dataBuf);

	os_free(topicBuf);
	os_free(dataBuf);

	// set relay
	int i;
	for(i=0;i<relay_count();i++){

		mqtt_relay *r = &relays[i];

		//compare topic
		if(os_strncmp(topic,r->topic,strlen(r->topic))==0){
			MQTT_DBG("Topic 1st part match");
			//check set suffix
			if(os_strncmp(topic+strlen(r->topic),MQTT_RELAY_SET_SUFIX,strlen(MQTT_RELAY_SET_SUFIX))==0)
			{
				MQTT_DBG("Topic 2nd part match");
				int state = data[0] - '0';

				if(state==0 || state == 1)
				{
					MQTT_DBG("Setting relay #%d to %d via MQTT", i, state);
					relay_set_state(i,state);
					break;
				}

				
			}
		} 
			

	}

	
}
Exemple #3
0
static void mqttConnectedCb(uint32_t *args)
{
	MQTT_Client* client = (MQTT_Client*)args;
	MQTT_DBG("MQTT: Connected");

	int i;
	char * buff = (char *)os_zalloc(64);
        uint32_t vdd33 = readvdd33();
        
	sensor_data data;
	sensors_get_data(&data);	

	c_sprintf(buff,"%f",data.sht21.temp);
	MQTT_Publish(&mqtt_client, "temperature/"SERIAL_NUMBER, buff, strlen(buff), 0, 1);

	os_memset(buff,0,64);
	c_sprintf(buff,"%f",data.sht21.hum);
	MQTT_Publish(&mqtt_client, "humidity/"SERIAL_NUMBER, buff, strlen(buff), 0, 1);

	os_memset(buff,0,64);
	c_sprintf(buff,"%f",(float)(vdd33/1000.0));
	MQTT_Publish(&mqtt_client, "voltage/"SERIAL_NUMBER, buff, strlen(buff), 0, 1);
        
        os_free(buff);

}
Exemple #4
0
static void mqtt_relay_change_cb(int id,int state){

	MQTT_DBG("mqtt_relay_change_cb #%d %d",id,state);

	mqtt_relay * relay = &relays[id];	

	if(state<0)
		state = relay_get_state(id);

	MQTT_Publish(&mqtt_client, relay->topic, state?"1":"0", 1, 0, 1);

}
Exemple #5
0
static void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
	char *topicBuf = (char*)os_zalloc(topic_len+1),
			*dataBuf = (char*)os_zalloc(data_len+1);

	MQTT_Client* client = (MQTT_Client*)args;

	os_memcpy(topicBuf, topic, topic_len);
	topicBuf[topic_len] = 0;

	os_memcpy(dataBuf, data, data_len);
	dataBuf[data_len] = 0;

	MQTT_DBG("Receive topic: %s, data: %s ", topicBuf, dataBuf);

	os_free(topicBuf);
	os_free(dataBuf);
}
Exemple #6
0
static void mqttConnectedCb(uint32_t *args)
{
	MQTT_Client* client = (MQTT_Client*)args;
	MQTT_DBG("MQTT: Connected");

	int i;
	for(i=0;i<relay_count();i++){

		char * setTopic = (char *)os_zalloc(strlen(relays[i].topic)+strlen(MQTT_RELAY_SET_SUFIX)+1);
		os_strcpy(setTopic,relays[i].topic);
		os_strcat(setTopic,MQTT_RELAY_SET_SUFIX);

		MQTT_Subscribe(client, setTopic, 1);

		os_free(setTopic);

		mqtt_relay_change_cb(i,-1); //force 1st publish
	}
	
	

}
Exemple #7
0
static void mqttDisconnectedCb(uint32_t *args)
{
	MQTT_Client* client = (MQTT_Client*)args;
	MQTT_DBG("MQTT: Disconnected");
}
Exemple #8
0
static void mqttPublishedCb(uint32_t *args)
{
	MQTT_Client* client = (MQTT_Client*)args;
	MQTT_DBG("MQTT: Published");
}