strange-v / MHZ19

Arduino library for MH-Z19/MH-Z19B CO2 sensor
GNU General Public License v3.0
57 stars 12 forks source link

error code:5 #12

Closed vignesh8458 closed 3 years ago

vignesh8458 commented 3 years ago

when i run hw_get_values.ino

i am getting error code:5

Please help

connection:

MH-Z19b | Esp8266

Vin | vin GND | GND Tx | Rx Rx | Tx

strange-v commented 3 years ago

Error code 5 represents communication timeout (MHZ19_RESULT_ERR_TIMEOUT).

So, you are using hardware serial to communicate with the sensor, what you are using for the debug? ESP8266 has only one HW serial.

vignesh8458 commented 3 years ago

i don't know, how to use debug?

vignesh8458 commented 3 years ago

I am new to IoT

what is the best way to get data from the MH-Z19?

strange-v commented 3 years ago

You've just said, that you got code: 5 (it is a debug info). Could you post the whole code here?

Use software serial for esp8266 or use esp32.

vignesh8458 commented 3 years ago

Code:

include

MHZ19 mhz(&Serial1);

void setup() { Serial.begin(115200); Serial.println(F("Starting..."));

Serial1.begin(9600); }

void loop() { MHZ19_RESULT response = mhz.retrieveData(); if (response == MHZ19_RESULT_OK) { Serial.print(F("CO2: ")); Serial.println(mhz.getCO2()); Serial.print(F("Temperature: ")); Serial.println(mhz.getTemperature()); Serial.print(F("Accuracy: ")); Serial.println(mhz.getAccuracy()); } else { Serial.print(F("Error, code: ")); Serial.println(response); }

delay(15000); }

vignesh8458 commented 3 years ago

could you please share the code for esp8266?

strange-v commented 3 years ago

Serial1 on esp8266 has only TX capabilities. Use sw_get_values.ino example.

vignesh8458 commented 3 years ago

Thanks

i will try and update to you.

vignesh8458 commented 3 years ago

I have tried in new Esp8266, I got this below error.

Arduino: 1.8.13 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

Executable segment sizes:

IROM : 237596 - code in flash (default or ICACHE_FLASH_ATTR)

IRAM : 28792 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)

DATA : 1248 ) - initialized variables (global, static) in RAM/HEAP

RODATA : 736 ) / 81920 - constants (global, static) in RAM/HEAP

BSS : 25328 ) - zeroed variables (global, static) in RAM/HEAP

Sketch uses 268372 bytes (25%) of program storage space. Maximum is 1044464 bytes.

Global variables use 27312 bytes (33%) of dynamic memory, leaving 54608 bytes for local variables. Maximum is 81920 bytes.

esptool.py v2.8

Serial port COM3

Connecting......................................____Traceback (most recent call last):

File "C:\Users\Vignesh\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py", line 65, in

esptool.main(cmdline)

File "C:/Users/Vignesh/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool\esptool.py", line 2890, in main

esp.connect(args.before)

File "C:/Users/Vignesh/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool\esptool.py", line 483, in connect

raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))

esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

_

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

vignesh8458 commented 3 years ago

I am using Esp8266 Amica nodemcu.

vignesh8458 commented 3 years ago

I have uploaded the sw_get_values.ino.

again I got error code: 5

connection:

MH-Z19b | Esp8266

Vin | Vin GND | GND Tx | Rx Rx | Tx

strange-v commented 3 years ago

Did you connect it like this?

MH-Z19b | Esp8266 Vin | Vin GND | GND Tx | 17 Rx | 16

vignesh8458 commented 3 years ago

I have connected like this:

MH-Z19b | Esp8266

Vin | Vin GND | GND Tx | Rx Rx | Tx

Could you please mark the connection on the below image(esp8266?

image

strange-v commented 3 years ago

Ok, replace in the code SoftwareSerial ss(17,16); with SoftwareSerial ss(5,4);

then connect MH-Z19b | Esp8266 Vin | Vin GND | GND Tx | GPIO5 Rx | GPIO4 and try.

With software serial, you can use almost any pins.

vignesh8458 commented 3 years ago

I think I am getting output,

But min co2 and Accuracy is 0? (i hope this is an issue)

And can i change the delay time 15000?

Output:

CO2: 862 Min CO2: 0 Temperature: 30 Accuracy: 0

vignesh8458 commented 3 years ago

Code:

include

include

SoftwareSerial ss(5,4); MHZ19 mhz(&ss);

void setup() { Serial.begin(115200); Serial.println(F("Starting..."));

ss.begin(9600); }

void loop() { MHZ19_RESULT response = mhz.retrieveData(); if (response == MHZ19_RESULT_OK) { Serial.print(F("CO2: ")); Serial.println(mhz.getCO2()); Serial.print(F("Min CO2: ")); Serial.println(mhz.getMinCO2()); Serial.print(F("Temperature: ")); Serial.println(mhz.getTemperature()); Serial.print(F("Accuracy: ")); Serial.println(mhz.getAccuracy()); } else { Serial.print(F("Error, code: ")); Serial.println(response); }

delay(15000); }

strange-v commented 3 years ago

Cool. Min CO2, accuracy and even temperature aren't described in the sensor's datasheet and were deducted by enthusiasts, so I wouldn't expect much here.

That code is only an example, you have to rewrite it to meet your needs.

vignesh8458 commented 3 years ago

I hope the CO2 output is the correct one?

strange-v commented 3 years ago

Yes, but you should know that this sensor performs auto-calibration by the lowest detected value once per 24 hours.

So I'd recommend opening a window for a couple of hours and allow it to do auto-calibration (leave connected to power for 24 hours), then disable auto-calibration.

vignesh8458 commented 3 years ago

how do I disable the auto-calibration?

strange-v commented 3 years ago

Add this line after ss.begin(9600); in the setup: mhz.setAutoCalibration(false);

vignesh8458 commented 3 years ago

thanks

what is your full name?

vignesh8458 commented 3 years ago

hi ,

Can I use other pins to getting sensor data, because I am using i2c display16*2?

code:

SoftwareSerial ss(5,4);

to

SoftwareSerial ss( );

strange-v commented 3 years ago

As I previously said

With software serial, you can use almost any pins.

vignesh8458 commented 3 years ago

When i add this line ( mhz.setAutoCalibration(false); )

I am getting a co2 level output is 3 ( co2: 3 )

Please help me with this.

Code:

include

include

include

include

include

include

include

define BLYNK_PRINT Serial // Comment this out to disable prints and save space

char auth[] = "FE6RisArcNKdGmfmCelk3pWsPRCtfnMs";

SoftwareSerial ss(5,4); MHZ19 mhz(&ss);

/ WiFi credentials / char ssid[] = "camera"; char pass[] = "falcon$1947";

SimpleTimer timer;

int data = 0; int LED = 12; int lcdColumns = 16; int lcdRows = 2; LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println(F("Starting...")); Blynk.begin(auth, ssid, pass); pinMode(LED, OUTPUT); timer.setInterval(1000L, getSendData); ss.begin(9600); mhz.setAutoCalibration(false);

}

void loop() {

MHZ19_RESULT response = mhz.retrieveData();
if (response == MHZ19_RESULT_OK)
Wire.begin(2,0);

// initialize LCD lcd.init(); // turn on LCD backlight
lcd.backlight(); lcd.print("CO2: "); lcd.println(mhz.getCO2());

if (1<5); { // put your main code here, to run repeatedly: Serial.print(F("CO2: ")); Serial.println(mhz.getCO2());

timer.run(); // Initiates SimpleTimer Blynk.run();

} if (response == MHZ19_RESULT_OK) if (mhz.getCO2()>800) {

digitalWrite(LED, HIGH); // turn the LED on delay(1000); // wait for a second tone(14, 780, 180); delay(1000); digitalWrite(LED, LOW); // turn the LED off //delay(1000); // wait for a second }

else { digitalWrite(LED, LOW); noTone(0); } delay(5000);

}

/***

strange-v commented 3 years ago
  1. Use a code tag
  2. Always post as a little example as possible, that reproduces the issue.

Are you saying that your code works okay without adding mhz.setAutoCalibration(false); and shows incorrect CO2 just after adding this line?

vignesh8458 commented 3 years ago

Yes,

Continuously I am getting CO2 output: 3

strange-v commented 3 years ago

Ok, I'll try to reproduce.

strange-v commented 3 years ago

Interesting, it works on my setup. According to the datasheet, the sensor shouldn't respond to the ABC logic on/off command, but maybe something was changed in the latest revisions. So try this:

ss.begin(9600);
mhz.setAutoCalibration(false);
// wait for the response and read it
delay(10);
while (ss.available() > 0)
{
  ss.read();
}
strange-v commented 3 years ago

Closed due to inactivity