Hello! I am using an ultrasonic sensor with esp8266. I want to publish the sensor values to MQTT but it publishes only one value. Please guide me on how can I publish more than 1 value?
Here is my code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
int respons;
int responss;
// network setting.
const char* ssid = "-----";
const char* password = "--------";
const char* mqtt_server = "broker.mqtt-dashboard.com";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
Serial.begin(19200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Serial.begin(19200);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
//if you MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
// Once connected, publish an announcement...
//char dataArray[10]; //an array to put data in
//sprintf(dataArray, "%d", responss); //turn the data into a C string
//client.publish("Topic", dataArray); //publish the data
//client.publish("Topic", dataArray1);
//delay(3000);
// ... and resubscribe
//client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
//respons = Serial.read();
if (now - lastMsg > 2000) {
lastMsg = now;
//++value;
String msg = "sensor value:";
msg = msg + responss;
char dataArray[58]; //an array to put data in
msg.toCharArray(dataArray, 58);
Serial.println(dataArray);
client.publish("Topic", dataArray);
//sprintf(dataArray, "%d", responss); //turn the data into a C string
//client.publish("Topic", dataArray);
//client.publish("Topic", dataArray); //publish the data
//client.publish("Topic", "sensor");
}
// sensor programming
Serial.begin(19200);
byte poll[] = {0xAF, 0xFC, 0xFE, 0x40};
Serial.write(poll, sizeof(poll));
delay(3000);
int x = 6;
while (x > 0)
{
responss = Serial.read();
Serial.println(responss, HEX);
delay(500);
x--;
}
Serial.end();
//resetFunc();
}
/*void output()
{
Serial.print("the value of responss is ");
Serial.println(responss);
}*/