# How to setup ESP32CAM for measurement capturing?

**URL:** <https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589>\
**Category:** General\
**Created:** [2 December 2022 02:02 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589 "2022-12-02T02:02:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pie25](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pie25](https://discourse.nodered.org/u/pie25)\
**Post date:** [2 December 2022 02:02 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589/1 "2022-12-02T02:02:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [2 December 2022 05:58 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589/2 "2022-12-02T05:58:22Z")

</div>

Hi @pie25,  
You might have a look at [this](https://discourse.nodered.org/t/3-phase-power-consumption-measurement/68489/31) 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.  
Bart

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [2 December 2022 06:30 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589/3 "2022-12-02T06:30:21Z")

</div>

> [@BartButenaers](#):
>
> There was also somebody last year who shared on Discourse a setup with image processing, but can't find it right now

[Here](https://discourse.nodered.org/t/extract-numbers-from-image/43529) you go...

---

<div class="post-metadata">

**Author:** ![RandomSporadicProjs](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@RandomSporadicProjs](https://discourse.nodered.org/u/RandomSporadicProjs)\
**Post date:** [2 December 2022 15:49 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589/4 "2022-12-02T15:49:35Z")

</div>

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 meterreader.py startingmeterreading  
eg  
python3 meterreading.py 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.

The

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.

```auto
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!")
        else:   
            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))
    time.sleep(15)

```

---

<div class="post-metadata">

**Author:** ![pie25](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@pie25](https://discourse.nodered.org/u/pie25)\
**Post date:** [21 January 2023 10:11 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589/5 "2023-01-21T10:11:53Z")

</div>

may I know your hardware setup?

---

<div class="post-metadata">

**Author:** ![RandomSporadicProjs](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@RandomSporadicProjs](https://discourse.nodered.org/u/RandomSporadicProjs)\
**Post date:** [21 January 2023 13:14 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589/6 "2023-01-21T13:14:27Z")

</div>

Hi

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.

HTH

 ![roi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/475bee77f93772145fb0fb1388d3cf642a3409f6.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 March 2023 13:14 UTC](https://discourse.nodered.org/t/how-to-setup-esp32cam-for-measurement-capturing/71589/7 "2023-03-22T13:14:41Z")

</div>

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