Here is an example script that posts values 0-9 every 5 seconds, I’ll happily help you merge the two, but I’m not sure how you want to stage the data based on the discussion above, will you want to simply forward every reg as it is read “hot potato” style or will you collate the data into batches or build up a single complete frame.
The best way IMO would to be send one complete packet.
#!/usr/bin/env python
import time
import socket
host = "localhost"
port = 50012
nodeid = 13
interval = 5 # in seconds
# Send the frame of data via a socket
def send(frame):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
frame = frame + '\r\n'
s.send(frame)
s.close()
# Create a "lastsent" time to count interval from
# this is done by dividing unixtime by the interval
# so the first send may be delayed by upto 1 interval.
lastsent = time.time()//interval*interval
while True:
# Determine current interval period
t = time.time()//interval*interval
# Check for 1 interval period since lastsent
if t >= (lastsent + interval):
##### DATA STAGED HERE eg
list = [0,1,2,3,4,5,6,7,8,9]
# Create a space seperated frame, timestamp nodeid val1 val2 etc
frame = ' '.join(str(val) for val in [t, nodeid] + list)
# Update time lastsent
lastsent = t
# Print and/or send data
print(frame)
send(frame)
# Don't loop too fast
time.sleep(0.10)
At emonHub you only need one interfacer set up as follows, plus the emoncms reporter will need your apikey and url (the default RFM2Pi interfacer can be deleted)
[interfacers]
[[Veris]]
Type = EmonHubSocketInterfacer
[[[init_settings]]]
port_nb = 50012
[[[runtimesettings]]]
timestamped = true
I’ve lifted this from one of my own scripts and I have adopted a new way of synchronizing my data packets using unixtime divided by interval, hopefully it makes sense, I’ve added comments, but you can use something more conventional if you prefer.