Skip to content

Added MCP3008 Node Support #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2019
Merged

Added MCP3008 Node Support #1

merged 1 commit into from
Oct 5, 2019

Conversation

P33ry
Copy link
Contributor

@P33ry P33ry commented Sep 21, 2019

Greetings,

I've added support for reading sensors using the MCP3008 (an Analog-to-Digital Converter) to MudPi as a different Node Type. This way one can connect analogue sensors directly to the RaspberryPi's GPIO after they went through the MCP3008 without involving an Arduino.

An example to the MCP3008 usage.

New SensorWorker class

I created an ADCMCP3008Worker (workers/adc_worker.py) similar to the SensorWorker class it dynamically imports the sensor classes, initializes them and reads them when running. Basically the same as the original SensorWorker but also taking care of initializing the mcp chip.

To do this cleanly I've used several new imports. They should all be covered by the packages:

  • adafruit-blinka
  • adafruit-circuitpython-mcp3xxx

They are available over pip for me.

Are the dependencies listed somewhere? A requirements.txt for pip might be useful.

Node Types

I understood MudPi expected the Soil Sensor to come from a node and Pi_Sensors to be only for optional (digital) sensors and not core ones like the Soil Sensor. Therefore I've introduced Node Types where I moved the original nodes to the type "arduino" and added "ADC-MCP3008"

For that purpose I've also rearranged the _sensors_ folder so that the original sensor classes (now under sensor/arduino) and the sensor classes for reading from the mcp3008 over SPI (sensor/MCP3xxx) aren't mixed up.

Created a new Node setting "type" to select original node ("arduino") or the new "ADC-MCP3008" Node and updated the mudpi.config.example with examples accordingly.

Testing

Tested changes with:

  • RPi 3B running MudPi
  • one MCP3008 chip connected to the Pi's GPIO (see the example for a detailed how above)
  • Soil Moisture Sensor from Sun3Drucker Model SX239
  • some Dirt

Also wanted to test the reaction with a Relay but haven't seen the right place to trigger switch events for it yet (or how to actually trigger a pump?) Is there code to trigger these events?

I'm also unable to fully test the SensorWorker class. I had to edit the method dynamic_sensor_import to fit the changed folder structure but don't have an Arduino.

Note

Critique, correction, etc. is welcome for both code and PR format (first time to make one).

Created a new ADCMCP3008Worker (workers/adc_worker.py) simila to SensorWorker to control sensors connected to GPIO via the MCP3008 analog-digital converter (e.g. analog Soil Moisture Sensor).
Split "sensors/" into "sensors/arduino" and "sensors/MCP3xxx" for the new different Node Types.
Created a new Node setting "type" to select original node ("arduino") or the new "ADC-MCP3008" Node and updated the mudpi.config.example with an example accordingly.
@olixr
Copy link
Member

olixr commented Sep 23, 2019

This is excellent. I will review and work on getting this merged in. More updates in the works as well.

Thank you for your support!

@olixr
Copy link
Member

olixr commented Oct 5, 2019

I had time to review this request and test it on my demo units. This is a great addition to the library @P33ry. I also have some other updates in the works I may push this weekend.

Your PR format is good and I was able to install the needed libraries to support the update. I agree there should be an updated package/dependancies list, I will work on that. The MCP3xxx is interesting and I like expanding node support. I would like to look into more of these options.

The folder restructure is also an acceptable change. There is some refactor and cleanup on the list that will help with removing some old testing code and legacy files.

In terms of testing the relays there are scripts in the tools folder that I don't think have been fully documented on the site yet. There are three scripts named redis_pub.py, redis_pubsub.py, and redis_sub.py. I used those for testing redis functions like triggering the relays. To trigger a relay you need to publish a switch or toggle event to the relay. You can read more about this on the docs section for relays.

However you would want to boot up the redis_pub.py script and use that to test out sending an event to the relay. You will need to know the topic value that was set in the relays config.

Testing

I tested the changes on a demo unit and the SensorWorker class is working fine.

Action Items

All changes approved and will be merged.

@olixr olixr merged commit 8b35417 into mudpi:master Oct 5, 2019
@P33ry
Copy link
Contributor Author

P33ry commented Oct 5, 2019

That's great to hear and thanks for the feedback @olixr !

The current state of MudPi seems not operational except for debugging & development purposes. Is that correct?

I've since added code in my fork to get my setup to switch the relay live on sensor readings. I assume such functionality is part of the update you mentioned?
That'd be interesting to know to prevent conflicts for possible future PRs.

Best of luck to you and MudPi!

@olixr
Copy link
Member

olixr commented Oct 6, 2019

Its operational but not to the full extent I have in plans. My own MudPi systems have been running on various versions for the past 2 years now. The triggering of relays and solenoids has been through manual controls (i.e. a dashboard). I have a scheduler that runs in the background and would also toggle the relays on a time basis. (supervisor running script that fires redis events)

The plan of what I am working on is better link-able actions. This could be manual or automated based on a number of conditions like sensor readings or schedules. Part of my system also involves this web app that I have not released much publicly yet. This web app enhances the open source core with additional features like my manual controls and scheduling.

Currently in the works I am working to bring some more linked actions to the core and ways to configure them. This also includes adding button support to have actions fired off bases on the button presses. Additionally I want to update the scheduling I have for relays to involve conditional triggers like sensor readings and other factors. Part of the challenge here was first designing a format that makes sense to allow this feature while also being configurable enough for folks to implement a wide array of uses.

As I get further here I would like to have a better shared roadmap with planned features and keep more frequent developer updates. I am sending out a newsletter once a month to include some of these developments and I have a twitter page for the project to share progress of work.

Thank you again for your help on the core and I plan to get more released soon here. Also I made a discord server to folks to keep in touch as well. Please feel free to join the tech talks with me. https://discord.gg/daWg2YH

olixr pushed a commit that referenced this pull request Nov 11, 2020
A bit of refactoring and linting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants