Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include "DHT.h"
  4.  
  5. /*****************************************************
  6. * LED settings
  7. ******************************************************/
  8. int LED_BUILTIN = 2;
  9.  
  10. /*****************************************************
  11. * MQTT settings
  12. * WIFI settings
  13. *****************************************************/
  14. const char* ssid = "w-eki";
  15. const char* password = "password";
  16. const char* mqttServer = "192.168.25.20";
  17. const int mqttPort = 1883;
  18. const char* mqttUser = "yourMQTTuser";
  19. const char* mqttPassword = "yourMQTTpassword";
  20.  
  21.  
  22. WiFiClient espClient;
  23. PubSubClient client(espClient);
  24.  
  25. /*****************************************************
  26. * ESP32 DHT Reading
  27. * DHT Input: ==> GPIO23.
  28. *****************************************************/
  29.  
  30. #define DHTPIN 33
  31. #define DHTTYPE DHT22
  32. DHT dht(DHTPIN, DHTTYPE);
  33. float localHum = 0;
  34. float localTemp = 0;
  35. char temp[35];
  36. char hum[35];
  37.  
  38.  
  39. /*****************************************************
  40. * Global settings
  41. *
  42. *****************************************************/
  43. int LoopCounter = 0;
  44.  
  45. /***************************************************
  46. * Get indoor Temp/Hum data
  47. ****************************************************/
  48. void getDHT()
  49. {
  50. float tempIni = localTemp;
  51. float humIni = localHum;
  52. localTemp = dht.readTemperature();
  53. localHum = dht.readHumidity();
  54. if (isnan(localHum) || isnan(localTemp)) // Check if any reads failed and exit early (to try again).
  55. {
  56. localTemp = tempIni;
  57. localHum = humIni;
  58. return;
  59. }
  60. }
  61.  
  62. void callback(char* topic, byte* payload, unsigned int length)
  63. {
  64. Serial.print("Message arrived in topic: ");
  65. Serial.println(topic);
  66. char msg[16];
  67. Serial.print("Message:");
  68. for (int i = 0; i < length; i++)
  69. {
  70. Serial.print((char)payload[i]);
  71. if ((char)payload[i] != ',')
  72. {
  73. msg[i]=(char)payload[i];
  74. }
  75. else
  76. {
  77. break;
  78. }
  79. }
  80. Serial.println();
  81. Serial.println("-----------------------");
  82. if ((char)payload[length-1] == '1')
  83. {
  84. digitalWrite(LED_BUILTIN, HIGH);
  85. }
  86. else
  87. {
  88. digitalWrite(LED_BUILTIN, LOW);
  89. }
  90. /*
  91. * TODO SOlve this!!
  92. * char ok_msg[]="ok=";
  93. strcat(ok_msg,msg);
  94. Serial.print("Lets reply back > ");
  95. Serial.println(ok_msg);
  96. client.publish("v1/r5462343-05b8-11e7-fj874-bdbd62fda600/things/654jk433-063e-11e7-a905-987453jfda43/reponse/",ok_msg);
  97. */
  98. }
  99.  
  100. void connect_mqtt()
  101. {
  102. while (!client.connected())
  103. {
  104. Serial.println("Connecting to MQTT...");
  105. if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
  106. Serial.println("connected to MQTT");
  107. }
  108. else
  109. {
  110. Serial.print("failed with state ");
  111. Serial.print(client.state());
  112. delay(2000);
  113. }
  114. }
  115. }
  116.  
  117. void setup()
  118. {
  119. pinMode(LED_BUILTIN, OUTPUT);
  120. Serial.begin(115200);
  121. WiFi.begin(ssid, password);
  122. while (WiFi.status() != WL_CONNECTED)
  123. {
  124. delay(500);
  125. Serial.println("Connecting to WiFi..");
  126. }
  127. Serial.println("Connected to the WiFi network");
  128. client.setServer(mqttServer, mqttPort);
  129. client.setCallback(callback);
  130. connect_mqtt();
  131. client.subscribe("v1/r5462343-05b8-11e7-fj874-bdbd62fda600/things/654jk433-063e-11e7-a905-987453jfda43/cmd/152");
  132. Serial.println("Setting up DHT");
  133. dht.begin();
  134. }
  135.  
  136. void loop()
  137. {
  138. //Serial.println("Looping");
  139. LoopCounter++;
  140. client.loop();
  141. getDHT();
  142. //Serial.print("From DHT temp=");
  143. //Serial.println(localTemp);
  144. //Serial.print("From DHT hum=");
  145. //Serial.println(localHum);
  146. if (LoopCounter > 10)
  147. {
  148. if (!client.connected())
  149. {
  150. connect_mqtt();
  151. }
  152. digitalWrite(LED_BUILTIN, HIGH);
  153. Serial.println("Publishing to J");
  154. sprintf(hum,"analog_actuator=%g",localHum);
  155. Serial.println(hum);
  156. client.publish("v1/r5462343-05b8-11e7-fj874-bdbd62fda600/things/654jk433-063e-11e7-a905-987453jfda43/data/151",hum);
  157. sprintf(temp,"analog_actuator=%g",localTemp);
  158. delay(100);
  159. Serial.println(temp);
  160. client.publish("v1/r5462343-05b8-11e7-fj874-bdbd62fda600/things/654jk433-063e-11e7-a905-987453jfda43/data/150",temp);
  161. LoopCounter = 0;
  162. digitalWrite(LED_BUILTIN, LOW);
  163. }
  164. delay(2000);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement