Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <DHT.h>
  4. #include <PubSubClient.h>
  5.  
  6. /************************* DHT22 Sensor *********************************/
  7. #define DHTTYPE DHT22
  8. #define DHTPIN 02
  9.  
  10. /************************* WiFi Access Point *********************************/
  11. const char* ssid = "TP-LINK_2.4GHz_B67322";
  12. const char* password = "5a0z3w8g4Q3m5x8";
  13.  
  14. /************************* MQTT Server *********************************/
  15. char* mqtt_server = "192.168.0.246";
  16. int mqtt_server_port = 1883;
  17. const char* mqtt_user = "w7m7HV";
  18. const char* mqtt_password = "w7m7HV";
  19. String message = "";
  20. String topicTemp = "Temp";
  21. String topicHumid = "Humid";
  22.  
  23. /************************* ESP8266 WiFiClient *********************************/
  24. WiFiClient wifiClient;
  25.  
  26. /************************* MQTT client *********************************/
  27. PubSubClient client(mqtt_server, mqtt_server_port, wifiClient );
  28.  
  29. /************************* DHT Sensor *********************************/
  30. DHT dht(DHTPIN, DHTTYPE, 11);
  31.  
  32.  
  33. float humidity, temp_f; // Values read from sensor
  34.  
  35. unsigned long previousMillis = 0; // will store last temp was read
  36. const long interval = 2000; // interval at which to read sensor
  37.  
  38. /*************not used yet, for subscription of messages ******************************/
  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. }
  48.  
  49. /************* Utility function to retrieve data from DHT22 ******************************/
  50. void gettemperature() {
  51.  // Wait at least 2 seconds seconds between measurements.
  52.  // if the difference between the current time and last time you read
  53.  // the sensor is bigger than the interval you set, read the sensor
  54.  // Works better than delay for things happening elsewhere also
  55.  unsigned long currentMillis = millis();
  56.  
  57.  if(currentMillis - previousMillis <= interval) {
  58.  // save the last time you read the sensor
  59.  previousMillis = currentMillis;
  60.  
  61.  // Reading temperature for humidity takes about 250 milliseconds!
  62.  // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
  63.  humidity = dht.readHumidity(); // Read humidity (percent)
  64.  temp_f = dht.readTemperature(); // Read temperature as Celsius
  65.  // Check if any reads failed and exit early (to try again).
  66.  if (isnan(humidity) || isnan(temp_f)) {
  67.  Serial.println("Failed to read from DHT sensor!");
  68.  return;
  69.  }
  70.  }
  71. }
  72.  
  73. /************* Functionname says it all! ******************************/
  74. void setup(void) {
  75.  
  76.  Serial.begin(115200);
  77.  dht.begin();
  78.  
  79.  // Create String for MQTT Topics
  80.  topicTemp = "iotdemo/temperature/"+ String( ESP.getChipId() );
  81.  topicHumid = "iotdemo/humidity/"+ String( ESP.getChipId() );
  82.  
  83.  Serial.print("Chip-ID =");
  84.  Serial.print ( ESP.getChipId() );
  85.  
  86.  // Connect to WiFi network
  87.  WiFi.begin(ssid, password);
  88.  Serial.print("\n\r \n\rConnecting to ");
  89.  Serial.println(ssid);
  90.  
  91.  
  92.  // Wait for connection
  93.  while (WiFi.status() != WL_CONNECTED) {
  94.  delay(500);
  95.  Serial.print(".");
  96.  }
  97.  Serial.println("\n\rESP8266 &amp;amp; DHT22 based temperature and humidity sensor working!");
  98.  Serial.print("\n\rIP address: ");
  99.  Serial.println(WiFi.localIP());
  100. }
  101.  
  102. /******* Utility function to connect or re-connect to MQTT-Server ********************/
  103. void reconnect() {
  104.  // Loop until we're reconnected
  105.  while (!client.connected()) {
  106.  Serial.print("Attempting MQTT connection to ");
  107.  Serial.print(mqtt_server);
  108.  Serial.print(" ");
  109.  
  110.  // Attempt to connect
  111.  if (client.connect(mqtt_server, mqtt_user, mqtt_password)) {
  112.  Serial.println("connected");
  113.  } else {
  114.  Serial.print("failed, rc=");
  115.  Serial.print(client.state());
  116.  Serial.println(" try again in 5 seconds");
  117.  
  118.  // Wait 5 seconds before retrying
  119.  delay(5000);
  120.  }
  121.  }
  122. }
  123.  
  124. /************* Functionname says it all! ******************************/
  125. void loop(void) {
  126.  
  127.  
  128.  if (!client.connected()) { // Connect to mqtt broker
  129.  reconnect();
  130.  }
  131.  client.loop();
  132.  
  133.  gettemperature(); // read sensordata
  134.  
  135.  // Now we can publish stuff!
  136.  message = "<value>" + String((int)temp_f) + "</value>";
  137.  
  138.  Serial.print(F("\nSending temperature value "));
  139.  Serial.print(message);
  140.  client.publish(topicTemp.c_str(), message.c_str());
  141.  
  142.  message = "<value>" + String((int)humidity) + "</value>";
  143.  Serial.print(F("\nSending humidity value "));
  144.  Serial.print(message);
  145.  client.publish(topicHumid.c_str(), message.c_str());
  146.  
  147.  delay(1000);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement