printf重定義資料來自江科大自動化協(xié)---<<stm32入門教程>>---<<串口發(fā)送+接收>>一節(jié)視頻講解
1、調(diào)試ESP8266
細(xì)節(jié)請看b站這個(gè)up主的視頻阿里云+ESP8266+STM32遠(yuǎn)程點(diǎn)燈(流程講解)
阿里云平臺配置細(xì)節(jié)請看10分鐘玩轉(zhuǎn)阿里云物聯(lián)網(wǎng)平臺設(shè)備接入、管理、運(yùn)維
這里只記錄一下大概內(nèi)容,方便日后調(diào)試。
1、用USB轉(zhuǎn)串口工具連接ESP8266,先調(diào)通esp8266到阿里云端的線路(此步在后續(xù)調(diào)試esp8266時(shí)也尤為重要,因?yàn)檫@樣不需要反復(fù)修改代碼反復(fù)燒寫,只需幾個(gè)AT指令)
ESP8266 | USB轉(zhuǎn)串口工具 |
---|---|
3.3 | 3.3 |
GND | GND |
TX | RX |
RX | TX |
?注意:有時(shí)會出現(xiàn)接線正確發(fā)送不出指令的情況,網(wǎng)上查到的原因是,USB轉(zhuǎn)串口工具輸出的3.3V電壓驅(qū)動能力不行,帶不動ESP8266,可以更換其他穩(wěn)定的供電設(shè)備,我手邊沒有萬用表,沒法測量驗(yàn)證,但是更換電源可以解決此問題;另外官方文件說ESP8266供電電壓范圍是3.0V-3.6V,我器件嘗試接過5V電壓,ESP8266沒有燒壞,但發(fā)熱明顯,保守期間不要嘗試此接法。
AT | |
AT+RST | 重啟模塊 |
AT+RESTORE | 恢復(fù)出廠設(shè)置--擦除所有保存在flash中的參數(shù) |
AT+CWMODE=1 | 設(shè)置wifi模式-----AP模式 |
AT+CIPSNTPCFG=1,8,"ntp1.aliyun.com" | 設(shè)置時(shí)域和SNTP服務(wù)器? |
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 | 發(fā)布自定義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 | 上報(bào)設(shè)備屬性 |
Clientld需要在‘,’之前加轉(zhuǎn)義字符 '\'
例:
指令中topic:
2、用stm32調(diào)試esp8266
STM32 | ESP8266 | USB轉(zhuǎn)串口工具 |
---|---|---|
3.3 | 3.3 | |
GND | GND | GND |
RX | TX | RX |
TX | RX |
可以使用面包板,對線路進(jìn)行拓展,將ESP8266的TX同時(shí)接到STM32和串口工具的RX引腳,方便使用電腦的串口調(diào)試軟件觀察ESP8266的配置狀態(tài),方便調(diào)試。
ESP8266的初始化函數(shù)(用STM32的串口發(fā)送AT指令給ESP8266)
//初始化ESP8266連接到阿里云函數(shù)
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");//加轉(zhuǎn)義字符
ESP_FeedBack(4);
Delay_ms(3000);
//5
Serial_String("AT+CWJAP=\"LAPTOP\",\"asdfghjkl\"\r\n");//加轉(zhuǎ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日志接收函數(shù)(STM32接收ESP8266發(fā)送回來的日志數(shù)據(jù),分析數(shù)據(jù)做出初始化反饋)
//查看錯(cuò)誤反饋函數(shù)
void ESP_FeedBack(uint8_t t)
{
Delay_ms(1000);
if( strstr((const char *)ESP8266_To_Serial,"ERROR") )
{
Refresh_DMA();//刷新DMA緩沖區(qū)以及計(jì)數(shù)器
memset(ESP8266_To_Serial,0,100);//將數(shù)組清零
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轉(zhuǎn)運(yùn)USART2_RX配置
ESP8266_Init();//ESP8266連接服務(wù)器
while(1)
{
i++;
//一次發(fā)送多個(gè)物模型數(shù)據(jù)
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);
//一次發(fā)送一個(gè)物模型數(shù)據(jù)
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);//延時(shí)10秒
}
}
請求數(shù)據(jù)格式要求:
?
發(fā)送數(shù)據(jù)時(shí)需要使用變量,若直接使用串口發(fā)送函數(shù),則不能存在變量,因此解決此問題需要重定義printf函數(shù)實(shí)現(xiàn)。步驟如下:
1、在keil中使用printf之前需要線設(shè)置一下
2、 打印函數(shù)
方法一:重定向printf
將printf打印的東西輸出到串口
重寫fputc()函數(shù),因?yàn)閜rintf函數(shù)使用時(shí)也是調(diào)用fputc函數(shù)一個(gè)個(gè)的打印。fputc函數(shù)發(fā)送到串口,printf函數(shù)自然也就打印輸出到了串口
重寫fputc函數(shù):
#include <stdio.h>
int fputc(int ch,FILE *f)
{
Serial_SendByte(ch);//這里調(diào)用自己寫的串口發(fā)送字節(jié)函數(shù)
return ch;
}
//主函數(shù)
void main(void)
{
printf("NUm=%d\n",666);
while(1)
{
}
}
方法二:用sprintf函數(shù)(此處是實(shí)現(xiàn)打印到串口的第二種方法)
sprintf函數(shù)不需要重定義,因?yàn)榇撕瘮?shù)第一個(gè)參數(shù)是指定打印位置
sprintf函數(shù)作用是把格式化字符輸出到一個(gè)字符串里
#include <stdio.h>
char string[100];//定義一個(gè)足夠大的緩存空間
sprintf(string,"NUM=5d\n",666);//輸出到string中
方法三:封裝sprintf函數(shù)(此方法用到了可變參數(shù)列表,記錄下來只是學(xué)習(xí)一下)
void Serial_printf(char *format,...)
{
char string[100];
va_list arg;//存放可變參數(shù)列表的變量
va_start(arg,format)//從format開始存放參數(shù)列表到arg中
vsprintf(string,format,arg);//打印格式 這里up解釋是sprintf只能接收直接寫的參數(shù)
va_end(arg);//釋放arg空間
Serial_SendByte(string);//調(diào)用自己寫的串口發(fā)送字節(jié)函數(shù)
}
keil修改一下配置文章來源:http://www.zghlxwxcb.cn/news/detail-425404.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-425404.html
到了這里,關(guān)于阿里云 MQTT協(xié)議 AT指令 ESP8266-01S 數(shù)據(jù)上下傳輸?shù)奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!