Skip to content

Instantly share code, notes, and snippets.

@bi119aTe5hXk
Last active May 2, 2024 12:00
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 bi119aTe5hXk/d9a683bcca669c65bd4a079522d38a21 to your computer and use it in GitHub Desktop.
Save bi119aTe5hXk/d9a683bcca669c65bd4a079522d38a21 to your computer and use it in GitHub Desktop.
M5Atom CO2+SHT MQTT Sample
#include <M5Atom.h>
#include <MHZ19.h>
#include <Wire.h>
#include <ArtronShop_SHT3x.h>
#include <SoftwareSerial.h>
#include <PubSubClient.h>
#include <WiFi.h>
#define RX_PIN 19 // Rx pin which the MHZ19 Tx pin is attached to
#define TX_PIN 23 // Tx pin which the MHZ19 Rx pin is attached to
#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)
SoftwareSerial mySerial(RX_PIN, TX_PIN);
ArtronShop_SHT3x sht3x(0x44, &Wire); // ADDR: 0 => 0x44, ADDR: 1 => 0x45
MHZ19 myMHZ19;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
/* Config for home assistant
mqtt:
sensor:
- name: "M5Atom Temperature"
state_topic: "homeassistant/sensor/m5atom"
value_template: "{{ value_json.Temperature }}"
unit_of_measurement: "°C"
- name: "M5Atom Humidity"
state_topic: "homeassistant/sensor/m5atom"
value_template: "{{ value_json.Humidity }}"
unit_of_measurement: "%RH"
- name: "M5Atom Co2"
state_topic: "homeassistant/sensor/m5atom"
value_template: "{{ value_json.CO2 }}"
unit_of_measurement: "ppm"
*/
const char* ssid = "<wifi ssid>";
const char* wifi_password = "<wifi passwoord>";
const char* mqttServer = "<mqtt server>";
const char* mqttClientID = "M5Atom";
const char* mqttUsername = "<mqtt username>";
const char* mqttPassword = "<mqtt password>";
const char* mqttTopicName = "homeassistant/sensor/m5atom";
const int mqttPort = 1883;
CRGB dispColor(uint8_t r, uint8_t g, uint8_t b) {
return (CRGB)((r << 16) | (g << 8) | b);
}
// use 255 will burn LEDs
int color[][3] = {
{0, 0, 0}, //0:BLACK
{200, 200, 200}, //1:WHITE
{0, 200, 0}, //2:GREEN
{200, 0, 0}, //3:RED
{0, 0, 200}, //4:BLUE
{200, 0, 70}, //5:PINK
{200, 70, 0}, //6:ORANGE
{200, 200, 0}, //7:YELLOW
{70, 0, 200} //8:PURPLE
};
//Matrix: white W
int matrixWhiteW[5][5] =
{ {1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 0}
};
//Matrix: green M
int matrixGreenM[5][5] =
{ {2, 0, 0, 0, 2},
{2, 2, 0, 2, 2},
{2, 0, 2, 0, 2},
{2, 0, 0, 0, 2},
{2, 0, 0, 0, 2}
};
//Matrix: red X
int matrixRedX[5][5] =
{ {3, 0, 0, 0, 3},
{0, 3, 0, 3, 0},
{0, 0, 3, 0, 0},
{0, 3, 0, 3, 0},
{3, 0, 0, 0, 3}
};
void displayMatrix(int arr[5][5]) {
int num = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
num = arr[i][j];
M5.dis.drawpix(i * 5 + j, dispColor(color[num][0], color[num][1], color[num][2]));
}
}
}
void wifi_connect(void) {
Serial.print("WiFi Connenting");
displayMatrix(matrixWhiteW);
WiFi.begin(ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("");
Serial.print("Connected : ");
Serial.println(WiFi.localIP());
long rssi = WiFi.RSSI();
Serial.print("RSSI:");
Serial.println(rssi);
client.setBufferSize(512);
client.setServer(mqttServer, mqttPort);
}
void setup() {
M5.begin(true, false, true);
delay(50);
Serial.begin(115200); // for debug log
mySerial.begin(BAUDRATE);
myMHZ19.begin(mySerial);
myMHZ19.autoCalibration();
Wire.begin(25, 21);
while (!sht3x.begin()) {
Serial.println("SHT3x not found !");
displayMatrix(matrixRedX);
delay(1000);
}
wifi_connect();
}
/*
white=1, blue=4, green=2, yellow=7, orange=6, red=3
1: Temperature: white(under-10) ~ blue(-10~0) ~ green(0~10) ~ yellow(10~20) ~ orange(20~30) ~ red(over30)
2: Humidity: white(0-20) ~ blue(20-40) ~ green(40-60) ~ yellow(60-80) ~ orange(80~90) ~ red(over90)
3: CO2: white(0-400) ~ blue(400-500) ~ green(500-700) ~ yellow(700-1000) ~ orange(1000~2000) ~ red(over2000)
4: Wifi RSSI: white(0-30) ~ blue(30-40) ~ green(40-50) ~ yellow(50-60) ~ orange(60~70) ~ red(over70)
5: N/A: Ramdon color every update
*/
int tempGroup[6][2] = {
{ -10, 1},
{0, 4},
{10, 2},
{20, 7},
{30, 6},
{0, 3}
};
int humiGroup[6][2] = {
{ 20, 1},
{40, 4},
{60, 2},
{80, 7},
{90, 6},
{0, 3}
};
int co2Group[6][2] = {
{ 400, 1},
{500, 4},
{700, 2},
{1000, 7},
{2000, 6},
{0, 3}
};
int rssiGroup[6][2] = {
{ 30, 1},
{40, 4},
{50, 2},
{60, 7},
{70, 6},
{0, 3}
};
void setMatrix(double temp, double humi, int co2, long rssi) {
setLine(temp, tempGroup, 0);
setLine(humi, humiGroup, 1);
setLine(double(co2), co2Group, 2);
setLine(double(abs(rssi)), rssiGroup, 3);
//5th line
for (int i = 0; i < 5; i++) {
M5.dis.drawpix(i, 4, dispColor(random(0, 200), random(0, 200), random(0, 200)));
}
}
void setLine(double value, int arr[6][2], int lineNum) {
for (int i = 0; i < 5; i++) {
if (value < arr[i][0]) {
setLineColor(arr[i][1], lineNum);
break;
} else {
//use the last one
setLineColor(arr[5][1], lineNum);
}
}
}
void setLineColor(int colorNum, int lineNum) {
for (int i = 0; i < 5; i++) {
M5.dis.drawpix(i, lineNum, dispColor(color[colorNum][0], color[colorNum][1], color[colorNum][2]));
}
}
void sendData(double temp, double humi, int co2, long rssi) {
String dataString = "{\"Temperature\":\"" + String(temp, 1) + "\",\"Humidity\":\"" + String(humi, 1) + "\",\"CO2\":\"" + co2 + "\",\"RSSI\":\"" + rssi + "\"}";
Serial.print("Publish MQTT: ");
Serial.println(dataString);
client.publish(mqttTopicName, dataString.c_str());
}
void loop() {
client.loop();
while (!client.connected() ) {
Serial.println("Connecting MQTT Server...");
displayMatrix(matrixGreenM);
if ( client.connect(mqttClientID, mqttUsername, mqttPassword) ) {
Serial.println("MQTT Connected");
break;
}
displayMatrix(matrixRedX);
delay(1000);
}
long rssi = WiFi.RSSI();
Serial.print("RSSI:");
Serial.println(rssi);
double temp = sht3x.temperature();
double humi = sht3x.humidity();
int co2 = myMHZ19.getCO2();
int temp2 = myMHZ19.getTemperature();
if (sht3x.measure()) {
Serial.print("Temperature: ");
Serial.print(temp, 1);
Serial.print(" *C, Humidity: ");
Serial.print(humi, 1);
Serial.println(" %RH");
} else {
Serial.println("SHT3x read error");
displayMatrix(matrixRedX);
}
Serial.print(co2);
Serial.print(" ppm, ");
Serial.print(temp2);
Serial.println(" *C");
if (temp != NULL && humi != NULL && co2 != NULL) {
sendData(temp, humi, co2, rssi);
setMatrix(temp, humi, co2, rssi);
}
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment