420 Magazine Background

Arduinos In The Grow Room: My Project

odam2k

Well-Known Member
Tuesday, August 20, 2019
Dazed and Confused...

I'm stumped... The new firmware is working extremely well, except for two things...

The module is configured to scan all attached sensors at specific intervals. When it's time, it fetches a lit of sensorId's from the database, and then loops through that liat and loads the configuration for each sensor.

For some reason, the first sensor configuration is loaded ok, but the rest are not, the records are empty, or in some cases contain weird data that appears to be left over from a previous query, like field names... I can't put my finger on the problem... My quick solution at the time was to simple close the database after each use, which has the effect of clearing out the memory buffers used, so the query works now, however, the whole process is slowed down as it needs to keep connecting to the database every query.

The second issue just popped up last night. When a sensor fails the Sensor Test, three things happen:

  1. Send a warning to the Serial Output
  2. Send a notification to my phone using Blynk
  3. Write an entry to the debug log database table
For whatever reason, it will not write the database entry (while every other read and write is working fine, including many writes to the debug log table) and causes the ESP8266 to reboot. If I remove the call to write the debug entry, it works just fine.

While I have a workaround for both issues, they must be a symptom of something being quite wrong. Whatever is causing this could be causing all sorts of unseen issues, and those can be the most dangerous bugs :)

So, I've tried all sorts of things, and if I can't get a handle on this soon, I guess I'll start over. That won't be so bad, I'll aim to get the sensor test code done first, and see if it still has the same problem, if not, it will be a simple matter of adding the rest of the code back in until it fails...

It looks like I have a game plan, it's certainly going to set me back timewise, but I'm sure I'll learn a lesson here somewhere. :)
 
Last edited:

odam2k

Well-Known Member
Really clean lines there man, nice work!
Except for the transistors :( They work, but they look like crap. I think for the next one (I'll need 3 altogether) I will just make a separate board with the resistors and transistors and it can just plug into the main board. It could be sort of an optional expansion pack :)
 

odam2k

Well-Known Member
More posts... click the titles to read the rest...


Tuesday, August 20, 2019
Cool new gadget... OLED Screen for Modules
In this previous post I mentioned that part of rewriting the Sensor Module was to remove bloated code that isn't used any more. Because for the most part, I'm learning as I go, sometimes I discover something that I just have to include.
I'm afraid that may never end...







Wednesday, August 21, 2019
Rumors, whispers, and a WTH?
Some good news, I've managed to convince the accountant wife to release a few more dollars this month for my hobby :) As it sits, I've got 20 pumps and 4 x 8 relay modules in my shopping baskets along with a few other goodies for some other projects. If priorities don't change before the end of the month, I will be moving forward with the automatic watering.
I know eh? I can hear the whispering... "He's said that before!"





Wednesday, August 21, 2019
Keeping track of ideas...
This is a different sort of post. I've mentioned before that I keep a text file open in my Visual Studio IDE while I'm coding, so if I have an idea I can jot it down immediately... It's absolutely true what they say about smoking pot, and short term memory... and I smoke a lot of pot... lol This acts as my to-do list, although it often contains thoughts that haven't quite coalesced into a coherent idea. It's like my "idea incubator", I often just glance through it, add new thoughts, update previous ones.. it is entirely free-form, grammar and spelling aren't important.


Wednesday, August 21, 2019

Restless night...
I can't seem to stay focused tonight...
I'm going through the code trying to figure out why it has problems with MySql, but only in two places... So as I'm following through some code, I get to the section that does the compare of CRC values in the payloads to see if it is a valid payload.
Here's the code I found....
 

odam2k

Well-Known Member
How is the sql data sent to the database? I can help here for sure but need a little more detail.
I'm using the MySql Connector

It is on an ESP8266 (ESP-01)

I don't think it's a problem with the connector, I'm sure it's just something stupid I did. I haven't really spent much time looking yet, I kind of got side tracked.

I only use snprintf these days, but it looks like the sort of behavior I used to see when I put 100 bytes into a 10 byte variable :) I'm pretty sure it's the same sort of mistake, just need to find it...

The reason I don't think it's a problem with the connector itself, is I am using it iin many different projects, and have NEVER had this problem before. At first I thought maybe it had updated the connector and that caused my problem, but nope, everything else still compiles and runs properly.

Here is how I use it...
Every time I do a query, I call connectedToSQL first to reconnect to MySql if we are not already connected. In the past, I stayed connected the entire time to avoid the overhead of continuously opening the database.. As part of my testing for this bug, I added a close database after every query... I'll highlight that one change... other than that, same code...

boolean connectedToSQL()
{
int counter = 0;
while ((!mySqlConnection.connected()) && (counter < 5))
{
IPAddress ip;
if (ip.fromString(espCfg.esp8266_Config.mySqlServerAddress))
{
mySqlConnection.connect(ip, espCfg.esp8266_Config.mySqlServerPort, espCfg.esp8266_Config.mySqlUsername, espCfg.esp8266_Config.mySqlPassword);
}
else
{
Serial.print("Cannot parse IP Address for Sql Server: ");
Serial.println(espCfg.esp8266_Config.mySqlServerAddress);
}
counter++;
}
if (mySqlConnection.connected())
{
lastSqlConnCheckOkTimestamp = millis();
}
else
{
lastSqlConnCheckFailedTimestamp = millis();
sendError("Unable to connect to MySql");
}
return mySqlConnection.connected();
}

and here is a standard query...


int fetchSensorIdList()
{
int _sensorCounter = 0;
snprintf_P(tmpBuffer, sizeof(tmpBuffer),
PSTR("SELECT `sensorId`, `blynkSendFreqSec`, `secondsBetweenScans` FROM `%s`.`vw_active_sensor_info` where `moduleId` = %i order by `portNumber`; "),
espCfg.esp8266_Config.mySqlInstance, moduleCfg.moduleId);
mySqlConnectionClose();
if (connectedToSQL())
{
clearSensorList();
MySQL_Cursor *cur_memList = new MySQL_Cursor(&mySqlConnection);
cur_memList->execute(tmpBuffer);
column_names *colsList = cur_memList->get_columns();
row_values *rowList = NULL;
do {
rowList = cur_memList->get_next_row();

if (rowList != NULL)
{
for (int f = 0; f < colsList->num_fields; f++)
{
if (strcmp(colsList->fields[f]->name, "sensorId") == 0) sensorList[_sensorCounter].sensorId = atoi(rowList->values[f]);
if (strcmp(colsList->fields[f]->name, "blynkSendFreqSec") == 0) sensorList[_sensorCounter].blynkSendFreqSec = atoi(rowList->values[f]);
if (strcmp(colsList->fields[f]->name, "secondsBetweenScans") == 0) sensorList[_sensorCounter].secondsBetweenScans = atoi(rowList->values[f]);
}
_sensorCounter++;
}
} while (rowList != NULL);
delete cur_memList;

mySqlConnectionClose(); // ADDED Aug 22 - does no good...
}
return _sensorCounter;
}
The mySqlConnectionClose() simply did a mySqlConnection.close(); but it didn't help, just slowed things down, so now the method is empty...

I've been using this same code since I started using MySql, so like I said, I really don't believe it is related to MySql, I just think that's where this issue has manifested itself...

Appreciate your offer of help, but I think you'd need to go through the entire projects code...[/QUOTE]
 

odam2k

Well-Known Member
Interesting, for a thread with only 3 people posting on it, we've had 1000 views in 19 days. Good enough reason to keep posting my summaries I suppose :)
 

odam2k

Well-Known Member
How is the sql data sent to the database? I can help here for sure but need a little more detail.

Interesting.... I've been working on this all day :( Just when I think I found the offending code, the issue changes...

I reread my last post, and it said adding the database close did NOT solve the problem, I must have been really tired or stoned, because in fact, it does solve the problem. The method I created gets called after every sql access, and closes the database connection. It is a bit slower, but for this particular module, it's not an issue...

What I did was change the method so you pass a boolean whether to close the db or not, and had everyone passing a false, and one by one I went through and set them to true until I found the one that fixed the issue... I narrowed it down to a method being called, so I commented out the call to the method (which did do database access) then it appeared to work, and I got all excited so went into the method and commented out most of it, hoping to narrow down the offending code. Well, I ended up commenting everything out and it still failed... Very confusing behavior, hard to keep track of what works and what doesn't....

Anyhow, at this point if I only close the database after that method, then another method fails, and so on... so I'm starting to think it's NOT something I've done, but I'm nowhere close to blaming the library yet...

I don't want to spend forever on this, but at the same time, I KNOW that something isn't right, and there's no guarantee it isn't still causing problems I just don't know about yet :(
 

odam2k

Well-Known Member
I decided to hold off on upgrading Sensor Module 2 to the new hardware/firmware until I get this resolved... I'm gonna give it a rest for a bit and come back to it. Since I had a wiring problem with module 2 (power to the first sensor was flaky) I was going to do this upgrade to my new "Mega Shield", but instead, created a mini-adapter :)

1566662103349.png


Works like a charm, and I'm back in business...

Well, sort of, I've got all my my plants pulled out of the flower tent so I could remove and clean the prefilter on the can filter. I washed it in the sink, now just waiting for it to dry...
 

odam2k

Well-Known Member
It's been running for 6 months now, I never really noticed it was dirty until recently, but really wasn't paying much attention to it.


1566709616349.png


It was a pain to get it off the can without taking it all apart since it was hanging in straps and zip ties for security, you can see all the white lines... Actually, it was even more of a pain to get it back on after...

I just vacuumed it first to get the outer layer of dust off it. You could actually see it get whiter. Then I washed it 3 times with dish soap in the kitchen sink, rinsing it each time, and wringing it out. To dry it, I hung it on our drying rack, you know, a treadmill that never gets used. :) I had a fan blowing on it, and it dried in about 20 minutes. I had to pull most of the plants out of the tent to do this, so once it was dry, and re-installed on the can, I put the plants back in the tent, but re-organised them. I have 2 plants about 5' tall, the rest are less than 3' (i do perpetual harvest, plants in and out all the time) so I added a small folding table we use for camping, and what a difference, not only did it bring them closer to the light, it let me put more plants in the tent, so I filled it up :) The two taller ones will be coming out in a couple weeks so as these others stretch and grow, it will make some room but in the mean time, it's pretty crowded in there...

1566710267226.png


Of course, that left the Veg area pretty bare... Not to worry, I have LOTS of recently rooted clones in small cups...
Here in Canada, it is legal to gift cannabis, including plants as long as they are not flowering. I met up with another grower who was giving away some cuttings that were still in a cup of water, this was through a reddit group. Anyhow, turns out he gave me 20 cuttings! I threw them into my "clone chamber" with my Automated Plant Mister on the Switch Module, and just left them... They were cut 4 days before he gave them to me, they rooted 6 days after I got them, I barely even checked on them, I love that automatic mister :)

The mister is an i2c device, I'm thinking of creating a wake-up command for it, when the Switch Module want's to mist the plants, it can send "Hey Mister!" :)

Anyhow, the Veg area, along with 16 rooted clones out of the 20 (these came from F2 seeds apparently), I also have 5 CB Dream (CropKing) rooted clones, and 2 more of an F1 cross from seed that a user on another forum sent to me. Gotta love that it's legal to do that now :)

So, at the moment, I'm right at my limit, I'm allowed 40 medical plants, plus we get to also have 4 recreational plants in addition as per the law here in Canada.

So, the future looks bright (at least in about 10-12 weeks!)
 
Last edited:

odam2k

Well-Known Member
Summary of recent posts from my blog

Click the title to read the full post.


Thursday, August 22, 2019
Sensor Module 2 Hardware/Software Upgrade
Yesterday I replaced all the sensor wires on Sensor Module #2 which hosts the sensors for the Flower Tent. There were a hodgepodge of wire types from really thin wire, that was part of a 20 wire ribbon cable, up to the speaker wire I'm using now. I did this for a few reasons, but mainly because I'm replacing all the DuPont connectors with JST connectors which are much more secure. Another reason is the different qualities of wire probably had some effect on the readings, plus having all the same type of wire looks much more pleasing :)


Friday, August 23, 2019

2 steps forward, one step back...
It never fails, when things are going well, strange things happen.
The prototype "Mega Shield" I've been running here for days now without problem, suddenly doesn't work.
I decided to take a break from building the new sensor module shield, and hooked up the prototype, planning to start some testing, and every sensor on every port was failing the initial power test. Eventually I grabbed the multi-meter and started measuring voltages...


Saturday, August 24, 2019

New plan for Module 2
I have the new Sensor Module "Mega Shield" and a new version of the firmware for the sensor module which I had planned on implementing, but due to the outstanding bugs, I've decided not to go forward with this until I resolve those issues.
So back to Module 2, and it's rats nest of wires...



Sunday, August 25, 2019

For every problem, there's a solution!
I recently purchased two small .96" 128x64 OLED screens and the first place I used it was on my prototype Mega Shield where I discovered that the I2C pin-out has the clock and data lines reversed, so my "universal i2c" connectors won't work with it. No problem, I'll put a "reverse wired" i2c 4-pin female header on the Mega Shield so I can just plug it in there.
That's not the problem though. The issue is, if I am placing a non-standard (at least for me) connector on the Mega Shield, what orientation should it be? It is a display screen after all, and I'll likely want to be able to read it.
 

odam2k

Well-Known Member
Wednesday, August 21, 2019
Rumors, whispers, and a WTH?
Some good news, I've managed to convince the accountant wife to release a few more dollars this month for my hobby :) As it sits, I've got 20 pumps and 4 x 8 relay modules in my shopping baskets along with a few other goodies for some other projects. If priorities don't change before the end of the month, I will be moving forward with the automatic watering.
I know eh? I can hear the whispering... "He's said that before!"
Oh oh, I'm going to do it again... Postpone?

I could REALLY benefit from a pcb etching kit. I know, you can get custom boards made very cheaply online, but I need to be a lot quicker at prototyping, I can't wait weeks, or even days for a board to come in the mail. By that time, I'll be three versions ahead!

I don't have a laser printer, and I really don't want to draw circuits by hand :( I'll do some research, and if I can afford a solution with the funds I'll have next week, then I think that's the smart move at this point.

Any suggestions?
 

odam2k

Well-Known Member
Two short posts from my blog. These are the full posts.

Sunday, August 25, 2019
I miss having whiteboards...

When I was working, we had an obsession with whiteboards, they were everywhere... almost every available wall space had whiteboards on it. It made design much easier, write, erase, rewrite, draw lines, boxes, arrows... Then once you have it all sorted out, you take a picture, and go to work....

The whiteboard was particularly useful to either design or understand software architecture. Since I'm in the middle of reviewing the Sensor Module software, and rewriting it, I sure could benefit from a whiteboard rather than trying to do it on paper.

I could use the whiteboard paint on the walls, but that's not overly appealing... I need to buy some Panda Plastic anyhow, perhaps I can just hang a sheet, white side out, on the wall when I need it?

Maybe I'll get lucky and find a great deal on a new or used one somewhere... and even luckier to have the funds for it :)




Monday, August 26, 2019

Do we need to be perfect?

Out of roughly 3,500 sensor readings being passed from the Mega to the ESP then on to the MySql server, only 2 records were not saved to the database. That number is so small that I'm not even going to go try to find out why.

That's only 0.0005% of the records had an issue, or as I like to see it, a 99.9994% success rate! :thumb:

 

odam2k

Well-Known Member
Recent blog posts, click the title to read the post.

It's been a busy couple of days!


Monday, August 26, 2019
Sensor Module "Mega Shield" v2
In a previous post, I added an OLED 4-way mounting adapter to a new Sensor Module shield I'm creating. Today I added the pins for the i2c header, and 2 digital ports. I can always add a couple more if I need them.


Monday, August 26, 2019
OLED Display messes up my serial comms
If I try to update the display while receiving data from the ESP8266 on the hardware serial port, it drops incoming characters, so my CRC check fails, and it has to resend the data. That's not really a problem, since it will resend, but I hate knowing there's an issue...


Monday, August 26, 2019
Humidity and the DHT22
It turns out these temperature/humidity sensors don't like to be kept in a high humidity environment for very long. I had one in my clone chamber, which was consistently about 28C and 90-100% humidity. That sensor now reads 99.9% humidity no matter what the actual humidity. Apparently this is pretty common. The good news is, they are only about $4 each, so I'll order some spares this time :) I wanted to create this post so I could share the link to some information on how you might fix it. For $4 I'll skip the fixing and just replace them as needed...


Tuesday August 27, 2019
Water Sensor Washout!
Some time ago I picked up 10 of these Water Sensors from an online retailer in China.


Tuesday August 27, 2018
WiFi Repeater repeater repeat...
I had this tiny little wireless router I picked up a long time ago, hopefully the link still works when you read this, the product has been discontinued for some time I think. I've played with it in the past, but never had much luck with it, until tonight! One of the biggest problems I have is the WiFi connection in the grow room. While it is the bedroom closest to our internet router, I usually got between 50%-60% signal strength, and sometimes couldn't connect at all. I'm not happy with their service but we are locked in for another 2 years :(


Tuesday, August 27, 2019
The shopping list!
While I'm planning on spending a little more money this month (very little ) I need a lot of things, some are cheap, some are not. I originally planned on buying all the pumps, hose, relays, etc I needed to get the Automatic Watering system up and running, but I can';t see spending all my money on that, when I need not only the usual replacement of consumables, but things like a new magnifying lens/light, a new cordless drill, a more comfortable chair, a whiteboard, plus my normal supplies for the grow room, a bale of promix, some nutrients, peat pellets...



Wednesday, August 28, 2019
Flower Tent Rewiring Complete!
I finally got around to upgrading the wiring for the sensors in the flowering tent. Now that Module 2 has the new connector board in it, the wiring hangs off the back so much better. There are 16 analog and two digital ports configured, and they are all used.
 

Latitude17

Well-Known Member
Sorry I haven't commented much, but following your activity. Your variable getting appended may be just be a good old-fashioned memory leak.

The mySqlConnectionClose() simply did a mySqlConnection.close(); but it didn't help, just slowed things down, so now the method is empty...
Did you put a mySqlConnection.close() back into your code at least?

As far as home-etching, I have yet to see a hobby version worth anything that's "affordable". If you find out, I'll be excited!
 

odam2k

Well-Known Member
Sorry I haven't commented much, but following your activity. Your variable getting appended may be just be a good old-fashioned memory leak.
Well it's good to hear from you :)

It's strange that this is the only software that exhibits this behavior, and it's almost exactly the same code as the original sensor module with a few changes, but the infrastructure hasn't changed. It's just like when sprintf's were writing more bytes that the variables could hold. (I only use snprintf now)

Did you put a mySqlConnection.close() back into your code at least?
Yes, I had no choice... I spent two days chasing this, but such a simple fix, I had to take it for now... The difference in speed really isn't too bad, and it really doesn't matter if it takes 1 minute or 5 minutes to scan all the sensors and save the data, since I only scan every 15 minutes.

As far as home-etching, I have yet to see a hobby version worth anything that's "affordable". If you find out, I'll be excited!
Yeah, it's not gonna happen this time around... my shopping list has grown, but my budget hasn't :( I've learned not to scrimp just because I can't afford it now, I'd rather wait and spend the money to get a quality item that's going to last, and work properly...
 

odam2k

Well-Known Member
Recent blog posts, click the title to read the rest of the post...


Wednesday, August 28, 2019
Water Pump Switching with Transistors?
I'm doing an experiment, or proof of concept, to see if I can control the pumps for the Automatic Watering System using transistors to switch the power on and off. I'm using 2N222A NPN transistors and will control them using digital pins from the arduino. In order to do that, I need to put a 1K Ohm resistor between the pin and the BASE leg of the transistor, and I'm switching the ground line so that the sensors get the full 5v and avoid the voltage drop caused by the transistor itself.



Thursday, August 29, 2019
Windows Re-Install
I've been using Microsoft Windows since at least Windows 3.1, likely earlier, and before that, DOS. One habit I got into was to re-install windows once a year as it always got slower and slower, and a re-install would bring life back to an old machine. The laptop I'm using as my main development machine is a nice fast one, and I've had it for quite a few years, so there's lots of junk installed on it, and I've never wiped it and started over, I've always been afraid I'd never get everything configured and working again, such as the ability to program all the little gadgets I've collected and used over the years. Some of them were a pain to get working... It's been getting slower and slower, and it was really causing me a lot of wasted time... 30 - 40 seconds just to rename a folder... 15 - 20 seconds to enter a folder and display the files...


Friday, August 30, 2019
Let there be light!
If you want to turn HID lights on and off, a cheap little 5v relay isn't going to cut it, and I know this from experience. When I tried it before, within 2 days, the relay had fused closed, and the light stayed on. After doing some research, I found out I needed something called a contactor, basically a heavy duty relay. So off I went to Home Depot and a few other places, but nobody knew what I was talking about :( While I was at the local grow shop picking up some ProMix, I thought why not ask if he knew what I was looking for. Turns out they had ONE, although it took him a bit to find it.
 

Latitude17

Well-Known Member
Just noticed this...

mySqlConnectionClose();
if (connectedToSQL())
{
...
}
but later on you do the mySqlConnectionClose(); again towards the end of the routine. How does your .execute() method even work correctly on a closed connection?
 

odam2k

Well-Known Member
Just noticed this...



but later on you do the mySqlConnectionClose(); again towards the end of the routine. How does your .execute() method even work correctly on a closed connection?
the call to connectedToSql() opens the connection to the database if it is not already connected.

The line you highlighted in red, was removed. I suspect it was there during my testing just to ensure the database connection was closed before .making the call to load the sensor list, since that's where the issue was popping up. Since I've added a close after every call to the database now, that line was redundant, and I removed it,

I've got a new problem...

I ended up re-installing Windows, and now everything I compile for the ESP just throws it into an endless loop crashing and rebooting :( I have a full backup, I could restore my environment, but I'm not in a rush, everything is running well, so I don't need to recompile anything for the live system. I'll figure it out, then I can get back to the Switch Module and my new contactor relay for the lights :)
 
Top Bottom