02-02-2024 05:00 AM
Hello,
I established a connection from my ESP8266 to the Labview Broker, but when I connect and subscribe to the topic, I receive the error message:
b:Serializer.FlattenedString.lvclass:unmarshalling.vi:3430001->OpenSerializer.lvlib:Serializer.lvclass:UnMarshall.vi:4390003->OpenSerializer.lvlib:Serializer.lvclass:UnMarshall (binary).vi:6550002->MQTT Base.lvlib:MQTT_Base.lvclass:Decode Publish Package.vi:2830001->MQTT Client.lvlib:Client.lvclass:Decode Application
Code from my ESP->
#include <Wire.h>
#include <Adafruit_SHT31.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
// Replace with your WiFi credentials
const char* ssid = "1234";
const char* password = "1234";
// Replace with your MQTT broker details
const char* mqtt_server = "123";
const int mqtt_port = 1883;
const char* mqtt_topic = "sensor_data";
// Create an instance of the SHT85 sensor
Adafruit_SHT31 sht;
// Create an instance of the WiFi client
WiFiClient espClient;
// Create an instance of the MQTT client
PubSubClient client(espClient);
void setup() {
// Start the serial communication
Serial.begin(9600);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Connect to MQTT broker
client.setServer(mqtt_server, mqtt_port);
// Initialize the SHT85 sensor
if (!sht.begin(0x44)) {
Serial.println("SHT85 not found. Check wiring!");
while (1);
}
}
void loop() {
// Read sensor data
float temperature = sht.readTemperature();
float humidity = sht.readHumidity();
// Print sensor data to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Publish sensor data to MQTT
if (client.connect("ESP8266Client")) {
char tempStr[10];
char humStr[10];
dtostrf(temperature, 4, 2, tempStr);
dtostrf(humidity, 4, 2, humStr);
client.publish(mqtt_topic, tempStr);
//client.publish(mqtt_topic, humStr);
client.disconnect();
}
// Wait for a while before reading the sensor again
delay(2000);
}
I have included all the information you need to know in this zip file.
Thanks for your support!