Electrical – Unable to send data to Thingspeak using ESP8266 and STM32 Nucleo

esp8266mbednucleostm32

I am using ARM's mbed compiler to program my STM32 Nucleo. I am also using an ESP8266 as my WiFi module.

My connections are as such:

  • Rx of the ESP8266 is connected to D8 on the Nucleo.
  • Tx of the ESP8266 is connected to D2 on the Nucleo.
  • Gnd of the ESP8266 to the gnd on the board.
    All other pins of the ESP8266 are connected to 3.3V on the Nucleo.

The following is my mbed code:

#include "mbed.h"  

Serial esp(D8, D2);  
DigitalOut myled(LED1);

void flush(void) {  
  while (esp.readable()) {  
    (void)esp.getc();  
  }    
}  

int main() {  
  esp.baud (115200);
  char server[]="GET /update?key=NR************2Z&field1=7";
  flush();  

  esp.printf("AT+RST\r\n");
  wait(2);  

  esp.printf("AT+CWMODE=1\r\n");  
  wait(3);  

  esp.printf("AT+CWJAP=\"Harsha\",\"*****\"\r\n"); 
  wait(3);  

  esp.printf("AT+CIPMUX=1\r\n");
  wait(3); 

  esp.printf("AT+CIPSTART=4,\"TCP\",\"184.106.153.149\",80\r\n");  
  wait(3);

  esp.printf("AT+CIPSEND=4,%d\r\n", sizeof(server)+2);  
  wait(2);

  esp.printf("%s\r\n", server); 
  wait(3);

  esp.printf("AT+CIPCLOSE\r\n"); 

  while(1) {  
    myled = 1;
    wait(0.1);

    myled = 0;
    wait(0.1);
   } 

  return 0;  
}

I am able to connect to my WiFi hotspot which means there is no problem with the connection. But the data I pass is not updated in Thingspeak at all. I have also tried it including HTTP/1.0 and HTTP/1.1 in the 'server' string, still nothing. Am I missing something?

Best Answer

I solved it by increasing the delay for establishing the Wifi connection after the reset. For those who might find this helpful, the working code is as below:

#include "mbed.h"  

Serial esp(D8, D2);  
DigitalOut myled(LED1);

void flush(void) {  
    while (esp.readable()) {  
        (void)esp.getc();  
    }    
}

char server[]="GET /update?api_key=9FL*********C2&field2=";

int main() {  
    int x=7;
    esp.baud(115200);
    flush();  

    esp.printf("AT+RST\r\n"); /* reset module */  
    wait(2);  
    flush();  

    esp.printf("AT+CWMODE=3\r\n");  
    wait(1);  
    flush();  

    // The huge delay was key to obtaining the IP address, so that further commands don't interfere with the ongoing process
    esp.printf("AT+CWJAP=\"Harsha\",\"*******\"\r\n"); /* configure as access point */  
    wait(20);  
    flush();  

    esp.printf("AT+CIPMUX=1\r\n");
    wait(5);
    flush();  
    //response();
    esp.printf("AT+CIPSTART=0,\"TCP\",\"api.thingspeak.com\",80\r\n");  
    wait(5);
    flush();  
    //response();

    esp.printf("AT+CIPSEND=0,%d\r\n", sizeof(server)+15);  
    wait(3);
    flush();  
    //response();

    esp.printf("%s", server);
    esp.printf("%d", x);
    esp.printf(" HTTP/1.0\r\n\r\n\r\n\r\n\r\n");
    wait(2);
    flush(); 

    while(1) {  
        //To indicate completion
        myled = 1;
        wait(0.1);
        myled = 0;
        wait(0.1);
    }   
    return 0;  
}