Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/
- // Get ESP8266 going with Arduino IDE
- // - https://github.com/esp8266/Arduino#installing-with-boards-manager
- // Required libraries (sketch -> include library -> manage libraries)
- // - PubSubClient by Nick ‘O Leary
- // - DHT sensor library by Adafruit
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <DHT.h>
- #define wifi_ssid "IA-Group3"
- #define wifi_password "industrie4.0"
- #define mqtt_server "192.168.254.1"
- // #define mqtt_user "your_username"
- // #define mqtt_password "your_password"
- #define humidity_topic "DHT22_1"
- #define temperature_topic "DHT22_1"
- #define DHTTYPE DHT22
- #define DHTPIN 3
- WiFiClient espClient;
- PubSubClient client(espClient);
- DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
- // NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
- // might need to increase the threshold for cycle counts considered a 1 or 0.
- // You can do this by passing a 3rd parameter for this threshold. It's a bit
- // of fiddling to find the right value, but in general the faster the CPU the
- // higher the value. The default for a 16mhz AVR is a value of 6. For an
- // Arduino Due that runs at 84mhz a value of 30 works.
- // Example to initialize DHT sensor for Arduino Due:
- //DHT dht(DHTPIN, DHTTYPE, 30); https://github.com/esp8266/Arduino/issues/118
- void setup() {
- Serial.begin(115200);
- dht.begin();
- setup_wifi();
- client.setServer(mqtt_server, 1883); // TCP/IP port 1883 is reserved with IANA for use with MQTT.
- }
- void setup_wifi() {
- delay(10);
- // Start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(wifi_ssid);
- WiFi.begin(wifi_ssid, wifi_password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- // If you do not want to use a username and password, change next line to
- // if (client.connect("ESP8266Client")) {
- // if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
- if (client.connect("ESP8266Client")) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- bool checkBound(float newValue, float prevValue, float maxDiff) {
- return !isnan(newValue) &&
- (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
- }
- long lastMsg = 0;
- float temp = 0.0;
- float hum = 0.0;
- float diff = 1.0;
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- long now = millis();
- if (now - lastMsg > 360) {
- lastMsg = now;
- float newTemp = dht.readTemperature();
- float newHum = dht.readHumidity();
- if (checkBound(newTemp, temp, diff)) {
- temp = newTemp;
- Serial.print("New temperature:");
- Serial.println(String(temp).c_str());
- client.publish(temperature_topic, String(temp).c_str(), true);
- }
- if (checkBound(newHum, hum, diff)) {
- hum = newHum;
- Serial.print("New humidity:");
- Serial.println(String(hum).c_str());
- client.publish(humidity_topic, String(hum).c_str(), true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement