Tuesday, May 24, 2022

AI on the edge vs. Traditional Engineering. Water Meter with ESP32-CAM

Artificial intelligence on the edge sounds modern and cool reading numbers or dials with an esp32 cam for a few dollars even cooler but does it work and how does it compare with a traditional barefoot approach let's have a closer look .

Great youtubers here is the guy with the swiss accent with a new episode and fresh ideas around sensors and microcontrollers remember if you subscribe you will always sit in the first row i have a shelley 3m to measure the energy consumption in our home now i wanted to measure our water .

Consumption too i found a project which innovatively does that it takes picture of the water counter using a simple esp32 cam does some artificial intelligence magic using tensorflow and finally transfers the reading via mqtt to my raspberry pi a wet dream for an engineer .

Let's start immediately i still have some cam boards in my esp32 treasure chest and an old four gigabyte sd card too downloading the project and copying the content of the sd folder to the sd card is a no-brainer and also the addition of the wi-fi credentials to the ini file fortunately we get the bin files .

Ready-made and only have to flash them to the esp32 cam board using the espressif tool you find the links and addresses in the well done readme of the project the most complicated thing is that the esp32 cam does not have an integrated to serial chip at least if you did not buy it with a .

Daughter board so i connect this adapter to the rx and tx pins then i add a push button to ground gpio 0 while pressing the reset pin all business as usual just pay attention that you switch the adapter to 3.3 volts and check before that the adapter also has 3.3 volts at .

The data pins some of them do not and might kill your esp32 now we can flash the software and after reboot you should be able to connect to the chip using your standard browser i do not have to search for its address because i entered a fixed address into the ini file .

The next step is to create a part to mount the camera at a certain distance from the counter 10 centimeters is okay now comes the first problem the project creator seems to live in germany and their counters are slightly different so i had to draw my own 3d design using fusion 360. not a big deal .

The camera hole has to be in the center and the opening for the led obviously where the led is after a few hours of printing the part was ready and i could continue the setup is straightforward and well made it is incredible what we can do with such an esp32 .

First we must create a new reference picture and save it to the sd card the lens has a variable focus but you only can adjust it after you remove the glue blob take a few pictures till you find the proper focus if we look at the picture we see the subsequent problems .

Obvious and not so obvious ones an obvious one my counter has different colors black is white and vice versa that should not be a problem because here we see a counter with the same color scheme another obvious one is that two of the four dials are covered by a rotating gray thing and the counter .

The not so obvious is the glare created by the led mounted on the camera board this led is switched on and off by the program by the way very handy unfortunately it creates a lot of glares particularly on the counter and the numbers are hard to read even for a human i experimented with several layouts .

Using a second led at a lower position and some diffusers this is the best picture i got after saving the reference we have to determine two fixed points and later mark all numbers and dials then the artificial intelligence does its thing this is the result red are the two .

Anchors blue the five digits and green the two dials and here are the numbers the one has too much layer and is determined as a zero the five is okay the nine is an eight and the last digit obviously cannot be red because the counter is between two digits .

So we get a n-a-n or not a number the analog meters also are not what i would expect 8.4 and 0.6 anyway let's have a trial run maybe this tensorflow thing is really intelligent after entering the data and credentials of the mqtt broker we get every three minutes a number transferred to node rate .

And here is the result the nine is okay now but the decimals are very wrong i opened the water during the test and we should have gotten an ascending number instead i would say we got more or less random results i have to say that other installations seem to work and maybe my problem is the .

Different colors still it shows the limitations of ai in reality it has nothing to do with intelligence it is purely pattern recognition i could now be disappointed give up and go for an evening beer but as always on this channel we want more an engineer's work always starts when something does .

Not work the other things can be done by the mbas when i search for documentation of my water meter i found that they offer this automatic reader so why should we not be able to create such a reader if they can and of course wirelessly not old-fashioned with wires .

They must get a signal from somewhere we remember the counter and its gray plate on the top of the left dial it rotates fast when water is used so sherlock holmes would see enough evidence this piece was added and i'm sure the inventor sold it as digitalization .

But how can we read it i would guess that they were looking for something cheap and reliable either a hole sensor or a photo sensor i tried a whole sensor and it did not provide any signal so this rotating piece is not magnetic and we have to try light fortunately i do not only have a .

Treasure chest of esp32s but also have many other such boxes and here is what i found a reflective optical sensor with the name cny70 it consists of an ir diode and a phototransistor here is how we can connect it .

Please connect this wire to 3.3 volts if you use an esp32 you can experiment with this serial resistor to get a reliable signal with the lowest current if we mount the sensor on top of this reflector we should get a digital signal 50 on and 50 .

Off right wrong as usual the real world is not digital and because the reflected light is diffuse we get something like a sinus curve somewhere between 3.3 and maybe one volt but it depends on many things so we have to use an analog input .

Instead of a digital one and we have to do some pattern recognition without a i of course that would be over my head we need a reliable signal from video number 340 we know that the analog input of the esp32 is noisy and the reflection also might differ from time to time this is why i divide .

The curve into four parts zero is where the first derivative and the reading are positive one is where the reading still is positive but the first derivative is negative two is where both are negative and three is where the derivative is positive and the reading is still .

Negative then everything starts over and we know for sure that we detected an entire cycle so we can add a water tick as usual i use a state machine that switches between the four phases this is the most readable way of dealing with complex structures .

Much easier than a ton of if then else statements now i only have to make sure that i do not lose the count in case of a power loss i plan to use a battery powered board like this one in the finished design in addition i save the counter every thousand counts in eeprom .

So the error after an unscheduled power down is less than one cubic meter and here are some tricks if you want to work with eeproms i use a magic number to determine if the ee prompt contains valid information for example if the number is 55 hex i can assume that this is not by chance if it's not 55 hex i set the stored .

Values either to zero or a preset value and of course set the magic number to 55 hex next time my sketch knows that it can rely on the number in the eeprom and if you want to change the preset value during the project not easy because the magic number is there your sketch will always read the .

Value and not update it in this case i just changed the magic number to 54 hex and add a new preset value now it stores the preset and you can change the magic number back to 55 hex or leave it because you can choose whatever magic number you want just not 0 or ff hicks because these .

Numbers could be in the ee prom from the factory the rest is work create an mqtt message with the consumption per time and the counter value and send it to node rate then store it in influx dp and show it in grafana and here you see the result the person who showered in the evening needed much .

More water than the one in the morning you decide who is who in this project i had my second encounter with ai on the edge the first was during a demonstration of an stm engineer he had a trained device with an m0 core that could or should have to distinguish between different photographs .

Also there i was disappointed with the results but on the other hand i'm astonished how good google translate works or also speech recognition maybe they are more advanced than what we have available for our projects and for sure they need quite some computing power so my learning for now is that if i need reliable sensing i still go back to .

Adequately engineered solutions that reduce complexity a lot but i stay tuned and do not give up testing new things if they are developed that was all for today as always you find all the relevant links in the description i hope this video was useful or at least interesting for you if true please .

Consider supporting the channel to secure its future existence thank you bye


Most Popular