赞
踩
基于stm32(103)+ESP01S使用http协议连接OnetNet平台上传采集数据
stm32通过ESP01用HTTP协议连接ONENET云平台,实现STM32连接云平台实现物联网需要用到ESP01S来进行数据的上传,对于WiFi模块,我相信很多人可能会用到。
首先,当我们第一次拿到模块,我们需要先对模块进行测试,确定模块是否是正常,在这里我就不做讲解,之前写过一篇博客,供大家参考:
https://blog.csdn.net/yanw1234/article/details/118709934?spm=1001.2014.3001.5502
本次采用的模块还有:温湿度传感器(dht11) 烟雾传感器 光敏传感器;
将通过对三个传感器数据的采集后,封装http协议上传到onenet平台上,实现物联网的交互;
在STM32端采用uart2接口连接esp进行通信;
对于stm32的连接方式如下:
sem32 | esp |
---|---|
RX(PA3) | TX |
TX(PA2) | RX |
3V3 | 3V3 |
GND | GND |
PA7 | RST |
不说了,直接上代码,需要的可自取不收费,网上扣扣嗖嗖的还有收费然后内容杂乱无章,本人不喜欢这样的,在这一切代码开源:
硬件连接图:
该代码框架如下:
onenet云平台:
串口打印信息:
wifi模块的主要配置流程代码,接口封装好,不管在什么平台只要调用接口即可:
wifi.h
#ifndef __WIFI_H
#define __WIFI_H
#include "usart.h"
#define LYSSID "XXXXXXXXXXX" //!< 路由器或热点账号
#define LYPASSWD "XXXXXXXXXXX" //!< 路由器或热点密码
void wifi_reset(void);
void WiFi_WiFiconfig(void);
void WiFi_RecdataPut(void);
#endif
wifi.c:
#include "wifi.h" #include "public.h" #include "delay.h" void wifi_reset(void) // 模块复位 { GPIO_ResetBits(GPIOA,GPIO_Pin_7); delay_ms(500); GPIO_SetBits(GPIOA,GPIO_Pin_7); delay_ms(500); } void WiFi_WiFiconfig(void) // 模式配置 { char SendATData[200]; switch(WiFiStatus.modeStatus) { case CMD_AT: u2_printf("AT\r\n",strlen("AT\r\n")); //测试通信是否正常 printf("\r\n**CSGSM Connect To MODULE...\r\n"); break; case CMD_CWMODE: u2_printf("AT+CWMODE=3\r\n",strlen("AT+CWMODE=3\r\n")); // 设置AP+ STA printf("Set WiFi ModuleAP+STA..\r\n"); break; case CMD_CIPMODE: u2_printf("AT+CIPMODE=1\r\n",strlen("AT+CIPMODE=1\r\n"));//设置透传 printf("Set WiFi Module transparent\r\n"); //?????????? break; case CMD_CWJAP: memset(SendATData,0,200); sprintf((char *)SendATData, "AT+CWJAP=\"%s\",\"%s\"\r\n",LYSSID, LYPASSWD); u2_printf(SendATData,strlen((char *)SendATData)); printf("Connect To WiFi Route... [name : %s] \r\n", LYSSID); //绑定路由器或者热点的账号密码 break; case CMD_CIPSTART: // 连接服务器,这个指的是onenet u2_printf("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n", strlen("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n")); break; case CMD_CIPSEND: //开始发送数据 u2_printf("AT+CIPSEND\r\n",strlen("AT+CIPSEND\r\n")); printf(" set wifi CIPSEND\r\n"); break; } } void WiFi_RecdataPut(void)//WiFi配置响应数据处理 { char *strx; switch(WiFiStatus.modeStatus){ case CMD_AT: strx=strstr((const char*)WiFiStatus.Recbuffer,(const char*)"OK");//·µ»Øok£¬ËµÃ÷Ä£¿éͨÐÅÕý³£ if (strx) { printf("WiFi CONNET MODULE OK\r\n"); WiFiStatus.modeStatus = CMD_CWMODE; } break; case CMD_CWMODE: strx=strstr((const char*)WiFiStatus.Recbuffer,(const char*)"OK");//ÉèÖÃģʽ3 if (strx) { printf("WiFi CONNET MODULE OK\r\n"); WiFiStatus.modeStatus=CMD_CIPMODE; } break; case CMD_CIPMODE: strx=strstr((const char*)WiFiStatus.Recbuffer,(const char*)"OK");//ÉèÖÃ͸´«Ä£Ê½ if(strx){ printf("TRANSPARNET [OK]\r\n"); WiFiStatus.modeStatus=CMD_CWJAP; } break; case CMD_CWJAP: strx=strstr((const char*)WiFiStatus.Recbuffer,(const char*)"OK"); if(strx){ printf("CONNECT Route OK\r\n"); WiFiStatus.modeStatus=CMD_CIPSTART; } break; case CMD_CIPSTART: strx=strstr((const char*)WiFiStatus.Recbuffer,(const char*)"OK"); if(strx){ printf( "CONNECT CLOUD OK\r\n"); WiFiStatus.modeStatus=CMD_CIPSEND; } break; case CMD_CIPSEND: strx=strstr((const char*)WiFiStatus.Recbuffer,(const char*)"OK");//??????c s t x if(strx){ printf("SEND CLOUD DATA.\r\n"); LED1_SET(0); // 连接成功后指示灯亮起 WiFiStatus.modeStatus=CMD_SEND;//????????? } break; default: break; } memset(WiFiStatus.Recbuffer, 0, strlen((char *)WiFiStatus.Recbuffer)); }
http协议封装接口代码:
http.h
#ifndef __HTTP_H
#define __HTTP_H
#include "public.h"
//onenet云平台的设备信息
#define devkey "XXXXXXXXXXXXX" //!< api-key(Master-APIkey)
#define devid "XXXXXXXXX" //!< 设备id
unsigned int http_send_post(sensor_data senData);
unsigned int http_receive_response(void);
#endif
http.c
#include "http.h" #include <stdio.h> #include "usart.h" static char sendPostPack[MAX_BUFF_SIZE]= {0}; unsigned int http_send_get() // 获取数据流,没写 { return 1; } unsigned int http_send_post(sensor_data senData) // 上传数据流 { unsigned char datastrem[256] = {0}; sprintf(sendPostPack,"POST /devices/%s/datapoints?type=3 HTTP/1.1\r\napi-key:%s\r\nHost:api.heclouds.com\r\n",devid,devkey); u2_printf((char *)sendPostPack, strlen(sendPostPack)); printf("%s",sendPostPack); memset(sendPostPack,0,MAX_BUFF_SIZE); sprintf((char *)datastrem, "{'temp':%d,'humi':%d,'light':%d,'smoke':%d}\r\n", senData.temp_sen,senData.humi_sen, senData.light_status, senData.smoke_sen); sprintf((char *)sendPostPack, "Content-Length:%d\r\n\r\n%s", strlen((char *)datastrem),datastrem); u2_printf((char *)sendPostPack, strlen(sendPostPack)); printf("%s",sendPostPack); memset(sendPostPack,0,MAX_BUFF_SIZE); if (http_receive_response()) { return 1; } return 0; } unsigned int http_receive_response(void) // 服务器响应信息 { if (strstr((const char*)WiFiStatus.Recbuffer,"succ")) { printf(" %s \r\n",WiFiStatus.Recbuffer); memset(WiFiStatus.Recbuffer, 0, strlen((char *)WiFiStatus.Recbuffer)); return 1; } return 0; }
main.c
#include "stdio.h" #include "string.h" #include "stdlib.h" #include "sys.h" #include "usart.h" #include "timer.h" #include "public.h" #include "sys_config.h" #include "wifi.h" #include "http.h" #include "dht11.h" sensor_data g_senorData; int main(void) { unsigned int send_http_req = 0, adc_get_timer = 0, light_get_timer = 0, dht11_get_timer = 0; float smoke_sen; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); sys_config(); TIM3_Init(199,7199);//设置20ms usart_config(115200); usart2_config(115200); wifi_reset(); while(1) { if (WiFiStatus.modeStatus != CMD_SEND) { // wifiÅäÖóõʼ»¯ WiFi_WiFiconfig(); delay_ms(5); WiFi_RecdataPut(); delay_ms(5); } else { if ((get_time_ms() - adc_get_timer)> 3 * 1000) { // 3s采集一次烟雾数据 smoke_sen = Get_Adc_Average(ADC_Channel_1,10); g_senorData.smoke_sen = (int)((smoke_sen / 4096) * 100); adc_get_timer = get_time_ms(); } if ((get_time_ms() - dht11_get_timer)> 3 * 1000) {//采集一次温湿度数据 DHT11_readData(&g_senorData.temp_sen, &g_senorData.humi_sen); dht11_get_timer = get_time_ms(); } if ((get_time_ms() - light_get_timer)> 3 * 1000) { if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_11)) { if (g_senorData.light_status == LIGHT_OFF) { GPIO_SetBits(GPIOA,GPIO_Pin_12); g_senorData.light_status = LIGHT_ON; } } else { if (g_senorData.light_status == LIGHT_ON) { GPIO_ResetBits(GPIOA,GPIO_Pin_12); g_senorData.light_status = LIGHT_OFF; } } light_get_timer = get_time_ms(); } if ((get_time_ms() - send_http_req) > 10 * 1000) { //10s上传一次数据到云平台 if (http_send_post(g_senorData)) { printf("\r\n[debug]----->> http request success !!!\r\n"); send_http_req = get_time_ms(); } } } } }
代码自取,有不懂的可以评论或者私信,又看到就会回复
链接:https://pan.baidu.com/s/1AB6tblcSPNkGJCz2Er0Vhw?pwd=ptxp
提取码:ptxp
网址失效的话,评论或者私信留言!!!
欢迎大家的批评指正
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。