Mqtt_Esp8266_Dhtv2.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //Thank you commuity for your help,obviously i'm a beginner and I hope this helps you but really all I did was use two awesome libraries DHT and PubSubclient
  2. //then combine the two great examples DHT_TEST and PubSubclient example mqtt_esp8266 and alter how I packed up for publishing
  3. //the tricky part for me was figuring out how to convert from a float supported by DHT.h to the string supported by PubSubclient see last section of code for that
  4. //This will publish the temperature and humidity from your DHT sensor connected to your ESP8266 to an Mqtt broker, I am using Mosquitto.
  5. #include <ESP8266WiFi.h>
  6. #include <PubSubClient.h>
  7. #include "DHT.h"
  8. DHT dht;
  9. WiFiClient espClient;
  10. PubSubClient client(espClient);
  11. long lastMsg = 0;
  12. int value = 0;
  13. // Configure your network here
  14. const char* ssid = "XXXXXX"; ///UPDATE with your network
  15. const char* password = "XXXXX"; ///Your password
  16. const char* mqtt_server = "XXXXXX"; ///your URL or IP address
  17. // Variables for printing temp and humidity
  18. String temp_str; //see last code block below use these to convert the float that you get back from DHT to a string =str
  19. String hum_str;
  20. char temp[50];
  21. char hum[50];
  22. //setting up Wifi didn't change this from the example PubSubclient example mqtt_esp8266
  23. void setup_wifi() {
  24. delay(10);
  25. // We start by connecting to a WiFi network
  26. Serial.println();
  27. Serial.print("Connecting to ");
  28. Serial.println(ssid);
  29. WiFi.begin(ssid, password);
  30. while (WiFi.status() != WL_CONNECTED) {
  31. delay(500);
  32. Serial.print(".");
  33. }
  34. Serial.println("");
  35. Serial.println("WiFi connected");
  36. Serial.println("IP address: ");
  37. Serial.println(WiFi.localIP());
  38. }
  39. void callback(char* topic, byte* payload, unsigned int length) {
  40. Serial.print("Message arrived [");
  41. Serial.print(topic);
  42. Serial.print("] ");
  43. for (int i = 0; i < length; i++) {
  44. Serial.print((char)payload[i]);
  45. }
  46. Serial.println();
  47. // Switch on the LED if an 1 was received as first character
  48. if ((char)payload[0] == '1') {
  49. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  50. // but actually the LED is on; this is because
  51. // it is acive low on the ESP-01)
  52. } else {
  53. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  54. }
  55. }
  56. void reconnect() {
  57. // Loop until we're reconnected
  58. while (!client.connected()) {
  59. Serial.print("Attempting MQTT connection...");
  60. // Attempt to connect
  61. if (client.connect("ESP8266Client")) {
  62. Serial.println("connected");
  63. // Once connected, publish an announcement...
  64. client.publish("outTopic", "hello world");
  65. // ... and resubscribe
  66. client.subscribe("inTopic");
  67. } else {
  68. Serial.print("failed, rc=");
  69. Serial.print(client.state());
  70. Serial.println(" try again in 5 seconds");
  71. // Wait 5 seconds before retrying
  72. delay(5000);
  73. }
  74. }
  75. }
  76. void setup()
  77. {
  78. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  79. Serial.begin(115200);
  80. setup_wifi();
  81. client.setServer(mqtt_server, 1883);
  82. client.setCallback(callback);
  83. Serial.println();
  84. Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)"); //debug serial print
  85. // Call out your data pin for your DHT sensor here
  86. dht.setup(13);
  87. //
  88. }
  89. //--(end setup )---
  90. void loop()
  91. {
  92. if (!client.connected()) {
  93. reconnect();
  94. }
  95. client.loop();
  96. delay(dht.getMinimumSamplingPeriod());
  97. //this is where you get the data from the sensor
  98. float humidity = dht.getHumidity();
  99. float temperature = dht.getTemperature();
  100. float ftemp = dht.toFahrenheit(temperature);
  101. //Using Serial print to debug
  102. Serial.print(dht.getStatusString());
  103. Serial.print("\t");
  104. Serial.print(humidity, 1);
  105. Serial.print("\t\t");
  106. Serial.print(temperature, 1);
  107. Serial.print("\t\t");
  108. Serial.println(dht.toFahrenheit(temperature), 1);
  109. //counter for the messages, see if I am missing any on the Mqtt broker
  110. long now = millis();
  111. if (now - lastMsg > 2000) {
  112. lastMsg = now;
  113. ++value;
  114. //Preparing for mqtt send
  115. temp_str = String(ftemp); //converting ftemp (the float variable above) to a string
  116. temp_str.toCharArray(temp, temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...
  117. hum_str = String(humidity); //converting Humidity (the float variable above) to a string
  118. hum_str.toCharArray(hum, hum_str.length() + 1); //packaging up the data to publish to mqtt whoa...
  119. Serial.print("Publish message: ");//all of these Serial prints are to help with debuging
  120. client.publish("Workshop Temperature", temp); //money shot
  121. client.publish("Workshop Humidity", hum);
  122. }
  123. }