Problems with uploading temperature to Thingspeak MQTT

I tried to get some data from an ESP8266 NodeMCU with BMP180 temperature sensor to Thingspeak over MQTT.

This is my code:

#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>

// WiFi-Verbindung
const char* ssid = "DAMNSON NEU";
const char* password = "........";
WiFiClient wifiClient;

// MQTT-Verbindung
const char* mqtt_server = "mqtt3.thingspeak.com";
const char* mqtt_username = "usr";
const char* mqtt_password = "pwd";
const char* mqtt_id = "id";
const char* topic = "channels/2056214/publish/writeapi";
//PubSubClient client(wifiClient);

PubSubClient client(mqtt_server, 1883, wifiClient);

// BMP180-Sensor
Adafruit_BMP085 bmp;

void setup() {
  Serial.begin(115200);
  delay(10);
  
  // WLAN verbinden
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // MQTT-Verbindung
  client.setServer(mqtt_server, 1883);
  while (!client.connected()) {
    Serial.println("Connecting to MQTT Broker...");
    if (client.connect(mqtt_id, mqtt_username, mqtt_password )) {
      Serial.println("Connected to MQTT Broker");
    } else {
      Serial.print("Failed with state ");
      Serial.println(client.state());
      delay(2000);
    }
  }
  
  // BMP180 initialisieren
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP180 sensor, check wiring!");
    while (1) {}
  }
}

void loop() {
  // Temperatur auslesen
  float temperature = bmp.readTemperature();

  // Temperatur über MQTT veröffentlichen
  String payload = String("field1=") + String(temperature);
  client.publish(topic, payload.c_str());
mqttPublish 
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");

  delay(10000); // 10 Sekunden warten
}

The Code runs and I get serial output but i cannot get any data to Thingspeak.
Maybe someone of you can help me with this.

The broker on things speak should have a log file associated with it. See if you can access it.
Also, what is "mqttPublish" right above the serial print statements. Is that a comment.?

How can I access the log file? I can't find anything on the website. Do I have to request it thru the code?

The "mqttpublish" is just something I tried and forgot to remove.

When you post do not use your true credentials. Use something in its place like "usrname", "password" etc...

Usually a broker will have a log file associated with it to track whats been happening. Is the broker on thingspeak something you installed ? You could also just set up a local broker on your pc and get that working first. You would learn a lot about mqtt then when that is working get the thing speak broker working.

Thank you for the hint. This is only for testing purposes so I will reset the password anyway.

You don't have to install Thingspeak broker explicitly, just set it up on the website. I thought I could maybe use it to realize some wifi temperature sensors in a quick way. Seems like I'll have to look into it a bit more. Thanks for the tip with the local MQTT broker, I will try that now.

Setting up a local mqtt broker was not that easy for me. I used the "eclipse" mqtt broker. It took me a few days to understand it completely. So it is an investment in time but for me but it was worth it. It is easy way to get data from your embedded device i.e. uno, esp32 onto a desktop computer. I used Node-red on my desktop as subscriber to a topic that the an esp32 was publishing. You know the usual temperature, humidity stuff. There are probably some good videos or tutorials out there to get you started. Good luck.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.