Saturday, September 25, 2021

Periodic Internet Speed Tracking on MAC

This is a quick little guide to setup a periodic job on MAC for measuring internet speed (Download & Upload) and capture the output as a csv file. The job will execute whenever your machine is on and keep collecting internet speed statistics in a csv file. Please follow below steps for setting this up.

[Note: Tested and working fine on MacBook Pro with macOS Catalina 10.15.7]

Install Brew

Brew is a utility for MAC that allows installing packages. We need this to install the speedtest utility package. To setup brew, fire up a terminal and copy-paste below single line command -

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

It will ask for your permission and password a few times.

Install Speedtest Command Line Utility

Execute below three commands in that order to install speedtest utility

brew tap teamookla/speedtest

brew update

brew install speedtest --force

Create Shell Script 'speedtest.sh'

Open your favorite editor and copy below contents in a new file. Save the file to your Desktop with name 'speedtest.sh'. Make sure NOT to have the .txt extension appended to it automatically by macOS.

#!/bin/bash
/usr/local/bin/speedtest -p no | grep -i 'Download\|Upload' > .tmp-bandwidth.txt 
if [ $? -ne 0 ]; then
  echo `date`,'0','0'
else
  download_speed=`cat .tmp-bandwidth.txt | grep -i 'download' | awk -F" " '{ print $3}'`
  upload_speed=`cat .tmp-bandwidth.txt | grep -i 'upload' | awk -F" " '{ print $3}'`
  echo `date`,$download_speed,$upload_speed
fi

Schedule the Script

We use cron scheduler to run this script periodically. Cron is a simple command line utility present on most *nix systems & macs that allows scheduling tasks.

Set the cron schedule

To schedule script to run every 10 minutes, copy-paste below single-line command. (Entering this command may trigger a pop-up asking permissions, say 'OK' in that case.) 

echo "*/10 * * * * cd ~/Desktop && bash speedtest.sh >> speedtest-results.csv" | crontab -

You can change the job execution interval minutes to a desired frequency by replacing '10' in above command.

Allow cron access disk

To be able to access the shell script and write to the CSV file, cron needs to be given permissions. Simply follow below steps -

  1. Open 'System Preferences' and then Click "Security & Privacy"


  2. Select 'Full Disk Access' from the list and at the bottom, click on 'Click the lock to make changes'. Enter your mac account password when prompted.


  3. Now you need to add 'cron' in the right side list. Click the '+' icon.


  4. This will open up a file browser. Now press "Command + Shift + G" and enter '/usr/sbin/cron' in the textbox and click 'Go', and you are done!


The Output

At this point your cron is set to execute and generate speed test report for you on your desktop as speedtest-results.csv

The generated file has three columns in that order -
Timetamp, Download speed in Mpbs, Upload speed in Mpbs. 
The file contents should look like below in a simple text editor
Sat Sep 25 19:57:45 IST 2021,18.79,7.26
Sat Sep 25 20:07:43 IST 2021,8.00,6.19
Sat Sep 25 20:17:43 IST 2021,11.13,7.01

If internet is down or any error occurs, the bandwidth will be shown as 0,0 for that particular time 

You can also open this file in Microsoft Excel and generate a nice graph out of it!




Sunday, May 2, 2021

DIY: Fully Automated Drip Irrigation For Your Home Plants With Alexa/Google

A Drip Emitter at the end
of Feeder Pipe

It's about a month my wife Shruti and I assembled this automated drip irrigation and now thought of writing this blog as it turned out to be an extremely successful experiment. We hardly have to do any manual intervention now, the system fully autonomous and the plants are flourishing like never before.

Earlier, it was a daily manual boring task to water all 25 plants we have in pots in our home terrace. It was not only time consuming but also each plant got a varied amount of water every time due to the manual process. Mostly, it was excessive water spilling out or leaking through the pot holes at bottom. The water requirements also change based on the season, and in summer we had to water them twice - morning and evening. And it's altogether a different story when you want to travel away from home for some days.

Then we decided to put a full stop to this manual work. Thanks to ready availability of many things online these days, it turned out to be just a matter of ordering few things and assembling them correctly. Of course assembling was tedious hard work, which took about a full day. Then it took about a week or two until we had to tune the flow to each of the plants as per individual water requirement. After that, it's pretty much on its own. 

Why?

  • No more manual watering of plants
  • Saves time
  • Saves lot of water
  • Good for plants as it avoids over or under watering
  • No more worrying about watering your plants when you are out of station for few days
  • Cost effective
  • Can be timer based as well as remotely controlled from your phone from anywhere in the world!

Things you need


  1. A Drip Kit :
    We used this kit on Amazon India. It is good comprehensive kit that has almost everything you need for drip irrigation piping. Good quality components and working as expected. Comes with a quick short user guide. You also have a choice of size (10, 20, 30, 50.. upto 150) based on number of plants you intend to water. Costed us Rs 602 for 30 plants drip kit. 





  2. Adjustable Drip Emitters:
    The drip emitters that come with above kit are 'fixed-flow' ones, meaning water flow cannot be finely controlled. So we additionally ordered these adjustable drip emitters. Cost: Rs 182 for 50 pieces.




    Items below this will be only needed if you want full automation while you don't have a water tap connection nearby. If you have water tap, then an electronic timer valve might be useful


  3. Submersible Pump:
    A submersible pump to force water through the pipes. There are many many alternatives online, we went with this and have no complaints. Cost: Rs 545.







  4. Smart Plug:
    You can go for a smart plug like this one for scheduling pump on-off times or remote operating the pump. Cost Rs 899. Go for a smart plug compatible with Amazon Alexa and Google so that it eases further automation steps.





Total Cost

Summarizing from above list, it costed us total Rs 2228 in all.
  1. Drip Kit (for 30 plants) Rs 602
  2. Adjustable Drip Emitters Rs 182
  3. Submersible Pump Rs 545
  4. Smart Plug Rs 899

Assembling

I won't add too much details here about assembling the irrigation kit components. The user manual that comes with the kit has it. 

We used two parallel
main pipes for two
rows of pots


Just few tips here -

  •  - Do a proper measurement and design of main pipe layout, feeder pipe lengths needed etc before embarking on assembling.
  •  - Inserting the feeder pipe in the main supply pipe and attaching the drip emitter to the feeder pipe are the most physically tiring tasks. It will hurt your fingers and wrists like hell, you have to take intermittent rest.
  •  - Use the PVC glue that comes with the kit at pipe joins. If you still see minor leakages, IGNORE. After some days, water residue will automatically fix those. Really!







Automation

Submersible Pump
in Bucket
Since we did not have a running water tap connection in our terrace, we had to rely on a bucket as a water reservoir. Connect your drip assembly to the pump and submerge it in a bucketful of water. Connect the power supply of the pump to the Smart Plug. Amazon Alexa app can be used to configure and connect the Smart Plug to WiFi and in turn to internet. Follow the Smart Plug's user guide for configuration. 
Bigger the reservoir, more number of days it will last once filled up. Occasionally you can also add some natural fertilizer to the water.




Watering Schedule

Three watering schedules


Steps in a Routine
After much experimentation and tuning we finally settled on a 3-times-per-day watering schedule, each lasting for 30 seconds - 7am, 1pm and 7pm. The schedules can be easily configured in the Amazon Alexa app as Routines

Each routine has three steps - Switch on the plug, wait 30 seconds and then switch off the plug.

While manual watering required a full bucket daily, now a bucket of water lasts for two days in peak summer. May be in monsoon a bucket may last for 3 or 4 days as we will tweak schedule to lower frequency and smaller watering periods.




And here we go ...





We also had few small plants in an isolated window. Here is what we did for it, using mostly throwaway material. Although not fully automated, this makes life easy, and plants are happy too!