Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Piman
#26452 Hi I'm trying to send data from my ESP8266 sensor which is a HTU21d to MQTT but seem to have the problem of the correct string in order to send that data hopefully someone can help with how I can do this all point me in the right direction thanks Andy
This is a sample of my code.
[code]#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include "HTU21D.h"
const char *ssid = "my"; // cannot be longer than 32 characters!
const char *pass = "my"; //
void callback(const MQTT::Publish& pub) {
Serial.print("Incoming: "); Serial.println(pub.payload_string());
}
WiFiClient wclient;
PubSubClient client(wclient, "mymqtt");
//Create an instance of the object
HTU21D myHumidity;
void setup() {
// Setup console
Serial.begin(115200);
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
myHumidity.begin();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);

if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
Serial.println("Connecting to MQTT server");
if (client.connect(MQTT::Connect("")
.set_auth("mymqtt", "mypass"))) {
Serial.println("Connected to MQTT server");
client.set_callback(callback);
client.subscribe("inTopic");
} else {
Serial.println("Could not connect to MQTT server");
}
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();

if (isnan(humd) || (isnan(temp)
// return;
String temp = pub,tempstring());
Serial.println(temp);

bool client.publish("/root/""temp"); char* payload)(temp));
client.publish("/root/""humd",String(humd));
}
}/code]
User avatar
By ridge
#26503 Have you verified that your mqtt broker is running by opening a couple of terminal windows and playing around with the
mosquitto pub and sub routines?

Assuming that the mosquitto message broker is installed and running at IP 192.168.1.114
Terminal window 1 listens to and displays everything passing through the broker.
I usually leave a terminal window open with this command active as a mqtt sniffer during development.
mosquitto_sub -h 192.168.1.114 -v -t '#'

Terminal window 2:
mosquitto_pub -h 192.168.1.114 -t /out/device/led2 -m "ON"

You should see the topic and payload from window 2 show up in window 1.


This is a snippit of code that is running OK for me.

Code: Select all   if (mqttClient.connected()){
      lightsensor = analogRead(A0);
       char sensor[8];
       itoa(lightsensor,sensor,10);  // integer to string, base 10 (decimal)
   
       if (mqttClient.publish("esp_117/light_sensor", sensor)) {
         Serial.println("mqtt light sensor publish ok");
       }
       else {
         Serial.println("mqtt light sensor publish failed");
       }
      }
    }
 
User avatar
By KmanOz
#26946 Here's a seperate little routine I have running that works fine. Hope it helps :D

Code: Select allvoid getTemp() {
  Serial.print(F("DHT read . . . "));
  float dhtH, dhtT;
  dhtH = dht.readHumidity();
  dhtT = (dht.readTemperature() - TEMP_OFFSET);  // I adjust my DHT to my Oregon Scientific room sensor which is accurate
  if (isnan(dhtH) || isnan(dhtT)) {
    mqttClient.publish(MQTT_TOPIC"/DHT","\"DHT Read Error\"");
    Serial.println(F("ERROR"));
    return;
  }
  String pubString = "{\"Temp\": " + String(dhtT) + ", " + "\"Humidity\": " + String(dhtH) + "}";
  pubString.toCharArray(message_buff, pubString.length()+1);
  mqttClient.publish(MQTT_TOPIC"/DHT",message_buff);
  Serial.println(F("OK"));
}


It outputs the data as a valid JSON string but of course you can modify it for whatever your purposes are.

Regards Kman