-->
Page 1 of 1

ESP8266 Nodemcu getting stuck

PostPosted: Fri Sep 24, 2021 1:06 am
by kp.puvvala
Hi, I have a setup with 3 i2c sensors and 2 analog sensors(via analog mux) connected to a ESP8266. I am powering everything using a DC power supply of +5V. I managed to get the sensor readings for few mins and after that the esp8266 is getting stuck until I unpowered and powered back the esp8266. and then again working for sometime. I am not using any pull-up resistors. Please help me with this issue. The code is below

Code: Select all//#include "TRIGGER_WIFI.h"               /*Includes ESP8266WiFi.h and WiFiClientSecure.h, just have these two libraries downloaded before*/
//#include "TRIGGER_GOOGLESHEETS.h"       /*Library file for Google Sheets, has to be used after Wi-Fi Client Secure declaration, here everything is in Trigger_WIFI.h, so using it after Trigger_WIFI.h*/


////----------------------------------Thinger.io config-------------------------------------------------------------------
#define DEBUG
#include <ThingerESP8266.h>
#define USERNAME "**********************"
#define DEVICE_ID "********************"
#define DEVICE_CREDENTIAL "*********************"
#define SSID "*********************"
#define SSID_PASSWORD "*********************"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);


//---------------------------------------firebase-----------------
#include <FirebaseESP8266.h>  // Install Firebase ESP8266 library
#define FIREBASE_HOST "****************************************************"
#define FIREBASE_AUTH "************************************************"

FirebaseData firebaseData;


int cnt = 3600;




//-------------------------------------------Multichannel Gas Sensor----------------------

#include <Wire.h>
#include "MutichannelGasSensor.h"
float array10[8];

//--------------------------------------------------------------------------- Co2 sensor -------------------------------------------------------

#include "SparkFun_SCD30_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_SCD30
SCD30 airSensor;
float array11[3];

// ------------------------------------------------Gas flow sensor------------------------------------

//int MSB,LSB,total;
float total;
byte MSB, LSB;
float array12[1];

//------------------------------------------------ Inlet Transducer Pressure----------------------------

float sensorType1 = 5.0;  // in Bar
const int offset1 = 102;   // Zero pressure adjust
const int fullScale1 = 922;    //Max pressure adjust
float pressure1_bar;

float array13[1];

// ------------------------------------------------ Outlet Transducer Pressure----------------------------

float sensorType2 = 5.0;  // in Bar
const int offset2 = 102;   // Zero pressure adjust
const int fullScale2 = 922;    //Max pressure adjust
float pressure2_bar;

float array14[1];


//------------------------------------------------MQ8 sensor------------------------------------

float MQ8sensorValue;
float array16[2];


//--------------------------------------Mux control pins-----------------------------
int s0 = 16;//D0
int s1 = 2;//D4
int s2 = 14;//D5
int s3 = 0;//D3
//EN connected with the ground
//Mux in "SIG" pin
float SIG_pin = A0;
/////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000);


  Serial.println("power on!");
  gas.begin(0x04);//the default I2C address of the slave is 0x04
  gas.powerOn();
  Serial.print("Firmware Version = ");
  Serial.println(gas.getVersion());

  delay(100);
  Wire.beginTransmission(0x07);
  Wire.write(0);
  Wire.endTransmission();


  //----------------------------------------------------------------multiplexer pins-----------------------------------------

  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);


  //////////////////////////////////////////////////////////////////////////////////////////



}

////////////////////////////////////////////////////////////////////////////////////

void loop() {

  //Thinger.io call--------------------------------------
  thing.handle();
  //--------------------------------------
  Serial.println(cnt);

  if (cnt == 0) {
    Serial.println("Reset..");
    ESP.restart();
  }

  cnt--;
  delay(1000);
  /////////////////////////////////////////////////////////////////////////

  MGS(array10); // call MGS function
  Co2(array11); // call Co2 function
  flow(array12); // call flow function


  //-------------------------------------------------Analog Multiplexer-----------------------------------
  //Loop through and read 2 values
  for (int i = 0; i < 16; i ++) {
    Serial.print("Value at channel(");
    Serial.print(i);
    Serial.print(")is : ");
    Serial.println(readMux(i));
    delay(10);

    // IMPORTANT, for each If loop I should make the nodemcu read the analoge pin again

    if (i == 0) {
      //call function for Inlet Transducer sensor
      transducerInlet(array13);
    }

    else if (i == 1) {
      //call function for Outlet Transducer sensor
      transducerOutlet(array14);
    }

    //    else if (i == 2) {
    //      //call function for Water Flow sensor
    //      waterFlow(array15);
    //    }

    else if (i == 3) {
      //call function for MQ8 sensor
      MQ8(array16);
    }
  }




  delay(500);

  //////////////////////////////////////////////////////////////////////////////

  //  Data_to_Sheets(No_of_Parameters,  array10[0], array10[1], array11[0], array12[0], array13[0], array14[0], array14[1], array14[2]);

  //  --------------------------firebase-----------------------------------------------
  ////////////////////////////////////// Firebase data//////////////////////
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);   // connect to firebase
  Firebase.reconnectWiFi(true);

  ///////////// Variable 1 - Multichannel NH3 Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/NH3", array10[0])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("NH3 = ");
    Serial.println(array10[0]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 2 - Multichannel NO2 Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/No2", array10[1])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("No2 = ");
    Serial.println(array10[1]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 3 - Multichannel CO Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/CO", array10[2])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("CO = ");
    Serial.println(array10[2]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 4 - Multichannel C3H8 Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/C3H8", array10[3])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("C3H8 = ");
    Serial.println(array10[3]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 5 - Multichannel C4H10 Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/C4H10", array10[4])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("C4H10 = ");
    Serial.println(array10[4]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 6 - Multichannel CH4 Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/CH4", array10[5])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("CH4 = ");
    Serial.println(array10[5]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 7 - Multichannel H2 Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/H2", array10[6])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("H2 = ");
    Serial.println(array10[6]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

   ///////////// Variable 8 - Multichannel C2H5OH Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/C2H5OH", array10[7])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("C2H5OH = ");
    Serial.println(array10[7]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  /////////////Variable 9 - Five CO2 Sensor//////////////


  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/Co2", array11[0])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("Co2 = ");
    Serial.println(array11[0]);
    Serial.println("\n");

    //val++;

    //  delay(4000);
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////////Variable 10 - Co2 Temperature Sensor//////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/Co2Temp", airSensor.getTemperature())) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("Co2 temp = ");
    Serial.println(airSensor.getTemperature());
    Serial.println("\n");

    //val++;
    //  delay(4000);

  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////////Variable 11 - Co2 Humidity Sensor//////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/Co2Hum", airSensor.getHumidity())) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("Co2 humidity = ");
    Serial.println(airSensor.getHumidity());
    Serial.println("\n");

    //val++;


  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 12 - Gas Flow Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/GasFlow", array12[0])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("flow = ");
    Serial.println(array12[0]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 13 - Transducer Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/InletTransducer", array13[0])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("bars = ");
    Serial.println(array13[0]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

  ///////////// Variable 14 - Transducer Sensor //////////////

  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/OutletTransducer", array14[0])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("bars = ");
    Serial.println(array14[0]);
    Serial.println("\n");

    //val++;
  }

  else {
    Serial.println(firebaseData.errorReason());
  }



  ///////////////Variable 17 - MQ8 H2 sensorvalue//////////////


  if (Firebase.setFloat(firebaseData, "MiddleNode/Array/Data1/MQ8Sensorvalue", array16[0])) {    // On successful Write operation, function returns 1
    Serial.println("Value Uploaded Successfully");
    Serial.print("sensorvalue = ");
    Serial.println(array16[0]);
    Serial.println("\n");

    //val++;

    //  delay(4000);
  }

  else {
    Serial.println(firebaseData.errorReason());
  }

}
//----------------------------------------Analog Mux---------------------------------------------------
int readMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int j = 0; j < 4; j ++) {
    digitalWrite(controlPin[j], muxChannel[channel][j]);
  }

  //read the value at the SIG pin
  float valm = analogRead(SIG_pin);

  //return the value
  return valm;

}


//----------------------------Multichannel Gas Sensor function-----------------------

void * MGS(float array10[8]) {
  float c;
  //  float cl;
  c = gas.measure_NH3();
  Serial.print("The concentration of NH3 is ");
  if (c >= 0) Serial.print(c);
  else Serial.print("invalid");
  Serial.println(" ppm");
  array10[0] = c;

  c = gas.measure_NO2();
  Serial.print("The concentration of NO2 is ");
  if (c >= 0) Serial.print(c);
  else Serial.print("invalid");
  Serial.println(" ppm");
  array10[1] = c;

  c = gas.measure_CO();
    Serial.print("The concentration of CO is ");
    if(c>=0) Serial.print(c);
    else Serial.print("invalid");
    Serial.println(" ppm");
      array10[2] = c;


  c = gas.measure_C3H8();
  Serial.print("The concentration of C3H8 is ");
  if (c >= 0) Serial.print(c);
  else Serial.print("invalid");
  Serial.println(" ppm");
    array10[3] = c;


  c = gas.measure_C4H10();
  Serial.print("The concentration of C4H10 is ");
  if (c >= 0) Serial.print(c);
  else Serial.print("invalid");
  Serial.println(" ppm");
    array10[4] = c;


  c = gas.measure_CH4();
  Serial.print("The concentration of CH4 is ");
  if (c >= 0) Serial.print(c);
  else Serial.print("invalid");
  Serial.println(" ppm");
    array10[5] = c;


  c = gas.measure_H2();
  Serial.print("The concentration of H2 is ");
  if (c >= 0) Serial.print(c);
  else Serial.print("invalid");
  Serial.println(" ppm");
    array10[6] = c;


  c = gas.measure_C2H5OH();
  Serial.print("The concentration of C2H5OH is ");
  if (c >= 0) Serial.print(c);
  else Serial.print("invalid");
  Serial.println(" ppm");
    array10[7] = c;

}

//-----------------------------------------Co2 sensor function------------------------------------

void * Co2 (float array11[3]) {

  Serial.println("SCD30");

  if (airSensor.begin(0x61) == false)
  {
    Serial.println("Air sensor not detected. Please check wiring. Freezing...");
  }
  if (airSensor.dataAvailable())
  {
    Serial.print("Co2(ppm):");
    Serial.print(airSensor.getCO2());

    Serial.print(" temp(C):");
    Serial.print(airSensor.getTemperature(), 1);

    Serial.print(" humidity(%):");
    Serial.print(airSensor.getHumidity(), 1);

    Serial.println();
  }
  else
    Serial.println("Waiting for new data");
  array11[0] = airSensor.getCO2() ;
  // delay(15000);
}


////-----------------------------------Gas flow function---------------------------------------

void * flow(float array12[1])
{
  Wire.beginTransmission(0x07);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x07, 2);
  if (Wire.available() == 2) {
    MSB = Wire.read();
    LSB = Wire.read();
  }
  total = (MSB << 8) + LSB;
  total = total / 1000;
  Serial.print("total=");
  Serial.println(total);
  Serial.println(" L/m");
  array12[0] = total;

}

//----------------------------------------- Inlet Transducer Function-------------------------------

void * transducerInlet (float array13[1]) {


  pressure1_bar = (analogRead(SIG_pin) - offset1) * sensorType1 / (fullScale1 - offset1);   // equation
  Serial.print("Pressure 1 = ");
  Serial.print(pressure1_bar);
  Serial.println("Bar");

  array13[0] = pressure1_bar;


}

//----------------------------------------- Outlet Transducer Function-------------------------------

void * transducerOutlet (float array14[1]) {

  pressure2_bar = (analogRead(SIG_pin) - offset2) * sensorType2 / (fullScale2 - offset2);   // equation
  Serial.print("Pressure 2 = ");
  Serial.print(pressure2_bar);
  Serial.println("Bar");

  array14[0] = pressure2_bar;

}


//---------------------------------------MQ8 function-------------------------------


void * MQ8(float array16[2])
{
  MQ8sensorValue = analogRead(SIG_pin);
  Serial.print("H2 Concentration:");
  float ppm = MQGetGasPercentage(MQRead(MQ_PIN) / Ro, GAS_H2);
  Serial.print(ppm );
  Serial.print( "ppm" );
  Serial.print("\n");
  array16[0] = MQ8sensorValue;
  array16[1] = ppm;

}