September 2019 Update

New! We're moving to the new forum!

ALL new Blog posts will be on the forum.

Please join us there.

This Blog will no longer be updated...

Friday, August 9, 2019

Is it configurable?

I was asked the following question on another forum, and my LONG answer has enough useful information I decided to post it here too...

Also, is this for a soil moisture sensor? I would think the web would have plenty of documented projects with functioning circuits and code. 


I took a different approach than most. Grab a coffee and a joint, this is fairly long :)

:nomo: :lot-o-toke:

Everything is configurable, nothing is hard-coded into the program.

As the user, I can add "sensor types" in the database, such as "DIY3-SOIL" which is my current version of the soil moisture sensor, or "PHOTO" which is just a photo-resistor. If I make a new soil moisture sensor, I add it to the sensor table, enter the sensor type, and it assigns a sensor id to it, and I can then assign it to a module (where is it plugged in) and a location, or plot... 

The one part I haven't quite figured out yet is how to make the calculations configurable... Right now, I still have to hard-code the calculations for each sensor type. For example, a soil moisture sensor is easy, just map it's raw value into a percentage (actually, I've made it a lot more complicated, but that's basically it) where a thermister has fairly complex math that must be done. I need a way to script the math... That's one of the RARE things I've hard-coded. Unfortunately, that means as a user, to add a new type of sensor that needs a different calculation type, the code has to be modified to accommodate it.

Here are some sensor types:


and then some sensors:


You can see the sensorTypeId ties this sensor back to the sensor type. This is all user configurable in the database.

Now you assign a sensor to a Sensor Module:


PortNumber is basically the Pin number, again, we configure a port number to be specific pins:


A Module has 16 analog "ports" and 53 digital "ports" 

In addition to a pin assigned to read the sensor, you may also define a power pin, a digital pin that we can switch HIGH and LOW to provide 5v to the sensor. (must be under 40ma MAX) I'm working on a proof of concept to use this to control a transistor, which will switch an external 5v power source, allowing for a more stable voltage, and higher current.

So for each port number you define, you configure which pin reads the sensor, it can be analog, digital;, or even an i2c device, plus you specify the pin to provide power, and whether to toggle that power on only when the sensor is being read. This prevents excessive electrolysis corroding the sensor in the case of soil moisture sensors. 

Now that the module knows it has a sensor to read, it has all the info it needs to read it. Now we just need to know where that sensor is. I have defined locations as configurable:


Each location has a grid defined, how many rows wide (columns) and how many rows deep (rows). Each of those "plots" can hold one plant. This is how I know where to find a particular plant. Just like assigning a sensor to a location, you also assign a sensor to a plant, and a plant to a location...


Virtual location is something I created because on my web page, I have a banner type thing across the top and can pop sensor info in there, like so:


The orange is telling me the sensor has not been read for longer than a specified time. In this case, these are on my development module, and it is not plugged in and running at the moment...

So now that we have a sensor mapped to a module to read it, and a to a plant so we can make use of the reading, and we have assigned the plant to a location....

The web page can display this for each location:


There's a lot of information there, plus you can click on one of the "plots" to see this:


This is the history of readings, you can clearly see when it was watered...

Plants are added as you go:


So, if you made it this far, you see, the code has to be very generic so that it can handle all these variable configurations, and that was the most challenging part of this project. You are only looking at a very small part of the system here... there are aver 50 database tables.

While I'm now more open to the idea of actually releasing all the code, when I started, this was just for me, never planned to release anything, so hard coding would have been easiest, but as a software engineer with 40 years experience, I knew it had to be configurable, even for my own use. 

So, to answer your question about why didn't I just grab someone else's code and make it work for me, the answer is that I wanted to be innovative. I started my programming career with a stint as a fairly successful shareware author, so know the appeal of innovation. I love the challenge of doing things my own way... There was some discussion early on here in another thread I think, about whether to roll your own or not, and while using someone elses code can save you time initially, in the long run, you still need to be as familiar with it as if you wrote it, so that you can fix bugs, or make changes to it. If you simply rely on others, they may not update their code going forward, and then you need to find something else, or write your own anyhow... 

Unfortunately, this complexity has left me with a software system that I don't even know how to install from scratch, as it was entirely built in place.... This is something I need to figure out, and document, before I can even think of releasing the code as Open Source.

No comments:

Post a Comment

Any comments deemed off topic or offensive will be removed