Last active
May 19, 2023 10:00
-
-
Save IdrisCytron/f13ba9871be1340b01af1355183bf28c to your computer and use it in GitHub Desktop.
Send Sensors Data (MLX90614) to Adaftuit IO Using ESP32.
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 <TFT_eSPI.h> | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_MLX90614.h> | |
#include <WiFi.h> | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
#define WLAN_SSID "YourSSID" | |
#define WLAN_PASS "YourWiFiPassword" | |
#define AIO_SERVER "io.adafruit.com" | |
#define AIO_SERVERPORT 1883 | |
#define AIO_USERNAME "YourAdafruitIOUsername" | |
#define AIO_KEY "YourAdafruitIOKey" | |
#define BUTTON1 35 | |
#define BUTTON2 0 | |
#define LED_GREEN 15 | |
#define LED_RED 17 | |
#define FF17 &FreeSans9pt7b | |
#define FF21 &FreeSansBold9pt7b | |
#define ROW1 0,16 | |
#define ROW2 0,38 | |
#define ROW3 0,60 | |
#define ROW4 0,82 | |
#define ROW5 0,104 | |
#define ROW6 0,126 | |
Adafruit_MLX90614 mlx = Adafruit_MLX90614(); | |
TFT_eSPI tft = TFT_eSPI(); | |
WiFiClient client; | |
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. | |
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); | |
Adafruit_MQTT_Subscribe ledControl = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/ledControl"); | |
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); | |
void setup() | |
{ | |
pinMode(BUTTON1, INPUT_PULLUP); | |
pinMode(BUTTON2, INPUT_PULLUP); | |
pinMode(LED_GREEN, OUTPUT); | |
pinMode(LED_RED, OUTPUT); | |
Serial.begin(115200); | |
// Connect to WiFi access point. | |
Serial.println(); Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(WLAN_SSID); | |
tft.init(); | |
tft.setRotation(1); | |
tft.fillScreen(TFT_BLACK); | |
tft.setFreeFont(FF17); | |
tft.setTextColor(TFT_WHITE); | |
tft.setCursor(ROW1); | |
tft.print(WLAN_SSID); | |
tft.setCursor(ROW2); | |
tft.print("Connecting..."); | |
WiFi.begin(WLAN_SSID, WLAN_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); Serial.println(WiFi.localIP()); | |
tft.fillScreen(TFT_BLACK); | |
tft.setCursor(ROW1); | |
tft.print(WLAN_SSID); | |
tft.setCursor(ROW2); | |
tft.print(WiFi.localIP()); | |
tft.setCursor(ROW3); | |
tft.print(AIO_SERVER); | |
tft.setTextColor(TFT_BLUE); | |
tft.setCursor(ROW4); | |
tft.print("Temperature (Celsius)"); | |
tft.setTextColor(TFT_BLUE); | |
tft.setCursor(ROW5); | |
tft.print("Ambient:"); | |
tft.setCursor(ROW6); | |
tft.print("Object:"); | |
mlx.begin(); | |
mqtt.subscribe(&ledControl); | |
} | |
float ambientCelsius = 0.0; | |
float objectCelsius = 0.0; | |
void loop() | |
{ | |
MQTT_connect(); | |
Adafruit_MQTT_Subscribe *subscription; | |
while ((subscription = mqtt.readSubscription(5000))) { | |
if (subscription == &ledControl) { | |
Serial.print(F("Got: ")); | |
Serial.println((char *)ledControl.lastread); | |
if (!strcmp((char*) ledControl.lastread, "ON")) { | |
digitalWrite(LED_GREEN, HIGH); | |
} | |
else { | |
digitalWrite(LED_GREEN, LOW); | |
} | |
} | |
} | |
ambientCelsius = mlx.readAmbientTempC(); | |
objectCelsius = mlx.readObjectTempC(); | |
Serial.print(F("\nSending temperature val ")); | |
Serial.print(objectCelsius); | |
Serial.print("..."); | |
tft.fillRect(90, 85, 150, 50, TFT_BLACK); | |
tft.setTextColor(TFT_MAGENTA); | |
tft.setCursor(90, 104); | |
tft.print(ambientCelsius); | |
tft.setCursor(90, 126); | |
tft.print(objectCelsius); | |
if (!temperature.publish(objectCelsius)) { | |
Serial.println(F("Failed")); | |
} | |
else { | |
Serial.println(F("OK!")); | |
} | |
} | |
void MQTT_connect() | |
{ | |
int8_t ret; | |
// Stop if already connected. | |
if (mqtt.connected()) { | |
return; | |
} | |
Serial.print("Connecting to MQTT... "); | |
uint8_t retries = 3; | |
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected | |
Serial.println(mqtt.connectErrorString(ret)); | |
Serial.println("Retrying MQTT connection in 5 seconds..."); | |
mqtt.disconnect(); | |
delay(5000); // wait 5 seconds | |
retries--; | |
if (retries == 0) { | |
// basically die and wait for WDT to reset me | |
while (1); | |
} | |
} | |
Serial.println("MQTT Connected!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment