Skip to content

ESP8266 Wireless "pm open,type:2 0" Problem... #3134

@cbrum11

Description

@cbrum11

INFO

Using a CO2 sensor with an ESP8266 via I2C. Transmitting data over wifi to a raspberry pi access point via MQTT Mosquitto broker. Works great for anywhere between 3 to 9 readings but then I get an error (or what I assume is an error)...

"pm open,type:2 0"

I assume this is the error because the readings always stop on the next set after this bugger shows up.
I have no idea what it means, so any suggestions would be GREATLY appreciated.

Below is both the code and debug output. Originally I removed all MQTT and Wireless parts from the code to narrow down the problem. When all of this was removed the readings were perfect for hours. Then I removed only the MQTT parts and left the wireless. BOOM... readings stop after a few iterations... the problem is somewhere in the wireless, not with MQTT. In addition to the pm open error, there also seem to be some weird reconnects (from debug) happening when the connection is first made. Are these supposed to occur?

DEBUG

The Greaux Engineering CO2 System is Active...
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 
state: 5 -> 2 (2c0)
rm 0
Initializing the Sensor...
reconnect
state: 2 -> 0 (0)
f r0, scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 

connected with greauxeng, channel 7
dhcp client start...
ip:192.168.0.11,mask:255.255.255.0,gw:192.168.0.1
Sensor Initialized

Connecting to greauxeng
state: 5 -> 0 (0)
rm 0
del if0
usl
mode : null
mode : sta(5c:cf:7f:10:4e:5a)
add if0
f r0, ....scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 
state: 5 -> 2 (2c0)
rm 0
..reconnect
state: 2 -> 0 (0)
f r0, scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 

connected with greauxeng, channel 7
dhcp client start...
.ip:192.168.0.11,mask:255.255.255.0,gw:192.168.0.1
.
WiFi connected
IP address: 
192.168.0.11
Measuring CO2:...
603
604
598
pm open,type:2 0
The differences between values are
1.00
6.00
5.00
CO2 Reading is ACCURATE...
CO2 is IN RANGE
598
Measuring CO2:...
0
0
0
Publish message: CO2 Value is Reading 0: SENSOR MALFUNCTION
Measuring CO2:...
0
0
0
Publish message: CO2 Value is Reading 0: SENSOR MALFUNCTION
Measuring CO2:...
0

CODE

#include <Wire.h>
#include <ESP8266WiFi.h>         //enable ESP8266 Wifi Library

/////////////////////////////////////////////
/////// Wifi Variables //////////////////////
/////////////////////////////////////////////
const char* ssid = "greauxeng";        //Internet Network Name
const char* password = "thankful4242";    //Wifi Password
WiFiClient espClient;           //Create Wifi client named espClient
char c[8];
long lastMsg = 0;
char msg[10];
int value = 0;
/////////////////////////////////////////////
// CO2 VARIABLES ////////////////////////////
/////////////////////////////////////////////
float min_diff = 50;
int CO2min = 200;       //Below which CO2 is considered LOW
int CO2max = 800;       //Above which CO2 is considered HIGH
int SolenoidPin = D8;
int SensorPin = D7;
int BuzzerPin = D5;
long previousMillis = 0;
long interval = 7000; //time to keep solenoid open

int diff1;    //Stores the difference between sequential readings
int diff2;
int diff3;

int CO2_1;    //Stores each CO2 reading
int CO2_2;
int CO2_3;


/////////////////////////////////////////////
// Wifi-Setup Function //////////////////////
/////////////////////////////////////////////

void setup_wifi() {

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.persistent(false); //These 3 lines are a required work around
  WiFi.mode(WIFI_OFF);    //otherwise the module will not reconnect
  WiFi.mode(WIFI_STA);    //if it gets disconnected
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


//////////////////////////////////
//////////////////////////////////
// Read CO2 over I2C Function ////
//////////////////////////////////
//////////////////////////////////

int readCO2()
{
  int co2Addr = 0x68; //address of CO2 sensor
  int co2_value = 0;  //CO2 value stored inside this variable.

  //////////////////////////
  /* Begin Write Sequence */
  //////////////////////////
  Wire.beginTransmission(co2Addr);
  Wire.write(0x22);
  Wire.write(0x00);
  Wire.write(0x08);
  Wire.write(0x2A);

  Wire.endTransmission();

  /////////////////////////
  /* End Write Sequence. */
  /////////////////////////

  /*
    We wait 10ms for the sensor to process our command.
    The sensors's primary duties are to accurately
    measure CO2 values. Waiting 10ms will ensure the
    data is properly written to RAM

  */

  delay(100);

  /////////////////////////
  /* Begin Read Sequence */
  /////////////////////////

  /*
    Since we requested 2 bytes from the sensor we must
    read in 4 bytes. This includes the payload, checksum,
    and command status byte.

  */

  Wire.requestFrom(co2Addr, 4);

  byte i = 0;
  byte buffer[4] = {0, 0, 0, 0};

  /*
    Wire.available() is not nessessary. Implementation is obscure but we leave
    it in here for portability and to future proof our code
  */
  while (Wire.available())
  {
    buffer[i] = Wire.read();
    i++;
  }

  ///////////////////////
  /* End Read Sequence */
  ///////////////////////

  /*
    Using some bitwise manipulation we will shift our buffer
    into an integer for general consumption
  */

  co2_value = 0;
  co2_value |= buffer[1] & 0xFF;
  co2_value = co2_value << 8;
  co2_value |= buffer[2] & 0xFF;

  byte sum = 0; //Checksum Byte
  sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow

  if (sum == buffer[3])
  {
    return co2_value;
  }
  else
  {
    Serial.println("Failure!");
    /*
      Checksum failure can be due to a number of factors,
      fuzzy electrons, sensor busy, etc.
    */
    return 0;
  }
}


void setup()
{

  Serial.begin(9600);
  Serial.setDebugOutput(true);
  Serial.println("The Greaux Engineering CO2 System is Active...");
  pinMode(BuzzerPin, OUTPUT);
  digitalWrite(BuzzerPin, LOW);
  pinMode(SolenoidPin, OUTPUT);
  digitalWrite(SolenoidPin, LOW);
  Wire.begin();
  delay(1000);
  Serial.println("Initializing the Sensor...");
  pinMode (SensorPin, OUTPUT);
  digitalWrite(SensorPin, LOW);
  delay(2000);
  digitalWrite(SensorPin, HIGH);
  Serial.println("Sensor Initialized");
  delay(1000);
  setup_wifi();                         //run wifi setup function
}

void loop()
{
  Serial.println("Measuring CO2:...");

  /////////////////////////////////////////
  //Trying to achieve an accurate CO2 Reading
  ////////////////////////////////////////

  CO2_1 = readCO2();
  Serial.println(CO2_1);
  delay(4000);
  CO2_2 = readCO2();
  Serial.println(CO2_2);
  delay(4000);
  CO2_3 = readCO2();
  Serial.println(CO2_3);
  delay(4000);

  if ((CO2_1 == 0) || (CO2_2 == 0) || (CO2_3 == 0))
  {
    Serial.println("Publish message: CO2 Value is Reading 0: SENSOR MALFUNCTION");
  }
  else {

    diff1 = CO2_1 - CO2_2;
    diff2 = CO2_2 - CO2_3;
    diff3 = CO2_3 - CO2_1;

    //////////////////////////////////////////
    //If the difference between three readings
    //is less than what we set, we can assume
    //reading is accurate.  If not we skip back up
    //and take 3 more readings
    //////////////////////////////////////////

    Serial.println("The differences between values are");
    Serial.println(fabs(diff1));
    delay(500);
    Serial.println(fabs(diff2));
    delay(500);
    Serial.println(fabs(diff3));
    delay(500);

    if ((fabs(diff1) <= min_diff) && (fabs(diff2) <= min_diff) && (fabs(diff3) <= min_diff))
    {


      Serial.println("CO2 Reading is ACCURATE...");

      ///////////////////////////////////////////////////////
      //Once we have an accurate reading, if the
      //CO2 level is outside our acceptable range
      //we open the solenoid for a specified amount of time
      //The millis stuff keeps looping the code until
      //we reach our set time interval
      /////////////////////////////////////////////////////

      if (CO2_3 <= CO2min) {
        Serial.print("Publish message: CO2 Reading is LOW: ");
        Serial.println(CO2_3);
        Serial.println("RELEASING CO2");

        unsigned long currentMillis = millis();
        Serial.println(currentMillis);
        while (abs(currentMillis - millis()) < interval)
        {
          digitalWrite(SolenoidPin, HIGH);
        }
        delay(7000);
        Serial.println(millis());
        digitalWrite(SolenoidPin, LOW);
      }


      else if ((CO2_3 > CO2min) && (CO2_3 < CO2max)) {
        Serial.println("CO2 is IN RANGE");
        Serial.println(CO2_3);
        delay(1000);
      }


      else {
        Serial.println("CO2 is HIGH");
        Serial.println(CO2_3);
        tone(BuzzerPin, 2000, 10000);
        delay(1000);
      }
    }
    else
    {
      Serial.println("Reading is NOT ACCURATE. BEGINNING NEW READINGS...");
      Serial.println(CO2_3);
      delay(1000);
    }
  }
  delay(2000);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions