Trying to adapt ESP8266WiFi example WiFiTelnetToSerial - problem on serial

I am trying to create a transparent serial to WiFi bridge for use on a data collection system.
So I started with the WiFiTelnetToSerial example available inside Arduino (menu File/Examples/ESP8266WiFi/)
But when I am debugging this I have found that when the ESP8266 powers on there is some unintelligible data being sent out the serial port before my sketch starts sending debug data.

I have put this on top of the ino file in order to be able to send debug messages to the normal serial port:

#define DEBUGPORTS //Set this to use Serial as the debug port
  #define SerialDebug Serial1   // Debug goes out on GPIO02
  #define SerialSS    Serial    // Device connected to the ESP UART
#else  //For debugging inside Arduino
  #define SerialDebug Serial   // Debug goes out on ESP UART
  #define SerialSS    Serial1    // Device connected to GPIO02

So the Termite serial monitor (on Serial) should show what I print to the SerialDebug port.
This is what I have in the setrup() code:

void setup() {
  String MsgDbg = "";  //Used for debug messages
  EEPROM.begin(512); //Initialize EEPROM to load from flash and write changes later on
  SerialDebug.println(""); // To get new line before printing debug messages
  MsgDbg = "Starting TCP <-> UART bridge"; SerialDebug.println(MsgDbg);
.. then more diagnostic code

So the very first thing that happens after initializing the serial port is writing "Starting TCP <-> UART bridge"
But when I load the sketch and start it I do not see what I expect, instead of just my starting message I see a lot of stuff before it:

Starting TCP <-> UART bridge

I am using the Termite serial monitor because it encloses non-displayable characters in brackets and shows them as hex code. So as far as I can tell the ESP8266 sends some kind of strange data out even before I gets into the setup() function!
I could count to these non-displayable characters with some data in between:

4 * 0x00  (NULL)
9 * 0x02  (STX?)
24 * 0x0C (FF?)
1 * 0x10  (DLE?)
6 * 0x12  (DC2?)
1 * 0x1C  (FS?)

What can cause this and can it be switched off?
I cannot have the ESP8266 spew out these whenever it starts up because it will confuse the data collection equipment...

I think that is coming from the bootloader. I believe it's at 74880 baud.

this is from esp8266 with 40MHz crystal. it prints the boot log at 115200 baud

'ready' is printed from AT firmware. everything before that is bootlog. perhaps som parts are Chinese :slight_smile:

esp8266 with 26MHz crystal prints the bootlog at 74880 baud and with 20MHz oscillator at 57600 baud

This is what my WeMos D1 Mini (which has the 26 MHz crystal) outputs at 74880 baud after I press the reset button:

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d

I haven't done anything with ESP8266 for over a year now but I remember there being some other stuff it prints at 115200 during a crash. It was always annoying to me that I got some gibberish no matter which baud rate I used. Maybe it would have all been at 115200 with a 40 MHz crystal.

So I probably need to add a gate with a delay to block all data for the initial say 1s then?
Not so good but doable on the TTL side of the RS232 converter, I guess. We need anyway to redesign the board for the new WiFi module.
Or maybe instead of a timer use a GPIO output which is activated inside the setup() function. When one gets there the extra output stuff should have been gone...
Note that I don't have the ESP AT firmware on it anymore, just the serial server sketch.

My output was from an ESP826 without AT firmware. It only has the Arduino bare minimum sketch uploaded to it.

there is a trick. the TX pin of Serial can be swapped to TX1 (io 2).

you connect the device to RX and TX1 and in setup of your sketch you call Serial.swap(). the boot log will go to TX0 and your data to TX1

I am not clear as to when in the startup of the ESP-07 the execution reaches something that can be affected by user code. I believed it to be in the setup() function, bit it seems like the bogus data have already been transmitted when it reaches here.
So how could I execute a swap function in advance of setup()?
Or do you mean that the bogus data actually get sent at the instant I call:


That would make it even more mysterious to me, because where will the data get buffered in such a case waiting for begin()?
OTOH, a solution not needing to add a gate to the PCB would make it much more testable now before the revised PCB even exists...

nothing will be connected to TX0 where the boot log prints. your data will go to TX1 after you swap TX. and your device will be connected to TX1.

So I tried to check this by moving my Tx connection on the breadboard and adding Serial.swap() before Serial.begin115200).
But meanwhile I had added a struct typedef which throws the strangest error I have seen...
The code:

typedef struct {
 unsigned int chksum;
 char[32] ssid;
 char[32] passwd;
 unsigned char[4] addr; //I tried with byte here but got the same error
 unsigned long baud ;
 unsigned int tcpport;
} ESPConfiguration;

When I build my sketch with this present I get this the error message:

TCP-UART_Bridge:63: error: expected unqualified-id before '[' token
   unsigned char[4] addr;

I had tried byte too but with exactly the same error.
I have Googled this but found no hit looking like this....

How about this instead:

typedef struct {
 unsigned int chksum;
 char ssid[32];
 char passwd[32];
 unsigned char addr[4]; //I tried with byte here but got the same error
 unsigned long baud ;
 unsigned int tcpport;
} ESPConfiguration;

Well I want to separate the definitions and usage in h vs c files (and ino).
Now I have put this into the espconfig.h file:

typedef struct 
  unsigned int chksum;
  char[32] ssid;
  char[32] passwd;
  byte[4] addr;
  long baud;
  unsigned int tcpport;
} ESPConfiguration; //76 bytes

The h file is included both in the main ino file and in the espconfig.c file where the struct variable will be used for processing the WiFi configuration. But the variable itself is declared in the ino file so it will become global.
I have now chnaged to byte[4] again, but I find the errors appearing anyway:

Arduino: 1.8.2 (Windows 7), Board: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, DIO, 4M (1M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200"

In file included from sketch\espconfig.c:5:0:
sketch\espconfig.h:19:7: error: expected identifier or '(' before '[' token
   char[32] ssid;
sketch\espconfig.h:20:7: error: expected identifier or '(' before '[' token
   char[32] passwd;
sketch\espconfig.h:21:3: error: expected ';' before 'byte'
   byte[4] addr;
exit status 1

Somehow it really looks like Arduino does not like 8 bit data (char or byte)...
But I thought these were really basic items when programming.

How about this instead:

typedef struct {

unsigned int chksum;
char ssid[32];
char passwd[32];
unsigned char addr[4]; //I tried with byte here but got the same error
unsigned long baud ;
unsigned int tcpport;
} ESPConfiguration;

Sorry, I did not see your changes to the placement of []....
I'm switching languages too frequently.
Need some sleep. It is 1 AM here now.

But now the strange error is no longer reported, instead it errors out in a few assignments from String to the struct members ssid and passwd. I have to dig up how to assign between text variables tomorrow.

I have this in the ino file:

ESPConfiguration ESPConf;
String APPASSWD = "Superxx01";
void setup(){
ESPConf.passwd = APPASSWD.c_str();

And the last assignment gives an error.