Guest User

mqtt

a guest
Dec 8th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. #include "DHT.h"
  5.  
  6. #define DHTTYPE DHT22
  7. long last;
  8. const int DHTPin = D2;
  9. const int APin = A0;
  10. int motion=2;
  11.  
  12. const char* ssid = "pi_ap";
  13. const char* password = "password";
  14. const char* mqttServer = "192.168.50.10";
  15. const int mqttPort = 1883;
  16. const char* mqttUser = "admin";
  17. const char* mqttPassword = "password";
  18. const char* mqttDeviceID = "ESP8266_01";
  19. const char* data;
  20. boolean pir;
  21.  
  22. DHT dht(DHTPin, DHTTYPE);
  23. WiFiClient espClient;
  24. PubSubClient client(espClient);
  25.  
  26. float temperature = 0;
  27. float humidity = 0;
  28. float Gas = 0;
  29. float LDR = 0;
  30. StaticJsonBuffer<200> jsonBuffer;
  31. JsonObject& root = jsonBuffer.createObject();
  32.  
  33. void setup() {
  34. Serial.begin(115200);
  35. dht.begin();
  36. pinMode(D6, OUTPUT);
  37. client.setServer(mqttServer, mqttPort);
  38. }
  39.  
  40. void reconnect() {
  41. WiFi.begin(ssid, password);
  42. while (WiFi.status() != WL_CONNECTED) {
  43. delay(500);
  44. Serial.println("Connecting to WiFi..");
  45. }
  46. Serial.println("Connected to the WiFi network");
  47.  
  48. while (!client.connected()) {
  49. Serial.println("Connecting to MQTT...");
  50.  
  51. if (client.connect(mqttDeviceID, mqttUser, mqttPassword )) {
  52.  
  53. Serial.println("connected");
  54.  
  55. } else {
  56. Serial.print("failed with state ");
  57. Serial.print(client.state());
  58. delay(3000);
  59. }
  60. }
  61. }
  62.  
  63. void readSensor() {
  64. float h = dht.readHumidity();
  65. float t = dht.readTemperature();
  66. digitalWrite(D6,LOW);
  67. delay(2);
  68. float sensorValue = analogRead(A0);
  69. LDR = (10*sensorValue/1023)*100;
  70.  
  71. digitalWrite(D6,HIGH);
  72. delay(2);
  73. float sensorValue2 = analogRead(A0);
  74. Gas = ((1,5*sensorValue2) / 1023)*100;
  75. pir = digitalRead(D1);
  76. if (pir == LOW) { motion = 0;}
  77. else { motion = 1; }
  78.  
  79. if (isnan(h) || isnan(t)) {
  80. Serial.println("Failed to read from DHT sensor!");
  81. temperature = 0;
  82. humidity = 0;
  83. return;
  84. }else {
  85. temperature = t;
  86. humidity = h;
  87. }
  88.  
  89. }
  90. void preparedata(){
  91. root["myName"] = mqttDeviceID;
  92. root["temp"] = temperature;
  93. root["humid"] = humidity;
  94. root["motion"] = motion;
  95. root["gas"] = Gas;
  96. root["LDR"] = LDR;
  97. }
  98.  
  99. void MinuteDelay(int number){
  100. for (int i = 0; i < number; ++i)
  101. {
  102. // use 6 10 second delays for each minute
  103. for(int j=0; j<6 ;j++)
  104. {
  105. Serial.print("*");
  106. delay(10000);
  107. }
  108. }
  109. }
  110.  
  111. void loop()
  112. {
  113.  
  114. if (!client.connected()) {
  115. reconnect();
  116. }
  117. if(millis()-last>300000){ //send message after every 5 minute
  118. last=millis();
  119. delay(550);//try to change this delay if you get problem in sending message
  120. client.loop();
  121. readSensor();
  122. preparedata();
  123. char JSONmessageBuffer[150];
  124. root.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  125. Serial.println(JSONmessageBuffer);
  126. client.publish("esp8266/esp_01/sensors", JSONmessageBuffer);
  127.  
  128.  
  129. }
  130.  
  131. }
Add Comment
Please, Sign In to add comment