Created
May 2, 2024 12:01
-
-
Save bi119aTe5hXk/97df4cea4a359c39fd7af39d92fe0ac1 to your computer and use it in GitHub Desktop.
M5Stack MQTT Client demo
This file contains 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
#include <M5Stack.h> | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
#include "time.h" | |
#define JST 3600*9 | |
#include <Arduino.h> | |
#include <vector> | |
#include <M5GFX.h> | |
M5GFX display; | |
#define LINE_COUNT 4 | |
std::vector<int> points[LINE_COUNT]; | |
int colors[] = { TFT_RED, TFT_GREEN, TFT_BLUE, TFT_CYAN, TFT_MAGENTA, TFT_YELLOW }; | |
int xoffset, yoffset, point_count; | |
int virtual_width; | |
int virtual_height; | |
int zoom = 1; | |
WiFiClient wifiClient; | |
PubSubClient client(wifiClient); | |
const char* ssid = "<wifi ssid>"; | |
const char* wifi_password = "<wifi passwoord>"; | |
const char* mqttServer = "<mqtt server>"; | |
const char* mqttClientID = "M5Core"; | |
const char* mqttUsername = "<mqtt username>"; | |
const char* mqttPassword = "<mqtt password>"; | |
const char* mqttTopicName = "homeassistant/sensor/m5atom"; | |
const int mqttPort = 1883; | |
void wifi_connect(void) { | |
Serial.print("WiFi Connenting"); | |
//M5.Lcd.print("WiFi Connenting"); | |
WiFi.begin(ssid, wifi_password); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
//M5.Lcd.print("."); | |
delay(1000); | |
} | |
Serial.println(""); | |
// M5.Lcd.println(""); | |
Serial.print("Connected : "); | |
//M5.Lcd.print("Connected : "); | |
Serial.println(WiFi.localIP()); | |
// M5.Lcd.println(WiFi.localIP()); | |
long rssi = WiFi.RSSI(); | |
Serial.print("RSSI:"); | |
//M5.Lcd.print("RSSI : "); | |
Serial.println(rssi); | |
// M5.Lcd.println(rssi); | |
} | |
void printLocalTime() { | |
struct tm timeinfo; | |
if (!getLocalTime(&timeinfo)) { | |
M5.Lcd.println("Failed to obtain time"); | |
return; | |
} | |
Serial.print(&timeinfo, "%A, %B %d %Y %H:%M:%S"); | |
M5.Lcd.setTextColor(WHITE, BLACK); | |
M5.Lcd.println(&timeinfo, "%A, %B %d %Y %H:%M"); | |
} | |
int getBaseColor(int x, int y) { | |
return ((x ^ y) & 3 || ((x - xoffset) & 31 && y & 31) ? TFT_BLACK : ((!y || x == xoffset) ? TFT_WHITE : TFT_DARKGREEN)); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
// M5.Lcd.print("Message arrived ["); | |
Serial.print(topic); | |
// M5.Lcd.print(topic); | |
Serial.print("] "); | |
// M5.Lcd.print("] "); | |
//Transform subject (topic) and value (payload) to String | |
payload[length] = '\0'; | |
String strTopic = String(topic); | |
String strPayload = String((char*)payload); | |
// for (int i = 0; i < length; i++) { | |
//// Serial.print((char)payload[i]); | |
// // M5.Lcd.print((char)payload[i]); | |
// } | |
Serial.println(strPayload); | |
// M5.Lcd.println(strPayload); | |
StaticJsonDocument<200> doc; | |
DeserializationError error = deserializeJson(doc, strPayload); | |
if (error) { | |
Serial.print(F("deserializeJson() failed: ")); | |
M5.Lcd.print(F("deserializeJson() failed: ")); | |
Serial.println(error.f_str()); | |
M5.Lcd.println(error.f_str()); | |
return; | |
} | |
double temp = doc["Temperature"]; | |
double humi = doc["Humidity"]; | |
int co2 = doc["CO2"]; | |
int rssi_dev = doc["RSSI"]; | |
static int count; | |
points[0][count % point_count] = temp * -1; //RED | |
points[1][count % point_count] = humi * -1; //GREEN | |
points[2][count % point_count] = (co2 / 50) * -1; //BLUE | |
points[3][count % point_count] = rssi_dev * -1; //CYAN | |
++count; | |
display.waitDisplay(); | |
display.startWrite(); | |
int index1 = count % point_count; | |
for (int x = 0; x < point_count - 1; x++) { | |
int index0 = index1; | |
index1 = (index0 + 1) % point_count; | |
for (int i = 0; i < LINE_COUNT; i++) { | |
int y = points[i][index0]; | |
if (y != points[i][index1]) { | |
display.fillRect(x * zoom, (y + yoffset) * zoom, zoom, zoom, getBaseColor(x, y)); | |
} | |
} | |
for (int i = 0; i < LINE_COUNT; i++) { | |
int y = points[i][index1]; | |
display.fillRect(x * zoom, (y + yoffset) * zoom, zoom, zoom, colors[i]); | |
} | |
} | |
display.endWrite(); | |
M5.Lcd.setCursor(1, 1); | |
printLocalTime(); | |
M5.Lcd.setCursor(1, M5.Lcd.height() - 8); | |
M5.Lcd.setTextColor(RED, BLACK); | |
M5.Lcd.print("T:"); | |
M5.Lcd.print(temp); | |
M5.Lcd.print("*C"); | |
M5.Lcd.print(", "); | |
M5.Lcd.setTextColor(GREEN, BLACK); | |
M5.Lcd.print("H:"); | |
M5.Lcd.print(humi); | |
M5.Lcd.print("%RH"); | |
M5.Lcd.print(", "); | |
M5.Lcd.setTextColor(BLUE, BLACK); | |
M5.Lcd.print("CO2:"); | |
M5.Lcd.print(co2); | |
M5.Lcd.print("ppm"); | |
M5.Lcd.print(", "); | |
M5.Lcd.setTextColor(M5.Lcd.color565(0, 255, 255), BLACK); | |
M5.Lcd.print("RSSI:"); | |
M5.Lcd.print(rssi_dev); | |
M5.Lcd.print("dBm"); | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
//M5.Lcd.print("Attempting MQTT connection..."); | |
if ( client.connect(mqttClientID, mqttUsername, mqttPassword)) { | |
Serial.println("connected"); | |
//M5.Lcd.println("connected"); | |
client.subscribe(mqttTopicName); | |
//break; | |
} else { | |
Serial.print("failed, rc="); | |
M5.Lcd.print("failed, rc="); | |
Serial.print(client.state()); | |
M5.Lcd.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
M5.Lcd.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void drawBG() { | |
display.begin(); | |
if (display.isEPD()) { | |
display.setEpdMode(epd_mode_t::epd_fastest); | |
display.invertDisplay(true); | |
display.clear(TFT_BLACK); | |
} | |
if (display.width() < display.height()) { | |
display.setRotation(display.getRotation() ^ 1); | |
} | |
zoom = (display.width() / 480) + 1; | |
virtual_width = display.width() / zoom; | |
virtual_height = display.height() / zoom; | |
xoffset = virtual_width >> 1; | |
yoffset = virtual_height >> 1; | |
point_count = virtual_width + 1; | |
for (int i = 0; i < LINE_COUNT; i++) { | |
points[i].resize(point_count); | |
} | |
display.startWrite(); | |
for (int y = 0; y < virtual_height; y++) { | |
for (int x = 0; x < virtual_width; x++) { | |
auto color = getBaseColor(x, y - yoffset); | |
if (color) { | |
display.fillRect(x * zoom, y * zoom, zoom, zoom, color); | |
} | |
} | |
} | |
display.endWrite(); | |
} | |
void setup() { | |
M5.begin(); | |
delay(50); | |
Serial.begin(115200); | |
wifi_connect(); | |
client.setBufferSize(512); | |
client.setServer(mqttServer, mqttPort); | |
client.setCallback(callback); | |
configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); | |
drawBG(); | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment