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.