Created
February 6, 2016 12:52
-
-
Save yoggy/d9431e0c74a3deb0fb9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// mqtt_mhz19_pub.ino - test program for esp-wroom-02 & mh-z19 | |
// | |
// ESP-WROOM-02 development board | |
// https://www.switch-science.com/catalog/2500/ | |
// | |
// MH-Z19 | |
// http://www.winsen-sensor.com/products/ndir-co2-sensor/mh-z19.html | |
// | |
// License: | |
// Copyright (c) 2016 yoggy <[email protected]> | |
// Released under the MIT license | |
// http://opensource.org/licenses/mit-license.php; | |
// | |
#include <Wire.h> | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> // https://github.com/knolleary/pubsubclient/ | |
#include <SoftwareSerial.h> // https://github.com/plerup/espsoftwareserial | |
char *wifi_ssid = "wifi ssid"; | |
char *wifi_password = "wifi password"; | |
char *mqtt_server = "mqtt.example.com"; | |
int mqtt_port = 1883; | |
char *mqtt_username = "username"; | |
char *mqtt_password = "password"; | |
char *mqtt_topic = "topic"; | |
SoftwareSerial sws = SoftwareSerial(4, 5); // RX, TX | |
byte mhz19_buf[9]; | |
WiFiClient wifi_client; | |
PubSubClient mqtt_client(mqtt_server, mqtt_port, NULL, wifi_client); | |
void setup() { | |
Serial.begin(9600); | |
sws.begin(9600); | |
WiFi.begin(wifi_ssid, wifi_password); | |
int wifi_count = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
if (wifi_count % 2 == 0) Serial.println("WIFI conn... "); | |
else Serial.println("WIFI conn.. "); | |
wifi_count ++; | |
delay(500); | |
} | |
Serial.println("MQTT conn... "); | |
delay(1000); | |
if (mqtt_client.connect("client_id", mqtt_username, mqtt_password) == false) { | |
Serial.println("conn failed... "); | |
delay(3000); | |
reboot(); | |
} | |
Serial.println("connected! "); | |
delay(500); | |
} | |
void loop() { | |
// for MQTT | |
if (!mqtt_client.connected()) { | |
reboot(); | |
} | |
mqtt_client.loop(); | |
// read co2 value from mh-z19 | |
int co2 = read_data_from_mh_z19(); | |
char buf[256]; | |
sprintf(buf, "{\"co2\":%d, \"tick\":%d}", co2, millis()); | |
mqtt_client.publish(mqtt_topic, buf); | |
if (co2 < 0) { | |
Serial.println("read failed..."); | |
} | |
else { | |
Serial.print("co2 = "); | |
Serial.print(co2); | |
Serial.println(" ppm"); | |
} | |
delay(1000); | |
} | |
int read_data_from_mh_z19() { | |
for (int i = 0; i < 10; ++i) { | |
// send command | |
sws.write((uint8_t)0xff); | |
sws.write((uint8_t)0x01); | |
sws.write((uint8_t)0x86); | |
sws.write((uint8_t)0x00); | |
sws.write((uint8_t)0x00); | |
sws.write((uint8_t)0x00); | |
sws.write((uint8_t)0x00); | |
sws.write((uint8_t)0x00); | |
sws.write((uint8_t)0x79); | |
sws.flush(); | |
// read | |
memset(mhz19_buf, 0, 9); | |
sws.readBytes(mhz19_buf, 9); | |
// parse | |
if (mhz19_buf[0] == 0xff && mhz19_buf[1] == 0x86) { | |
int co2 = mhz19_buf[2] * 256 + mhz19_buf[3]; | |
return co2; | |
} | |
delay(100); | |
} | |
return -1; | |
} | |
// ESP8266 special | |
void reboot() { | |
Serial.println("reboot..."); | |
delay(1000); | |
ESP.restart(); | |
while (true) {}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment