Reading particle counting from DYLOS DC1100 data via ESP8266

RS-232 Cable to ESP8266

ESP8266-based module such as one produced by WeMOS is really affordable nowadays, costs about $2 on AliExpress. For this application, we can also used ESP8266-01 but is difficult for debugging and extra steps to flash a new skech. ESP-01 costs about $1.4.

Choose any two wires in WeMOS board and used SoftwareSerial library to read data comming from RS232-TTL module.

First, let wire the RS-232-TTL to a WeMOS board

wemos-rs232

Besides, GND and VCC pins, an RX pin on RS-232 is connected to D6 and TX to D5. D6 and D5 should be be declared by SoftwareSerial.h as SoftwareSerial swSer(RX, TX, false, buffer_size); likes this SoftwareSerial swSer(D6, D5, false, 256);. The built-in led is used to indicated when data is transferred.

The excerpt below show the basic blocks flashed into WeMOS ESP8266. I used PubSubClient.h library to push data to an MQTT server, and ArduinoJson to format the data format in JSON.

//excerpt code, 
//full code hosted here
//pin map: GND-GND, VCC-VCC, D6-RX, D5-TX
*_____________________SETUP, RUN ONE_______________________*/
void setup() {
    Serial.begin(9600);
    swSer.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT), 
    setup_wifi();
    flashLED(500,3);
    setup_OTA();
    client.setServer(mqtt_server, mqtt_port);
}

/*_____________________MAIN LOOP, REPEAT FOREVER_______________________*/
void loop() {
    ArduinoOTA.handle();
    client.loop();

    while (swSer.available() > 0) {
        character = swSer.read();
        content.concat(character);
    }
    if (content.length() > 3){
        Serial.println(content);
        // split content and strim white space,
        small = getValue(content, ',', 0);
        small.trim();
        large = getValue(content, ',', 1);
        large.trim();
        Serial.println(small);
        Serial.println(large);
        timestamp = millis()/1000;
        pushData();
        content = "";
        swSer.flush();
        flashLED(200,5);
    }
    delay(100);
}

/*___________________SPLIT STR BY COMMA_____________________*/
// written by someone else
String getValue(String data, char separator, int index){
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Uploading the sketch (the script written above) to the WeMOS ESP8266 is quit easy. Well, it is much easir than using ESP8266-01. Conect the USB cable to the board, select the right board under Tools, name the board, and the port, hit Verify to complile the code, then Upload, and wait for some fast blinking on the board's LED.

Next, we will work on the MQTT server side to capture the data transmitted by the ESP8266.