Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created November 24, 2016 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/66fb68ce94a02e60852436802cf1f0f9 to your computer and use it in GitHub Desktop.
Save jenschr/66fb68ce94a02e60852436802cf1f0f9 to your computer and use it in GitHub Desktop.
LoRa Feather M0 with extra serial (CO2 sensor on Serial2 + DHT11)
#include <Arduino.h> // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTTYPE DHT11
#define DHTPIN 5
int ppm = 0;
int ppmLast = 0;
// To request a new reading:
// FF=startbyte, 01=devicenum, 86=command, 00=5xnull bytes, 79=checksum
// byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
// Commands are: 86=gas concentration, 87=Calibrate zero, 88=Calibrate span
const byte MHZ19_CMD_READ_CO2[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
const byte MHZ19_CMD_CALIBRATE_ZERO[9] = {0xFF,0x01,0x87,0x00,0x00,0x00,0x00,0x00,0x78};
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
DHT_Unified dht(DHTPIN, DHTTYPE);
void SERCOM1_Handler()
{
Serial2.IrqHandler();
}
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
dht.begin();
// Assign pins 10 & 11 SERCOM functionality
pinPeripheral(10, PIO_SERCOM);
pinPeripheral(11, PIO_SERCOM);
pinMode(13, OUTPUT);
digitalWrite( 13, HIGH );
delay(1000);
digitalWrite( 13, LOW );
delay(500);
readCO2(1);
delay(1000);
}
void loop() {
ppm = readCO2(0);
if( ppmLast != ppm ){
Serial.print("PPM: ");
Serial.print(ppm);
Serial.print(" : ");
readDHT();
}
ppmLast = ppm;
delay(100);
}
void readDHT()
{
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
}
else {
Serial.print(event.temperature);
Serial.print(" *C ");
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else {
Serial.print(": ");
Serial.print(event.relative_humidity);
Serial.println("%");
}
}
// from: https://github.com/jehy/arduino-esp8266-mh-z19-serial/blob/master/arduino-esp8266-mhz-19-serial.ino
int readCO2( int calibrate )
{
// command to ask for data
char response[9]; // for answer
if( calibrate == 1)
{
Serial2.write(MHZ19_CMD_CALIBRATE_ZERO, 9); //request PPM CO2 calibration
Serial.println("Calibrating!");
}
else
{
Serial2.write(MHZ19_CMD_READ_CO2, 9); //request PPM CO2
}
Serial2.readBytes(response, 9);
if (response[0] != 0xFF)
{
Serial.println("Wrong starting byte from co2 sensor!");
return -1;
}
if (response[1] != 0x86)
{
Serial.println("Wrong command from co2 sensor!");
return -1;
}
// Return value:
// FF=startbyte, 86=command, 02=high byte, 60=low byte, 00=4xnull bytes, 79=checksum
// byte cmd[9] = {0xFF, 0x86, 0x02, 0x60, 0x47, 0x00, 0x00, 0x00, 0xD1};
// Gas concentration: (high byte*256)+low byte
int responseHigh = (int) response[2];
int responseLow = (int) response[3];
int ppm = (256 * responseHigh) + responseLow;
return ppm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment