[SOLVED] Serial monitor prints garbage when I set up input and output pins

Hello,

I’m using an ESP8266 board to play with mesh networking. I’m using an example from a tutorial, and for some reason, when I add code designed to add functionality, my serial monitor fails and prints garbage when i reset the 8266 board.

Here’s my functioning code:

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-mesh-esp32-esp8266-painlessmesh/
  
  This is a simple example that uses the painlessMesh library: https://github.com/gmag11/painlessMesh/blob/master/examples/basic/basic.ino
*/

#include "painlessMesh.h"

#define   MESH_PREFIX     "whateverYouLike"
#define   MESH_PASSWORD   "somethingSneaky"
#define   MESH_PORT       5555

// Declare size of input output arrays and declare arrays
#define ARRSIZE 1
// set pin numbers of LED 1 and 2 and input
int outpinlist[] = { 1 }; 
// Array of Input Pin Numbers (D5, D6 and D7 acted weird, stayed low once pulled low i.e. pressed)
int inpinlist[] = { D2 };
// Array to receive input values from inputs
int valArr[ARRSIZE];


Scheduler userScheduler; // to control your personal task
painlessMesh  mesh;

// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain

Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );

void sendMessage() {
  String msg = "Hi from node1";
  msg += mesh.getNodeId();
  mesh.sendBroadcast( msg );
  taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
}

// Needed for painless library
void receivedCallback( uint32_t from, String &msg ) {
  Serial.printf("Received from %u msg = %s\n", from, msg.c_str());
  // toggle light on / off when message recieved. 

}

void newConnectionCallback(uint32_t nodeId) {
    Serial.printf("New Connection, nodeId = %u\n", nodeId);
}

void changedConnectionCallback() {
  Serial.printf("Changed connections\n");
}

void nodeTimeAdjustedCallback(int32_t offset) {
    Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}

void setup() {
  Serial.begin(74880);


//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages

  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);

  taskSendMessage.enable();
}

void loop() {
  // it will run the user scheduler as well
  mesh.update();

  // if input pin is high, set task to send 'light on message' to other units
  // userScheduler.addTask( taskSendMessage );
}

I connect the module to my computer, upload my code and set the baud rate on my serial monitor to match my code (I’ve tried a range of values from 4800 to 115200 - they all work so long as the values match between my code the serial monitor dropdown menu).

When other ESP8266 Modules join the mesh network, I get this output on the serial monitor as I expect:

16:28:00.822 -> setLogLevel: ERROR | STARTUP |
16:28:00.822 -> STARTUP: init(): 0
16:28:00.822 -> STARTUP: AP tcp server established on port 5555
16:28:30.720 -> Changed connections
16:28:30.720 -> New Connection, nodeId = 3811911176
16:28:30.720 -> Adjusted time 4299033. Offset = -25693434
16:28:30.954 -> Adjusted time 4513322. Offset = -4308
16:28:32.001 -> Received from 3811911176 msg = Hi from node13811911176
16:28:35.770 -> Received from 3811911176 msg = Hi from node13811911176
16:28:38.756 -> Received from 3811911176 msg = Hi from node13811911176
16:28:43.084 -> Received from 3811911176 msg = Hi from node13811911176
16:28:44.334 -> Received from 3811911176 msg = Hi from node13811911176

So now I add some code to set up some input and output pins. (I’m building towards sending messages according to the inputs and responding to messages received by toggling the outputs).

    // Set up input / output
  for (int i = 0; i < ARRSIZE; i++) {
    // set output pins as outputs
    pinMode(outpinlist[i], OUTPUT);
    // set input pins as inputs - INPUT_PULLUP enabled the internal Pullup resistor so will read HIGH until pulled low
    // https://tttapa.github.io/ESP8266/Chap04%20-%20Microcontroller.html
    pinMode(inpinlist[i], INPUT_PULLUP);
    // pull up by default - we'll ground this with our push to make button
    // digitalWrite(inpinlist[i], HIGH);
  }

This code goes in my setup() function, so the overall code reads:

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-mesh-esp32-esp8266-painlessmesh/
  
  This is a simple example that uses the painlessMesh library: https://github.com/gmag11/painlessMesh/blob/master/examples/basic/basic.ino
*/

#include "painlessMesh.h"

#define   MESH_PREFIX     "whateverYouLike"
#define   MESH_PASSWORD   "somethingSneaky"
#define   MESH_PORT       5555

// Declare size of input output arrays and declare arrays
#define ARRSIZE 1
// set pin numbers of LED 1 and 2 and input
int outpinlist[] = { 1 }; 
// Array of Input Pin Numbers (D5, D6 and D7 acted weird, stayed low once pulled low i.e. pressed)
int inpinlist[] = { D2 };
// Array to receive input values from inputs
int valArr[ARRSIZE];


Scheduler userScheduler; // to control your personal task
painlessMesh  mesh;

// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain

Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );

void sendMessage() {
  String msg = "Hi from node1";
  msg += mesh.getNodeId();
  mesh.sendBroadcast( msg );
  taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
}

// Needed for painless library
void receivedCallback( uint32_t from, String &msg ) {
  Serial.printf("Received from %u msg = %s\n", from, msg.c_str());
  // toggle light on / off when message recieved. 

}

void newConnectionCallback(uint32_t nodeId) {
    Serial.printf("New Connection, nodeId = %u\n", nodeId);
}

void changedConnectionCallback() {
  Serial.printf("Changed connections\n");
}

void nodeTimeAdjustedCallback(int32_t offset) {
    Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}

void setup() {
  
  Serial.begin(74880);

//
//
// NEW CODE BELOW HERE
//
//

  // Set up input / output
  for (int i = 0; i < ARRSIZE; i++) {
    // set output pins as outputs
    pinMode(outpinlist[i], OUTPUT);
    // set input pins as inputs - INPUT_PULLUP enabled the internal Pullup resistor so will read HIGH until pulled low
    // https://tttapa.github.io/ESP8266/Chap04%20-%20Microcontroller.html
    pinMode(inpinlist[i], INPUT_PULLUP);
    // pull up by default - we'll ground this with our push to make button
    // digitalWrite(inpinlist[i], HIGH);
  }

//
//
// END NEW CODE
//
//

//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages

  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  
  taskSendMessage.enable();
  
}

void loop() {
  // it will run the user scheduler as well
  mesh.update();

  // if input pin is high, set task to send 'light on message' to other units
  // userScheduler.addTask( taskSendMessage );
}

Once the for loop has been executed, no further output is sent to the serial monitor.

If I move the for loop so that the mesh.init command runs before the loop, then I see the messages from the mesh.init command, before the serial monitor stops.

If I add additional code to toggle my LED output within the receivedCallback function, the LED I’ve attached flashes, so I know that the code itself continues to function.

Can anyone see why my code is causing the serial monitor to fail?

Adrian

@prawnballnightmare

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

The ESP8266 defaults to its prefered baud during reset and sends out diagnostic messages to the serial console. Your code changes that baud with the serial.begin(xxxx); statement. Prior to your statement you console is set to ?? look on the bottom, it will tell you, if that does not match the ESP8266 default baud you get the garbage after reset and until your serial.begin statement is executed. How are you powering the ESP8266? Consider posting your schematic, not a frizzy drawing. The ESP8266 requires more power on transmit.

Thanks for the reply, I can see the diagnostic messages if I change my console baud rate to 74880 baud.

Interesting you mention power, I'd not considered this. I'm just using the usb connection for power, connected to a powered hub. At the moment it's literally just a ESP8266 and a usb cable to the computer.

It occurred to me that I have the ability to dig into this by commenting out lines from this code block in the setup() function.

My pinout array was as follows:

int outpinlist[] = { 1, 2 };

By trial and error, it is the presence of this line in the snippet below that disables the serial monitor output:

pinMode(1, OUTPUT);

Full snippet with functioning serial monitor:

Serial.begin(74880);

    // Set up input / output
  for (int i = 0; i < ARRSIZE; i++) {
    // set output pins as outputs
    //pinMode(outpinlist[i], OUTPUT);
    //pinMode(1, OUTPUT);
    pinMode(2, OUTPUT);
    // set input pins as inputs - INPUT_PULLUP enabled the internal Pullup resistor so will read HIGH until pulled low
    // https://tttapa.github.io/ESP8266/Chap04%20-%20Microcontroller.html
    pinMode(inpinlist[i], INPUT_PULLUP);
    // pull up by default - we'll ground this with our push to make button
    digitalWrite(inpinlist[i], HIGH);
  }

GPIO1 on my pinout diagram is the TX pin. For some reason, using this as an output pin causes issues with the serial output.

If I switch my output array to something like this, so that the TX pin isn’t set as an output, the issue is resolved:

int outpinlist[] = { D5, 2 };

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.