Directly connecting to Optical Pulse Counter with RPi?

Hi Dan,

thanks.

I’l rather plug the Optical sensor into a RG45 female plug, and then run out of there lines that i will plug into the RPI’s board, that way the optical sensor is untouched, should I change my mind later and get a OEM board and want to then plug it into a RG45 female connector.

G

1 Like

Sorry to revive an old thread. I am trying to use an optical sensor via RJ45 breakout socket. Headers soldered and jumpers connected to the Rpi as above. Some activity from monitor.py but no luck with the actual sensing. LED doesn’t light either, I’ve used a 3.3v pin as advice above, should I use 5v?

Now thinking about constructive vandalism, removing RJ45 plug and soldering directly. However, if I do this, any clues guidance about the cable colour codes inside?

I need just what’s in this thread, rather than the whole of emon, hence my rather direct approach. Thanks in advance.

If you’re looking for details of what’s inside to Optical Pulse sensor, there’s a link in post no.2.

Thanks! Will look.

I have a working set up now. Removing the RJ45 and soldering jumpers directly worked. I find that I benefit for looking at different programming approaches to a single problem, so here’s my simple flash collector → influxdb + thence to grafana.

#!/usr/bin/env python

# (c) hugh barnard 2022 Based on emon work and the need for something 'simpler' 
# hughbarnard.org and @hughbarnard

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

from gpiozero import LightSensor
import time
import datetime
from influxdb import InfluxDBClient

import configparser

# unused currently
import schedule

# read configuration file
config = configparser.ConfigParser()
config.read('config/config.ini')
total_watts = 0


#influx_client = InfluxDBClient('10.0.0.100', '8086', 'user', 'password', 'temp') this is 'old' influxdb
influx_client = InfluxDBClient(config['server']['ip_address'], config['server']['port'], '', '', config['server']['database'])
sensor = LightSensor(config['gpiozero']['bcm_sensor_pin'], queue_len=int(config['gpiozero']['queue_length']), threshold=config['gpiozero']['threshold'])

# config['data']['cost'] is the cost of 1 watt = 1 impression (flash)
# need to upgrade to influxdb line protocol and send batches later on

def write_influxdb(config,client,value,total_watts):
    json_body = [
       {
        "measurement": config['data']['measurement'],
        "tags": {
            "sensor": config['data']['sensor'],
            "house": config['data']['house']
        },
        "fields": {
            "watt": value,
            "cost": config['data']['cost']
        }
       }
    ]
    total_watts += 1
    # print("writing influx, total_watts are " + str(total_watts))
    client.write_points(json_body)
    return total_watts


#Catch when script is interrupted, cleanup correctly
try:
    # Main loop
    while True:
        sensor.wait_for_light()
        total_watts = write_influxdb(config,influx_client,1,total_watts)
        sensor.wait_for_dark()

except KeyboardInterrupt:
    pass
finally:
    quit()
1 Like

Just install emonhub.

That is rather a neat way to do it, using LightSensor.

Here’s one way to use line protocol - via the Requests module - from a Python script:

# measurement names and values
        meas = {
            'LEG1':LEG1,
            'LEG2':LEG2,
            'GENW':GENW,
            'PVWH':PVWH,
            'CONS':CONS,
            'TOTL':TOTL,
            'TECO':TECO,
            'IMPT':IMPT,
            'EXPT':EXPT,
            'NETT':NETT,
            'VOLT':VOLT,
            'L1PF':L1PF,
            'L2PF':L2PF,
            'PVPF':PVPF,
            'DRCT':DRCT,
            'IMPW':IMPW,
            'EXPW':EXPW
        }

        tmpData = []
        for key, val in meas.items():
            tmpData.append('{} value={} {}'.format(key, val, int(time.time())))
            Data = '\n'.join(tmpData)

        requests.post('http://192.168.1.61:8086/write?db=energy&precision=s', data=Data, timeout=1)

Used with InfluxDB v1.8 and no security, as it’s on a LAN that’s not connected to the internet.
If you’d like a copy of the script, I’m happy to share.

Thanks, I have most of one from an earlier project, that I borrowed from this:

I’m mainly doing something for a friend’s storage heater problems, we don’t need a full monitoring setup/CMS etc.

Here’s the config.ini

[general]
language = en

[gpiozero]
bcm_sensor_pin = 27
sleep = 0.1
queue_length = 1
threshold = 0.1
[server]
ip_address = 10.0.0.100
port = 8086
database = temp
#user = root
#password = none

[data]
measurement = watts
sensor = 2
house = house_address
# cost in pence of one impression = 1 watt
cost = 0.0345

[schedule]
# not used at present

Also, I’ve had some trouble with the cost, that seemed to default as a string, so:
cost = float(config['data']['cost'])
before constructing the json, solves it.