The idea of making an automation system comes from our village house, where we have chickens, a dog and plants. My father made an electronic system to automate daily tasks such as watering the plants, feeding the dog and chickens, opening the door for chickens, etc. He used timing relays for that system and it works well most of the time. However, the electricity goes off frequently in our village house which causes problems with my father's automation system. If the electricity goes off between opening and closing time of an relay, it will not arrange relay's state when the electricity comes back. That is a huge problem when we deal with plants, and mostly animals. Moreover, the cost of timing relays are really high comparing with Netduino + Relay, and usage is not easy as you think. Thus, I've decided to solve this problem by using a Netduino.
For solving this problem, I applied Idea Round of Netduino Contest and my idea was accepted. I bought a 16 Channel 5V relay module for multiple relay control. I needed to know the time. Thus, I used an external RTC module DS3231 to get not only real time but also the day of week which encouraged me to write a program works weekly. I wrote a loop function that determines the relays' states according to the time. This function loops every second and checks every relays' situation then compares the timings of relays from a SD card (or from directly code --I explained in the code) and opens/closes relays depending on time. However, I wanted to use sensors independently from time, to complete urgent needs. I managed to connect three sensors to exemplify sensors' working processes with my system.
Development process was a bit hard for me, due to lack of internet connection, time and stock. I was at our village house and I had to use my old sensors, I couldn't have enough time to buy new ones because shipment to the villages are restricted. Thus, I completed it what I have. I'm familiar with Arduino, that's why pin diagram or circuits of sample projects are easy for me. I have struggled mostly integrating DS3231 and using sensors(including LDR, Water Level, and Motion Detection) with it. Although, there are tutorials for that, most tutorials are a bit old. However, Netduino Foundation is great and mostly up-to date!
I have used Netduino Foundation DS32xx integration codes to fetch time data from DS3231 RTC module. For sensors, I tried using SecretLabs.NETMF.Hardware.AnalogInput reference, but VS 2015 showed me errors about checking your device again and again. Without this reference, all of the project ran like a charm. That's why, I checked AnalogInput's library to understand how can I get data from an analog input. Afterwards, I got values from Microsoft.SPOT.Hardware like below:
Microsoft.SPOT.Hardware.AnalogInput waterLevel = new Microsoft.SPOT.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_0);
Debug.Print("Water Level Sensor: " + (int)(waterLevel.Read() * 100));
I wrote a function to determines relay's situation. Here is the example and meaning of the parameters of my function below:
// Relay no:2 12:10 - 16:27 Only Tuesday
TimeToAct(2, currentHour, currentMinute, DayOfWeekToString(dayOfWeek), 12, 10, 16, 27, DetectDay(3));
/// <summary>
/// Main Alarm System
/// </summary>
/// <param name="relayNo">Relay No</param>
/// <param name="currentHour">current Hour</param>
/// <param name="currentMinute">current Minute</param>
/// <param name="currentDay">current Day String</param>
/// <param name="hourAct">Relay opens: hr</param>
/// <param name="minuteAct">Relay opens: min</param>
/// <param name="hourReAct">Relay closes: hr</param>
/// <param name="minuteReAct">Relay closes: min</param>
/// <param name="daysAct">Relay opens at specific day or days of week</param>
static void TimeToAct(int relayNo, int currentHour, int currentMinute, String currentDay, int hourAct, int minuteAct, int hourReAct, int minuteReAct, String[] daysAct) {
My code ran just fine for one day operations(for instance Relay 6 works only Wednesday). However, I aimed my relays to work multiple days of week. I thought about how to understand how many days are selected for multiple actions, and here is my solution, using prime numbers :)
/// <summary>
/// Detect Day depending on first 7 prime numbers
/// 2 --> Monday
/// 3 --> Tuesday
/// 5 --> Wednesday
/// 7 --> Thursday
/// 11 --> Friday
/// 13 --> Saturday
/// 17 --> Sunday
/// If we want our relay to open only one day, we need to enter days' prime number, for instance 5 for Wednesday.
/// If we want our delay to open multiple days, we need to multiply days' prime numbers.
/// For instance, all the days of week, we need to enter 2*3*5*7*11*13*17 = 510510
/// Monday + Tuesday = 2*3 = 6
/// Wednesday + Saturday + Sunday = 5*13*17 = 1105
/// </summary>
/// <param name="dayNumber">dayNumber</param>
/// <returns>Detect Day String array</returns>
static String[] DetectDay(int dayNumber)
{
String[] days = { "", "", "", "", "", "", "" };
for (int i = 0; i < 7; i++)
{
days[i] = "";
}
if (dayNumber % 2 == 0) { days[0] = "Monday"; }
if (dayNumber % 3 == 0) { days[1] = "Tuesday"; }
if (dayNumber % 5 == 0) { days[2] = "Wednesday"; }
if (dayNumber % 7 == 0) { days[3] = "Thursday"; }
if (dayNumber % 11 == 0) { days[4] = "Friday"; }
if (dayNumber % 13 == 0) { days[5] = "Saturday"; }
if (dayNumber % 17 == 0) { days[6] = "Sunday"; }
Debug.Print("DayNo: " + dayNumber);
Debug.Print("Days: ");
for (int i = 0; i < 7; i++)
{
if (days[i].Length > 5)
{
Debug.Print("-" + days[i]);
}
}
return days;
}
My last challenge was storing data in SD Card. I have many micro SD (SDHC) cards that are bigger than even 8GB. However, Netduino page says you need to use SD card(not SDHC) up to 2GB which prevented me from using my cards. I have tried using with my cards but it did not work. Then I decided to use DS32xx's EEPROM to store all data. I had found a library from Netduino.Foundation but this library caused problems with VS (same error above ) I needed to store the data, so I wrote the codes for SD Card, but I couldn't test with SD card.
Instructions (For making the project)- Go to Netduino web site and download firmware updater for Netduino.
- Update your Netduino with the program.
- Download and install Visual Studio 2015.
- Install the.NET MicroFramework and Netduino SDKs on Windows.
- Connect sensors and relays to the Netduino according to my schematic.
- Download and open my project with VS 2015.
- Before deploying read and apply these official instructions. (Look up for Deploy part)
- Deploy the code!
I'm so happy and proud to complete this project and find solution to our problem! My system can handle many things such as:
- Programming 8 relays according to hour, minute and day of week (now it is limited to 8 but the relay count can increase)
- Relay system works weekly which prevents problems depending on day, month or year.
- Automation system helps farmers to grow better crops and reduce their job.
- As my system doesn't require internet connection, it can work anywhere in the world.
- Relays can be controlled by sensors' values which also assists farmers to do what plant or animal needs in an emergency.
- Using a SD Card is beneficial not only for storing and retrieving the data, but also programming the relays easier.
- Time system and Sensors can work simultaneously. (Sensors have the priority for controlling the relays)
- Farmers can add many sensors for their purposes.
- If electricity cut happens, my system works just fine.
- I can add Internet connection, whether is WiFi or ethernet, to transmit data efficiently and program the relays easier.
- With an active internet connection, I can make an app for monitoring the data, programming relays, determining sensor's value limit... This can simplify the usage of my system.
- I can use Grove sensors to increase the sensor count.
- I can increase the relay count.
- I can add a Bluetooth sensor to communicate better and modify relays' schedules without taking off SD Card. This is of course for offline usage.
I really thank you to Wilderness Labs to give a chance to solve our problem with Netduino, their high-level language supported card.
Comments