Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. #define DEBUG false
  6. #define pin_rx D6
  7. #define pin_tx D5
  8.  
  9. SoftwareSerial Device(pin_rx, pin_tx);
  10.  
  11. const int AutoSendOn[4] = {0x68, 0x01, 0x40, 0x57};
  12. const int AutoSendOff[4] = {0x68, 0x01, 0x20, 0x77};
  13. const int StartPmMeasure[4] = {0x68, 0x01, 0x01, 0x96};
  14. const int StopPmMeasure[4] = {0x68, 0x01, 0x02, 0x95};
  15. const int ReadPm[4] = {0x68, 0x01, 0x04, 0x93};
  16.  
  17. int isAutoSend = true;
  18. int useReading = true;
  19.  
  20. int pm25 = 0; // PM2.5
  21. int pm10 = 0; // PM10
  22.  
  23. String P25String;
  24. char P25[100];
  25. String P10String;
  26. char P10[100];
  27.  
  28. unsigned long lastReading = 0;
  29.  
  30.  
  31.  
  32. // Paramètre WiFi, MQTT - WiFi, MQTT parameters
  33. const char* ssid = "LgHs"; // WiFi SSID
  34. const char* password = "Alain le Margoulain"; // WiFi Password
  35. const char* mqtt_server = "192.168.42.149"; // IP Broker MQTT
  36. const char* TopicP10 = "HONEY_P10"; // Topic MQTT
  37. const char* TopicP25 = "HONEY_P25"; // Topic MQTT
  38. const char* HostName = "ESP_HONEY";
  39.  
  40. unsigned long Now = 0;
  41. int SleepTime = 10000;
  42.  
  43. WiFiClient espClient;
  44. PubSubClient client(espClient);
  45.  
  46.  
  47. void sendCommand(const int *cmd) {
  48. int i;
  49. for(i=0;i<4; i++) {
  50. Device.write(cmd[i]);
  51. }
  52. // let a unicorn pass
  53. delay(10);
  54. }
  55.  
  56. int readResponse(int l = 32) {
  57. int i = 0;
  58. int buf[l];
  59.  
  60. unsigned long start = millis();
  61.  
  62. while(Device.available() > 0 && i < l) {
  63.  
  64. buf[i] = Device.read(); // read bytes from device
  65.  
  66. if(DEBUG) {
  67. Serial.print("i: "); Serial.print(i);
  68. Serial.print(" buf[i]: "); Serial.println(buf[i], HEX);
  69. }
  70.  
  71. // check for HEAD or skip a byte
  72. if(i == 0 && !(buf[0] == 0x40 || buf[0] == 0x42 || buf[0] == 0xA5 || buf[0] == 0x96)) {
  73. if(DEBUG) { Serial.println("Skipping Byte"); }
  74. continue;
  75. } else {
  76. i++;
  77. }
  78.  
  79. if(buf[0] == 0x42 && buf[1] == 0x4d) { // Autosend
  80. if(DEBUG) { Serial.println("Autosend"); }
  81. l=32;
  82. }
  83.  
  84. if(buf[0] == 0x40 && buf[2] == 0x4) { // Reading
  85. if(DEBUG) { Serial.println("Reading"); }
  86. l=8;
  87. }
  88.  
  89. if(buf[0] == 0xA5 && buf[1] == 0xA5) { // Pos. ACK
  90. if(DEBUG) { Serial.println("ACK"); }
  91. return true;
  92. }
  93.  
  94. if(buf[0] == 0x96 && buf[1] == 0x96) { // Neg. ACK
  95. if(DEBUG) { Serial.println("NACK"); }
  96. return false;
  97. }
  98.  
  99. if (millis() - start > 1000) { // trigger Timeout after 1 sec
  100. Serial.println("Timeout");
  101. return false;
  102. }
  103.  
  104. }
  105.  
  106. // check checksum in Reading
  107. if(buf[2] == 0x04) {
  108. // HEAD+LEN+CMD
  109. int cs = buf[0] + buf[1] + buf[2];
  110. int c;
  111.  
  112. // DATA
  113. for(c = 3; c < (2 + buf[1]); c++) {
  114. // Serial.println(buf[c]);
  115. cs += buf[c];
  116. }
  117. // CS = MOD((65536-(HEAD+LEN+CMD+DATA)), 256)
  118. cs = (65536 - cs) % 256;
  119.  
  120. // validate checksum
  121. if(cs == buf[c]) {
  122. // calculate PM values
  123. pm25 = buf[3] * 256 + buf[4];
  124. pm10 = buf[5] * 256 + buf[6];
  125. return true;
  126. } else {
  127. Serial.println("Checksum mismatch");
  128. }
  129. } else if(buf[3] == 0x1c) { // Autoreading
  130. int cs = 0;
  131. int c;
  132. // DATA
  133. for(c = 0; c <= buf[3]; c++) {
  134. // Serial.println(buf[c]);
  135. cs += buf[c];
  136. }
  137. int checksum = buf[30] * 256 + buf[31];
  138. if(DEBUG) {
  139. Serial.print("Checksum: "); Serial.print(checksum, HEX);
  140. Serial.print(" CS: "); Serial.println(cs, HEX);
  141. }
  142.  
  143. if(cs == checksum) {
  144. // calculate PM values
  145. pm25 = buf[6] * 256 + buf[7];
  146. pm10 = buf[8] * 256 + buf[9];
  147. return true;
  148. } else {
  149. Serial.println("Checksum mismatch");
  150. }
  151. } else {
  152. // unkown
  153. }
  154.  
  155. return false;
  156. }
  157.  
  158.  
  159.  
  160. void setup_wifi() {
  161. delay(10);
  162. // We start by connecting to a WiFi network
  163. Serial.println();
  164. Serial.print("Connecting to ");
  165. Serial.println(ssid);
  166. WiFi.mode(WIFI_STA);
  167. WiFi.hostname(HostName);
  168. WiFi.begin(ssid, password);
  169.  
  170. while (WiFi.status() != WL_CONNECTED) {
  171. delay(500);
  172. Serial.print(".");
  173. }
  174.  
  175. Serial.println("");
  176. Serial.println("WiFi connected");
  177. Serial.println("IP address: ");
  178. Serial.println(WiFi.localIP());
  179. }
  180.  
  181.  
  182. void reconnect() {
  183. // Loop until we're reconnected
  184. while (!client.connected()) {
  185. Serial.print("Attempting MQTT connection...");
  186. // Attempt to connect
  187. if (client.connect(HostName)) {
  188. Serial.println("connected");
  189. digitalWrite(LED_BUILTIN, LOW);
  190. //client.subscribe(Topic);
  191. } else {
  192. Serial.print("failed, rc=");
  193. Serial.print(client.state());
  194. Serial.println(" try again in 5 seconds");
  195. digitalWrite(LED_BUILTIN, HIGH);
  196. // Wait 5 seconds before retrying
  197. delay(5000);
  198. }
  199. }
  200. }
  201.  
  202. void setup() {
  203. Serial.begin(115200); Serial.println();
  204. Device.begin(9600); Device.println();
  205. setup_wifi();
  206. client.setServer(mqtt_server, 1883);
  207. pinMode(LED_BUILTIN, OUTPUT);
  208. }
  209.  
  210. void loop() {
  211.  
  212. if (!client.connected()) {
  213. reconnect();
  214. }
  215. client.loop();
  216.  
  217. if(millis() - lastReading >= 1000 || lastReading == 0) {
  218. lastReading = millis();
  219.  
  220. // handle AutoSend
  221. if(isAutoSend) {
  222. if(useReading) {
  223. if(readResponse()) {
  224. Serial.print("PM 2.5: "); Serial.print(pm25);
  225. Serial.print(" / PM 10: "); Serial.println(pm10);
  226. P25String = String(pm25);
  227. P10String = String(pm10);
  228. P25String.toCharArray(P25, 100);
  229. P10String.toCharArray(P10, 100);
  230. client.publish(TopicP25, P25);
  231. client.publish(TopicP10, P10);
  232. }
  233. } else { // disable
  234. Serial.println("Stop AutoSend");
  235. sendCommand(AutoSendOff);
  236. if(readResponse()) {
  237. Serial.println("AutoSend disabled.");
  238. isAutoSend = !isAutoSend;
  239. }
  240. }
  241. } else {
  242. sendCommand(ReadPm);
  243. if(readResponse()) {
  244. Serial.print("PM 2.5: "); Serial.print(pm25);
  245. Serial.print(" / PM 10: "); Serial.println(pm10);
  246. P25String = String(pm25);
  247. P10String = String(pm10);
  248. P25String.toCharArray(P25, 100);
  249. P10String.toCharArray(P10, 100);
  250. client.publish(TopicP25, P25);
  251. client.publish(TopicP10, P10);
  252. }
  253. }
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement