Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <SoftwareSerial.h>
- #define DEBUG false
- #define pin_rx D6
- #define pin_tx D5
- SoftwareSerial Device(pin_rx, pin_tx);
- const int AutoSendOn[4] = {0x68, 0x01, 0x40, 0x57};
- const int AutoSendOff[4] = {0x68, 0x01, 0x20, 0x77};
- const int StartPmMeasure[4] = {0x68, 0x01, 0x01, 0x96};
- const int StopPmMeasure[4] = {0x68, 0x01, 0x02, 0x95};
- const int ReadPm[4] = {0x68, 0x01, 0x04, 0x93};
- int isAutoSend = true;
- int useReading = true;
- int pm25 = 0; // PM2.5
- int pm10 = 0; // PM10
- String P25String;
- char P25[100];
- String P10String;
- char P10[100];
- unsigned long lastReading = 0;
- // Paramètre WiFi, MQTT - WiFi, MQTT parameters
- const char* ssid = "LgHs"; // WiFi SSID
- const char* password = "Alain le Margoulain"; // WiFi Password
- const char* mqtt_server = "192.168.42.149"; // IP Broker MQTT
- const char* TopicP10 = "HONEY_P10"; // Topic MQTT
- const char* TopicP25 = "HONEY_P25"; // Topic MQTT
- const char* HostName = "ESP_HONEY";
- unsigned long Now = 0;
- int SleepTime = 10000;
- WiFiClient espClient;
- PubSubClient client(espClient);
- void sendCommand(const int *cmd) {
- int i;
- for(i=0;i<4; i++) {
- Device.write(cmd[i]);
- }
- // let a unicorn pass
- delay(10);
- }
- int readResponse(int l = 32) {
- int i = 0;
- int buf[l];
- unsigned long start = millis();
- while(Device.available() > 0 && i < l) {
- buf[i] = Device.read(); // read bytes from device
- if(DEBUG) {
- Serial.print("i: "); Serial.print(i);
- Serial.print(" buf[i]: "); Serial.println(buf[i], HEX);
- }
- // check for HEAD or skip a byte
- if(i == 0 && !(buf[0] == 0x40 || buf[0] == 0x42 || buf[0] == 0xA5 || buf[0] == 0x96)) {
- if(DEBUG) { Serial.println("Skipping Byte"); }
- continue;
- } else {
- i++;
- }
- if(buf[0] == 0x42 && buf[1] == 0x4d) { // Autosend
- if(DEBUG) { Serial.println("Autosend"); }
- l=32;
- }
- if(buf[0] == 0x40 && buf[2] == 0x4) { // Reading
- if(DEBUG) { Serial.println("Reading"); }
- l=8;
- }
- if(buf[0] == 0xA5 && buf[1] == 0xA5) { // Pos. ACK
- if(DEBUG) { Serial.println("ACK"); }
- return true;
- }
- if(buf[0] == 0x96 && buf[1] == 0x96) { // Neg. ACK
- if(DEBUG) { Serial.println("NACK"); }
- return false;
- }
- if (millis() - start > 1000) { // trigger Timeout after 1 sec
- Serial.println("Timeout");
- return false;
- }
- }
- // check checksum in Reading
- if(buf[2] == 0x04) {
- // HEAD+LEN+CMD
- int cs = buf[0] + buf[1] + buf[2];
- int c;
- // DATA
- for(c = 3; c < (2 + buf[1]); c++) {
- // Serial.println(buf[c]);
- cs += buf[c];
- }
- // CS = MOD((65536-(HEAD+LEN+CMD+DATA)), 256)
- cs = (65536 - cs) % 256;
- // validate checksum
- if(cs == buf[c]) {
- // calculate PM values
- pm25 = buf[3] * 256 + buf[4];
- pm10 = buf[5] * 256 + buf[6];
- return true;
- } else {
- Serial.println("Checksum mismatch");
- }
- } else if(buf[3] == 0x1c) { // Autoreading
- int cs = 0;
- int c;
- // DATA
- for(c = 0; c <= buf[3]; c++) {
- // Serial.println(buf[c]);
- cs += buf[c];
- }
- int checksum = buf[30] * 256 + buf[31];
- if(DEBUG) {
- Serial.print("Checksum: "); Serial.print(checksum, HEX);
- Serial.print(" CS: "); Serial.println(cs, HEX);
- }
- if(cs == checksum) {
- // calculate PM values
- pm25 = buf[6] * 256 + buf[7];
- pm10 = buf[8] * 256 + buf[9];
- return true;
- } else {
- Serial.println("Checksum mismatch");
- }
- } else {
- // unkown
- }
- return false;
- }
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.hostname(HostName);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(HostName)) {
- Serial.println("connected");
- digitalWrite(LED_BUILTIN, LOW);
- //client.subscribe(Topic);
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- digitalWrite(LED_BUILTIN, HIGH);
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- Serial.begin(115200); Serial.println();
- Device.begin(9600); Device.println();
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- pinMode(LED_BUILTIN, OUTPUT);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- if(millis() - lastReading >= 1000 || lastReading == 0) {
- lastReading = millis();
- // handle AutoSend
- if(isAutoSend) {
- if(useReading) {
- if(readResponse()) {
- Serial.print("PM 2.5: "); Serial.print(pm25);
- Serial.print(" / PM 10: "); Serial.println(pm10);
- P25String = String(pm25);
- P10String = String(pm10);
- P25String.toCharArray(P25, 100);
- P10String.toCharArray(P10, 100);
- client.publish(TopicP25, P25);
- client.publish(TopicP10, P10);
- }
- } else { // disable
- Serial.println("Stop AutoSend");
- sendCommand(AutoSendOff);
- if(readResponse()) {
- Serial.println("AutoSend disabled.");
- isAutoSend = !isAutoSend;
- }
- }
- } else {
- sendCommand(ReadPm);
- if(readResponse()) {
- Serial.print("PM 2.5: "); Serial.print(pm25);
- Serial.print(" / PM 10: "); Serial.println(pm10);
- P25String = String(pm25);
- P10String = String(pm10);
- P25String.toCharArray(P25, 100);
- P10String.toCharArray(P10, 100);
- client.publish(TopicP25, P25);
- client.publish(TopicP10, P10);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement