19

Raising Baby Chicks with Javascript

 5 years ago
source link: https://www.tuicool.com/articles/hit/3MRZfem
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Raising Baby Chicks with JavaScript

f2yIni7.jpg!web

A few years back I decided to start raising chickens. By that time I had beetles, worms, snakes, and frogs; you name it! But raising these cute yellow nuggets is a different beast altogether.

At first, it sounds fun. All you do is buy a dozen online and follow the instructions. But what I didn’t know is that your entire experience is driven by the survival rate of your chicks.

So be sure you are ready to commit to their well being. It’s not easy being a chicken master! Many of them pass into the light right in front of you.

In fact, mastering the light is the most important instrument in raising your chicks. The lamp provides constant heat, regulating their body temperature. It must stay consistent at 95–100 degrees, especially in the first two weeks.

I’ve raised a dozen with a regular thermometer and a heat lamp. And noticed that sometimes chicks would get cold and cuddle into a large ball, only to squish a few in the process.

aaMfYv3.jpg!web

But then when the heat got high, some of the chicks would sweat and often fall ill.

This time, I knew controlling the heat would help the chicks through the initial two weeks. I had to find a way to control the light and measure the temperature.

The easiest path to hardware hacking is by using Wireless Sensor Tags. Which come with JavaScript in the cloud capabilities right out of the box.

All you need is the Wireless Sensor Tag (with Temperature) and device to control the light.

https://store.wirelesstag.net/products/wireless-tag

7RV7jeF.png!web

Its true, they are very small and come with 700 ft range and 1-year battery life. I have been using them for years.

Wireless Sensor Tagsintegrates with Belkin Wemo Switch , which controls an appliance power.

yeUFNfY.png!web

You can buy the Belkin Wemo Switch here or on e-bay

https://www.belkin.com/us/p/P-F7C027/

After you have both, all you do is plug the Tag Manager into your router and set up the Belkin Wemo Switch , it just works.

IvIbIfu.jpg!web

This is one of the easiest setups on the market.

Afterward, go to http://my.wirelesstag.net/ and create an account for your tag manager. There you’ll be able to associate it and your Wemo Switch . Just click the + Associate button on the top left corner.

uYjYJ3b.png!web

After setting it all up, you should have both light and sensor in Wireless Tag List :

baI7jyf.png!web
ryYbAnm.png!web

1) Now, click on the Kumo Apps to set up your app

2) In the top right corner select Install

qiEjIjJ.png!web

3) Click on this icon

UB36nyF.png!web

4) In the search start typing “ When temperature is too high, turn on Wemo, when temperature is back to normal, turn off Wemo (with delay) ” and select it. We are going to use this application as a starter.

mqUVNfZ.png!web

5) In the “ Configure the App ” screen select your sensor, switch & pick delay of 5. Click Apply

beIzuqa.png!web

6) You should have an app setup in the Kumo Apps list

biueE32.png!web

7) Expand it and click on View Code

BvQVBff.png!web

8) Take a minute to study the code and you’ll find that the program is rather simple. It consists of two main functions. The wemoAction & sensor.temperatureCross

A detailed description of all the KumoApp API calls:

http://wirelesstag.net/kumoapp/17/tags-kumosensors-kumostats

sensor.temperatureCross — gets executed when the tag has too hot/too cold monitoring enabled. This boundary is configured as part of the sensor configuration.

This routine then calls wemoAction to turn on or off the Wemo Switch .

iAvInun.png!web
viU3IzN.png!web

This should have been good enough, but I found that KumoApp would not work as intended when Wemo Switch disconnected from the internet. This happens with WIFI enabled devices.

The temperatureCross event would execute but my Wemo Switch can’t receive the event. I decided to re-write the program with a more pro-active approach.

Instead of having to wait for a temperatureCross callback from the sensor, I need to have the program ping the sensor to see if the heat lamp needs to be on.

J7baamR.png!web

9) Go ahead and copy paste the following code into the application, and click Create / Update App . I will explain how it works below:

10) Click the Start Button!

Nb2qiqY.png!web

Let’s go over the code.

In the beginning, I added initial properties used by the application.

3yaeMzN.png!web
  • updateFrequencyMin is a very important parameter. It indicates how often the program will ask the sensor for the latest temperature value. The less often you query the sensor, the longer its battery life. In fact, I’ve drained the battery by 50% while making the program. Since I ended up pinging the sensor every few seconds during testing.
  • tempRange is the temperature range. The chicks should have 95–100 degrees for the first two weeks. Afterward, reduce by 5 degrees each week until chicks are a month old. I set mine from 95–98.5.

The KumoApp.Log outputs only the latest message passed to it. So I added console.log & console.flush methods to make debugging easier. Every console.log message gets appended to console.str , which gets passed to KumoApp.Log when console.flush executes at the end of the program.

This simplified my debugging.

uER3Mn6.png!web

I changed the wemoAction method to make sure the Wemo Switch is connected. The program pings the wemo switch for its latest properties with wemo.ReloadProperties()

AZRNrij.png!web

Then added application initialization routine initApp() , where I update the latest sensor temperature. It figures out if the heat lamp needs to be on or off. This only gets executed once when the program starts.

Fr2ymy2.png!web

Removed the temperatureCross event from the sensor logic. Modified the logic to use KumoApp.setInterval , which executes every so often. We are executing this logic every 5 seconds.

Every time the setInterval executes, it reloads sensor properties, as indicated in updateFrequencyMin . It then uses the tempRange.higher & tempRange.lower heat range values to figure out whether to turn the heat lamp off or on.

Conclusion

After the first night of testing, I conclude my program a success.

Here is the temperature without the program where I turn off and turn on the heat lamp. The temperature fluctuates between 79–110 degrees.

Jv6zieU.png!web

And here is the temperature with the program, under control, fluctuating between 93–99 degrees.

qaAv63V.png!web

Update: After two weeks of testing, the survival rate of the chicks increased by over 50%. I had 11 chicks die in my prior batch vs 5 chicks this time around.

Check out my other articles

ZNVvmyF.png!web

This story is published inThe Startup, Medium’s largest entrepreneurship publication followed by + 371,663 people.

Subscribe to receive our top stories here .

EjAzqun.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK