Skip to content

Instantly share code, notes, and snippets.

@profesor79
Created April 7, 2024 09:10
Show Gist options
  • Save profesor79/9b311399e0894a3b8b3d9167f6f34a00 to your computer and use it in GitHub Desktop.
Save profesor79/9b311399e0894a3b8b3d9167f6f34a00 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <WiFi.h>
#define MYPORT_TX 18 //16
#define MYPORT_RX 16
#include "EspMQTTClient.h"
unsigned long previousMillis = 0;
// constants won't change:
const long interval = 250;
EspMQTTClient client(
"wifi",
"pass",
"192.168.1.xx", // MQTT Broker server ip
"", // Can be omitted if not needed
"", // Can be omitted if not needed
"juntect", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
String inputString = ""; // a String to hold incoming data
String queueName = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
bool firstRun = true;
void setup() {
inputString.reserve(50);
queueName.reserve(20);
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, MYPORT_RX, MYPORT_TX);
// Optional functionalities of EspMQTTClient
// client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overridded with enableHTTPWebUpdater("user", "password").
client.enableOTA("ns"); // Enable OTA (Over The Air) updates. Password defaults to MQTTPassword. Port is the default OTA port. Can be overridden with enableOTA("password", port).
// client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
//start reading
Serial1.println(":R51=1,2,1,");
}
// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
// Publish a message to "mytopic/test"
client.publish("greg/juntech/test", "startered"); // You can activate the retain flag by setting the third parameter to true
}
void serialEvent1() {
while (Serial1.available()) {
// get the new byte:
char inChar = (char)Serial1.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
client.publish("greg/juntech/part" , inputString);
}
}
void loop()
{
unsigned long currentMillis = millis();
// save the last time you blinked the LED
previousMillis = currentMillis;
// print the string when a newline arrives:
if (stringComplete) {
// clear the string:
client.publish("greg/juntech/full" , inputString);
inputString = "";
stringComplete = false;
}
delay(5000);
client.publish("greg/juntech/debug" , "sending request");
Serial1.println(":R50 =1,2, 1,");
client.publish("greg/juntech/debug" , "request sent");
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment