stm32 实现温度采集,上传到云平台、Qt,获取天气数据

1、主函数

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
#include "Delay.h"
#include "OLED.h"
#include "led.h"
#include "key.h"
#include "timer.h"//定时器
#include "dht11.h"//正点原子改
#include "tDHT11.h"//老师提供
#include "beep.h"
#include "usart.h"
#include "string.h"
#include "stdlib.h"
#include "beep.h"//可以忽略
#include "show_weather.h"



//全局变量
int usart1Len = 0;	//usart1接收的数据长度
char usart1Buf[512];	//usart1接收缓存

int usart2Len = 0;	//usart2接收的数据长度
char usart2Buf[512];	//usart2接收缓存

int usart3Len = 0;	//usart3接收的数据长度
char usart3Buf[1024];	//usart3接收缓存

unsigned char usart4Len = 0;	//usart4接收的数据长度
unsigned char usart4Buf[64];	//usart4接收缓存
char check_men;

uint8_t key_value;//判断按键
uint8_t men = 32;//门限温度
uint8_t hum;//湿度
uint8_t temp = 0;//检测温度
uint32_t nums = 1;//数字显示?
uint32_t t=0; //控制温湿度读取时间


/*
************************************************************
*	函数名称:	Hardware_Init
*
*	函数功能:	硬件初始化
*
*	入口参数:	无
*
*	返回参数:	无
*
*	说明:		初始化单片机功能以及外接设备
************************************************************
*/
void Hardware_Init(void)
{
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);		//中断控制器分组设置

	led_init();//led灯
	Key_Init();//按键
	OLED_Init();//屏幕
  //TIM2_Int_Init(9999,7199);
	TIM4_Int_Init(9999,7199);//定时器
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组 2
	
//    DHT11_Init();//DHT11初始化	
	tDHT11_start();//dht11

	Beep_Init();                                        //蜂鸣器初始化	
	
	
	Usart1_Init(115200);								//初始化串口1,波特率115200

	Usart2_Init(115200);                                //初始化串口2,波特率921600
	
	Usart3_Init(9600);                                  //初始化串口3,波特率9600
	
	Usart4_Init(115200);                                //初始化串口4,波特率115200
	
	
}

void onenet_init(){
	
	OLED_Clear();//清屏
	OLED_ShowString(1,1,"connect onenet");

	OLED_ShowNum(2,1,10,2);
  Usart_SendString(USART2,(unsigned char *)"+++",3);//退出透传
	Delay_ms(1000);
	OLED_ShowNum(2,1,11,2);
	Usart_SendString(USART2,(unsigned char *)"+++",3);//多尝试几次
	Delay_ms(1000);
	
	OLED_ShowNum(2,1,12,2);
	Usart_SendString(USART2,(unsigned char *)"+++",3);//再来一次
	Delay_ms(500);
	
	OLED_ShowNum(2,1,13,2);
	UsartPrintf(USART2,"AT+RST\r\n");//重置
	Delay_ms(5000);
	
	OLED_ShowNum(2,1,0,2);
	UsartPrintf(USART2,"AT+CIPSTART=\"TCP\",\"183.230.40.40\",1811\r\n");//连接到服务器
	Delay_ms(500);
	
	OLED_ShowNum(2,1,1,2);
	UsartPrintf(USART2, "AT+CIPMODE=1\r\n");//开启透传
	Delay_ms(500);
	
	OLED_ShowNum(2,1,2,2);
	UsartPrintf(USART2,"AT+CIPSEND\r\n");//开始透传
	Delay_ms(500);

	OLED_ShowNum(2,1,3,2);
	UsartPrintf(USART2,"*537147#esp8266#test*");//开始认证
//	UsartPrintf(USART2,"*537077#8266#samplelua*");//开始认证

	Delay_ms(500);
  OLED_ShowString(3,1,"succ!");
	Delay_ms(500);
	
	OLED_Clear();//清屏
}


void wifi_init(){
	
	OLED_Clear();//清屏
	OLED_ShowString(1,1,"wifi init");
	OLED_ShowNum(2,1,0,1);
	
	Usart_SendString(USART2,(unsigned char *)"+++",3);//退出透传
	Delay_ms(1000);

	Usart_SendString(USART2,(unsigned char *)"+++",3);//多尝试几次
	Delay_ms(1000);
	
	OLED_ShowNum(2,1,1,1);
	Usart_SendString(USART2,(unsigned char *)"+++",3);//再来一次
	Delay_ms(500);
	
	OLED_ShowNum(2,1,2,1);
	UsartPrintf(USART2,"AT+CWMODE=1\r\n");//设置sta模式
	Delay_ms(5000);
	
	OLED_ShowNum(2,1,3,1);
	UsartPrintf(USART2,"AT+RST\r\n");//重置
	Delay_ms(5000);
	
	OLED_ShowNum(2,1,4,1);
	UsartPrintf(USART2,"AT+CWJAP=\"mz17\",\"qwe123456\"\r\n");//连接wifi
	Delay_ms(8000);
	
	OLED_Clear();//清屏
}


void onenet_send(uint8_t men,uint8_t temp){
	char onenet_buf[32];
	
	snprintf(onenet_buf,3,"%02d:%02d",men,temp);//更新门限温度数据给onnet
	
	UsartPrintf(USART2, onenet_buf);
}


/*
************************************************************
*	函数名称:	main
*
*	函数功能:	温度检测和天气查询
*		
************************************************************
*/
int main(void)
{
  int loop_i=0;//用于循环
	char buf[32];//用于发送数据
	char menx[3];//用于解析门限指令
	
	int fun_num;//功能代号  1温度检测  2 天气获取
	int city;//城市代号 0连接服务器 1成都 2南充 3拉萨 4石家庄 5乌鲁木齐
	int check_city;//判断用户是否查询天气数据
	Hardware_Init();//硬件初始化
	
	UsartPrintf(USART1,"  this is USART1");
	fun_num = 1;//默认温度检测
	city = 0;//配置wifi 并查询南充天气
	
	OLED_Clear();
	OLED_ShowString(1,1,"press key");//等待用户判断
	OLED_ShowString(2,1,"key1 weather");//先天气
	OLED_ShowString(3,1,"key2 temperature");//先温度
	OLED_ShowString(4,1,"key3 wifi init");//先重新连接wifi
	
	while(1){
		key_value = key_scan();
		
		if(key_value == 1){
			fun_num = 2;
			UsartPrintf(USART1, "weather");
			OLED_weather_loading(0,0);
			check_city = 1;
			city = 0;
			break;
		}
		else if(key_value == 2){
			onenet_init();
			fun_num = 1;
			break;
		}
		else if(key_value == 3){
			wifi_init();
			OLED_ShowString(1,1,"wifi ok");
		  OLED_ShowString(2,1,"key1 weather");
	    OLED_ShowString(3,1,"key4 temperature");
		}
	}
	
	key_value = 0;
	
	while(1)
	{
		/*切换天气检测和温度检测*/
		key_value = key_press();
		if(key_value == 4){//温度检测
			
			while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3) == 0);//松手检测
			GPIO_ResetBits(GPIOA,GPIO_Pin_7);//关闭天气预报的指示灯
			onenet_init();//连接onenet
			fun_num = 1;
			OLED_Clear();//清屏
	    key_value = 0;
		}
		else if(key_value == 1){//天气预报
			
			while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0) == 0);
			GPIO_ResetBits(GPIOA,GPIO_Pin_4);//关闭温度检测的指示灯
			
			fun_num = 2;
			OLED_Clear();
			UsartPrintf(USART1, "  weather");
			OLED_weather_loading(0,0);
			check_city = 1;
//			city = 0;
			key_value = 0;
		}
		
		/* 温度检测 */
		if(fun_num == 1){
			
		TIM4_IRQHandler(&t);
			
			if(check_men == 1){
				men--;
				check_men = 0;
				onenet_send(men,temp);
			}else if(check_men == 2){
				men++;
				check_men = 0;
				onenet_send(men,temp);
			}
			
			
		/* 手动修改门限 */
		switch(key_value){
			case 2:
				
				GPIO_SetBits(GPIOA,GPIO_Pin_5);//点亮第2个灯
				while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1) == 0);//松手检测
				GPIO_ResetBits(GPIOA,GPIO_Pin_5);//关闭第2个灯
				
				men--;
			  onenet_send(men,temp);
				break;
			case 3:
				
				GPIO_SetBits(GPIOA,GPIO_Pin_6);
				while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2) == 0);
				GPIO_ResetBits(GPIOA,GPIO_Pin_6);
			
				men++;
			  onenet_send(men,temp);
				break;
			
			default:
				key_value = 0;
				break;
		}

	  /* 温度读取 */
		if(t==3) //每 3s 读取一次
		{ 
			tDHT11_rec_data(&temp,&hum); //读取温湿度值
//			DHT11_Read_Data(&temp,&hum); 
			t=0;
			
			snprintf(buf,16,"dht11 %02d:%02d 0000",temp,men);//更新dht11的数据给上位机
			UsartPrintf(USART1, buf);
			onenet_send(men,temp);
    }
   
		OLED_Show(temp,men);

		nums = nums+1;
		
		OLED_ShowNum(4,1,nums,5);
		OLED_ShowNum(4,10,t,2);
			
		
	/*判断接收指令,更改门限*/
	//raise0000  增加门限
	//reduce000  减小门限
	//setmen:xx  设置门限
    if(usart1Len == 9){
			if(strncmp((char *)usart1Buf,"raise",5) == 0){
				men++;
			}
			else if(strncmp((char *)usart1Buf,"reduce",6) == 0){
				men--;
			}
			else if(strncmp((char *)usart1Buf,"setmen:",7) == 0){
				
				strncpy(menx,(char *)usart1Buf+7,2);
				
				menx[2] = 0;
				men = atoi(menx);
				
			}
			
			snprintf(buf,16,"dht11 %02d:%02d 1000",temp,men);//响应上位机的操作,然后数据同步
			UsartPrintf(USART1, buf);
			
			OLED_Show(temp,men%99); 
			usart1Len = 0;
		}
		
		
		//天气获取
		
		}else{
			GPIO_SetBits(GPIOA,GPIO_Pin_7);
			switch(key_value){
				case 2:
				{
					GPIO_SetBits(GPIOA,GPIO_Pin_5);//点亮第2个灯
					while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1) == 0);//松手检测
					GPIO_ResetBits(GPIOA,GPIO_Pin_5);//关闭第2个灯
					
					city--;//切换城市
					if(city <1){
						city = 5;
					}
					check_city = 1;//用于执行查询天气数据
					break;
				}
				case 3:
				{
					GPIO_SetBits(GPIOA,GPIO_Pin_6);
					while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2) == 0);
					GPIO_ResetBits(GPIOA,GPIO_Pin_6);
				
					city++;
					if(city > 5 ){
						city = 1;
					}
					check_city = 1;
					break;
				}
				default:
					key_value = 0;
					break;
			}
			if(check_city > 0){
				OLED_Clear();
				GPIO_ResetBits(GPIOA,GPIO_Pin_7);
				OLED_ShowNum(2,1,city,2);//显示要查询的城市代号

				show_city_weather(city,&check_city);
			}
		}
		
	}
}

2、演示

qt上位机忘了录,可以串口 udp通信
https://live.csdn.net/v/232226?spm=1001.2014.3001.5501

3、整个代码

链接:https://pan.baidu.com/s/142mizM9ZLAO0KUw7UZuhaQ?pwd=j5nc
提取码:j5nc

  • 4
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值