Emonhub + Emoncms local installation, "thread is dead " message revisited

Some months ago, I had a problem where Emonhub was crashing with the following messages:

2016-01-21 20:29:05,490 DEBUG    MQTT       CONACK => Return code: 0
2016-01-21 20:29:05,595 INFO     MQTT       on_subscribe
2016-01-21 20:29:15,589 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:29:15,847 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:29:16,105 WARNING  MainThread ArduinoMini thread is dead

My setup is an Arduino mini, connected to a Raspberry 2 via the serial port.
At this time, I had a discussion with Paul (pb66) and I suggested to use an external button that would generate a “sudo service emonhub restart” command. I built all that stuff and it finally worked.

Last week I revisited this project and found a solution that is now acceptable for me!

For some reasons, the mini generates “null” chars on the serial port when it is reset. Quite strange and I don’t know if it’s a particularity with this device. Anyway, these chars were just crashing Emonhub.

To sort this out, I have added 2 debug messages in the function below in EmonHubSerialInterfacer.py to show the differences and put a regular expression in between to get rid of all chars, except the ones from Octal 040 to 071 (Dec 32 to 57). This is the range I need and nothing else. This solved my problem!

EmonHubSerialInterfacer.py (portion)

import serial
import time
import Cargo
from pydispatch import dispatcher
import emonhub_interfacer as ehi
import re


def read(self):
    """Read data from serial port and process if complete line received.

    Return data as a list: [NodeID, val1, val2]
    
    """
    # Read serial RX
    self._rx_buf = self._rx_buf + self._ser.readline()
    # If line incomplete, exit
    if '\r\n' not in self._rx_buf:
        return

    # Remove CR,LF
    f = self._rx_buf[:-2]

    # Keep only chars from 040 to 071 in octal and get rid of the message "thread is dead" 
    self._log.debug("Serial Port buffer content before: " + str(f))
    f = re.sub('[^\040-\071]', '', f)
    self._log.debug("Serial Port buffer content after: " + str(f))

    # Reset buffer
    self._rx_buf = ''

Hope it can be of any help to you.

Walter