Last active
December 8, 2023 05:26
-
-
Save AlexzPurewoko/c6afb8244c945b3ad857d9e440a0201e to your computer and use it in GitHub Desktop.
ESP32 MQTT PubSub Client With Display Screen (ST7789)
This file contains hidden or 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 <Adafruit_GFX.h> | |
#include <Adafruit_ST7789.h> | |
#define TFT_DC D1 // GPIO 5 | |
#define TFT_RST D2 // GPIO 4 | |
#define TFT_CS -1 | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
#ifndef STASSID | |
#define STASSID "" | |
#define STAPSK "" | |
#endif | |
char* ssid = STASSID; | |
char* password = STAPSK; | |
StaticJsonDocument<200> doc; | |
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); //lcd | |
// Broker | |
const char* mqtt_server = "mqtt.iteqno.com"; | |
WiFiClient wifiClient; | |
PubSubClient client(wifiClient); | |
void print_temp_and_humidity(double *temp, double *hum); | |
void print_status(char *); | |
void setup() { | |
tft.init(240, 240, SPI_MODE2); | |
tft.setRotation(3); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
} | |
void setup_wifi() { | |
// We start by connecting to a WiFi network | |
WiFi.begin(ssid, password); | |
boolean inc = false; | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
if(inc){ | |
print_status("Connecting.."); | |
} else { | |
print_status("Connecting."); | |
} | |
inc = !inc; | |
} | |
print_status("WiFi Connected!"); | |
} | |
void callback(char* topic, byte* message, unsigned int length) { | |
DeserializationError error = deserializeJson(doc, message); | |
// display to screen if count any deserialize error | |
if(error) { | |
print_status((char *) error.f_str()); | |
return; | |
} | |
double tempA = doc["data"]["tempA"]; | |
double humA = doc["data"]["humidityA"]; | |
print_temp_and_humidity(&tempA, &humA); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
// Attempt to connect | |
String clientId = "ESP8266Client-"; | |
clientId += String(random(0xffff), HEX); | |
if (client.connect(clientId.c_str())) { | |
print_status("MQTT Connected"); | |
// Subscribe | |
client.subscribe("temptron/json"); | |
} else { | |
print_status("Failed, wait!"); | |
delay(5000); | |
} | |
} | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
} | |
void print_temp_and_humidity(double *temp, double *hum){ | |
tft.fillScreen(ST77XX_WHITE); | |
tft.setCursor(20, 60); | |
tft.setTextColor(ST77XX_BLACK); | |
tft.setTextWrap(true); | |
tft.setTextSize(3); | |
tft.println("Temperature"); | |
tft.setTextColor(ST77XX_BLUE); | |
tft.setTextSize(1); | |
tft.println(); | |
tft.setTextSize(3); | |
tft.print(" "); | |
tft.print(*temp); | |
tft.print(" C"); | |
tft.println("\n"); | |
tft.setTextColor(ST77XX_BLACK); | |
tft.setTextWrap(true); | |
tft.setTextSize(3); | |
tft.println(" Humidity"); | |
tft.setTextColor(ST77XX_RED); | |
tft.setTextSize(1); | |
tft.println(); | |
tft.setTextSize(3); | |
tft.print(" "); | |
tft.print(*hum); | |
tft.print(" %"); | |
} | |
void print_status(char *state){ | |
tft.fillScreen(ST77XX_BLACK); | |
tft.setCursor(20, 100); | |
tft.setTextColor(ST77XX_WHITE); | |
tft.setTextWrap(true); | |
tft.setTextSize(3); | |
tft.print(state); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment