// Codice per il monitoraggio da remoto della temperatura e umidità
// con NodeRED su http://NODEREDCLIENT_IPADDRESS/8080/ui
// con webserver ESP_IPADDRESS
// e display SSD1306
#include "Adafruit_Sensor.h"
#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <AsyncTCP.h>
//#include <ESPAsyncWebServer.h>
#include <ESPAsyncWebSrv.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Wokwi Virtual WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";// MQTT Broker address
int mqtt_broker_port = 1883;
char* mqttTopic_DHT_temp = "esp32/iot1/dht/temp"; // MQTT topic2: sensor-temerature
char* mqttTopic_DHT_hum = "esp32/iot1/dht/hum"; // MQTT topic3: sensor-humidity
WiFiClient wifiClient;
PubSubClient pubSubClient(wifiClient);
// Se si volessero introdurre certificati di SSL/TLS bisognerebbe
// creare qui gli oggetti a partire dal client(espClient)
//Constants
#define DHTPIN 4 // pin DATI sul GPIO
#define DHTTYPE DHT11 // Sensore DHT11 (AM2302)
//#define DHTTYPE DHT22 // DHT 11 (AM2302)
DHT dht(DHTPIN, DHTTYPE); // Inizializza il sensore
float temp; // valori sensore temperatura
float humidity; // valori sensore umidità
int LEDPIN = 34; //GPIO pin usato dal LED
const int ledPin = 35; //Pin controllato dal WebServer
bool ledState = 0; //Stato pin contrallato dal WebServer
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
}
h1 {
font-size: 1.8rem;
color: white;
}
h2{
font-size: 1.5rem;
font-weight: bold;
color: #143642;
}
.topnav {
overflow: hidden;
background-color: #143642;
}
body {
margin: 0;
}
.content {
padding: 30px;
max-width: 600px;
margin: 0 auto;
}
.card {
background-color: #F8F7F9;;
box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5);
padding-top:10px;
padding-bottom:20px;
}
.button {
padding: 15px 50px;
font-size: 24px;
text-align: center;
outline: none;
color: #fff;
background-color: #0f8b8d;
border: none;
border-radius: 5px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
/*.button:hover {background-color: #0f8b8d}*/
.button:active {
background-color: #0f8b8d;
box-shadow: 2 2px #CDCDCD;
transform: translateY(2px);
}
.state {
font-size: 1.5rem;
color:#8c8c8c;
font-weight: bold;
}
</style>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
</head>
<body>
<div class="topnav">
<h1>ESP WebSocket Server</h1>
</div>
<div class="content">
<div class="card">
<h2>Output - GPIO 2</h2>
<p class="state">state: <span id="state">%STATE%</span></p>
<p><button id="button" class="button">Toggle</button></p>
</div>
</div>
<script>
var gateway = `ws://${window.location.hostname}/ws`;
var websocket;
window.addEventListener('load', onLoad);
function initWebSocket() {
console.log('Trying to open a WebSocket connection...');
websocket = new WebSocket(gateway);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage; // <-- add this line
}
function onOpen(event) {
console.log('Connection opened');
}
function onClose(event) {
console.log('Connection closed');
setTimeout(initWebSocket, 2000);
}
function onMessage(event) {
var state;
if (event.data == "1"){
state = "ON";
}
else{
state = "OFF";
}
document.getElementById('state').innerHTML = state;
}
function onLoad(event) {
initWebSocket();
initButton();
}
function initButton() {
document.getElementById('button').addEventListener('click', toggle);
}
function toggle(){
websocket.send('toggle');
}
</script>
</body>
</html>
)rawliteral";
void notifyClients() {
ws.textAll(String(ledState));
}
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
data[len] = 0;
if (strcmp((char*)data, "toggle") == 0) {
ledState = !ledState;
notifyClients();
}
}
}
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_CONNECT:
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
break;
case WS_EVT_DISCONNECT:
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
void initWebSocket() {
ws.onEvent(onEvent);
server.addHandler(&ws);
}
String processor(const String& var){
Serial.println(var);
if(var == "STATE"){
if (ledState){
return "ON";
}
else{
return "OFF";
}
}
return String();
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
static const unsigned char image_Lock_7x8_bits[] PROGMEM = {0x1c,0x22,0x22,0x7f,0x7f,0x77,0x7f,0x3e}; //8
static const unsigned char image_Bluetooth_Idle_5x8_bits[] PROGMEM = {0x04,0x0d,0x16,0x0c,0x0c,0x16,0x0d,0x04}; //8
static const unsigned char image_Volup_8x6_bits[] PROGMEM = {0x48,0x8c,0xaf,0xaf,0x8c,0x48}; //6
static const unsigned char image_Alert_9x8_bits[] PROGMEM = {0x10,0x00,0x38,0x00,0x28,0x00,0x6c,0x00,0x6c,0x00,0xfe,0x00,0xee,0x00,0xff,0x01}; //16
// 'tcpconn1', 50x50px
const unsigned char tcpConn1 [] PROGMEM = {
0xff, 0xc0, 0x00, 0x00, 0x00, 0xff, 0xc0, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0xf8, 0x00,
0x00, 0x00, 0x00, 0x07, 0xc0, 0xf0, 0x00, 0x00, 0x00, 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0x00, 0x00, 0x01, 0xfc, 0x40, 0x8f, 0xff, 0x00, 0x00, 0x3f, 0xfc, 0x40,
0x8f, 0xff, 0x00, 0x00, 0x3f, 0xfc, 0x40, 0x8f, 0xff, 0x00, 0x00, 0x3f, 0xfc, 0x40, 0x8f, 0xff,
0x00, 0x00, 0x3f, 0xfc, 0x40, 0x8f, 0xff, 0x00, 0x00, 0x3f, 0xfc, 0x40, 0x8f, 0xff, 0xc0, 0x00,
0xff, 0xfc, 0x40, 0x8f, 0xff, 0xe0, 0x00, 0xff, 0xfc, 0x40, 0x8f, 0xff, 0xe0, 0x00, 0xff, 0xfc,
0x40, 0x8f, 0xff, 0xe0, 0x00, 0xff, 0xfc, 0x40, 0x87, 0xff, 0xe0, 0x00, 0xff, 0xf8, 0x40, 0x87,
0xff, 0xff, 0xff, 0xff, 0xf8, 0x40, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xc0, 0xc3, 0xff, 0xff,
0xff, 0xff, 0xf0, 0xc0, 0xe1, 0xff, 0xff, 0xff, 0xff, 0xe1, 0xc0, 0xe0, 0x7f, 0xff, 0xff, 0xff,
0x81, 0xc0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0,
0xfe, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0
};
// 'AntennaSending', 50x50px
const unsigned char antennaSending1 [] PROGMEM = {
0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xc0, 0x00, 0xff, 0xff, 0xc0, 0xff, 0xfe,
0x00, 0x00, 0x3f, 0xff, 0xc0, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff, 0xc0, 0xff, 0xe0, 0x1f, 0xfe,
0x03, 0xff, 0xc0, 0xff, 0xc0, 0xff, 0xff, 0x81, 0xff, 0xc0, 0xff, 0x83, 0xfc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0xc7, 0x8f, 0xfe, 0x3f, 0xf8, 0xf1, 0xc0, 0xc3, 0x87, 0xfe, 0x3f, 0xf0, 0xe1, 0xc0, 0xe3, 0xc3,
0xfe, 0x3f, 0xf1, 0xe3, 0xc0, 0xe1, 0xe1, 0xfe, 0x3f, 0xfb, 0xc3, 0xc0, 0xf0, 0xf3, 0xfe, 0x3f,
0xff, 0x87, 0xc0, 0xf8, 0x7f, 0xfe, 0x3f, 0xff, 0x87, 0xc0, 0xf8, 0x7f, 0xfe, 0x3f, 0xff, 0x0f,
0xc0, 0xfc, 0x3f, 0xfe, 0x3f, 0xff, 0xdf, 0xc0, 0xfe, 0x3f, 0xfe, 0x3f, 0xff, 0xff, 0xc0, 0xff,
0xff, 0xfe, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfe,
0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfe, 0x3f, 0xff,
0xff, 0xc0, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xc0,
0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0
};
// 'tcpconn1Inv', 50x50px
const unsigned char tcpConn1Inv [] PROGMEM = {
0x00, 0x3f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x07, 0xff,
0xff, 0xff, 0xff, 0xf8, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0x80, 0x00, 0x00,
0x00, 0x7e, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x0f,
0x00, 0x78, 0x07, 0xff, 0xff, 0xf8, 0x07, 0x80, 0x78, 0x0f, 0xff, 0xff, 0xfe, 0x07, 0x80, 0x78,
0x0f, 0xff, 0xff, 0xfe, 0x07, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff,
0xff, 0xfe, 0x03, 0x80, 0x70, 0x1c, 0xc9, 0x32, 0x46, 0x03, 0x80, 0x70, 0x1c, 0xc9, 0x32, 0x46,
0x03, 0x80, 0x70, 0x1c, 0xc9, 0x32, 0x46, 0x03, 0x80, 0x70, 0x1c, 0xc9, 0x32, 0x46, 0x03, 0x80,
0x70, 0x1c, 0xc9, 0x32, 0x46, 0x03, 0x80, 0x70, 0x1c, 0xd9, 0xb2, 0x66, 0x03, 0x80, 0x70, 0x1f,
0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff,
0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03,
0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70,
0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff,
0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe,
0x03, 0x80, 0x70, 0x1f, 0xff, 0xff, 0xfe, 0x03, 0x80, 0x70, 0x00, 0xff, 0xff, 0xc0, 0x03, 0x80,
0x70, 0x00, 0xff, 0xff, 0xc0, 0x03, 0x80, 0x70, 0x00, 0xff, 0xff, 0xc0, 0x03, 0x80, 0x70, 0x00,
0xff, 0xff, 0xc0, 0x03, 0x80, 0x70, 0x00, 0xff, 0xff, 0xc0, 0x03, 0x80, 0x70, 0x00, 0x3f, 0xff,
0x00, 0x03, 0x80, 0x70, 0x00, 0x1f, 0xff, 0x00, 0x03, 0x80, 0x70, 0x00, 0x1f, 0xff, 0x00, 0x03,
0x80, 0x70, 0x00, 0x1f, 0xff, 0x00, 0x03, 0x80, 0x78, 0x00, 0x1f, 0xff, 0x00, 0x07, 0x80, 0x78,
0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x38, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x3c, 0x00, 0x00,
0x00, 0x00, 0x0f, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00,
0x7e, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00,
0x01, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'AntennaSending1Inv', 50x50px
const unsigned char antennaSending1Inv [] PROGMEM = {
0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01,
0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x01,
0xfc, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x7c, 0x03, 0xf0, 0x1f, 0x80,
0x00, 0x00, 0xf0, 0x3f, 0xfe, 0x07, 0xc0, 0x00, 0x01, 0xe0, 0xff, 0xff, 0x83, 0xe0, 0x00, 0x03,
0xc3, 0xfe, 0x3f, 0xe1, 0xe0, 0x00, 0x07, 0x87, 0xe0, 0x03, 0xf0, 0xf0, 0x00, 0x0f, 0x0f, 0x80,
0x00, 0xf8, 0x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x01, 0xc0, 0x00, 0xf0,
0x00, 0x03, 0xc0, 0x01, 0xc0, 0x00, 0x20, 0x00, 0x01, 0xc0, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// ------------------- For i2c -------------------
//// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
void setup_wifi() { // Connessione al WiFi
WiFi.mode(WIFI_STA); // default, se non lo si dichiara non serve
//delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); // Indirizzo IP associato all'ESP32 una volta connesso
}
// Process incoming MQTT message and control the servo motor
//void callback(char* topic, byte* payload, unsigned int length) {
//if (topic = mqttTopic_DHT_temp){
//}
//}
void setup() {
Serial.begin(115200); // Inizializzazione del monitor seriale (solo per il debugging!)
//myservo.attach(5); // Evetuale servo sul GPIO 5
pinMode(LEDPIN, OUTPUT);
dht.begin();
setup_wifi();
pubSubClient.setServer(mqtt_server, mqtt_broker_port); // Server e porta MQTT del broker al quale puntare
//pubSubClient.setCallback(callback); // Funzione di callback per i messaggi in ingresso
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
initWebSocket();
// Route root webpage : /
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.begin(); // Start Web Server
delay(2000);
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
while (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Initialize OLED display with address 0x3C
Serial.println(F("Failed to start SSD1306 OLED"));
delay(500);
}
//oled.width(), oled.height()
//` 0, 0` sono le coordinate (X, Y) in cui vuoi posizionare l'immagine.
//`128, 64` sono le dimensioni del logo.
//`WHITE` è il colore dei pixel accesi (con libreria come Adafruit GFX)
// Show initial display buffer contents on the screen --
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE); // Set text color
oled.clearDisplay(); // Clear display
oled.setCursor(5, 32); // Set position to display (x,y)
oled.println("Avvio OLED ...");
oled.display(); // Display on OLED
delay(2000); // Wait 2 seconds for loading screen
oled.clearDisplay(); // Clear display
oled.drawBitmap( 9, 7, tcpConn1Inv, 50, 50, WHITE);
oled.drawBitmap( 68, 7, antennaSending1Inv, 50, 50, WHITE);
oled.display(); // Display on OLED
delay(4000); // Wait 2 seconds for loading screen
oled.display();
delay(500);
}
void mqttReconnect() { // Riconnessione al broker MQTT
while (!pubSubClient.connected()) {
Serial.print("Attempting MQTT connection...");
if (pubSubClient.connect("ESPClient")) {
Serial.println("connected");
pubSubClient.subscribe(mqttTopic_DHT_temp);
pubSubClient.subscribe(mqttTopic_DHT_hum);
} else {
Serial.print("failed, rc=");
Serial.print(pubSubClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!pubSubClient.connected()) { // Verifica lo stato della connessione MQTT
digitalWrite(LEDPIN, LOW);
mqttReconnect();
} else {
digitalWrite(LEDPIN, HIGH);
}
pubSubClient.loop();
delay(100); // ritardo per gestire il canale loop
temp = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print("Temp : ");
Serial.print(temp);
Serial.println(" C ");
Serial.print("Humidity : ");
Serial.print(humidity);
Serial.println(" % ");
String messaget = String(temp); // Formato di scambio stringhe con pubsubclient
String messageh = String(humidity); // Formato di scambio stringhe con pubsubclient
pubSubClient.publish(mqttTopic_DHT_temp, messaget.c_str());
pubSubClient.publish(mqttTopic_DHT_hum, messageh.c_str());
ws.cleanupClients();
digitalWrite(ledPin, ledState);
delay(200);
}