|
|
|
@ -1,5 +1,5 @@ |
|
|
|
#include <ESP8266WiFi.h> |
|
|
|
#include <PubSubClient.h> |
|
|
|
#include <ArduinoMqttClient.h> |
|
|
|
#include <SPI.h> |
|
|
|
#include <Wire.h> |
|
|
|
#include <Adafruit_GFX.h> |
|
|
|
@ -29,6 +29,7 @@ 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; |
|
|
|
@ -41,22 +42,44 @@ float temperature; |
|
|
|
|
|
|
|
//Start WiFi client
|
|
|
|
WiFiClient espClient; |
|
|
|
PubSubClient MQTTclient(espClient); |
|
|
|
MqttClient MQTTclient(espClient); |
|
|
|
|
|
|
|
void callback(char* topic, byte* payload, unsigned int length) { |
|
|
|
// void callback(char* topic, byte* payload, unsigned int length) {
|
|
|
|
|
|
|
|
Serial.print("Message arrived in topic: "); |
|
|
|
Serial.println(topic); |
|
|
|
// 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 onMqttMessage(int messageSize) { |
|
|
|
// Serial.println("Received a message with topic '");
|
|
|
|
// Serial.print(MQTTclient.messageTopic());
|
|
|
|
// Serial.print("', length ");
|
|
|
|
// Serial.print(messageSize);
|
|
|
|
// Serial.println(" bytes:");
|
|
|
|
|
|
|
|
Serial.print("Message:"); |
|
|
|
global_power = ""; |
|
|
|
for (int i = 0; i < length; i++) { |
|
|
|
Serial.print((char)payload[i]); |
|
|
|
global_power += char(payload[i]); |
|
|
|
while (MQTTclient.available()) { |
|
|
|
//Serial.print((char)MQTTclient.read());
|
|
|
|
global_power += char(MQTTclient.read()); |
|
|
|
} |
|
|
|
// Serial.println(global_power);
|
|
|
|
// Serial.println();
|
|
|
|
} |
|
|
|
|
|
|
|
void showDisplay() { |
|
|
|
//int length = global_power.length();
|
|
|
|
//Serial.println(lenght);
|
|
|
|
if (global_power.length() > 5) { |
|
|
|
global_power = global_power.substring(0,global_power.length()-1); |
|
|
|
} |
|
|
|
|
|
|
|
display.clearDisplay(); |
|
|
|
display.setTextSize(1); |
|
|
|
display.setTextColor(WHITE); |
|
|
|
@ -92,12 +115,30 @@ String prepareHtmlPage() |
|
|
|
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..."); |
|
|
|
|
|
|
|
// if (MQTTclient.connect()) {
|
|
|
|
// Serial.println("Connected to mqtt!");
|
|
|
|
// } else {
|
|
|
|
// Serial.print("failed with state ");
|
|
|
|
// Serial.print(MQTTclient.connectError());
|
|
|
|
// delay(2000);
|
|
|
|
// }
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void setup() { |
|
|
|
|
|
|
|
Serial.begin(115200); |
|
|
|
@ -115,22 +156,13 @@ void setup() { |
|
|
|
} |
|
|
|
Serial.println("Connected to WiFi"); |
|
|
|
|
|
|
|
MQTTclient.setServer(mqttServer, mqttPort); |
|
|
|
MQTTclient.setCallback(callback); |
|
|
|
|
|
|
|
while (!MQTTclient.connected()) { |
|
|
|
Serial.println("Connecting to MQTT..."); |
|
|
|
//MQTTclient.setServer(mqttServer, mqttPort);
|
|
|
|
//MQTTclient.setCallback(callback);
|
|
|
|
MQTTclient.onMessage(onMqttMessage); |
|
|
|
MQTTclient.subscribe(topic); |
|
|
|
|
|
|
|
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"); |
|
|
|
//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()); |
|
|
|
@ -147,7 +179,19 @@ void setup() { |
|
|
|
} |
|
|
|
|
|
|
|
void loop() { |
|
|
|
MQTTclient.loop(); |
|
|
|
if (!MQTTclient.connected()) { |
|
|
|
reconect(); |
|
|
|
//Serial.print("loop ");
|
|
|
|
//Serial.println(MQTTclient.connected());
|
|
|
|
MQTTclient.poll(); |
|
|
|
MQTTclient.available(); |
|
|
|
//Serial.println((char)MQTTclient.read());
|
|
|
|
} |
|
|
|
MQTTclient.poll(); |
|
|
|
MQTTclient.available(); |
|
|
|
MQTTclient.subscribe(topic); |
|
|
|
//Serial.println((char)MQTTclient.read());
|
|
|
|
|
|
|
|
WiFiClient serverClient = server.available(); |
|
|
|
if (serverClient) |
|
|
|
{ |
|
|
|
@ -171,16 +215,31 @@ void loop() { |
|
|
|
// close the connection:
|
|
|
|
serverClient.stop(); |
|
|
|
} |
|
|
|
String MQTTmessage; |
|
|
|
char out[255]; |
|
|
|
|
|
|
|
unsigned long currentMillis = millis(); |
|
|
|
//Serial.println("outside for");
|
|
|
|
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()); |
|
|
|
if (!MQTTclient.connected()) { |
|
|
|
reconect(); |
|
|
|
} |
|
|
|
//Serial.println(MQTTclient.connected());
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
|
|
// MQTTclient.publish("my/office/Temperature", String(temperature).c_str());
|
|
|
|
// MQTTclient.publish("my/office/Humidity", String(humidity).c_str());
|
|
|
|
|
|
|
|
} |
|
|
|
} |