Skip to content

Instantly share code, notes, and snippets.

@joseftsch
Created March 4, 2017 16:38
Show Gist options
  • Save joseftsch/e8d615632bf59cab233446e9a3686177 to your computer and use it in GitHub Desktop.
Save joseftsch/e8d615632bf59cab233446e9a3686177 to your computer and use it in GitHub Desktop.
This gist is all about connecting a Wemos 1D to your WiFi and publish SHT30 temperature and humidity stats to your MQTT broker
/*
*
* This gist is all about connecting a Wemos 1D to your WiFi and publish SHT30 temperature and humidity stats to your MQTT broker
* Hardware used: Wemos 1D, Wemos SHT30 shield
* MQTT broker used: mosquitto
*
* This Arduino code uses code blocks/snippets/ideas from other open source resources
* Links to some of them:
* https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi
* https://github.com/knolleary/pubsubclient
*/
// include libraries
#include <ESP8266WiFi.h>
#include <WEMOS_SHT3X.h>
#include <PubSubClient.h>
SHT3X sht30(0x45);
WiFiClient espClient;
PubSubClient client(espClient);
// variables
const char* ssid = "wifi_ssid";
const char* password = "wifi_password";
const char* mqtt_server = "1.2.3.4"; // ip address of your mqtt broker
const char* mqtt_username = "wemosroom1";
const char* mqtt_password = "password";
const char* temptopic = "home/indoor/room1/temperature"; // Topic used to publish the temperature values
const char* humitopic = "home/indoor/room1/humidity"; // Topic used to publish the humidity values
const char* mqttnodename = "WemosSHT30_room1"; // "node name" for the mqtt client
char tempstring[15];
char humistring[15];
void setup() {
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA); // Setting WiFi into client mode
WiFi.begin(ssid, password); // log in to Wifi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
client.setServer(mqtt_server, 1883); // establish connection to MQTT broker
delay(500);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Trying to connect to MQTT broker with username and password
if (client.connect(mqttnodename, mqtt_username, mqtt_password)) {
Serial.println("Connected to MQTT server");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
sht30.get(); //get data from SHT30 sensor
dtostrf(sht30.cTemp, 7, 2 , tempstring); // convert float temperature value to char
dtostrf(sht30.humidity, 7, 2 , humistring); // convert float humidity value to char
Serial.print("Publish Temperature message: ");
Serial.println(tempstring);
Serial.print("Publish Humidity message: ");
Serial.println(humistring);
client.publish(temptopic, tempstring); //publish temperature data into topic
client.publish(humitopic, humistring); //publish humidity data into topic
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment