You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
4.8 KiB
198 lines
4.8 KiB
#include <ESP8266WiFi.h>
|
|
#include <ArduinoMqttClient.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";
|
|
const char topic[] = "solar/114182126368/0/power";
|
|
|
|
//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;
|
|
MqttClient MQTTclient(espClient);
|
|
|
|
void onMqttMessage(int messageSize) {
|
|
global_power = "";
|
|
while (MQTTclient.available()) {
|
|
global_power += char(MQTTclient.read());
|
|
}
|
|
}
|
|
|
|
void showDisplay() {
|
|
if (global_power.length() > 5) {
|
|
global_power = global_power.substring(0,global_power.length()-1);
|
|
}
|
|
|
|
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("</p>"
|
|
"<p><b>MQTT Connected:</b> ");
|
|
htmlPage += MQTTclient.connected();
|
|
htmlPage += "</p>";
|
|
htmlPage += F("</html>"
|
|
"\r\n");
|
|
return htmlPage;
|
|
}
|
|
|
|
void reconect() {
|
|
while (!MQTTclient.connect(mqttServer, mqttPort)) {
|
|
Serial.println("Connecting to MQTT...");
|
|
}
|
|
}
|
|
|
|
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.onMessage(onMqttMessage);
|
|
MQTTclient.subscribe(topic);
|
|
|
|
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() {
|
|
if (!MQTTclient.connected()) {
|
|
reconect();
|
|
MQTTclient.poll();
|
|
MQTTclient.available();
|
|
}
|
|
MQTTclient.poll();
|
|
MQTTclient.available();
|
|
MQTTclient.subscribe(topic);
|
|
|
|
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();
|
|
}
|
|
|
|
unsigned long currentMillis = millis();
|
|
//Serial.println("outside for");
|
|
if(currentMillis - previousMillis > interval) {
|
|
previousMillis = currentMillis;
|
|
humidity = dht.readHumidity();
|
|
temperature = dht.readTemperature() - 5;
|
|
showDisplay();
|
|
|
|
if (!MQTTclient.connected()) {
|
|
reconect();
|
|
}
|
|
|
|
MQTTclient.beginMessage("my/office/Temperature");
|
|
MQTTclient.print(String(temperature).c_str());
|
|
MQTTclient.endMessage();
|
|
|
|
MQTTclient.beginMessage("my/office/Humidity");
|
|
MQTTclient.print(String(humidity).c_str());
|
|
MQTTclient.endMessage();
|
|
}
|
|
}
|