0

Currently I am working with esp01 powered from Arduino uno and I am using MQTT protocol

I tried the to publish data to thingspeak cloud using the AT commands so I start connecting to the Wi-Fi and send first the connect frame and esp respond with "SEND OK" and then I send the publish frame and it respond with "SEND OK" as well BUT data didn't reach the cloud.

I tried before the http and everythung works fine but i am not sure what is wrong could it be a powering issue?

is someone can help me to find the problem

this is me using docklight to send AT commands sending connect and publish frame

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);
    }
}
hardillb
  • 54,545
  • 11
  • 67
  • 105
  • 2
    Please do not post photos of the screen, post the actual text and use the toolbar to format as code. Images are really hard to read, impossible for users that need to use screen readers and you can't search for them. – hardillb Mar 01 '23 at 15:10
  • shouldn't there be some response from the server? it just closes the connection. – Juraj Mar 01 '23 at 15:52
  • @hardillb sure I will post the actual text – Nada Essam Mar 02 '23 at 08:40
  • @Juraj I am not sure if there should be a response or not but I will edit the question and add photos of the frames that i send – Nada Essam Mar 02 '23 at 08:56
  • 1
    Good on you for waiting for a final result code before continuing with the next command! However `OK` is not the only possible final result code, and you should handle all of them (see [this answer](https://stackoverflow.com/a/33266839/23118) for example links). Also the command line termination should be [only `\r`, not `\r\n`](https://stackoverflow.com/a/67186801/23118). – hlovdal Mar 02 '23 at 18:26
  • Is MQTT data correctly formatted? If not, maybe, the cloud server is simply discarding it.. – Roberto Caboni Mar 03 '23 at 05:49
  • @hlovdal I tried to terminate the commands with just \r but nothing changed :(, for the responses I was looking in the espressif AT commands related to the esp and the only responses mentioned is (OK ,ERORR ,SEMD OK). – Nada Essam Mar 03 '23 at 11:29
  • @RobertoCaboni I checked the format multiple times and tried to change some bytes but nothing changed :( – Nada Essam Mar 03 '23 at 11:33

1 Answers1

0

I remember I faced the same problem while working with STM32 low layer register and i think the solution was to put my sent data in a buffer using sprintf(buffer,"frame") function.

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);
}

I think that you might need to add +1 or +2 to the length while sending AT+CIPSEND=44+1\r\n

med amine
  • 33
  • 9