How to setup ESP32CAM for measurement capturing?

Hi everyone.
I need some suggestions regarding my project. I want to set up an ESP32CAM to capture images of reading on a fuel station pump and want to display it in the Node-RED dashboard. Is there any suggestion to learn the algorithm for image processing of the readings and Node-RED dashboard construction?

Hi @pie25,
You might have a look at this solution from @RandomSporadicProjs

There was also somebody last year who shared on Discourse a setup with image processing, but can't find it right now.

Here you go...

If this is of any use, here is my python code to read from a live image of my electricity meter.

I installed open-cv for python then wrote the script below - it grabs in image from a camera looking at the meter.

The script is invoked by
python3 startingmeterreading
python3 75000

So it starts counting from the given starting value for the meter.
The script looks for the presence of white pixels. I don't do any ocr with this script, I just check for black gaps between white digits, and count them. I have tuned the area of interest to be a narrow sliver across the digit, slightly smaller than the gap between digits.

I have the script running in a detached 'screen' so it just runs all the time.


if meter_reading % 10 == 0:
meter_reading = meter_reading + 1

is a bodge as for some reason one of the gaps isn't detected (on a regular basis ie every 10th reading is missed) so I just allow for it - and can be omitted if unnecessary.

import matplotlib.pyplot as plt
import imutils
import cv2
import numpy as np
import time
import random
import sys

from paho.mqtt import client as mqtt_client
meter_reading = 74001
last_incremented = 0

n = len(sys.argv)
print (n)

if n == 2:
    meter_reading = int(sys.argv[1])
    print("Using Meter Reading of ", meter_reading)

broker = 'MQTT broker IP address'
port = 1883
topic = 'MQTT reading topic'
client_id = f'python-mqtt-{random.randint(0, 1000)}'

username = 'BrokerUsername'
password = 'BrokerPassword'

currentstate = -1
previousstate = -1
can_be_incremented = 1

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
            print("Failed to connect, return code %d\n", rc)
    client = mqtt_client.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker, port)
    return client

client = connect_mqtt()
client.publish(topic, str(meter_reading))

while True: 
    now = int(time.time())    
    url = "url for live image of meter"
    meterpic = imutils.url_to_image(url)
#    rotated = imutils.rotate(meterpic, -1.25)
    cropped = meterpic[226:230,470:550]
    imggray = cv2.cvtColor(cropped,cv2.COLOR_BGR2GRAY)
    thresh = 20
    maxValue = 255
    th, dst = cv2.threshold(imggray, thresh, maxValue, cv2.THRESH_BINARY)
    n_white_pix = np.sum(dst == 255)
    print('Number of white pixels', n_white_pix)
    if n_white_pix > 60:
        print ("Digit in window")
        currentstate = 1
    if n_white_pix < 10:   
        print ("Digit not in window")
        currentstate = 2
    if n_white_pix > 40:
        can_be_incremented = 1
    if currentstate != previousstate:
        if currentstate == 2 and previousstate != 2:
            if can_be_incremented == 1:
                print ("Increment reading")
                meter_reading = meter_reading + 1
                if meter_reading % 10 == 0:
                    meter_reading = meter_reading + 1
                previousstate = currentstate

#       cv2.waitKey(0)
#       cv2.destroyAllWindows()

    print('Current state ', currentstate)
    print('Previous state', previousstate)
    print('Time now = ', now)
    print('Last incremented = ', last_incremented)
    print('Time since last increment = ', (now - last_incremented))
    print('Can be incremented = ', can_be_incremented)
    print(' ')
    client = connect_mqtt()
    client.publish(topic, str(meter_reading))

may I know your hardware setup?


It's simply a pi zero W, connected to a basic rpi camera. The red LED on the camera is sufficient to provide enough illumination in a dark cupboard. I installed MotionEye OS on it to provide a stream of images (and a still) from the device.

The script above runs on my home automation RPi4, and pulls in the image from the pi zero W and it works out if the value should be incremented or not.

I've added an image, and the region of interest where I count the white pixels - it's on the unit column of my meter.


1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.