目前我正在使用由 Arduino uno 提供支持的 esp01,我正在使用 MQTT 协议
我尝试使用 AT 命令将数据发布到 thingspeak 云,所以我开始连接到 Wi-Fi 并首先发送连接帧,esp 以“SEND OK”响应,然后我发送发布帧,它以“SEND”响应好的”,但数据没有到达云端。
我在 http 之前尝试过,一切正常,但我不确定哪里出了问题,这可能是供电问题吗?
有人可以帮我找到问题
void Wifi_init(void){
//Set_Bit(SREG,SREG_I);
Enable_RXInterrupt();
UART_RX_SetCallBack(interrupt_func);
data_ready=0;
buff_lenght=0;
memset(receive_buffer,0,300); //reset buffer ,but 0 to all buffer elements
UART_SendString("AT+RST\r\n"); //reset wifi module
while (response_success("OK")!=OK);
_delay_ms(100);
UART_SendString("ATE0\r\n"); //disable echo
while (response_success("OK")!=OK);
_delay_ms(100);
UART_SendString("AT+CWMODE=3\r\n"); //Wifi mode 1--station,2--AP,3--station+AP
while (response_success("OK")!=OK);
_delay_ms(100);
UART_SendString("AT+CIPMUX=0\r\n"); //single connection mode
while (response_success("OK")!=OK);
_delay_ms(100);
UART_SendString("AT+CIPMODE=0\r\n"); //transfer mode 0--normal mode,1--wifi pass-through mode
while (response_success("OK")!=OK);
_delay_ms(100);
UART_SendString("AT+CWJAP=\"VodafoneMobileWiFi-3ABC36\",\"6996086485\"\r\n"); //connect to APs
while (response_success("OK")!=OK);
_delay_ms(100);
UART_SendString("AT+CIPSTART=\"TCP\",\"mqtt3.thingspeak.com\",1883\r\n"); //connect to the raspberry pi
while (response_success("OK")!=OK);
_delay_ms(1000);
}
}
void CONNECT (void){
uint8 remaining_length=16+23+23+24;
UART_SendString("AT+CIPSEND=88\r\n");
while (response_success("OK")!=OK);
_delay_ms(100);
UART_SendChar(0x10); //control field
UART_SendChar(0x56); //0x56 //remaining length
UART_SendChar(0x00); //protocol name length
UART_SendChar(0x04);
UART_SendString("MQTT");
UART_SendChar(0x04); //protocol level
UART_SendChar(0xC2); //connect flag (means the payload doesn't have a user name or a password and sessions are cleared)
UART_SendChar(0x00); //keep alive
UART_SendChar(0x3C);
UART_SendChar(0x00); // client id length
UART_SendChar(0x17);
UART_SendString("AQIJOwYtAzkqJSkQEQQ3Bxg"); //client id
UART_SendChar(0x00);
UART_SendChar(0x17);
UART_SendString("AQIJOwYtAzkqJSkQEQQ3Bxg");
UART_SendChar(0x00);
UART_SendChar(0x18);
UART_SendString("hSxegr0GjrvuxUHtzHmBeozg");
while (response_success("SEND OK")!=OK);
while (response_success("+IPD")!=OK);
}
void PUBLISH (uint8 * Topic,uint8 * data){
uint8 topic_bytes = strlen(Topic);
uint8 data_bytes = strlen(data);
uint8 frame_string [3];
uint8 send_command[20];
uint8 topic_length =string_length_hex(Topic);
uint8 data_length =string_length_hex(data);
UART_SendString("AT+CIPSEND=44\r\n"); //send the command
while (response_success("OK")!=OK);
_delay_ms(200);
///////CHECK THE RESPONSE
UART_SendChar(0x30); //protocol name length
UART_SendChar(0x2A); //44
UART_SendChar(0x00);
UART_SendChar(0x26);
UART_SendString((char *)Topic);
UART_SendString((char *)data);
while (response_success("SEND OK")!=OK);
_delay_ms(200);
}
int main(){
Set_Bit(SREG,SREG_I);
Dio_init();
USART_init();
Wifi_init();
CONNECT();
while(1){
PUBLISH("channels/1966346/publish/fields/field1","42");
_delay_ms(2000);
}
}
我记得我在使用 STM32 低层寄存器时遇到了同样的问题,我认为解决方案是使用
sprintf(buffer,"frame")
函数将我发送的数据放入缓冲区。
void PUBLISH(uint8_t* topic, uint8_t* data) {
uint8_t buffer[];
uint8_t topic_len = strlen(topic);
uint8_t data_len = strlen(data);
// MQTT message
sprintf(buffer, "AT+CIPSEND=%d\r\n", topic_len+data_len+1);
UART_SendString(buffer);
while (response_success("OK") != OK);
_delay_ms(200);
sprintf(buffer, "%c%c%s%s", 0x30, 0x02 + topic_len + data_len, topic, data);
UART_SendString(buffer);
while (response_success("SEND OK") != OK);
_delay_ms(200);
}
我认为您可能需要在发送时将长度添加 +1 或 +2
AT+CIPSEND=44+1\r\n