FreeRTOS 阿里云MQTT 上报温湿度 下行命令控制led三色灯

优先级: 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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值