Arduino Mini + Pi2 + emonSD-22Dec2015.img

I am running the following configuration:
- Arduino Mini with emonTxV3_DirectSerial.ino loaded, slightly adapted for my needs.
- PI2 with emonSD-22Dec2015 image
- Ethernet cable
Arduino is connected directly to the Pi2 serial port through a level adapter (5V --> 3.3V)

My problem:
When I first boot Pi2 and Arduino, I get an "MainThread ArduinoMini thread is dead" message.
I then stop and restart the emonhub service.
Then everything works as expected. Nodes/Inputs are created, OpenHAB works just fine. If I reboot the PI, same message. I need to restart the service again.

Here is the emonhub.log file:

2016-01-21 20:29:05,332 INFO     MainThread EmonHub emonHub 'emon-pi' variant v1.1
2016-01-21 20:29:05,334 INFO     MainThread Opening hub...
2016-01-21 20:29:05,336 INFO     MainThread Logging level set to DEBUG
2016-01-21 20:29:05,338 INFO     MainThread Creating EmonHubSerialInterfacer 'ArduinoMini' 
2016-01-21 20:29:05,347 DEBUG    MainThread Opening serial port: /dev/ttyAMA0 @ 9600 bits/s
2016-01-21 20:29:05,349 DEBUG    MainThread Setting ArduinoMini subchannels: ['ToRFM12']
2016-01-21 20:29:05,351 DEBUG    MainThread Interfacer: Subscribed to channel' : ToRFM12
2016-01-21 20:29:05,355 DEBUG    MainThread Setting ArduinoMini pubchannels: ['ToEmonCMS']
2016-01-21 20:29:05,358 DEBUG    MainThread Interfacer: Subscribed to channel' : ToRFM12
2016-01-21 20:29:05,361 INFO     MainThread Creating EmonHubMqttInterfacer 'MQTT' 
2016-01-21 20:29:05,364 INFO     MainThread MQTT Init mqtt_host=127.0.0.1 mqtt_port=1883
 mqtt_user=emonpi
2016-01-21 20:29:05,372 DEBUG    MainThread MQTT Subscribed to channel' : ToEmonCMS
2016-01-21 20:29:05,375 INFO     MainThread Creating EmonHubEmoncmsHTTPInterfacer 'emoncmsorg' 
2016-01-21 20:29:05,379 DEBUG    MainThread emoncmsorg Subscribed to channel' : ToEmonCMS
2016-01-21 20:29:05,482 INFO     MQTT       Connecting to MQTT Server
2016-01-21 20:29:05,488 INFO     MQTT       connection status: Connection successful
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
//REMOVED DUPLICATED
2016-01-21 20:29:35,368 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:29:35,382 INFO     emoncmsorg sending: http://localhost/emoncms/input
/bulk.json?apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y&data=[]&sentat=1453408175
2016-01-21 20:29:35,622 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:29:35,818 WARNING  emoncmsorg send failure: wanted 'ok' but got 'Error:
 Format error, json string supplied is not valid
'
2016-01-21 20:29:35,893 WARNING  MainThread ArduinoMini thread is dead
//REMOVED DUPLICATED
2016-01-21 20:30:05,355 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:30:05,395 INFO     emoncmsorg sending: http://localhost/emoncms/input
/bulk.json?
apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y&data=[]&sentat=1453408205
2016-01-21 20:30:05,483 WARNING  emoncmsorg send failure: wanted 'ok' but got 'Error: 
Format error, json string supplied is not valid
'
2016-01-21 20:30:05,484 INFO     emoncmsorg sending: http://localhost/emoncms/myip/set.json?
apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y
2016-01-21 20:30:05,536 WARNING  emoncmsorg emoncmsorg couldn't send to server, HTTPError: 406
2016-01-21 20:30:05,604 WARNING  MainThread ArduinoMini thread is dead
//REMOVED DUPLICATED
2016-01-21 20:31:05,366 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:31:05,427 INFO     emoncmsorg sending: http://localhost/emoncms/myip/set.json?
apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y
2016-01-21 20:31:05,472 WARNING  emoncmsorg emoncmsorg couldn't send to server, HTTPError: 406
2016-01-21 20:31:05,576 INFO     emoncmsorg sending: http://localhost/emoncms/input
/bulk.json?apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y&data=[]&sentat=1453408265
2016-01-21 20:31:05,618 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:31:05,657 WARNING  emoncmsorg send failure: wanted 'ok' but got 'Error:
 Format error, json string supplied is not valid
'

Run the command:
sudo service emonhub stop

2016-01-21 20:31:57,693 DEBUG    MainThread SIGINT received.
2016-01-21 20:31:57,694 WARNING  MainThread ArduinoMini thread is dead
2016-01-21 20:31:57,895 INFO     MainThread Exiting hub...
2016-01-21 20:31:57,930 INFO     MainThread Exit completed

On the command line:

pi@emonpi:~$ sudo service emonhub status
● emonhub.service - LSB: Start/stop emonHub
   Loaded: loaded (/etc/init.d/emonhub)
   Active: active (running) since Thu 2016-01-21 20:29:03 GMT; 2min 24s ago
  Process: 472 ExecStart=/etc/init.d/emonhub start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/emonhub.service
           └─674 python /usr/share/emonhub/emonhub.py --logfile /var/log/emonhub/emonhub.log
 --config-file /home/pi/data/emo...

Jan 21 20:29:03 emonpi systemd[1]: Started LSB: Start/stop emonHub.
Jan 21 20:29:15 emonpi emonhub[472]: Exception in thread ArduinoMicro:
Jan 21 20:29:15 emonpi emonhub[472]: Traceback (most recent call last):
Jan 21 20:29:15 emonpi emonhub[472]: File "/usr/lib/python2.7/threading.py", line 810,
 in __bootstrap_inner
Jan 21 20:29:15 emonpi emonhub[472]: self.run()
Jan 21 20:29:15 emonpi emonhub[472]: File "/home/pi/emonhub/src/interfacers
​/emonhub_interfacer.py", line 73, in run
Jan 21 20:29:15 emonpi emonhub[472]: rxc = self.read()
Jan 21 20:29:15 emonpi emonhub[472]: File "/home/pi/emonhub/src/interfacers
​/EmonHubSerialInterfacer.py", line 90, in read
Jan 21 20:29:15 emonpi emonhub[472]: c.nodeid = int(f[0])
Jan 21 20:29:15 emonpi emonhub[472]: ValueError: invalid literal for int() with base 10: ''
pi@emonpi:~$ sudo service emonhub stop

Run command:
sudo service emonhub restart

2016-01-21 20:49:36,542 INFO     MainThread EmonHub emonHub 'emon-pi' variant v1.1
2016-01-21 20:49:36,543 INFO     MainThread Opening hub...
2016-01-21 20:49:36,544 INFO     MainThread Logging level set to DEBUG
2016-01-21 20:49:36,545 INFO     MainThread Creating EmonHubSerialInterfacer 'ArduinoMini' 
2016-01-21 20:49:36,549 DEBUG    MainThread Opening serial port: /dev/ttyAMA0 @ 9600 bits/s
2016-01-21 20:49:36,550 DEBUG    MainThread Setting ArduinoMini subchannels: ['ToRFM12']
2016-01-21 20:49:36,552 DEBUG    MainThread Interfacer: Subscribed to channel' : ToRFM12
2016-01-21 20:49:36,553 DEBUG    MainThread Setting ArduinoMini pubchannels: ['ToEmonCMS']
2016-01-21 20:49:36,554 DEBUG    MainThread Interfacer: Subscribed to channel' : ToRFM12
2016-01-21 20:49:36,556 INFO     MainThread Creating EmonHubMqttInterfacer 'MQTT' 
2016-01-21 20:49:36,558 INFO     MainThread MQTT Init mqtt_host=127.0.0.1 mqtt_port=1883
 mqtt_user=emonpi
2016-01-21 20:49:36,562 DEBUG    MainThread MQTT Subscribed to channel' : ToEmonCMS
2016-01-21 20:49:36,564 INFO     MainThread Creating EmonHubEmoncmsHTTPInterfacer 'emoncmsorg' 
2016-01-21 20:49:36,567 DEBUG    MainThread emoncmsorg Subscribed to channel' : ToEmonCMS
2016-01-21 20:49:36,664 INFO     MQTT       Connecting to MQTT Server
2016-01-21 20:49:36,669 INFO     MQTT       connection status: Connection successful
2016-01-21 20:49:36,671 DEBUG    MQTT       CONACK => Return code: 0
2016-01-21 20:49:36,773 INFO     MQTT       on_subscribe
2016-01-21 20:49:45,131 DEBUG    ArduinoMini 1 NEW FRAME : 10 9.52 4.03
2016-01-21 20:49:45,132 DEBUG    ArduinoMini 1 Timestamp : 1453409385.13
2016-01-21 20:49:45,133 DEBUG    ArduinoMini 1 From Node : 10
2016-01-21 20:49:45,134 DEBUG    ArduinoMini 1    Values : [9.52, 4.03]
2016-01-21 20:49:45,136 INFO     ArduinoMini Publishing: emonhub/rx/10/values 9.52,4.03
2016-01-21 20:49:45,139 INFO     ArduinoMini Publishing: emonhub/rx/10/rssi 0
2016-01-21 20:49:45,141 DEBUG    ArduinoMini 1 adding frame to buffer =>
 [1453409385, 10, 9.52, 4.03]
2016-01-21 20:49:45,142 DEBUG    ArduinoMini 1 Sent to channel' : ToEmonCMS
2016-01-21 20:49:54,377 DEBUG    ArduinoMini 2 NEW FRAME : 10 9.10 3.87
2016-01-21 20:49:54,379 DEBUG    ArduinoMini 2 Timestamp : 1453409394.38
2016-01-21 20:49:54,380 DEBUG    ArduinoMini 2 From Node : 10
2016-01-21 20:49:54,381 DEBUG    ArduinoMini 2    Values : [9.1, 3.87]
2016-01-21 20:49:54,382 INFO     ArduinoMini Publishing: emonhub/rx/10/values 9.1,3.87
2016-01-21 20:49:54,385 INFO     ArduinoMini Publishing: emonhub/rx/10/rssi 0
2016-01-21 20:49:54,387 DEBUG    ArduinoMini 2 adding frame to buffer =>
 [1453409394, 10, 9.1, 3.87]
2016-01-21 20:49:54,389 DEBUG    ArduinoMini 2 Sent to channel' : ToEmonCMS
2016-01-21 20:50:06,360 DEBUG    ArduinoMini 3 NEW FRAME : 10 12.44 4.84
2016-01-21 20:50:06,362 DEBUG    ArduinoMini 3 Timestamp : 1453409406.36
2016-01-21 20:50:06,363 DEBUG    ArduinoMini 3 From Node : 10
2016-01-21 20:50:06,364 DEBUG    ArduinoMini 3    Values : [12.44, 4.84]
2016-01-21 20:50:06,366 INFO     ArduinoMini Publishing: emonhub/rx/10/values 12.44,4.84
2016-01-21 20:50:06,376 INFO     ArduinoMini Publishing: emonhub/rx/10/rssi 0
2016-01-21 20:50:06,379 DEBUG    ArduinoMini 3 adding frame to buffer =>
 [1453409406, 10, 12.44, 4.84]
2016-01-21 20:50:06,380 DEBUG    ArduinoMini 3 Sent to channel' : ToEmonCMS
2016-01-21 20:50:06,626 INFO     emoncmsorg sending: http://localhost/emoncms/input/bulk.json?
apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y&data=[[1453409385,10,9.52,4.03],
[1453409394,10,9.1,3.87],[1453409406,10,12.44,4.84]]&sentat=1453409406
2016-01-21 20:50:06,677 DEBUG    emoncmsorg acknowledged receipt with 'ok' from http://localhost/emoncms
2016-01-21 20:50:14,747 DEBUG    ArduinoMini 4 NEW FRAME : 10 9.23 4.23
2016-01-21 20:50:14,749 DEBUG    ArduinoMini 4 Timestamp : 1453409414.75
2016-01-21 20:50:14,750 DEBUG    ArduinoMini 4 From Node : 10
2016-01-21 20:50:14,751 DEBUG    ArduinoMini 4    Values : [9.23, 4.23]
2016-01-21 20:50:14,752 INFO     ArduinoMini Publishing: emonhub/rx/10/values 9.23,4.23
2016-01-21 20:50:14,754 INFO     ArduinoMini Publishing: emonhub/rx/10/rssi 0
2016-01-21 20:50:14,757 DEBUG    ArduinoMini 4 adding frame to buffer =>
 [1453409414, 10, 9.23, 4.23]
2016-01-21 20:50:14,758 DEBUG    ArduinoMini 4 Sent to channel' : ToEmonCMS
2016-01-21 20:50:24,392 DEBUG    ArduinoMini 5 NEW FRAME : 10 9.22 4.11
2016-01-21 20:50:24,393 DEBUG    ArduinoMini 5 Timestamp : 1453409424.39
2016-01-21 20:50:24,394 DEBUG    ArduinoMini 5 From Node : 10
2016-01-21 20:50:24,395 DEBUG    ArduinoMini 5    Values : [9.22, 4.11]
2016-01-21 20:50:24,396 INFO     ArduinoMini Publishing: emonhub/rx/10/values 9.22,4.11
2016-01-21 20:50:24,398 INFO     ArduinoMini Publishing: emonhub/rx/10/rssi 0
2016-01-21 20:50:24,401 DEBUG    ArduinoMini 5 adding frame to buffer =>
 [1453409424, 10, 9.22, 4.11]
2016-01-21 20:50:24,402 DEBUG    ArduinoMini 5 Sent to channel' : ToEmonCMS
2016-01-21 20:50:34,450 DEBUG    ArduinoMini 6 NEW FRAME : 10 8.57 3.71
2016-01-21 20:50:34,452 DEBUG    ArduinoMini 6 Timestamp : 1453409434.45
2016-01-21 20:50:34,453 DEBUG    ArduinoMini 6 From Node : 10
2016-01-21 20:50:34,453 DEBUG    ArduinoMini 6    Values : [8.57, 3.71]
2016-01-21 20:50:34,455 INFO     ArduinoMini Publishing: emonhub/rx/10/values 8.57,3.71
2016-01-21 20:50:34,458 INFO     ArduinoMini Publishing: emonhub/rx/10/rssi 0
2016-01-21 20:50:34,461 DEBUG    ArduinoMini 6 adding frame to buffer =>
 [1453409434, 10, 8.57, 3.71]
2016-01-21 20:50:34,462 DEBUG    ArduinoMini 6 Sent to channel' : ToEmonCMS
2016-01-21 20:50:36,401 DEBUG    MainThread SIGINT received.
2016-01-21 20:50:36,402 INFO     MainThread Exiting hub...
2016-01-21 20:50:36,508 INFO     MainThread Exit completed

There are other threads in the forum talking about a similar issue but I could not find any solution.

Can someone help with this?

Thanks for your help.

Walter

daturach's picture

Re: Arduino Mini + Pi2 + emonSD-22Dec2015.img

I am going to use "shutdownpi" from your archive folder and modify it so that it calls a service emonhub restart when doing a short press on a button and a shutdown for a long press.

That should resolve my problem I hope.

Walter

pb66's picture

Re: Arduino Mini + Pi2 + emonSD-22Dec2015.img

Seems the problem was the first value in the packet was not an integer, 

Jan 21 20:29:15 emonpi emonhub[472]: File "/home/pi/emonhub/src/interfacers ​/EmonHubSerialInterfacer.py", line 90, in read
Jan 21 20:29:15 emonpi emonhub[472]: c.nodeid = int(f[0]) 
Jan 21 20:29:15 emonpi emonhub[472]: ValueError: invalid literal for int() with base 10: ''

The code should be more robust so as not to crash when faced with a non-integer. Perhaps a try-catch (around lines 86 to 91) to abort further processing without falling over, that way it wouldn't then continue to try and send the failed packet to emoncms.

2016-01-21 20:29:35,382 INFO emoncmsorg sending: http://localhost/emoncms/input /bulk.json?apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y&data=[]&sentat=1453408175

The "data=[]" contains the content of the packet that failed ie nothing!

2016-01-21 20:31:05,657 WARNING emoncmsorg send failure: wanted 'ok' but got 'Error: Format error, json string supplied is not valid

This response from emoncms means that that whole payload was discarded which in this instance was just 1 empty packet, but could have been a whole 30 seconds worth of data from several nodes.

Is it possible the arduino pro mini has sent something other than an integer as a first value?

is there any startup messages that occur after the serial connection is established?

If this is happening every time you start then there probably is, however if it happens randomly there is a less probable change of a timing issue causing the first (integer) value to be missed and emonhub seeing one of the following floats (2nd or 3rd values) as the first (node id) value.

Removing any print statements from your sketch will be a much better solution, especially if you have a watchdog in your sketch, as any reseting of the pro-mini will more than likely crash emonhub.

Paul

 

 

daturach's picture

Re: Arduino Mini + Pi2 + emonSD-22Dec2015.img

Thanks for your answers.

I ran Minicom but could not see anything wrong.

It happens every time. This is why I am looking at a solution with a script.

Something else comes to my mind. I am powering both modules from the same power supply. I noticed the Arduino is taking quite "some" time before sending its first values to the serial port. It seems the PI2 is faster to boot than the mini. Maybe that's the problem.

What do you think?

I am going to use a second power supply and see what happens.

Walter

 

daturach's picture

Re: Arduino Mini + Pi2 + emonSD-22Dec2015.img

With one power supply as before, I have rebooted the Pi2, leaving the arduino running. Nothing has changed.

I am going to stick with the script and a button. I will upload it here once done.

Walter

pb66's picture

Re: Arduino Mini + Pi2 + emonSD-22Dec2015.img

"Something else comes to my mind. I am powering both modules from the same power supply. I noticed the Arduino is taking quite "some" time before sending its first values to the serial port. It seems the PI2 is faster to boot than the mini. Maybe that's the problem."

Most probably yes. There's no way the arduino should take longer to get started than the Pi takes to load and start an OS and then start several services including emonhub. That is where you need to look.

Why does the sketch take so long to start?

Does it start the serial connection at the start, or towards the end of its setup?

Does it print any start up messages?

With minicom running, reset just the arduino and watch the serial output. Does every line, without exception, start with the node id?

The script & button isn't really a fix for this issue, since any power interruption no matter how brief, will need you to push the button. Will you always know? Will you always be at home? What if it happens during the night? Delaying emonhub's start would be marginally better, but  it should not be necessary. The Arduino should be much quicker to boot.

Paul  

daturach's picture

Re: Arduino Mini + Pi2 + emonSD-22Dec2015.img

Why does the sketch take so long to start? Does it start the serial connection at the start or towards the end of it's setup? Does it print any start up messages?

I am using the sketch in https://github.com/openenergymonitor/emonTxFirmware/blob/master/emonTxV3/noRF/emonTxV3_DirectSerial , compiled for an Arduino mini. 3 values are sent (Node, realPower and Voltage) every 10 secs.

It prints "emonTx V3 Direct serial Example" at the start.

With minicom running, reset just the arduino and watch the serial output does every line without exception start with the node id?

Yes, every line is clean, nodeid, then the 2 values, separated by a space.

The script & button isn't really a fix for this issue since any power interruptions no matter how brief will need you to push the button. will you always know? will you always be at home? what if it happens during the night? delaying emonhub's start would be marginally better but  it should not be necessary, the arduino should be much quicker to boot.

Leaving the Arduino running and rebooting the PI did not solve the problem. Why is emonhub not catching the beginning of the transmission? I have a level adapter (HEF4050) between the mini TX out  and the PI RX in. If there was a problem there, minicom should catch it.

As a side note, I have another system measuring my 3 phases system using another combination of components (emonTx V3.4 + RF + Pi B+). No problems so far, running since 6 months.

For this project, I intend to use this device for measuring and comparing different appliances. It should not run for a long time.

Walter

daturach's picture

Re: Arduino Mini + Pi2 + emonSD-22Dec2015.img

I am now using a usb2serial adapter and it's definitely better. When I boot both Arduino and PI, the data is going through, without me having to restart the service. However, I must not restart the Arduino at a later stage. The message "ArduinoMini thread is dead" is back.

When I used the serial port, I suspect my programming device was pulling the Arduino reset signal to ground for a certain time. This increased the boot time.

I think I am going to keep both options: through the serial or the USB ports. I will decide later.

Next step is to move everything to a HDD or SSD drive.

Thanks for your help.

Walter

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.