i'm working on a voltage sensor that reads the input voltage and sends it through MQTT to a mobile application . i need help in the part of the code that is related to publishing the readings to the mqtt broker .
/* ---------- DO NOT EDIT ANYTHING IN THIS FILE
UNLESS YOU KNOW WHAT YOU ARE DOING---------- */
#include
<FS.h> //this
needs to be first, or it all crashes and burns...
#include
<Servo.h>
#include
<ESP8266WiFi.h>
//https://github.com/esp8266/Arduino
#include
<MQTTClient.h>
//https://github.com/256dpi/arduino-mqtt
#include
<DNSServer.h>
#include
<ESP8266WebServer.h>
#include
<WiFiManager.h>
//https://github.com/tzapu/WiFiManager
#include
<ArduinoJson.h>
//https://github.com/bblanchon/ArduinoJson
#include
<ESP8266mDNS.h>
#include
<ESP8266HTTPUpdateServer.h>
//define your default values here, if there are
different values in config.json, they are overwritten.
char
host[34];
char
mqtt_server[40];
char
mqtt_port[6] = "1883";
char
mqtt_topic[50];
char
mqtt_isAuthentication[7] = "FALSE";
char
mqtt_username[40];
char
mqtt_password[40];
char
update_username[40];
char
update_password[40];
char
update_path[34] = "/firmware";
//Unique device ID
const
char* mqttDeviceID;
//Form Custom SSID
String
ssidAP = "MK-FireSensor"
+ String(ESP.getChipId());
//flag for saving data
bool
shouldSaveConfig = false;
//callback notifying us of the need to save config
void
saveConfigCallback ()
{
shouldSaveConfig
= true;
}
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we
assume all detection has stopped
long unsigned int pause = 100;
//sensor variables
boolean lockLow = true;
boolean takeLowTime;
//the digital pin connected to the fire sensor's output
int sensorPin = 13;
ESP8266WebServer
httpServer(80);
ESP8266HTTPUpdateServer
httpUpdater;
WiFiClient
net;
MQTTClient
client;
unsigned
long lastMillis = 0;
void
connect();
void
setup()
{
if
(SPIFFS.begin())
{
if
(SPIFFS.exists("/config.json"))
{
//file exists,
reading and loading
File configFile =
SPIFFS.open("/config.json", "r");
if (configFile)
{
size_t size =
configFile.size();
// Allocate a buffer to store contents
of the file.
std::unique_ptr<char[]> buf(new
char[size]);
configFile.readBytes(buf.get(),
size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json =
jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success())
{
strcpy(host,
json["host"]);
strcpy(update_username,
json["update_username"]);
strcpy(update_password,
json["update_password"]);
strcpy(mqtt_isAuthentication,
json["mqtt_isAuthentication"]);
strcpy(mqtt_username, json["mqtt_username"]);
strcpy(mqtt_password,
json["mqtt_password"]);
strcpy(update_path,
json["update_path"]);
strcpy(mqtt_server,
json["mqtt_server"]);
strcpy(mqtt_port,
json["mqtt_port"]);
strcpy(mqtt_topic,
json["mqtt_topic"]);
}
else
{
}
}
}
}
else
{
}
//end read
// The extra
parameters to be configured (can be either global or just in the setup)
// After connecting,
parameter.getValue() will get you the configured value
// id/name placeholder/prompt default
length
WiFiManagerParameter
custom_text0("<p>Select your wifi network and type in your password,
if you do not see your wifi then scroll down to the bottom and press scan to
check again.</p>");
WiFiManagerParameter
custom_text1("<h1>Hostname/MQTT ID</h1>");
WiFiManagerParameter
custom_text2("<p>Enter a name for this device which will be used for
the hostname on your network and identify the device from MQTT.</p>");
WiFiManagerParameter
custom_host("name", "Device Name", host, 32);
WiFiManagerParameter
custom_text3("<h1>MQTT</h1>");
WiFiManagerParameter
custom_text4("<p>Enter the details of your MQTT server and then
enter the topic for which the device sends Fire Detector State MQTT messages
to. If your server requires authentication then set it to True and enter your
server credentials otherwise leave it at false and keep the fields blank.</p>");
WiFiManagerParameter
custom_mqtt_server("server", "MQTT Server IP", mqtt_server,
40);
WiFiManagerParameter
custom_mqtt_port("port", "MQTT Server Port", mqtt_port, 5);
WiFiManagerParameter
custom_mqtt_topic("topic", "MQTT Out Topic", mqtt_topic, 50);
WiFiManagerParameter
custom_mqtt_isAuthentication("isAuthentication", "MQTT Authentication?",
mqtt_isAuthentication, 7);
WiFiManagerParameter
custom_mqtt_username("userMQTT", "Username For MQTT
Account", mqtt_username, 40);
WiFiManagerParameter
custom_mqtt_password("passwordMQTT", "Password For MQTT
Account", mqtt_password, 40);
WiFiManagerParameter
custom_text5("<h1>Web Updater</h1>");
WiFiManagerParameter
custom_text6("<p>The web updater allows you to update the firmware
of the device via a web browser by going to its ip address or hostname
/firmware ex. 192.168.0.5/firmware you can change the update path below. The
update page is protected so enter a username and password you would like to use
to access it. </p>");
WiFiManagerParameter
custom_update_username("user", "Username For Web Updater",
update_username, 40);
WiFiManagerParameter
custom_update_password("password", "Password For Web
Updater", update_password, 40);
WiFiManagerParameter
custom_device_path("path", "Updater Path", update_path, 32);
WiFiManagerParameter
custom_text7("<p>*To reset device settings restart the device and
quickly move the jumper from RUN to PGM, wait 10 seconds and put the jumper
back to RUN.*</p>");
WiFiManagerParameter custom_text8("");
//WiFiManager
//Local intialization. Once its
business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config
save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
//wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99),
IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//add all your
parameters here
wifiManager.setCustomHeadElement("<style>.c{text-align:
center;} div,input{padding:5px;font-size:1em;} input{width:95%;}
body{text-align: center;font-family:oswald;} button{border:0;background-color:#313131;color:white;line-height:2.4rem;font-size:1.2rem;text-transform:
uppercase;width:100%;font-family:oswald;} .q{float: right;width: 65px;text-align:
right;} body{background-color: #575757;}h1 {color: white; font-family: oswald;}p
{color: white; font-family: open+sans;}a {color: #78C5EF; text-align: center;line-height:2.4rem;font-size:1.2rem;font-family:oswald;}</style>");
wifiManager.addParameter(&custom_text0);
wifiManager.addParameter(&custom_text1);
wifiManager.addParameter(&custom_text2);
wifiManager.addParameter(&custom_host);
wifiManager.addParameter(&custom_text3);
wifiManager.addParameter(&custom_text4);
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
wifiManager.addParameter(&custom_mqtt_topic);
wifiManager.addParameter(&custom_mqtt_isAuthentication);
wifiManager.addParameter(&custom_mqtt_username);
wifiManager.addParameter(&custom_mqtt_password);
wifiManager.addParameter(&custom_text5);
wifiManager.addParameter(&custom_text6);
wifiManager.addParameter(&custom_update_username);
wifiManager.addParameter(&custom_update_password);
wifiManager.addParameter(&custom_device_path);
wifiManager.addParameter(&custom_text7);
wifiManager.addParameter(&custom_text8);
//reset
settings - for testing
//wifiManager.resetSettings();
//set minimu
quality of signal so it ignores AP's under that quality
//defaults to 8%
//wifiManager.setMinimumSignalQuality();
//sets timeout
until configuration portal gets turned off
//useful to make it all retry or go to
sleep
//in seconds
wifiManager.setTimeout(120);
//fetches ssid
and pass and tries to connect
//if it does not connect it starts an
access point with the specified name
//here
"AutoConnectAP"
//and goes into a blocking loop
awaiting configuration
if
(!wifiManager.autoConnect(ssidAP.c_str()))
{
delay(3000);
//reset and try again, or maybe put it
to deep sleep
ESP.reset();
delay(5000);
}
//read updated
parameters
strcpy(host, custom_host.getValue());
strcpy(mqtt_server,
custom_mqtt_server.getValue());
strcpy(mqtt_port,
custom_mqtt_port.getValue