Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into minimalmodbus
Browse files Browse the repository at this point in the history
  • Loading branch information
TrystanLea committed May 8, 2021
2 parents 2a7b555 + 76b42ad commit f834edf
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 88 deletions.
8 changes: 4 additions & 4 deletions conf/interfacer_examples/directserial-serialtx3e/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ Interfacer for serial output from emonTx V3 (firmware V2.4 and above)

emonTx firmware V2.4+ outputs serial CSV string pairs compatiable with emonESP:

`name:value,name:value`
`MSG:value,name:value,name:value`

e.g

`ct1:100,ct2:300` ....
`MSG:12,ct1:100,ct2:300` ....

Default baudrate is `115200`
Messages **must** start with the `MSG:number` key:value pair. The message number does not need to increment but the interfacer looks for `MSG` as the identifier that the new string is a data string.

Default baudrate is `115200`

## Config example

Add the following to `emonhub.conf` in the `[interfacers]` section:


```
[interfacers]
### This interfacer manages the EmonTx3 ESP format serial
Expand Down
2 changes: 2 additions & 0 deletions configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ For data to be passed, the name of the 2 channels must match.

Each interfacer can have multiple channels defined and multiple interfacers can listen to the same channel. e.g. data published by the Serial Interfacer can be listened (subscribed) for by the MQTT and the HTTP interfacer.

**Note** The channel definition is a list so must end with a comma e.g. `pubchannels = ToEmonCMS,` or `pubchannels = ToEmonCMS,ToXYZ,`

## 3. `nodes`

Nodes holds the decoder configuration for rfm12/69 node data which are sent as binary structures.
Expand Down
4 changes: 2 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ if [ "$emonSD_pi_env" = "1" ]; then

# RaspberryPi Serial configuration
# disable Pi3 Bluetooth and restore UART0/ttyAMA0 over GPIOs 14 & 15;
# Review should this be: dtoverlay=pi3-miniuart-bt?
sudo sed -i -n '/dtoverlay=pi3-disable-bt/!p;$a dtoverlay=pi3-disable-bt' /boot/config.txt
# Review should this be: dtoverlay=miniuart-bt?
sudo sed -i -n '/dtoverlay=disable-bt/!p;$a dtoverlay=disable-bt' /boot/config.txt

# We also need to stop the Bluetooth modem trying to use UART
sudo systemctl disable hciuart
Expand Down
28 changes: 0 additions & 28 deletions src/emonhub_interfacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import logging
import threading
import traceback
import requests

import emonhub_coder as ehc
import emonhub_buffer as ehb
Expand Down Expand Up @@ -230,33 +229,6 @@ def _process_post(self, data):
"""
pass

def _send_post(self, post_url, post_body=None):
"""
:param post_url:
:param post_body:
:return: the received reply if request is successful
"""
"""Send data to server.
data (list): node and values (eg: '[node,val1,val2,...]')
time (int): timestamp, time when sample was recorded
return True if data sent correctly
"""

try:
if post_body:
reply = requests.post(post_url, post_body, timeout=60)
else:
reply = requests.get(post_url, timeout=60)
reply.raise_for_status() # Raise an exception if status code isn't 200
return reply.text
except requests.exceptions.RequestException as ex:
self._log.warning("%s couldn't send to server: %s", self.name, ex)
return reply.text

def _process_rx(self, cargo):
"""Process a frame of data
Expand Down
15 changes: 6 additions & 9 deletions src/interfacers/EmonHubBleInterfacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

class EmonHubBleInterfacer(EmonHubInterfacer):

def __init__(self, name, device_addr=''):
def __init__(self, name, device_addr):
"""Initialize interfacer
device_addr (string): BLE MAC address to connect to
Expand All @@ -69,7 +69,7 @@ def close(self):
"""Close serial port"""

# Close serial port
if self._ble is not None:
if self._ble:
self._log.debug("Closing Bluetooth connection")
self._ble.disconnect()

Expand Down Expand Up @@ -100,10 +100,8 @@ def read(self):
rh = self._get_humidity()
bat = self._get_bat_level()

data = '{}, {}, {}'.format(temp, rh, bat )

# Create a Payload object
c = Cargo.new_cargo(rawdata=data)
c = Cargo.new_cargo()
c.realdata = (temp, rh, bat)

if int(self._settings['nodeoffset']):
Expand All @@ -117,7 +115,7 @@ def set(self, **kwargs):

for key, setting in self._private_settings.items():
# Decide which setting value to use
if key in kwargs.keys():
if key in kwargs:
setting = kwargs[key]
else:
setting = self._private_settings[key]
Expand Down Expand Up @@ -155,8 +153,7 @@ def _get_bat_level(self):
self._bat_readings.insert(0, val)
self._bat_readings = self._bat_readings[0:20]

val = sum(self._bat_readings)/float(len(self._bat_readings))
#self._log.debug('Batt: {} -> {}'.format(self._bat_readings, val))
val = sum(self._bat_readings)//len(self._bat_readings)

return round(val)

Expand All @@ -167,7 +164,7 @@ def _connect(self):
try:
self._ble = btle.Peripheral(self._addr)
except btle.BTLEException as e:
self._log.error(e)
self._log.exception("Failed to read from BTLE device")
return False

self._temperature = self._ble.getCharacteristics(uuid=btle.AssignedNumbers.temperature)[0]
Expand Down
90 changes: 46 additions & 44 deletions src/interfacers/EmonHubEmoncmsHTTPInterfacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,51 @@ def _process_post(self, databuffer):
# Return true to clear buffer if the apikey is not set
return True

data_string = json.dumps(databuffer, separators=(',', ':'))

# Prepare URL string of the form
# http://domain.tld/emoncms/input/bulk.json?apikey=12345
# &data=[[0,10,82,23],[5,10,82,23],[10,10,82,23]]
# &sentat=15' (requires emoncms >= 8.0)

# time that the request was sent at
sentat = int(time.time())

# Construct post_url (without apikey)
post_url = self._settings['url'] + '/input/bulk.json?'

self._log.info("sending: %s data=%s&sentat=%s&apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y", post_url, data_string, sentat)
if self._settings['senddata']:
data_string = json.dumps(databuffer, separators=(',', ':'))

# Prepare URL string of the form
# http://domain.tld/emoncms/input/bulk.json?apikey=12345
# &data=[[0,10,82,23],[5,10,82,23],[10,10,82,23]]
# &sentat=15' (requires emoncms >= 8.0)

# time that the request was sent at
sentat = int(time.time())

# Construct post_url (without apikey)
post_url = self._settings['url'] + '/input/bulk.json?'

self._log.info("sending: %s data=%s&sentat=%s&apikey=E-M-O-N-C-M-S-A-P-I-K-E-Y", post_url, data_string, sentat)

result = False
try:
reply = requests.post(post_url, {'apikey': self._settings['apikey'], 'data': data_string, 'sentat': str(sentat)}, timeout=60)
reply.raise_for_status() # Raise an exception if status code isn't 200
result = reply.text
except requests.exceptions.RequestException as ex:
self._log.warning("%s couldn't send to server: %s", self.name, ex)
return False

if result == 'ok':
self._log.debug("acknowledged receipt with '%s' from %s", result, self._settings['url'])
return True
else:
self._log.warning("send failure: wanted 'ok' but got '%s'", result)
return False

result = False
try:
reply = requests.post(post_url, {'apikey': self._settings['apikey'], 'data': data_string, 'sentat': str(sentat)}, timeout=60)
reply.raise_for_status() # Raise an exception if status code isn't 200
result = reply.text
except requests.exceptions.RequestException as ex:
self._log.warning("%s couldn't send to server: %s", self.name, ex)

if result == 'ok':
self._log.debug("acknowledged receipt with '%s' from %s", result, self._settings['url'])
return True
else:
self._log.warning("send failure: wanted 'ok' but got '%s'", result)
return False

def sendstatus(self):
if 'apikey' not in self._settings or len(str(self._settings['apikey'])) != 32 \
or str(self._settings['apikey']).lower() == 'x' * 32:
return

# MYIP url
post_url = self._settings['url'] + '/myip/set.json?apikey='
# Print info log
self._log.info("sending: " + post_url + "E-M-O-N-C-M-S-A-P-I-K-E-Y")
# add apikey
post_url = post_url + self._settings['apikey']
# send request
self._send_post(post_url)
# Sends status to myip module if enabled
if self._settings['sendstatus']:
post_url = self._settings['url'] + '/myip/set.json?apikey='
self._log.info("sending: " + post_url + "E-M-O-N-C-M-S-A-P-I-K-E-Y")
post_url = post_url + self._settings['apikey']
try:
reply = requests.get(post_url, timeout=60)
reply.raise_for_status()
# self._log.debug(reply.text)
except requests.exceptions.RequestException as ex:
self._log.warning("%s couldn't send myip status update to server: %s", self.name, ex)

return True

def set(self, **kwargs):
"""
Expand Down Expand Up @@ -126,11 +128,11 @@ def set(self, **kwargs):
continue
elif key == 'senddata':
self._log.info("Setting %s senddata: %s", self.name, setting)
self._settings[key] = setting
self._settings[key] = int(setting)
continue
elif key == 'sendstatus':
self._log.info("Setting %s sendstatus: %s", self.name, setting)
self._settings[key] = setting
self._settings[key] = int(setting)
continue
else:
self._log.warning("'%s' is not valid for %s: %s", setting, self.name, key)
4 changes: 4 additions & 0 deletions src/interfacers/EmonHubSDM120Interfacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def read(self):
self._log.error("Could not read from SDM120: " + str(e))
# for i in r:
# self._log.debug(i+" "+str(r[i]))

# Can r be False in any reasonable situation? Why not just return in the exception handler above?
# Unless read_all can return, e.g., [] or None then this is just overcomplicating things.
if r:
try:
for i in range(len(self._settings['datafields'])):
Expand All @@ -103,6 +106,7 @@ def read(self):

c.names.append(self._settings['prefix']+name)
c.realdata.append(value)

self._log.debug(c.realdata)
except Exception as e:
self._log.error("Error parsing data: " + str(e))
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3
2.2.4

0 comments on commit f834edf

Please sign in to comment.