-->
Page 1 of 1

Can you help me solve this error?

PostPosted: Mon Jan 12, 2015 10:15 am
by markswift
Dear all,

I've been playing with the ESP the last week (No prior coding experience at all, skills mainly consist of copy / paste, trying to figure things out as I go!)

I have the following code (kindly copied from an instructables project):

Code: Select all#include <SoftwareSerial.h>
#include <DHT.h>

#define DEBUG

#define _ledpin     13

//*-- Hardware Serial
#define _baudrate   9600

//*-- Software Serial
#define _rxpin      2
#define _txpin      3
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX

//*-- DHT11
#define _dhtpin     8
#define _dhttype    DHT11

DHT dht11( _dhtpin, _dhttype );
uint8_t dhtbuf[2];

//*-- IoT Information
#define SSID "SSID"
#define PASS "PASSWORD"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149

//*-- Build GET Command
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=MYKEYHERE";

void setup() {
    Serial.begin( _baudrate );
    debug.begin( _baudrate );

    sendDebug("AT");
    delay(5000);
    if(Serial.find("OK"))
    {
        debug.println("ESP8266 CONNECTED\nREADY TO SEND DATA");
        connectWiFi();
    }

    // Start DHT11 Sensors
    dht11.begin();
   
    pinMode( _ledpin, OUTPUT );
    digitalWrite( _ledpin, LOW );
}

void loop() {
    dhtbuf[0] = dht11.readHumidity();
    dhtbuf[1] = dht11.readTemperature();

    // DHT11 Section
    if( isnan(dhtbuf[0]) || isnan(dhtbuf[1]) )
    {
        debug.println( "FAILED TO READ FROM DHT SENSOR" );
    }
    else
    {
        digitalWrite( _ledpin, HIGH );
        char buf[3];
        String HH, TT;
        buf[0] = 0x30 + dhtbuf[1] / 10;
        buf[1] = 0x30 + dhtbuf[1] % 10;
        TT = (String(buf)).substring( 0, 2 );
        buf[0] = 0x30 + dhtbuf[0] / 10;
        buf[1] = 0x30 + dhtbuf[0] % 10;
        HH = (String(buf)).substring( 0, 2 );

        updateDHT11( TT, HH );
        #ifdef DEBUG
            debug.print("Humidity: ");
            debug.print( HH );
            debug.print(" %\t");
            debug.print("Temperature: ");
            debug.print( TT );
            debug.println(" *C\t");
        #endif
        digitalWrite( _ledpin, LOW );
    }

    delay(60000);   // 60 second
}

void updateDHT11( String T, String H )
{
    // ESP8266 Client
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += IP;
    cmd += "\",80";
    sendDebug(cmd);
    delay(2000);
    if( Serial.find( "Error" ) )
    {
        debug.print( "CANNOT CONNECT\nEXITING" );
        return;
    }

    cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
    Serial.print( "AT+CIPSEND=" );
    Serial.println( cmd.length() );
    if(Serial.find( ">" ) )
    {
        debug.print(">");
        debug.print(cmd);
        Serial.print(cmd);
    }
    else
    {
        sendDebug( "AT+CIPCLOSE" );
    }
    if( Serial.find("OK") )
    {
        debug.println( "DATA SENT" );
    }
    else
    {
        debug.println( "DATA NOT SENT\nEXITING" );
    }
}

void sendDebug(String cmd)
{
    debug.print("SEND: ");
    debug.println(cmd);
    Serial.println(cmd);
}
 
boolean connectWiFi()
{
    Serial.println("AT+CWMODE=1");
    delay(2000);
    String cmd="AT+CWJAP=\"";
    cmd+=SSID;
    cmd+="\",\"";
    cmd+=PASS;
    cmd+="\"";
    sendDebug(cmd);
    delay(5000);
    if(Serial.find("OK"))
    {
        debug.println("WIFI CONNECTED");
        return true;
    }
    else
    {
        debug.println("ERROR CONNECTING TO WIFI");
        return false;
    }

    cmd = "AT+CIPMUX=0";
    sendDebug( cmd );
    if( Serial.find( "Error") )
    {
        debug.print( "CANNOT SET SINGLE CONNECTION MODE" );
        return false;
    }
}


It seems that first time around the ESP fails to send the data... I receive the following in the debug terminal:

Code: Select allSEND: AT
ESP8266 CONNECTED
READY TO SEND DATA
SEND: AT+CWJAP="SSID","PASSWORD"
WIFI CONNECTED
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
DATA NOT SENT
EXITING
Humidity: 56 %.Temperature: 22 *C.


After the 1st loop it seems to be connected fine and continues along indefinitely with no more errors.

To try and figure out why I connected the ESP directly to my terminal and tried issuing the commands:

Code: Select allAT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=54
GET /update?key=MYKEYHERE&field1=23&field2=56


It worked fine without errors the first time around...

Can anyone help me understand why my program seems to have issues first time around? I've literally been trying to figure it out for a week!

I feel the grey hairs coming ;-)

Re: Can you help me solve this error?

PostPosted: Fri Mar 06, 2015 1:05 am
by swilson
Code: Select all// ESP8266 Client
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += IP;
    cmd += "\",80";
    sendDebug(cmd);
    delay(2000);


You might try adding Serial.println(cmd); in there so it will print with CR to the ESP the first time. Like this.

Code: Select all// ESP8266 Client
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += IP;
    cmd += "\",80";
    Serial.println(cmd);
    sendDebug(cmd);
    delay(2000);

Re: Can you help me solve this error?

PostPosted: Tue Mar 10, 2015 12:22 am
by ricg
if i understood your post correctly, your are saying it fails to send the first time, but works fine after that. This sounds so much like a timing problem. in other words your first send occurs too soon after configuring the esp, but by the time it sends again the esp is stable and ready to accept data. Try putting a large delay, ie.. 1s after the call to wifi connect and see if it that works.