Created
August 20, 2016 16:08
-
-
Save what-name/d6b985768daff5fd091b1c26302bb3f0 to your computer and use it in GitHub Desktop.
ESP8266 MQTT Weather station
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PubSubClient.h> | |
#include <ESP8266WiFi.h> | |
#include <DHT.h> | |
#define wifi_ssid "ssid" //your Wifi SSID | |
#define wifi_password "password123" //your Wifi password | |
int DHTPIN = 2; //the pin your dht board's signal is connected to | |
#define DHTTYPE DHT11 //the type of dht board you are using - DHT11, DHT22... | |
#define ClientID "BalconyESP8266" //the clientID what your MQTT server will see | |
#define username "myuser" //your MQTT server username | |
#define password "mypassword1234" //your MQTT server password | |
#define topic "home/balcony/temperature" //your MQTT server topic | |
#define server "http://cloudmqtt.com" //your MQTT server - f.e http://cloudmqtt.com or just the name on local network | |
int port = 1883; //MQTT server's port | |
int humidity = 0; | |
int temperature = 0; | |
char MQTTTemp[] = "1"; | |
DHT dht(DHTPIN, DHTTYPE); | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
void setup() { | |
Serial.begin(115200); | |
setup_wifi(); | |
dht.begin(); | |
} | |
void setup_wifi() { | |
delay(1000); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(wifi_ssid); | |
WiFi.begin(wifi_ssid, wifi_password); //connects to wifi | |
WiFi.mode(WIFI_STA); //sets the module to connect to AP only and won't act as hotspot | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); //prints network details | |
} | |
void getTemp () { | |
delay(5000); | |
humidity = dht.readHumidity(); | |
temperature = dht.readTemperature(); //reads dht sensor | |
sprintf(MQTTTemp, "%d", temperature); //converts dht int to char format | |
if (isnan(temperature)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
Serial.print("Humidity: "); | |
Serial.print(humidity); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(temperature); | |
Serial.println(" *C "); | |
} | |
void connectMQTT () { | |
client.setServer(server, port); | |
if (client.connect(ClientID, username, password)) { //connects to MQTT server | |
Serial.println("MQTT Connected!"); | |
} else { | |
Serial.println("MQTT *NOT* Connected...reconnecting..."); | |
} | |
} | |
void loop() { | |
delay(5000); | |
getTemp(); | |
if(WiFi.status() == WL_CONNECTED) { | |
Serial.println("Wifi is connected."); | |
} else { | |
Serial.println("Wifi is NOT connected...reconnecting"); | |
setup_wifi(); | |
} | |
connectMQTT(); | |
if(client.publish (topic, MQTTTemp)) { //sends data to MQTT server | |
Serial.println("Payload sent!"); | |
} else { | |
Serial.println("Payload *NOT* sent."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment