My ESP Now Receiver program is running like a champ, but MAGENTA is too dark. I wanted to change the color.
While I was at it, I tried to add a non-GFX font. It didn’t work, so I deleted all of that stuff. but in the process i had to add the SD library. This TFT has an SD slot, but I’m not using it.
I plan to use SPIFFS to datalog pressure. But I’m not there yet.
But when I put the SD library back into .ini, there’s a conflict. Says wrong platform.
Can you tell me why it’s demanding SD? All of a sudden?
How can I make it stop?
Here’s the code:
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
#include "ESPAsyncWebServer.h"
#include <Arduino_JSON.h>
#include "Adafruit_GFX.h"
#include "Fonts/FreeSans9pt7b.h"
#include <MCUFRIEND_kbv.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#define NTP_OFFSET -14400 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "pool.ntp.org"
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define BLACK 0x0000
#define PURPLE 0x780F
#define PINK 0xFC9F
#define PINK1 0xF8FF
#define AQUA 0x04FF
// Replace with your network credentials (STATION)
const char *ssid = "TP-Link_7C28";
const char *password = "64411811";
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message
{
int id;
float temperature;
float humidity;
float pressure;
float Gas;
float CO;
float smoke;
float moisture;
float UV;
float CO2;
float NH3;
float NO2;
float VOC;
float TVOC;
float H2;
float EtOH;
int readingId;
} struct_message;
struct_message incomingReadings;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
MCUFRIEND_kbv tft;
JSONVar board;
AsyncWebServer server(80);
AsyncEventSource events("/events");
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len)
{
// Copies the sender mac address to a string
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
board["id"] = incomingReadings.id, len;
board["temperature"] = incomingReadings.temperature;
board["humidity"] = incomingReadings.humidity;
board["pressure"] = incomingReadings.pressure;
board["Gas"] = incomingReadings.Gas;
board["CO"] = incomingReadings.CO;
board["smoke"] = incomingReadings.smoke;
board["moisture"] = incomingReadings.moisture;
board["UV"] = incomingReadings.UV;
board["CO2"] = incomingReadings.CO2;
board["NH3"] = incomingReadings.NH3;
board["NO2"] = incomingReadings.NO2;
board["VOC"] = incomingReadings.VOC;
board["TVOC"] = incomingReadings.TVOC;
board["H2"] = incomingReadings.H2;
board["EtOH"] = incomingReadings.EtOH;
board["readingId"] = incomingReadings.readingId;
Serial.printf("Board ID %u: %u bytes\n", incomingReadings.id, len);
String jsonString = JSON.stringify(board);
events.send(jsonString.c_str(), "new_readings", millis());
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>Monster Laboratories Smart Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="icon" href="data:,">
<style>
html {font-family: Arial; display: inline-block; text-align: center;}
p { font-size: 1.2rem;}
body { margin: 0;}
.topnav { overflow: hidden; background-color: #2f4468; color: white; font-size: 1.7rem; }
.content { padding: 20px; }
.card { background-color: white; box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5); }
.cards { max-width: 3500px; margin: 0 auto; display: grid; grid-gap: 2rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.reading { font-size: 2.8rem; }
.packet { color: #bebebe; }
.card.temperature { color: #fd7e14; }
.card.humidity { color: #1b78e2; }
.card.pressure { color: #17b8e2; }
.card.UV { color: #17b8e2; }
</style>
</head>
<body>
<div class="topnav">
<h3>Monster Laboratories SmartHome- Air Quality</h3>
</div>
<div class="content">
<div class="cards">
<div class="card temperature">
<h4><i class="fas fa-thermometer-half"></i> Pod 1 - TEMPERATURE</h4>
<p><span class="reading"><span id="t1"></span> °C</span></p>
<p class="packet">Reading ID: <span id="rt1"></span></p>
</div>
<div class="card humidity">
<h4><i class="fas fa-tint"></i> Pod 1 - HUMIDITY</h4>
<p><span class="reading"><span id="h1"></span> %</span></p>
<p class="packet">Reading ID: <span id="rh1"></span></p>
</div>
<div class="card pressure">
<h4><i class="fas fa-tint"></i> Pod 1 - PRESSURE</h4>
<p><span class="reading"><span id="p1"></span> "Hg</span></p>
<p class="packet">Reading ID: <span id="rp1"></span></p>
</div>
<div class="card temperature">
<h4><i class="fas fa-thermometer-half"></i> Pod 3 - TEMPERATURE</h4>
<p><span class="reading"><span id="t3"></span> °C</span></p>
<p class="packet">Reading ID: <span id="rt3"></span></p>
</div>
<div class="card UV">
<h4><i class="fas fa-tint"></i> Pod 3 - UVindex</h4>
<p><span class="reading"><span id="u1"></span> ... </span></p>
<p class="packet">Reading ID: <span id="ru1"></span></p>
</div>
<div class="card pressure">
<h4><i class="fas fa-tint"></i> Pod 3 - PRESSURE</h4>
<p><span class="reading"><span id="p3"></span> "Hg</span></p>
<p class="packet">Reading ID: <span id="rp3"></span></p>
</div>
</div>
</div>
<script>
if (!!window.EventSource) {
var source = new EventSource('/events');
source.addEventListener('open', function(e) {
console.log("Events Connected");
}, false);
source.addEventListener('error', function(e) {
if (e.target.readyState != EventSource.OPEN) {
console.log("Events Disconnected");
}
}, false);
source.addEventListener('message', function(e) {
console.log("message", e.data);
}, false);
source.addEventListener('new_readings', function(e) {
console.log("new_readings", e.data);
var obj = JSON.parse(e.data);
document.getElementById("t"+obj.id).innerHTML = obj.temperature.toFixed(0);
document.getElementById("h"+obj.id).innerHTML = obj.humidity.toFixed(0);
document.getElementById("p"+obj.id).innerHTML = obj.pressure.toFixed(0);
document.getElementById("u"+obj.id).innerHTML = obj.UV.toFixed(0);
document.getElementById("rt"+obj.id).innerHTML = obj.readingId;
document.getElementById("rh"+obj.id).innerHTML = obj.readingId;
document.getElementById("rp"+obj.id).innerHTML = obj.readingId;
document.getElementById("ru"+obj.id).innerHTML = obj.readingId;
console.log(obj.readingId);
}, false);
}
</script>
</body>
</html>)rawliteral";
;
void setup()
{
Serial.begin(9600);
uint16_t ID = tft.readID();
tft.begin(ID);
// Set the device as a Station and Soft Access Point simultaneously
WiFi.mode(WIFI_AP_STA);
// Set device as a Wi-Fi Station
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Setting as a Wi-Fi Station..");
}
IPAddress local_IP(192, 168, 0, 100);
Serial.print("Station IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Wi-Fi Channel: ");
Serial.println(WiFi.channel());
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send_P(200, "text/html", index_html); });
events.onConnect([](AsyncEventSourceClient *client)
{
if (client->lastId())
{
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
// send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!", NULL, millis(), 10000); });
server.addHandler(&events);
server.begin();
}
void loop()
{
timeClient.begin();
timeClient.update();
String formattedTime = timeClient.getFormattedTime();
tft.fillScreen(BLACK);
tft.setFont(&FreeSans9pt7b);
tft.setTextSize(1);
tft.setTextColor(YELLOW);
tft.setCursor(1, 20);
tft.print("Station IP : ");
tft.println(WiFi.localIP());
if(incomingReadings.id == 1){
tft.setTextColor(CYAN);
}
else if (incomingReadings.id == 2){
tft.setTextColor(PINK1);
}
else if (incomingReadings.id == 3){
tft.setTextColor(AQUA);
}
tft.print("Time: ");
tft.println(formattedTime);
tft.print("Board ID: ");
tft.println(incomingReadings.id);
tft.print("Temperature: ");
tft.print(incomingReadings.temperature, 1);
tft.println(" °C");
tft.print("Pressure: ");
tft.print(incomingReadings.pressure, 0);
tft.println(" in Hg");
tft.print("Humidity: ");
tft.print(incomingReadings.humidity, 0);
tft.println(" %");
tft.print("CO: ");
tft.print(incomingReadings.CO, 0);
tft.println(" PPM");
tft.print("LPG: ");
tft.print(incomingReadings.Gas, 0);
tft.println(" PPM");
tft.print("Smoke: ");
tft.println(incomingReadings.smoke, 0);
tft.print("Uv index: ");
tft.println(incomingReadings.UV, 0);
tft.print("CO2: "),
tft.print(incomingReadings.CO2, 0);
tft.println(" PPM");
tft.print("VOC: ");
tft.print(incomingReadings.VOC, 0);
tft.println(" PPM");
tft.print("TVOC: ");
tft.print(incomingReadings.TVOC, 0);
tft.println(" PPM");
tft.print("H2: ");
tft.print(incomingReadings.H2, 0);
tft.println(" PPM");
tft.print("EtOH: ");
tft.print(incomingReadings.EtOH, 0);
tft.println(" PPM");
tft.print("NH3: "),
tft.print(incomingReadings.NH3, 0);
tft.println(" PPM");
tft.print("NO2: "),
tft.print(incomingReadings.NO2, 0);
tft.println(" PPM");
tft.print("Reading ID: ");
tft.print(incomingReadings.readingId);
Serial.print("Pressure: ");
Serial.print(incomingReadings.pressure, 0);
Serial.println(" in Hg");
static unsigned long lastEventTime = millis();
static const unsigned long EVENT_INTERVAL_MS = 5000;
if ((millis() - lastEventTime) > EVENT_INTERVAL_MS)
{
events.send("ping", NULL, millis());
lastEventTime = millis();
}
delay(10000);
}
Archiving .pio\build\esp32dev\libFrameworkArduinoVariant.a
In file included from .pio/libdeps/esp32dev/ESP32-TFT-Library-ILI9486/src/tft.cpp:3:
.pio/libdeps/esp32dev/ESP32-TFT-Library-ILI9486/src/tft.h:7:10: fatal error: SD.h: No such file or directory