I’ve tried searching online but I can’t come up whit the right keywords and I can’t find anything about my problem.
Target receive a topic (temperatire sensor value) and send its content to Arduino IOTcloud so can I access to see it everywhere.
I’m using and ESP8266 to receive MQTT messages and mosquitto as a broker (HA), every time I publish a message via HA I'm able to receive it with the ESP8266 but I'm not able to update it ArduinoIOTcloud . I'm a beginner and I'm pretty confused mainly on how manage the two different configuration to manage the subscription to the "internal" MQTT and to the ArduinoIOTcloud ("external" MQTT) using Pubsubclient library. If I split the two thing, send the data to the cloud or receive topic in MQTT the two codes works fine, toghether as shown below seems that everithing work but the value in ArduinoIOCloud is not updated. The Arduino IOTcloud variable is temperatura_salotto, ...please help and be patient with my lack of knowladge, Thanks
#include "arduino_secrets.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* mqttServer = "192.168.0.xxx";
const int mqttPort = 1883;
const char* mqttUser = "secret";
const char* mqttPassword = "secret";
#include "thingProperties.h"
WiFiClient espClient;
PubSubClient client(espClient);
//PubSubClient client(MQTT_SERVER, 1883, espClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
//trasforma il payload in float
payload[length] = '\0';
String s = String((char*)payload);
float f = s.toInt();
Serial.println(f);
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
temperatura_salotto = f;
Serial.print("send data ");
Serial.println(temperatura_salotto);
//delay(10000);
ArduinoCloud.update();
WiFi.begin(SECRET_SSID, SECRET_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
client.subscribe("trogolo/tempera");
}
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(115200);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
WiFi.begin(SECRET_SSID, SECRET_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
client.subscribe("trogolo/tempera");
}
void loop() {
client.loop();
}