Skip to content

Instantly share code, notes, and snippets.

@nickbalch
Created June 26, 2021 17:12
Show Gist options
  • Save nickbalch/aca871a4e1a64a1d83f1bca818b9ea72 to your computer and use it in GitHub Desktop.
Save nickbalch/aca871a4e1a64a1d83f1bca818b9ea72 to your computer and use it in GitHub Desktop.
Homie esp8266 arduino sketch for MHZ-16
#include <Homie.h>
#include <SoftwareSerial.h>
/***************************************************
* Infrared CO2 Sensor 0-50000ppm(Wide Range)
* ****************************************************
* The follow example is used to detect CO2 concentration.
* @author lg.gang(lg.gang@qq.com)
* @version V1.0
* @date 2016-6-6
* GNU Lesser General Public License.
* See <http://www.gnu.org/licenses/> for details.
* All above must be included in any redistribution
* ****************************************************/
//Todo - Allow for warm up time of 3 minutes, recalibrate command?
SoftwareSerial mySerial(12, 13); // D6, D7 RX, TX
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; //Read the gas density command /Don't change the order
const int COLLECT_INTERVAL = 30;
unsigned long lastSent = 0;
const int PIN_LED = 13;
const int PIN_BUTTON = 0;
HomieNode airqNode("co2", "CO2", "co2");
void loopHandler() {
if (millis() - lastSent >= COLLECT_INTERVAL * 1000UL || lastSent == 0) {
Homie.getLogger() << "Collecting" << endl;
mySerial.write(hexdata,9);
Homie.getLogger() << "Collected" << endl;
delay(500);
for(int i=0,j=0;i<9;i++)
{
if (mySerial.available()>0)
{
Homie.getLogger() << "Data Received" << endl;
long hi,lo,CO2;
int ch=mySerial.read();
if(i==2){ hi=ch; } //High concentration
if(i==3){ lo=ch; } //Low concentration
if(i==8) {
CO2=hi*256+lo;
airqNode.setProperty("ppm").send(String(CO2));
Homie.getLogger() << String(CO2) << endl;
lastSent = millis();
}
}
}
}
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
mySerial.begin(9600);
Homie_setFirmware("wemos-mhz16", "0.0.1");
airqNode.advertise("CO2").setName("CO2")
.setDatatype("float")
.setUnit("ppm");
Homie.setLoopFunction(loopHandler);
Homie.setup();
}
void loop() {
Homie.loop();
}
@nickbalch
Copy link
Author

I find I have a lot of wifi issues with the homie framework, and now primarily use Tasmota for esp8266 devices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment