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