优先级: mqttTask06 > ReceiveTask5 = Transmit_task2
//优先级最高,连接wifi和MQTT后,挂起任务
void mqttTask06(void const * argument)
{
/* USER CODE BEGIN mqttTask06 */
/* Infinite loop */
for(;;)
{
int rv;
char ipaddr[16];
char gateway[16];
unsigned char wifi_flag=0;
if(!(wifi_flag&FLAG_WIFI_CONNECTED))
{
if( esp8266_join_network(DEF_ROUTER_SSID,DEF_ROUTER_PWD))
{//0 is connect success
esp8266_module_init();
osDelay(2000);
continue;
}
if(esp8266_get_ipaddr(ipaddr,gateway,sizeof(ipaddr)))
{
osDelay(1000);
continue;
}
if(esp8266_ping_test(gateway))
{
osDelay(1000);
continue;
}
wifi_flag |= FLAG_WIFI_CONNECTED;
}
if(!(wifi_flag&FLAG_SOCK_CONNECTED))
{
rv=mqtt_connect(MQTT_BROKER_HOSTNAME,MQTT_BROKER_PORT,MQTT_CLIENT_ID,MQTT_BROKER_USERNAME,MQTT_BROKER_PASSWORD);
if( rv )
{//0 is connect success
osDelay(2000);
continue;
}
mqtt_subscribe_topic(MQTT_SUB_TOPIC,Qos0,1);//订阅主题
wifi_flag |= FLAG_SOCK_CONNECTED;
vTaskSuspend(NULL);//挂起态(挂起自己)
}
osDelay(1);
}
/* USER CODE END mqttTask06 */
}
//接收阿里云下行的命令,并做报文解析和JSON解析
void ReceiveTask5(void const * argument)
{
/* USER CODE BEGIN ReceiveTask5 */
/* Infinite loop */
for(;;)
{
unsigned char buf[256];
int packet_type;
MQTTString topicName;
unsigned char dup;
int qos;
unsigned char retained;
unsigned short msgid;
unsigned char *payload = NULL;
int payloadlen = 0;
int rv;
char correct_json[256]={0};
memset(buf,0,sizeof(buf));
if( (packet_type=MQTTPacket_read(buf,sizeof(buf),transport_getdata)) <0 )
{
printf("Wait aliyun command\n");
osDelay(3000);
continue;//没收到阿里云下行的命令的时,一般((packet_type=-1)<0),一直在此进行循环,等待命令到来
}
switch( packet_type )
{
case 0:
break;
case PUBLISH:
{
rv=MQTTDeserialize_publish(&dup,&qos,&retained,&msgid,&topicName,&payload,&payloadlen,buf,sizeof(buf));
if( rv == 1)
{
printf("MQTT payload:%s\n",payload);
aliyun_json((char *)payload,payloadlen,correct_json);
parser_led_json(correct_json,strlen(correct_json));
}
else
{
printf("ERROR:MQTTDeserialize_publish failure,rv=%d\n",rv);
}
break;
}
case CONNACK:
case PUBACK:
case SUBACK:
case UNSUBACK:
case PUBREC:
case PUBREL:
case PUBCOMP:
break;
}
osDelay(1);
}
/* USER CODE END ReceiveTask5 */
}
//上报温湿度值
void Transmit_task2(void const * argument)
{
/* USER CODE BEGIN Transmit_task2 */
/* Infinite loop */
for(;;)
{
int rv;
rv=report_tempRH2_json();//此函数是上报温湿度的,代码下面会贴上
if( 0 == rv )
{
printf("ESP8266 MQTT publish message ok\n");
clear_atcmd_buf();//因为阿里云接收到温湿度值后会回馈一段数据,占buffer空间导致buffer溢出
}
vTaskDelay(30000);//进入阻塞态30s,即30秒上报一次温湿度值
}
/* USER CODE END Transmit_task2 */
}
//用的是sht20温湿度传感器
int report_tempRH2_json(void)
{
char buf[128];
float temperature,humidity;
int rv;
if( (sht20_sample_temp_or_hum(0xF3,&temperature) <0 ) || (sht20_sample_temp_or_hum(0xF5,&humidity) <0 ))
{
printf("ERROR:sht20 sample data failure\n");
return -1;
}
memset(buf,0,sizeof(buf));
snprintf(buf,sizeof(buf),"{\"params\":{\"CurrentTemperature\":%.2f,\"CurrentHumidity\":%.2f}}",temperature,humidity);
rv=mqtt_publish(MQTT_PUB_TOPIC,Qos0,buf);
printf("MQTT publish topic [%s] with msg '%s'%s,rv=%d\n",MQTT_PUB_TOPIC,buf,rv? "failure":"ok",rv);
return rv;
}