debug.println prints to dev0

Hello!

I´m working on a sketch for weeks now (still not working as intended). A Problem i´m facing all the time: I can´t see all debug messages.

First things first, the sketch:

#include <SoftwareSerial.h>
int sensor_temp = A0;
int value_temp;
int sensor_light = A1;
int value_light;
int sensor_humid = A2;
int value_humid;


#define DEBUG FALSE //comment out to remove debug msgs

//*-- Hardware Serial
#define _baudrate 9600

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

//*-- IoT Information
#define SSID "blupp?"
#define PASS "12345678810"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149

// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&field2=[data 2]...;
String GET = "GET /update?key=XYZ4NCC3FTS26PVTH";

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

  sendDebug("AT");
  delay(5000);
  if(Serial.find("OK"))
  {
    debug.println("RECEIVED: OK\nData ready to sent!");
    connectWiFi();
  }

}

void loop() {
  value_temp = analogRead(sensor_temp);
  value_light = analogRead(sensor_light);
  value_humid = analogRead(sensor_humid);
  String temp =String(value_temp);// turn integer to string
  String light= String(value_light);// turn integer to string
  String humid=String(value_humid);// turn integer to string
  updateTS(temp,light, humid);
  delay(3000); //
}
//----- update the  Thingspeak string with 3 values
void updateTS( String T, String L , String H)
{
  // ESP8266 Client
  String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
  cmd += IP;
  cmd += "\",80";
  sendDebug(cmd);
  delay(2000);
  if( Serial.find( "ERROR" ) )
  {
    debug.print( "RECEIVED: Error\nExit1" );
    return;
  }

  cmd = GET + "&field1=" + T +"&field2="+ L + "&field3=" + H +"\r\n";
  Serial.print( "AT+CIPSENDEX=" );
  Serial.println( cmd.length() );
  if(Serial.find( ">" ) )
  {
    debug.print(">");
    debug.print(cmd);
    Serial.print(cmd);
  }
  else
  {
    sendDebug( "AT+CIPCLOSE" );//close TCP connection
  }
  if( Serial.find("OK") )
  {
    debug.println( "RECEIVED: OK" );
  }
  else
  {
    debug.println( "RECEIVED: Error\nExit2" );
  }
}

void sendDebug(String cmd)
{
  debug.print("SEND: ");
  debug.println(cmd);
  Serial.println(cmd);
}

boolean connectWiFi()
{
  Serial.println("AT+CWQAP");//WiFi STA mode - if '3' it is both client and AP
  delay(5000);
  Serial.println("AT+CWMODE_CUR=1");//WiFi STA mode - if '3' it is both client and AP
  delay(2000);
  //Connect to Router with AT+CWJAP="SSID","Password";
  // Check if connected with AT+CWJAP?
  String cmd="AT+CWJAP_CUR=\""; // Join accespoint
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  sendDebug(cmd);
  delay(5000);
  if(Serial.find("OK"))
  {
    debug.println("RECEIVED: OK");
    return true;
  }
  else
  {
    debug.println("RECEIVED: Error");
    return false;
  }

  cmd = "AT+CIPMUX=0";// Set Single connection
  sendDebug( cmd );
  if( Serial.find( "ERROR") )
  {
    debug.print( "RECEIVED: Error" );
    return false;
  }
}

So, I´ve connected the ESP on Arduino UNO (Hardware RX->TX; TX->RX; COM3) and an FTDI1232 (Software Serial RX->TX; TX->RX; COM4).

Output on COM3 (Serial Monitor):

AT
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSENDEX=67
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSENDEX=67
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSENDEX=67
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80

And so on.

Output on COM4 (Serial Monitor):

Exit2
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
RECEIVED: Error
Exit2
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
RECEIVED: Error
Exit2
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
RECEIVED: Error

And so on.

So, my question: I´m missing some serial output, where is it?

For example:

debug.println("RECEIVED: OK\nData ready to sent!");

Or why can´t i see the result of

sendDebug(cmd);

while seeing

sendDebug("AT");

on serial monitor?

I think i´m missing something fundamental.

Any help? Thanks in forward!

Greetings,

MCNoobESP

PS: And of course, any hints, why this Sketch doesn´t work, are welcome too :).

As with all of these ESP8266 problems, have you tried sending AT commands to the module and reading its replies manually?

Take the Arduino Uno out of the equation and communicate with the ESP8266 directly?

First thing weird is that in your debug you don't see

AT+CWJAP_CUR="SSID","PWD"

Did you remove it or it is never printed?

If it is not printed then all bets are off, you are likely not reaching the network....

Assuming you removed it from the debug log, and wifi connection is established

imagine you don't receive the '>' sign after sending

Serial.print( "AT+CIPSENDEX=" );
  Serial.println( cmd.length() );
  if(Serial.find( ">" ) )
....

Then all the prints you see make total sense because your ESP is probably in a weird state, may expecting the n bytes of the GET command you wanted to send and thus

if( Serial.find("OK") )

Will probably fail and you'll see

RECEIVED: Error
Exit2

Then you loop again and same happens

Question you need to ask yourself is why you did not see the '>' sign after CIPSENDEX?

(Have you tried as Well with AT+CIPSENDBUF instead?)

And best for this as suggested by ieee488 is to first try sending those commands manually to your ESP and see exactly what happens.

J-M-L:
And best for this as suggested by ieee488 is to first try sending those commands manually to your ESP and see exactly what happens.

I have found with my own frustrating experience with the ESP8266 that communicating directly with the ESP8266 using a Terminal program is the only way to go.

It was a major pain in the butt.

ieee488:
As with all of these ESP8266 problems, have you tried sending AT commands to the module and reading its replies manually?

Take the Arduino Uno out of the equation and communicate with the ESP8266 directly?

Yupp, did that. FTDI --> ESP, tried every single command, everything works (connection, sending data etc.).

J-M-L:
AT+CWJAP_CUR="SSID","PWD"
Did you remove it or it is never printed?

Ha! Nope, you´re right, i can´t see that output too! But how to explain, that i can connect manually but not in that sketch?

Will try AT+CIPSENDBUF later at home.

ieee488:
It was a major pain in the butt.

I can sign that.

MCNoobESP:
Will try AT+CIPSENDBUF later at home.

As I recall, I did not have to send this.

I think the problem is already before AT+CWJAP_CUR.

Take a look, changed Sketch a bit:

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

  sendDebug("AT");
  delay(5000);
  if(Serial.find("OK"))
  {
    debug.println("RECEIVED: OK\nData ready to sent!");
    connectWiFi();
  }
  else
  {
    debug.println("Error at first AT Command");
  }

}

Result on COM4 (Debug):

SEND: AT
Error at first AT Command
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
RECEIVED: Error
Exit2
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
RECEIVED: Error
Exit2

Conclusion: Not a single AT-Command is reaching ESP.

After some try and error:

Changed Baudrate from 9600 to 115200.

Result:

SEND: AT
RECEIVED: OK
Data ready to sent!
SEND: AT+CWJAP_CUR="blupp?","12345678810"
RECEIVED: OK
SEND: AT+CIPSTART="TCP","184.106.153.149",80

GET /update?key=XYZ4NCC3FTS26PVTH&field1=403&field2=378&field3=375
RECEIVED: OK
SEND: AT+CIPSTART="TCP","184.106.153.149",80
GET /update?key=XYZ4NCC3FTS26PVTH&field1=403&field2=378&field3=375
RECEIVED: OK
SEND: AT+CIPSTART="TCP","184.106.153.149",80
GET /update?key=XYZ4NCC3FTS26PVTH&field1=403&field2=378&field3=375
RECEIVED: OK
[...]
SEND: AT+CIPSTART="TCP","184.106.153.149",80
GET /update?key=XYZ4NCC3FTS26PVTH&field1=403&field2=378&field3=375
RECEIVED: OK
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
RECEIVED: Error
Exit2
SEND: AT+CIPSTART="TCP","184.106.153.149",80
SEND: AT+CIPCLOSE
RECEIVED: Error
Exit2
SEND: AT+CIPSTART="TCP","184.106.153.149",80
RECEIVED: Error
Exit1SEND: AT+CIPSTART="TCP","184.106.153.149",80
RECEIVED: Error
Exit1SEND: AT+CIPSTART="TCP","184.106.153.149",80
RECEIVED: Error

I thought i did it! But why is he loosing connection again? Sigh...

Any help is welcome...

I found that at 115200 bauds things do not work with software serial and ESP

I got things very stable at half that (57600) but because i was not memory bound, I modified the Software Serial Library and doubled the buffer size (with Software serial you only have 64 bytes of buffering).

suggestion: set you ESP <-> Arduino connexion to as slow as possibly acceptable for your needs and empty buffers as soon as possible.

Is there ANY way to view all threads i´ve created? It was a lot of work to find this one again.

Last working Sketch (for all those looking for a "working" solution):

#include <SoftwareSerial.h>
#include "DHT.h"

#define DEBUG FALSE //comment out to remove debug msgs

#define _ledpin     13

//*-- Hardware Serial
#define _baudrate 115200

//*-- 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 "Keks?"
#define PASS "3366344639210936"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149

// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&field2=[data 2]...;
String GET = "GET /update?key=VWA0N1C17S26PVTH";

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

  sendDebug("AT");
  delay(5000);
  if(Serial.find("OK"))
  {
    debug.println("RECEIVED: OK\nData ready to sent!");
    connectWiFi();
  }
  else
  {
    debug.println("Error at first AT Command");
  }

    // DHT11
    dht11.begin();

    pinMode( _ledpin, OUTPUT );
    digitalWrite( _ledpin, LOW );
}

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

    // Check if Data is empty
    if( isnan(dhtbuf[0]) || isnan(dhtbuf[1]) )
    {
        debug.println( "Failed to read form DHT11" );
    }
    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 )
{
    // Client
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += IP;
    cmd += "\",80";
    sendDebug(cmd);
    delay(2000);
    if( Serial.find( "ERROR" ) )
    {
        debug.print( "RECEIVED: Error\nExit1" );
        return;
    }

    cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
    Serial.print( "AT+CIPSENDEX=" );
    Serial.println( cmd.length() );
    
    if(Serial.find( ">" ) )
    {
        debug.print(">");
        debug.print(cmd);
        Serial.print(cmd);
    }
    else
    {
        debug.print(cmd);
        sendDebug( "AT+CIPCLOSE" );
    }
    if( Serial.find("OK") )
    {
        debug.println( "RECEIVED: OK" );
    }
    else
    {
        debug.println( "RECEIVED: Error\nExit2" );
    }
}

void sendDebug(String cmd)
{
    debug.print("SEND: ");
    debug.println(cmd);
    Serial.println(cmd);
} 
 
boolean connectWiFi()
{
  Serial.println("AT+CWQAP"); //Quit Connection
  delay(10000);
  Serial.println("AT+CWMODE_CUR=1");//WiFi STA mode - if '3' it is both client and AP
  delay(2000);
  //Connect to Router with AT+CWJAP="SSID","Password";
  // Check if connected with AT+CWJAP?
  String cmd="AT+CWJAP_CUR=\""; // Join accespoint
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  sendDebug(cmd);
  delay(5000);
  if(Serial.find("OK"))
  {
    debug.println("RECEIVED: OK");
    return true;
  }
  else
  {
    debug.println("RECEIVED: No Connection est.");
    return false;
  }

    cmd = "AT+CIPMUX=0";
    sendDebug( cmd );
    if( Serial.find( "ERROR") )
    {
        debug.print( "RECEIVED: Error" );
        return false;
    }
}

Worked with Uno+ESP-01+DHT-11.

After that i´ve tried Nano+ESP and Arduino IDE+ESP only. Didn´t work at all.

My personal summary: ESP8266 is cheap. And a pain in the ass.

Greetings

MCNoobESP:
Is there ANY way to view all threads i´ve created? It was a lot of work to find this one again.

Type your username in the search input field at the top of the page.

And yes, the forum search is quite primitive.

Is there ANY way to view all threads i´ve created? It was a lot of work to find this one again.

Click on your pseudo name in any of your post - that takes you to your profile and on the left side you can see a link to all your posts

J-M-L:
Click on your pseudo name in any of your post - that takes you to your profile and on the left side you can see a link to all your posts

Yeah, but than one must first find one of his / her posts that one can't find. So running around in circles.