Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #define mqtt_server "192.168.1.148"
- #define mqtt_user "your_username"
- #define mqtt_password "your_password"
- const char* ssid = "Tower of Power";
- const char* password = "m00m00m00";
- #include "DHT.h"
- // DHT 11 sensor
- #define DHTPIN 2
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE, 15);
- WiFiClient wifiClient;
- PubSubClient client(wifiClient);
- void setup() {
- Serial.begin(115200);
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- client.setServer(mqtt_server, 1883);
- client.connect(mqtt_server);
- dht.begin();
- }
- void loop() {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // create a temporary buffer (aka string)
- char msg[16];
- // format the float into the string buffer
- sprintf(msg, "%0.2f", h);
- // publish the temp buffer, containing our float as a string
- client.publish("chicken1/humidity", msg);
- Serial.print("Humdity: ");
- Serial.println(msg);
- // do the same thing for temperature
- sprintf(msg, "%0.2f", t);
- client.publish("chicken1/temp", msg);
- Serial.print("Temp: ");
- Serial.println(msg);
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement