trying to convert my programs over for a mesh network but ive stumbled across a problem. It seems everything works fine if comment out display.display(); from my loop. otherwise the program crashes on startup? any advice?
#include "painlessMesh.h"
// WiFi Credentials
#define MESH_PREFIX "MNW"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(OLED_RESET);
#include <FS.h> //Include File System Headers
Scheduler userScheduler;
painlessMesh mesh;
void sendMessage() ;
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
int addr = 0;
unsigned long now = 0;
bool uartReady = 0;
bool pwmReady = 1;
bool calibrate = 0;
unsigned long CO2 = 0;
int calibration = 200; //subtract from co2 ppm reading
//int pwmPIN = 13;
void ICACHE_RAM_ATTR PwmPinISR();
void ICACHE_RAM_ATTR getPwmReading();
volatile unsigned long pwmPulseDuration;
volatile boolean pwmPulseCaptured = false;
volatile boolean errorFlag = false;
const byte pwmPin = 13;//connect pwm input to D7 GPIO 13
unsigned long th, tl, pwmPPM = 0;
unsigned long now2 = 0;
unsigned long now3 = 0;
unsigned long pwmMIN = 50000;
unsigned long pwmMAX = -10;
unsigned long ch = 0;
SoftwareSerial mySerial(12, 14); // RX, TX
unsigned char hexdata[10] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; //Read the gas density command /Don't change the order
unsigned char hexcal[10] = {0xFF, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78};
struct {
unsigned long pwmc02ppm = 0;
unsigned long uartc02ppm = 0;
unsigned long SecondsOn = 0;
} _packet;
void sendMessage() {
DynamicJsonDocument doc(2024);
doc["ID"] = "C02NODE1";
doc["pwmc02ppm"] = _packet.pwmc02ppm;
doc["uartc02ppm"] = _packet.uartc02ppm;
doc["SecondsOn"] = _packet.SecondsOn;
String msg ;
serializeJson(doc, msg);
mesh.sendBroadcast( msg );
Serial.println(msg);
taskSendMessage.setInterval((TASK_SECOND * 1));
}
void newConnectionCallback(uint32_t nodeId) {
Serial.printf("--> startHere: 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 PwmPinISR()
{
if (pwmPulseCaptured == false)//ok to read new pulse
{
static unsigned long startTime;
if (digitalRead(pwmPin)) // Gone HIGH
startTime = micros();
else // Gone LOW
{
pwmPulseDuration = micros() - startTime;
pwmPulseCaptured = true;
if (pwmPulseDuration > 1100000ul)//greater than max time 1004 ms
errorFlag = true;
}
}
}
void getPwmReading()
{
static unsigned long th, tl = 0;
if (errorFlag)
{
Serial.println("PWM read error");
errorFlag = false;
}
else
{
th = pwmPulseDuration / 1000;
Serial.print("pwm pulse length high ms = ");
Serial.println(th);
tl = 1004 - th;
pwmPPM = 50000UL * (th - 2) / (th + tl - 4);
Serial.print("PWM PPM=");
Serial.println(pwmPPM);
_packet.pwmc02ppm = pwmPPM;
}
pwmPulseCaptured = false;//reset for next capture
}
void receivedCallback( uint32_t from, String &msg ) {
String json;
DynamicJsonDocument doc(1024);
json = msg.c_str();
DeserializationError error = deserializeJson(doc, json);
if (error)
{
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
}
}
void setup() {
Serial.begin(115200);
SPIFFS.begin();
Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
pinMode(12, INPUT);
attachInterrupt(digitalPinToInterrupt(pwmPin), PwmPinISR, CHANGE);
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);
userScheduler.addTask( taskSendMessage );
taskSendMessage.enable();
Serial.println("booted");
while (!Serial) {}
mySerial.begin(9600);
}
void loop() {
if ( millis() - now >= 1000) {
_packet.SecondsOn++;
now = millis();
mySerial.write(hexdata, 9);
// delay(500);
for (int i = 0, j = 0; i < 9; i++) {
if (mySerial.available() > 0) {
long hi, lo;
ch = mySerial.read();
if (i == 2) {
hi = ch; //High concentration
}
if (i == 3) {
lo = ch; //Low concentration
}
if (i == 8) {
CO2 = hi * 256 + lo; //CO2 concentration
Serial.print("UART PPM=");
Serial.println(CO2);
_packet.uartc02ppm = CO2;
}
}
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("UART-PPM=");
display.println(CO2);
display.print("PWM-PPM=");
display.println(pwmPPM);
display.print("Time=");
display.println(_packet.SecondsOn);
display.display(); ///comment this and program works
}
if (millis() - now3 >= 2000) {
now3 = millis();
if (pwmPulseCaptured)//this could be put on a periodic timer
{
getPwmReading();
}
}
mesh.update();
}