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]