3 changed files with 194 additions and 108 deletions
@ -1,108 +0,0 @@ |
|||||
#include <ESP8266WiFi.h> |
|
||||
#include <PubSubClient.h> |
|
||||
#include <SPI.h> |
|
||||
#include <Wire.h> |
|
||||
#include <Adafruit_GFX.h> |
|
||||
#include <Adafruit_SSD1306.h> |
|
||||
|
|
||||
#define OLED_RESET 0 // GPIO0
|
|
||||
Adafruit_SSD1306 display(OLED_RESET); |
|
||||
|
|
||||
#if (SSD1306_LCDHEIGHT != 48) |
|
||||
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); |
|
||||
#endif |
|
||||
|
|
||||
const char* ssid = "IoT-Zecevic"; |
|
||||
const char* password = "IoTSifra123456!"; |
|
||||
const char* mqttServer = "192.168.1.11"; |
|
||||
const int mqttPort = 1883; |
|
||||
const char* mqttUser = "yourMQTTuser"; |
|
||||
const char* mqttPassword = "yourMQTTpassword"; |
|
||||
|
|
||||
WiFiClient espClient; |
|
||||
PubSubClient client(espClient); |
|
||||
|
|
||||
void callback(char* topic, byte* payload, unsigned int length) { |
|
||||
|
|
||||
Serial.print("Message arrived in topic: "); |
|
||||
Serial.println(topic); |
|
||||
|
|
||||
Serial.print("Message:"); |
|
||||
display.clearDisplay(); |
|
||||
display.setTextSize(1); |
|
||||
display.setTextColor(WHITE); |
|
||||
display.setCursor(0,0); |
|
||||
display.println("Power: "); |
|
||||
for (int i = 0; i < length; i++) { |
|
||||
Serial.print((char)payload[i]); |
|
||||
display.setTextSize(2); |
|
||||
display.print((char)payload[i]); |
|
||||
} |
|
||||
|
|
||||
display.display(); |
|
||||
Serial.println(); |
|
||||
Serial.println("-----------------------"); |
|
||||
|
|
||||
} |
|
||||
|
|
||||
void setup() { |
|
||||
|
|
||||
Serial.begin(115200); |
|
||||
|
|
||||
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
|
|
||||
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
|
|
||||
// init done
|
|
||||
|
|
||||
// Clear the buffer.
|
|
||||
display.clearDisplay(); |
|
||||
|
|
||||
WiFi.begin(ssid, password); |
|
||||
|
|
||||
while (WiFi.status() != WL_CONNECTED) { |
|
||||
delay(500); |
|
||||
Serial.println("Connecting to WiFi.."); |
|
||||
} |
|
||||
Serial.println("Connected to WiFi"); |
|
||||
display.setTextSize(1); |
|
||||
display.setTextColor(WHITE); |
|
||||
display.setCursor(0,0); |
|
||||
display.println("Connected to the WiFi network!"); |
|
||||
display.display(); |
|
||||
|
|
||||
client.setServer(mqttServer, mqttPort); |
|
||||
client.setCallback(callback); |
|
||||
|
|
||||
while (!client.connected()) { |
|
||||
Serial.println("Connecting to MQTT..."); |
|
||||
|
|
||||
if (client.connect("ESP32Client", mqttUser, mqttPassword )) { |
|
||||
|
|
||||
Serial.println("connected"); |
|
||||
Serial.println("Connected to mqtt!"); |
|
||||
display.setTextSize(1); |
|
||||
display.setTextColor(WHITE); |
|
||||
display.println("Connected to mqtt!"); |
|
||||
display.println("No data yet"); |
|
||||
display.display(); |
|
||||
|
|
||||
} else { |
|
||||
|
|
||||
Serial.print("failed with state "); |
|
||||
Serial.print(client.state()); |
|
||||
delay(2000); |
|
||||
|
|
||||
} |
|
||||
} |
|
||||
//solar power
|
|
||||
client.subscribe("solar/114182126368/0/power"); |
|
||||
|
|
||||
/*client.subscribe("tele/tasmota_server_C32969/SENSOR");
|
|
||||
client.subscribe("tele/tasmota_LR-C23E87/SENSOR"); |
|
||||
client.subscribe("tele/tasmota_OfficeNET_17A00F/SENSOR"); |
|
||||
client.subscribe("tele/tasmota_OfficePC1856E2/SENSOR");*/ |
|
||||
|
|
||||
} |
|
||||
|
|
||||
void loop() { |
|
||||
client.loop(); |
|
||||
} |
|
||||
@ -0,0 +1,8 @@ |
|||||
|
{ |
||||
|
// Use IntelliSense to learn about possible attributes. |
||||
|
// Hover to view descriptions of existing attributes. |
||||
|
"version": "0.2.0", |
||||
|
"configurations": [ |
||||
|
|
||||
|
] |
||||
|
} |
||||
@ -0,0 +1,186 @@ |
|||||
|
#include <ESP8266WiFi.h> |
||||
|
#include <PubSubClient.h> |
||||
|
#include <SPI.h> |
||||
|
#include <Wire.h> |
||||
|
#include <Adafruit_GFX.h> |
||||
|
#include <Adafruit_SSD1306.h> |
||||
|
#include <DHT.h> |
||||
|
|
||||
|
// Display Definition
|
||||
|
#define OLED_RESET 0 // GPIO0
|
||||
|
Adafruit_SSD1306 display(OLED_RESET); |
||||
|
#if (SSD1306_LCDHEIGHT != 48) |
||||
|
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); |
||||
|
#endif |
||||
|
|
||||
|
// DHT Definitnion
|
||||
|
#define DHTPIN 2 |
||||
|
#define DHTTYPE DHT11 |
||||
|
DHT dht(DHTPIN, DHTTYPE); |
||||
|
|
||||
|
//Start web server
|
||||
|
WiFiServer server(80); |
||||
|
|
||||
|
//Wifi Data
|
||||
|
const char* ssid = "IoT-Zecevic"; |
||||
|
const char* password = "IoTSifra123456!"; |
||||
|
//MQTT data
|
||||
|
const char* mqttServer = "192.168.1.11"; |
||||
|
const int mqttPort = 1883; |
||||
|
const char* mqttUser = "yourMQTTuser"; |
||||
|
const char* mqttPassword = "yourMQTTpassword"; |
||||
|
|
||||
|
//Diplay and DHT update data
|
||||
|
long previousMillis = 0; |
||||
|
long interval = 2000; //ms
|
||||
|
|
||||
|
//Global Vars
|
||||
|
String global_power; |
||||
|
float humidity; |
||||
|
float temperature; |
||||
|
|
||||
|
//Start WiFi client
|
||||
|
WiFiClient espClient; |
||||
|
PubSubClient MQTTclient(espClient); |
||||
|
|
||||
|
void callback(char* topic, byte* payload, unsigned int length) { |
||||
|
|
||||
|
Serial.print("Message arrived in topic: "); |
||||
|
Serial.println(topic); |
||||
|
|
||||
|
Serial.print("Message:"); |
||||
|
global_power = ""; |
||||
|
for (int i = 0; i < length; i++) { |
||||
|
Serial.print((char)payload[i]); |
||||
|
global_power += char(payload[i]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void showDisplay() { |
||||
|
display.clearDisplay(); |
||||
|
display.setTextSize(1); |
||||
|
display.setTextColor(WHITE); |
||||
|
display.setCursor(0,0); |
||||
|
display.println("Power: "); |
||||
|
display.setTextSize(2); |
||||
|
display.print(global_power); |
||||
|
display.println(); |
||||
|
display.setTextSize(1); |
||||
|
display.println("Temp: "); |
||||
|
display.setTextSize(2); |
||||
|
display.println(temperature); |
||||
|
display.display(); |
||||
|
} |
||||
|
|
||||
|
// prepare a web page to be send to a client (web browser)
|
||||
|
String prepareHtmlPage() |
||||
|
{ |
||||
|
String htmlPage; |
||||
|
htmlPage.reserve(1024); // prevent ram fragmentation
|
||||
|
htmlPage = F("HTTP/1.1 200 OK\r\n" |
||||
|
"Content-Type: text/html\r\n" |
||||
|
"Connection: close\r\n" // the connection will be closed after completion of the response
|
||||
|
"Refresh: 5\r\n" // refresh the page automatically every 5 sec
|
||||
|
"\r\n" |
||||
|
"<!DOCTYPE HTML>" |
||||
|
"<html>" |
||||
|
"<p><b>Power Producing:</b> "); |
||||
|
htmlPage += global_power; |
||||
|
htmlPage += F("</p>" |
||||
|
"<p><b>Temperature:</b> "); |
||||
|
htmlPage += temperature; |
||||
|
htmlPage += F("</p>" |
||||
|
"<p><b>Humidity:</b> "); |
||||
|
htmlPage += humidity; |
||||
|
htmlPage += "</p>"; |
||||
|
htmlPage += F("</html>" |
||||
|
"\r\n"); |
||||
|
return htmlPage; |
||||
|
} |
||||
|
|
||||
|
void setup() { |
||||
|
|
||||
|
Serial.begin(115200); |
||||
|
dht.begin(); |
||||
|
|
||||
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); |
||||
|
|
||||
|
display.clearDisplay(); |
||||
|
|
||||
|
WiFi.begin(ssid, password); |
||||
|
|
||||
|
while (WiFi.status() != WL_CONNECTED) { |
||||
|
delay(500); |
||||
|
Serial.println("Connecting to WiFi.."); |
||||
|
} |
||||
|
Serial.println("Connected to WiFi"); |
||||
|
|
||||
|
MQTTclient.setServer(mqttServer, mqttPort); |
||||
|
MQTTclient.setCallback(callback); |
||||
|
|
||||
|
while (!MQTTclient.connected()) { |
||||
|
Serial.println("Connecting to MQTT..."); |
||||
|
|
||||
|
if (MQTTclient.connect("ESP32Client", mqttUser, mqttPassword )) { |
||||
|
Serial.println("Connected to mqtt!"); |
||||
|
} else { |
||||
|
Serial.print("failed with state "); |
||||
|
Serial.print(MQTTclient.state()); |
||||
|
delay(2000); |
||||
|
} |
||||
|
} |
||||
|
//Get info from Solar
|
||||
|
MQTTclient.subscribe("solar/114182126368/0/power"); |
||||
|
|
||||
|
server.begin(); |
||||
|
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); |
||||
|
|
||||
|
//show 5 sec IP on display
|
||||
|
display.clearDisplay(); |
||||
|
display.setTextSize(1); |
||||
|
display.setTextColor(WHITE); |
||||
|
display.setCursor(0,0); |
||||
|
display.println("IP addr: "); |
||||
|
display.println(WiFi.localIP().toString().c_str()); |
||||
|
display.display(); |
||||
|
delay(5000); |
||||
|
} |
||||
|
|
||||
|
void loop() { |
||||
|
MQTTclient.loop(); |
||||
|
WiFiClient serverClient = server.available(); |
||||
|
if (serverClient) |
||||
|
{ |
||||
|
while (serverClient.connected()) |
||||
|
{ |
||||
|
// read line by line what the client (web browser) is requesting
|
||||
|
if (serverClient.available()) |
||||
|
{ |
||||
|
String line = serverClient.readStringUntil('\r'); |
||||
|
if (line.length() == 1 && line[0] == '\n') |
||||
|
{ |
||||
|
serverClient.println(prepareHtmlPage()); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
while (serverClient.available()) { |
||||
|
serverClient.read(); |
||||
|
} |
||||
|
// close the connection:
|
||||
|
serverClient.stop(); |
||||
|
} |
||||
|
String MQTTmessage; |
||||
|
char out[255]; |
||||
|
unsigned long currentMillis = millis(); |
||||
|
if(currentMillis - previousMillis > interval) { |
||||
|
previousMillis = currentMillis; |
||||
|
humidity = dht.readHumidity(); |
||||
|
temperature = dht.readTemperature(); |
||||
|
showDisplay(); |
||||
|
|
||||
|
MQTTclient.publish("my/office/Temperature", String(temperature).c_str()); |
||||
|
MQTTclient.publish("my/office/Humidity", String(humidity).c_str()); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue