/*
*/
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>
#include <ModbusRTU.h>
#include <PJONThroughSerial.h>
PJONThroughSerial bus(45);
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
WiFiClient ethClient;
PubSubClient mqttClient(ethClient);
void printLocal() {
LCD.setCursor(8, 0);
LCD.println("web terminal");
//mqttClient.publish("My_LED",printBuffer);//
LCD.setCursor(0, 1);
//LCD.println("");
}
void setup() {
Serial.begin(115200);
Serial2.begin(38400); //RX-16:Tx-17 SERIAL_8N1 web terminal out
bus.strategy.set_serial(&Serial2); // Pass the Serial object you want to use for communication
bus.begin();
bus.send_repeatedly(44, "B", 1, 1000000); // Send B to device 44 every second
// Serial2.println("Hello Serial 2");
mqttClient.setServer("broker.mqttdashboard.com",1883);
// client is now ready for use
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to"); //Connecting to
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
if(mqttClient.connect ("LED-demo",NULL,NULL)) {
Serial.print ("Connected to MQTT Broker");
} else {
Serial.print("MQTT Broker connection failed");
Serial.print (mqttClient.state());
delay(200);
}
delay(3000);
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
}
void loop() {
printLocal();
bus.update();
//Serial2.println("Hello Serial2 !!");
delay(1000);
}