greenhouse sensors set-up

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

greenhouse sensors set-up

Post by ajocius » Wed Apr 11, 2018 8:21 am

I am very close to complete what I was making. Very close, but no cigar yet :)
I have connected temp, humidity and soil moisture sensors to ESP32 running micropython code it communicates sensor values to MQTT server running on Raspberry Pi, that also runs MySQL and Node Red. Node Red triggers mqtt message at selected interval, ESP32 listens for mqtt calls, reads sensor value, publishes back to mqtt, then Node Red post value to database on MySQL.

All is working well, except... :
1. Micropython code breaks after a while. I am not sure what triggers that. Usually I get some feedback from ESP32, when I am connected to it via rshell and run repl on it. But when I close connection, there is not feedback from ESP32, so I do not see any potential error message prior it stops working. What could be good way to find what is triggering my code to break?
2. Soil moisture (SM) sensor returns value of 4096 when it is completely dry and when it is only sensor that is connected to ESP32. I have now 3 sensors connected and this might impact SM sensor readings. It is below 2000. I guess it is possible to advice me on this without seeing electric scheme so I will try to visualize it somehow in future. But any words of advice? I have connected it via separate pin on ESP32, where I turn on 3,3V only when I do reading (to preserve sensor). Data connection is obviously on a separate pin as well. So only ground connection is shared with the rest of the grid. Also, i noticed that there is unusual spike in one reading, please see picture:
https://ibb.co/iEEvpx

Image
, not realy sure what could cause this. Sensor was laying on the table the whole time.
3. Time that I record to MySQL is by few hours different from real time. I have checked Raspberry Pi time zone value, seems to be correctly set. Time is feeded directly from Node Red, so possibly this is not right forum to ask this question, but I am sure there are many users here.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: greenhouse sensors set-up

Post by pythoncoder » Thu Apr 12, 2018 6:54 am

Are you using the official or Loboris ESP32 firmware? If you're using Loboris which MQTT code are you using, his or the official library? The reason I ask is that the official MQTT library has limitations which might be affecting you. It doesn't respond well to WiFi disruptions. The Loboris one may be better but I haven't yet found time to test it properly.

As for the electrical query (I think I can speak for others here) - we're completely in the dark without a schematic.

Unless someone pops up your Node Red/MySQL queries might be best pursued elsewhere, where there might be more people with experience of these applications.
Peter Hinch
Index to my micropython libraries.

ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Re: greenhouse sensors set-up

Post by ajocius » Fri Apr 13, 2018 6:40 am

I am using official firmware. Can I just flash Loboris firmware, copy my *.py file with code and test it? Or will I need to change code due to some syntax differencies?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: greenhouse sensors set-up

Post by pythoncoder » Sun Apr 15, 2018 9:20 am

The Loboris port runs standard MicroPython but his MQTT library is different and has different calling conventions. As I said, it's on my (rather lengthy) todo list to test this to see if it's more resilient than the official version.
Peter Hinch
Index to my micropython libraries.

ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Re: greenhouse sensors set-up

Post by ajocius » Fri Apr 20, 2018 10:46 am

ok, I will try to redo code with Loboris port, need to figure out what needs to be changed in my code, so this will take time.
At the same time I would like about other options that I have, would be great to hear comments on those:
  • - Run loop on micropython code to send sensor data to MQTT every 10 minutes, instead of listening to MQTT request to read data (i use Node-Red that sends request to read data every 10min). I am afraid same wifi/mqtt problem will break code execution and it will stop, just like now
    - Is there any way to check status of ESP32 and send few Micropython command lines to it. I will explain what I mean. Currently, when I see that data from sensors are not coming (meaning code has stopped because of wifi/mqtt problem), I connect to RaspberryPi, then rshell to ESP32 and send few lines (import machine) and (machine.reset() ) . These two lines restarts ESP32 and main.py file and sensor data is becoming available again. Can this be managed something from another device (checking ESP32 status and sending reset command? )
    - Any other was to ensure that code does not break ? Some on_error handling ? Like if wifi is disconnected, then reconnect. Or disconnect between each data read and reconnect before new data read, check wifi link, mqtt link and only then execute sensor data read and send to mqtt ?
    - Should I consider other languages on ESP32 that is more stable? Like Arduino IDE or something else (not even sure what else can run on ESP32)
I feel like I am very close to what I want to do, but I need to somehow be able to avoid code breaking.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

MQTT resilience

Post by pythoncoder » Sat Apr 21, 2018 7:51 am

I think it's fair to say that writing a resilient MQTT driver is non-trivial. By "resilient" I mean able to run for long periods while recovering from the following events:
  • Sudden WiFi dropouts
  • Broker outages
  • Slow connections (if the broker is on the internet).
  • Gradual complete loss and subsequent recovery of WiFi signal.
  • Errors in WiFi communication such as missing PUBACK messages.
The official driver does not achieve this. As far as I know nobody has done the kind of testing required to verify the quality of the Loboris implementation.

The fundamental point that writers of drivers can miss is that wireless links are inherently unreliable. WiFi is no exception. So driver code has to be written to be capable of handling errors at every stage of the process. It is easy to miss possible failure modes.

I wrote a resilient driver but this only achieved its aim on ESP8266. The last time I tested it on ESP32 results were not satisfactory, and it needed some nasty hacks like arbitrary delays to come near. I have been told that these hacks are no longer needed and I intend to re-test later this year. Testing resilience, of its nature, takes a long time.

In terms of your own project I'd set up a simple test with the Loboris port. Send a qos==1 publication every minute (say an incrementing count) from your greenhouse. Run some code on a machine in your house to receive these. Check for missed messages and complete crashes of the ESP32. See if it survives a week...
Peter Hinch
Index to my micropython libraries.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: greenhouse sensors set-up

Post by rdagger » Sun Apr 22, 2018 5:01 pm

The Lobo build has an MQTT autoreconnect feature. It also allows you to poll the MQTT state to accommodate Wi-Fi disruptions. For example:

Code: Select all

station = WLAN(STA_IF)

client = mqtt(CLIENT_ID, SERVER,
              autoreconnect=True,
              cleansession=True,
              connected_cb=conncb,
              disconnected_cb=disconncb,
              published_cb=pubcb,
              data_cb=datacb)
status = client.status()[1]              
if status == 'Stopped':
    try:
        if station.isconnected():
            print('Trying to start.')
            client.start()
        else:
            print('Station disconnected.')
    except:
        print('Failed to start.'
)

Post Reply