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.
108 lines
2.7 KiB
108 lines
2.7 KiB
#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();
|
|
}
|