LoRa32-CO2-Meter/LoRa32-CO2-Meter.ino

338 lines
8.6 KiB
Arduino
Raw Normal View History

2024-01-19 10:53:02 +01:00
#include "LoRaWan_APP.h"
#include <Adafruit_NeoPixel.h>
#include "HT_SSD1306Wire.h"
#include <SensirionI2cScd30.h>
#include <Wire.h>
extern SSD1306Wire display;
Adafruit_NeoPixel pixels(1, 45, NEO_RGB + NEO_KHZ400);
/* OTAA para*/
uint8_t devEui[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x06, 0x44, 0xF0 };
uint8_t appEui[] = { 0xa0, 0x57, 0x81, 0x00, 0x01, 0x12, 0xaa, 0xf4 };
uint8_t appKey[] = { 0x88, 0x88, 0x81, 0x88, 0x89, 0x88, 0x88, 0xa8, 0x88, 0x68, 0x88, 0x85, 0x88, 0x80, 0x66, 0x01 };
/* ABP para*/
uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };
uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };
uint32_t devAddr = ( uint32_t )0x007e6ae1;
/*LoraWan channelsmask, default channels 0-7*/
uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };
/*LoraWan region, select in arduino IDE tools*/
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
/*LoraWan Class, Class A and Class C are supported*/
DeviceClass_t loraWanClass = CLASS_C;
/*the application data transmission duty cycle. value in [ms].*/
uint32_t appTxDutyCycle = 60000;
/*OTAA or ABP*/
bool overTheAirActivation = true;
/*ADR enable*/
bool loraWanAdr = true;
/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = true;
/* Application port */
uint8_t appPort = 2;
/*!
* Number of trials to transmit the frame, if the LoRaMAC layer did not
* receive an acknowledgment. The MAC performs a datarate adaptation,
* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
* to the following table:
*
* Transmission nb | Data Rate
* ----------------|-----------
* 1 (first) | DR
* 2 | DR
* 3 | max(DR-1,0)
* 4 | max(DR-1,0)
* 5 | max(DR-2,0)
* 6 | max(DR-2,0)
* 7 | max(DR-3,0)
* 8 | max(DR-3,0)
*
* Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
* the datarate, in case the LoRaMAC layer did not receive an acknowledgment
*/
uint8_t confirmedNbTrials = 4;
2024-01-26 12:10:20 +01:00
typedef enum { off_e, red_e, green_e, yellow_e, blue_e, white_e } color_t;
2024-01-19 10:53:02 +01:00
static void led(color_t color) {
switch (color) {
case off_e:
pixels.setPixelColor(0, pixels.Color(0,0,0));
break;
case red_e:
pixels.setPixelColor(0, pixels.Color(50,0,0));
break;
case yellow_e:
pixels.setPixelColor(0, pixels.Color(50,50,0));
break;
case green_e:
pixels.setPixelColor(0, pixels.Color(0,50,0));
break;
case blue_e:
pixels.setPixelColor(0, pixels.Color(0,0,50));
break;
2024-01-26 12:10:20 +01:00
case white_e:
pixels.setPixelColor(0, pixels.Color(255,255,255));
break;
2024-01-19 10:53:02 +01:00
}
pixels.show();
}
static void printDisplay(float co2con, float temp, float hum, unsigned long restTime, bool calibrationRunning) {
display.clear();
char dispbuf[128];
sprintf(dispbuf, "%.0f ppm CO2", co2con);
display.drawString(1, 0, dispbuf);
sprintf(dispbuf, "%.0f °C", temp);
display.drawString(1, 17, dispbuf);
if (calibrationRunning) {
sprintf(dispbuf, "Calib. in %d s", restTime);
display.drawString(1, 34, dispbuf);
} else {
sprintf(dispbuf, "%.0f %%H", hum);
display.drawString(1, 34, dispbuf);
}
display.display();
}
SensirionI2cScd30 sensor;
static char errorMessage[128];
static int16_t error;
static int calibrationMode;
static unsigned long calibrationStartTime;
/* Prepares the payload of the frame */
static void prepareTxFrame( uint8_t port )
{
/*appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in "commissioning.h".
*appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.
*if enabled AT, don't modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.
*if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.
*for example, if use REGION_CN470,
*the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in "RegionCN470.h".
*/
2024-01-26 12:10:20 +01:00
led(white_e);
2024-01-19 10:53:02 +01:00
struct __attribute__((__packed__)) {
uint8_t status;
int32_t co2con;
int32_t temp;
int32_t hum;
2024-02-16 10:30:22 +01:00
int32_t bri;
2024-01-19 10:53:02 +01:00
} values;
float co2con = 0.0;
float temp = 0.0;
float hum = 0.0;
2024-02-16 10:30:22 +01:00
2024-01-19 10:53:02 +01:00
int16_t error = sensor.blockingReadMeasurementData(co2con, temp, hum);
if (error != NO_ERROR) {
values.status = 0;
values.co2con = 0;
values.temp = 0;
values.hum = 0;
} else {
values.status = 1;
values.co2con = co2con * 100;
values.temp = temp * 100;
values.hum = hum * 100;
}
2024-02-16 10:30:22 +01:00
values.bri = analogRead(6);
2024-01-19 10:53:02 +01:00
Serial.print("status: ");
Serial.print(values.status);
Serial.print("\t");
Serial.print("co2Concentration: ");
Serial.print(co2con);
Serial.print("\t");
Serial.print("temperature: ");
Serial.print(temp);
Serial.print("\t");
Serial.print("humidity: ");
Serial.print(hum);
2024-02-16 10:30:22 +01:00
Serial.print("\t");
Serial.print("brightness: ");
Serial.print(values.bri);
2024-01-19 10:53:02 +01:00
Serial.println();
appDataSize = sizeof(values);
memcpy(appData, &values, sizeof(values));
long waitTimeForCalibration = 0;
if (calibrationMode == LOW) {
waitTimeForCalibration = (60 * 10) - ((millis() - calibrationStartTime) / 1000);
if (waitTimeForCalibration <= 0) {
Serial.println("Execute calibration now");
error = sensor.forceRecalibration(400);
if (error != NO_ERROR) {
Serial.print("Error trying to execute forceRecalibration(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
}
calibrationMode = HIGH;
} else {
Serial.print("Waiting for calibration: ");
Serial.print(waitTimeForCalibration);
Serial.println();
}
}
2024-01-26 12:10:20 +01:00
#define TONECNTMAX 10
static int toneCnt = TONECNTMAX;
2024-01-19 10:53:02 +01:00
if (calibrationMode == HIGH) {
if (co2con < 1100) {
led(green_e);
2024-01-26 12:10:20 +01:00
toneCnt = TONECNTMAX;
2024-01-19 10:53:02 +01:00
} else if (co2con < 2000) {
led(yellow_e);
2024-01-26 12:10:20 +01:00
if (toneCnt >= TONECNTMAX) {
tone(7, 500, 250);
toneCnt = 0;
}
toneCnt++;
2024-01-19 10:53:02 +01:00
} else {
led(red_e);
2024-01-26 12:10:20 +01:00
tone(7, 1000, 250);
toneCnt = TONECNTMAX;
2024-01-19 10:53:02 +01:00
}
}
printDisplay(co2con, temp, hum, waitTimeForCalibration, (calibrationMode == LOW));
}
//if true, next uplink will add MOTE_MAC_DEVICE_TIME_REQ
void setup() {
Serial.begin(115200);
digitalWrite(Vext,LOW);
display.init();
display.setFont(ArialMT_Plain_16);
display.clear();
display.display();
display.drawString(1, 0, "Start up");
display.display();
pixels.begin();
pixels.setPixelColor(0, pixels.Color(0,0,0));
led(off_e);
pinMode(46, INPUT_PULLUP);
calibrationMode = digitalRead(46);
if (calibrationMode == LOW) {
calibrationStartTime = millis();
led(blue_e);
} else {
led(off_e);
}
2024-01-26 12:10:20 +01:00
pinMode(7, OUTPUT);
tone(7, 1000, 500);
2024-02-16 10:30:22 +01:00
pinMode(6, INPUT);
2024-01-19 10:53:02 +01:00
Wire1.begin(41, 42, 10000);
//sensor.begin(Wire, SCD30_I2C_ADDR_61);
sensor.begin(Wire1, 0x61);
sensor.stopPeriodicMeasurement();
delay(2000);
sensor.softReset();
delay(2000);
uint8_t major = 0;
uint8_t minor = 0;
error = sensor.readFirmwareVersion(major, minor);
if (error != NO_ERROR) {
Serial.print("Error trying to execute readFirmwareVersion(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
} else {
Serial.print("firmware version major: ");
Serial.print(major);
Serial.print("\t");
Serial.print("minor: ");
Serial.print(minor);
Serial.println();
}
error = sensor.startPeriodicMeasurement(0);
if (error != NO_ERROR) {
Serial.print("Error trying to execute startPeriodicMeasurement(): ");
errorToString(error, errorMessage, sizeof errorMessage);
Serial.println(errorMessage);
}
Mcu.begin();
deviceState = DEVICE_STATE_INIT;
}
void loop()
{
switch( deviceState )
{
case DEVICE_STATE_INIT:
{
#if(LORAWAN_DEVEUI_AUTO)
LoRaWAN.generateDeveuiByChipID();
#endif
LoRaWAN.init(loraWanClass,loraWanRegion);
break;
}
case DEVICE_STATE_JOIN:
{
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND:
{
prepareTxFrame( appPort );
LoRaWAN.send();
deviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
{
// Schedule next packet transmission
txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.sleep(loraWanClass);
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
}
}