Guest User

Untitled

a guest
Feb 19th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <PubSubClient.h>
  2. #include <ArduinoJson.h>
  3. #include <dht.h>
  4. #include <Arduino.h>
  5. #include <SPI.h>
  6. #include <SoftwareSerial.h>
  7. #include <LiquidCrystal.h>
  8. #include <Ethernet.h>
  9.  
  10. #define DHT11_PIN 7
  11. #define RX_PIN 2
  12. #define TX_PIN 3
  13.  
  14. IPAddress ip(169, 169, 100, 98);
  15. byte mac[] = {
  16. 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
  17. };
  18.  
  19. const char *mqtt_server = ""; // Add broker address
  20. const int mqtt_port = 0; // Set the port
  21. const char *mqtt_user = ""; // Add username
  22. const char *mqtt_pass = ""; // Add password
  23. const char *mqtt_client_name = "arduinoClient1"; // Client connections cant have the same connection name
  24.  
  25.  
  26. EthernetClient ethClient;
  27. PubSubClient client(ethClient);
  28.  
  29. SoftwareSerial pmSerial(RX_PIN, TX_PIN);
  30. dht DHT;
  31.  
  32. int pm1;
  33. int pm2_5;
  34. int pm10;
  35. unsigned long id;
  36. //File myFile;
  37. String s;
  38. StaticJsonBuffer<200> jsonBuffer;
  39. JsonObject& root = jsonBuffer.createObject();
  40.  
  41. void setup() {
  42. Serial.begin(57600);
  43. pmSerial.begin(9600);
  44.  
  45. id = 0;
  46. pm1 = 0;
  47. pm2_5 = 0;
  48. pm10 = 0;
  49.  
  50. if (Ethernet.begin(mac) == 0)
  51. {
  52. Serial.println("Failed to configure Ethernet using DHCP");
  53. // attempt with fixed ip addr
  54. Ethernet.begin(mac, ip);
  55. }
  56.  
  57. client.setServer(mqtt_server, mqtt_port);
  58. client.setCallback(callback);
  59.  
  60. delay(2000);
  61.  
  62. Serial.println(Ethernet.localIP());
  63. client.connect("arduinoClient", mqtt_user, mqtt_pass);
  64. Serial.print("rc=");
  65. Serial.print(client.state());
  66. Serial.print("\n");
  67. }
  68.  
  69. void loop() {
  70. int index = 0;
  71. char value;
  72. char previousValue;
  73.  
  74. if (!client.connected())
  75. {
  76. if (client.connect("arduinoClient", mqtt_user, mqtt_pass)) {
  77. Serial.println("connected");
  78. }
  79. }
  80.  
  81. while (pmSerial.available()) {
  82. value = pmSerial.read();
  83. if ((index == 0 && value != 0x42) || (index == 1 && value != 0x4d)) {
  84. Serial.println("Cannot find the data header.");
  85. return;
  86. }
  87.  
  88. if (index == 4 || index == 6 || index == 8 || index == 10 || index == 12 || index == 14) {
  89. previousValue = value;
  90. }
  91. else if (index == 5) {
  92. pm1 = 256 * previousValue + value;
  93. root["pm1"] = abs(pm1);
  94. }
  95. else if (index == 7) {
  96. pm2_5 = 256 * previousValue + value;
  97. root["pm2_5"] = abs(pm2_5);
  98. }
  99. else if (index == 9) {
  100. pm10 = 256 * previousValue + value;
  101. root["pm10"] = abs(pm10);
  102.  
  103. }
  104. else if (index > 15) {
  105. break;
  106. }
  107.  
  108. index++;
  109. }
  110.  
  111. while (pmSerial.available()) pmSerial.read();
  112.  
  113. int chk = DHT.read11(DHT11_PIN);
  114.  
  115. if (DHT.temperature == -999 || DHT.humidity == -999) {
  116. root["temperature"] = "N/A";
  117. root["humidity"] = "N/A";
  118. } else {
  119. root["temperature"] = DHT.temperature;
  120. root["humidity"] = DHT.humidity;
  121. }
  122.  
  123. sendResults();
  124.  
  125. id++;
  126.  
  127. delay(5000);
  128. }
  129.  
  130. void sendResults() {
  131. // publish to MQTT
  132. char jsonChar[100];
  133. root.printTo(jsonChar);
  134. Serial.println(client.publish("arduino", jsonChar));
  135.  
  136.  
  137. // debug to serial
  138. root.printTo(Serial);
  139. Serial.print('\n');
  140. }
  141.  
  142.  
  143. // Handles messages arrived on subscribed topic(s)
  144. void callback(char* topic, byte* payload, unsigned int length) {
  145. }
Add Comment
Please, Sign In to add comment