赞
踩
printf重定义资料来自江科大自动化协---<<stm32入门教程>>---<<串口发送+接收>>一节视频讲解
细节请看b站这个up主的视频阿里云+ESP8266+STM32远程点灯(流程讲解)
阿里云平台配置细节请看10分钟玩转阿里云物联网平台设备接入、管理、运维
这里只记录一下大概内容,方便日后调试。
ESP8266 | USB转串口工具 |
---|---|
3.3 | 3.3 |
GND | GND |
TX | RX |
RX | TX |
注意:有时会出现接线正确发送不出指令的情况,网上查到的原因是,USB转串口工具输出的3.3V电压驱动能力不行,带不动ESP8266,可以更换其他稳定的供电设备,我手边没有万用表,没法测量验证,但是更换电源可以解决此问题;另外官方文件说ESP8266供电电压范围是3.0V-3.6V,我器件尝试接过5V电压,ESP8266没有烧坏,但发热明显,保守期间不要尝试此接法。
AT | |
AT+RST | 重启模块 |
AT+RESTORE | 恢复出厂设置--擦除所有保存在flash中的参数 |
AT+CWMODE=1 | 设置wifi模式-----AP模式 |
AT+CIPSNTPCFG=1,8,"ntp1.aliyun.com" | 设置时域和SNTP服务器 |
AT+CWJAP="wifiname","password" | 连接AP |
AT+MQTTUSERCFG=0,1,"NULL","username","passwd",0,0,"" | |
AT+MQTTCLIENTID=0,"ClientId" | |
AT+MQTTCONN=0,"mqttHostUrl",1883,1 | |
AT+MQTTSUB=0,"/{ProductKey}/{DeviceName}/user/get",1 | 订阅自定义topic |
AT+MQTTPUB=0,"/{ProductKey}/{DeviceName}/user/update","{\"temp\":50.5}",1,0 | 发布自定义Topic |
AT+MQTTSUB=0,"/sys/{ProductKey}/{DeviceName}/thing/service/property/set",1 | 订阅物模型 |
AT+MQTTPUB=0,"/sys/{ProductKey}/{DeviceName}/thing/event/property/post","{\"params\":{\"EnvironmentTemperature\":35}}",1,0 | 上报设备属性 |
Clientld需要在‘,’之前加转义字符 '\'
例:
指令中topic:
STM32 | ESP8266 | USB转串口工具 |
---|---|---|
3.3 | 3.3 | |
GND | GND | GND |
RX | TX | RX |
TX | RX |
可以使用面包板,对线路进行拓展,将ESP8266的TX同时接到STM32和串口工具的RX引脚,方便使用电脑的串口调试软件观察ESP8266的配置状态,方便调试。
ESP8266的初始化函数(用STM32的串口发送AT指令给ESP8266)
- //初始化ESP8266连接到阿里云函数
- void ESP8266_Init(void)
- {
- OLED_ShowString(1,1,"loading...");
- //1
- Serial_String("AT+RST\r\n");
- ESP_FeedBack(1);
- Delay_ms(2000);
- //2
- Serial_String("AT+RESTORE\r\n");
- ESP_FeedBack(2);
- Delay_ms(2000);
- //3
- Serial_String("AT+CWMODE=1\r\n");
- ESP_FeedBack(3);
- Delay_ms(2000);
- //4
- Serial_String("AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"\r\n");//加转义字符
- ESP_FeedBack(4);
- Delay_ms(3000);
- //5
- Serial_String("AT+CWJAP=\"LAPTOP\",\"asdfghjkl\"\r\n");//加转义字符
- ESP_FeedBack(5);
- Delay_ms(3000);
- //6
- Serial_String("AT+MQTTUSERCFG=0,1,\"NULL\",\"ESP8266-01&ibln7d8PcHp\",\"e0ae924e8007d0259c7a7d31bd3f01fd1bfdc270b8e689fa757e4f23de4cccb4\",0,0,\"\"\r\n");
- ESP_FeedBack(6);
- Delay_ms(5000);
- //7
- Serial_String("AT+MQTTCLIENTID=0,\"ibln7d8PcHp.ESP8266-01|securemode=2\\,signmethod=hmacsha256\\,timestamp=1679206129784|\"\r\n");
- ESP_FeedBack(7);
- Delay_ms(5000);
- //8
- Serial_String("AT+MQTTCONN=0,\"iot-06z00ehfqvp2bgm.mqtt.iothub.aliyuncs.com\",1883,1\r\n");
- ESP_FeedBack(8);
- Delay_ms(5000);
- //9
- // Serial_String("/ibln7d8PcHp/ESP8266-01/user/get\",1\r\n");
- // ESP_FeedBack(9);
- // Delay_ms(5000);
- //10订阅物模型
- Serial_String("AT+MQTTSUB=0,\"/sys/ibln7d8PcHp/ESP8266-01/thing/service/property/set\",1\r\n");
- ESP_FeedBack(9);
- Delay_ms(5000);
-
- }

ESP8266日志接收函数(STM32接收ESP8266发送回来的日志数据,分析数据做出初始化反馈)
- //查看错误反馈函数
- void ESP_FeedBack(uint8_t t)
- {
- Delay_ms(1000);
- if( strstr((const char *)ESP8266_To_Serial,"ERROR") )
- {
- Refresh_DMA();//刷新DMA缓冲区以及计数器
- memset(ESP8266_To_Serial,0,100);//将数组清零
- OLED_ShowString(2,1,"ERR:");
- OLED_ShowNum(2,4+t,t,1);
- }
- }
main.c
- int main(void)
- {
- OLED_Init();
- Serial_Init();//初始化串口2配置
- USART_DMA_Init();//初始化DMA转运USART2_RX配置
- ESP8266_Init();//ESP8266连接服务器
-
- while(1)
- {
- i++;
- //一次发送多个物模型数据
- printf("AT+MQTTPUB=0,\"/sys/ibln7d8PcHp/ESP8266-01/thing/event/property/post\",\"{\\\"params\\\":{\\\"EnvironmentTemperature\\\":%d},\\\"LightLux\\\":%d,\\\"AirSpeed\\\":%d}}\",1,0\r\n",i,i+1,i+2);
-
- //一次发送一个物模型数据
- printf("AT+MQTTPUB=0,\"/sys/ibln7d8PcHp/ESP8266-01/thing/event/property/post\",\"{\\\"params\\\":{\\\"LightLux\\\":%d}}\",1,0\r\n",i);
- // printf("AT+MQTTPUB=0,\"/sys/ibln7d8PcHp/ESP8266-01/thing/event/property/post\",\"{\\\"params\\\":{\\\"LightLux\\\":%d}}\",1,0\r\n",i);
- Delay_ms(10000);//延时10秒
- }
- }

请求数据格式要求:
将printf打印的东西输出到串口
重写fputc()函数,因为printf函数使用时也是调用fputc函数一个个的打印。fputc函数发送到串口,printf函数自然也就打印输出到了串口
重写fputc函数:
- #include <stdio.h>
-
- int fputc(int ch,FILE *f)
- {
- Serial_SendByte(ch);//这里调用自己写的串口发送字节函数
- return ch;
- }
-
-
- //主函数
- void main(void)
- {
- printf("NUm=%d\n",666);
-
- while(1)
- {
-
- }
-
- }

sprintf函数不需要重定义,因为此函数第一个参数是指定打印位置
sprintf函数作用是把格式化字符输出到一个字符串里
- #include <stdio.h>
-
- char string[100];//定义一个足够大的缓存空间
-
- sprintf(string,"NUM=5d\n",666);//输出到string中
- void Serial_printf(char *format,...)
- {
- char string[100];
- va_list arg;//存放可变参数列表的变量
- va_start(arg,format)//从format开始存放参数列表到arg中
- vsprintf(string,format,arg);//打印格式 这里up解释是sprintf只能接收直接写的参数
- va_end(arg);//释放arg空间
- Serial_SendByte(string);//调用自己写的串口发送字节函数
- }
keil修改一下配置
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。