Pieterlpl Napisano Styczeń 12, 2020 Udostępnij Napisano Styczeń 12, 2020 Cześć. Mam mały problem z wysyłaniem kilku odczytanych parametrów na serwer przy pomocy protokołu MQTT. Jeden parametr (w tej chwili w kodzie jest to TEMPERATURA) jest bezproblemowo przesyłany, natomiast gdy chcę dorzucić kolejne (WILGOTNOSC, CISNIENIE) w taki sposób jak mam to w swoim programie, nie przesyłają się żadne wartości. Czy korzystaliście może kiedyś z biblioteki PubSubClient i udało się Wam przesyłać naraz kilka wartości? Może jest jakiś błąd w składni w tym moim programie? Z góry dziękuję za pomoc. #include <WiFi.h> #include <Wire.h> #include <Adafruit_BME280.h> #include <PubSubClient.h> #define WIFISSID "SSID" // Put your WifiSSID here #define PASSWORD "PASS" // Put your wifi password here #define TOKEN "TOKEN" // Put your Ubidots' TOKEN #define MQTT_CLIENT_NAME "RandomName" // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string; //it should be a random and unique ascii string and different from all other devices /**************************************** * Define Constants ****************************************/ #define TEMPERATURA "temperatura" #define CISNIENIE "cisnienie" #define WILGOTNOSC "wilgotnosc" #define DEVICE_LABEL "esp32" // Assig the device label char mqttBroker[] = "things.ubidots.com"; char payload[100]; char topic[150]; char topicSubscribe[100]; // Space to store values to send char str_temperaturaC[10]; char str_cisnienie[10]; char str_wilgotnosc[10]; Adafruit_BME280 bme; // I2C float temperaturaC = 0; float cisnienie = 0; float wilgotnosc = 0; /**************************************** * Auxiliar Functions ****************************************/ WiFiClient ubidots; PubSubClient client(ubidots); void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.println("Attempting MQTT connection..."); // Attemp to connect if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) { Serial.println("Connected"); client.subscribe(topicSubscribe); } else { Serial.print("Failed, rc="); Serial.print(client.state()); Serial.println(" try again in 2 seconds"); // Wait 2 seconds before retrying delay(2000); } } } /**************************************** * Main Functions ****************************************/ void setup() { Serial.begin(115200); WiFi.begin(WIFISSID, PASSWORD); bme.begin(0x76); pomiaryBME280(); Serial.println(); Serial.print("Wait for WiFi..."); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi Connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); client.setServer(mqttBroker, 1883); sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, TEMPERATURA, CISNIENIE, WILGOTNOSC); client.subscribe(topicSubscribe); } void loop() { if (!client.connected()) { client.subscribe(topicSubscribe); reconnect(); } pomiaryBME280(); sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL); sprintf(payload, "%s", ""); // Cleans the payload sprintf(payload, "{\"%s\":", TEMPERATURA); // Adds the variable label //sprintf(payload, "{\"%s\":", CISNIENIE); // Adds the variable label //sprintf(payload, "{\"%s\":", WILGOTNOSC); // Adds the variable label /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/ dtostrf(temperaturaC, 4, 2, str_temperaturaC); dtostrf(cisnienie, 4, 2, str_cisnienie); dtostrf(wilgotnosc, 4, 2, str_wilgotnosc); sprintf(payload, "%s {\"value\": %s}}", payload, str_temperaturaC); // Adds the value //sprintf(payload, "%s {\"value\": %s}}", payload, str_cisnienie); // Adds the value //sprintf(payload, "%s {\"value\": %s}}", payload, str_wilgotnosc); // Adds the value Serial.println("Publishing data to Ubidots Cloud"); client.publish(topic, payload); client.loop(); delay(1000); } void pomiaryBME280() { temperaturaC = bme.readTemperature(); cisnienie = bme.readPressure() / 100; wilgotnosc = bme.readHumidity(); Serial.print("Temperatura = "); Serial.print(temperaturaC, 2); Serial.print(" *C; "); Serial.print("Ciśnienie = "); Serial.print(cisnienie, 2); Serial.print("hPa; "); Serial.print("Wilgotność = "); Serial.print(wilgotnosc, 2); Serial.print("% "); } Link do komentarza Share on other sites More sharing options...
Pomocna odpowiedź
Bądź aktywny - zaloguj się lub utwórz konto!
Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony
Utwórz konto w ~20 sekund!
Zarejestruj nowe konto, to proste!
Zarejestruj się »Zaloguj się
Posiadasz własne konto? Użyj go!
Zaloguj się »