iot aquarium 2

27
Automating a fishtank with python and IoT sensors Version 2.0 Ben Chodroff, CTO – CloudOne

Upload: benjamin-chodroff

Post on 08-Feb-2017

1.721 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: IoT Aquarium 2

Automating a fishtank with python

and IoT sensorsVersion 2.0

Ben Chodroff, CTO – CloudOne

Page 2: IoT Aquarium 2

Why?• I left the water running after a water change and the tank overflowed

• Solutions:• Set a timer while filling my tank, or…• Over engineer a solution

Page 3: IoT Aquarium 2

Over Engineering It• Collect all data points:

• Water pH• Water Temperature• Water level• Filter flow• Light state (on/off)• Topoff state (on/off)• Removal state (on/off)• Photosynthesis rate (IR sensor)• Dosing, water changes, filter changes and other scheduled maintenance events

• Automate maintenance• CO2 solenoid control of pH• Chemical dosing (N, P, K, Fe, Micro)• Water topoff and changes

• Automate alerts• CO2 replacement (5 weeks)• Filter replacement (8 weeks)• Water level low/high/critical• Water flow low/high• Temperature low/high• pH low/high

Page 4: IoT Aquarium 2

Demo

Page 5: IoT Aquarium 2

Platform – Raspberry Pi2• Cheap and well supported ($30) • Provides full Linux environment:

https://www.raspberrypi.org/help/noobs-setup/• Easy to interface to most digital hardware sensors via GPIO pins • Provides python environment (easier than TI LaunchPad/Arduino for

generic tasks)• Camera integration via RaspiCam ($25):

http://www.amazon.com/Raspberry-5MP-Camera-Board-Module/dp/B00E1GGE40• Easy to IoT enable ($10):

http://www.amazon.com/Kootek-Raspberry-Wifi-Dongle-Adapter/dp/B00FWMEFES/ref=sr_1_2?s=pc&ie=UTF8&qid=1439503682&sr=1-2&keywords=raspberry+pi+wifi

Page 6: IoT Aquarium 2

Platform – Raspberry Pi2 Pinout Mapping

Page 7: IoT Aquarium 2

Sensors – 8 Channel relay control• Control up to AC 250V 10A or DC 30V 10A devices• CO2 solenoid (DC 12V)• Dosing peristaltic pumps (DC 12V)

• Amazon for $10: http://www.amazon.com/JBtek-Channel-Relay-Arduino-Raspberry/dp/B00KTELP3I/ref=sr_1_2?ie=UTF8&qid=1439503217• Easy to hook up – connect ground and voltage pins, then connect

GPIO pins to each relay controller. • No circuit knowledge required• Use Python GPIO library to set each pin to high or low

Page 8: IoT Aquarium 2

Sample Code – 8 Channel relay control

English:Import the raspberry pi GPIO (so we can talk with devices)Import the time module (so we can time the commands)Go into GPIO.BCM mode (so we can map pin numbers)

pinList is an array of pins connected to the relaysFor each pin in the list:

Set the pin to “output” digital mode (on or off)Initialize the pin to ”HIGH” (which equals off)

For each pin in the list:Set the pin to LOW (which equals on)Sleep for one secondSet the pin to HIGH (which equals off)

Python:

Page 9: IoT Aquarium 2

Sensors – Water Level• eTape Liquid Water Level Sensor (adafruit.com $60)

• Make sure you buy the one with the jacket so you get a built in voltage divider• The non-jacket version requires a little circuit knowledge and is prone to damage• Red wire -> 3.3v, black wire -> ground, white wire -> voltage correspends to level

• Measure the white wire by connecting to a MCP3008 ”CH0”• Raspberry Pi has no ADC - use a MCP3008-I/P (adafruit.com $4)• Great guide on using MCP3008 with a Raspberry Pi using “Bit banging”:

https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/overview

• Hardest sensor• Raspberry Pi has no analog pin so MCP3008 is required and it has a learning curve• The sensor is difficult to calibrate and may require some software finessing to get reliable accurate

readings• Achieved accuracy to +-0.5cm but took a lot of work• Might be able to use ultrasonic distance sensor? Float switch?

Page 10: IoT Aquarium 2

Sample Code – Water LevelEnglish:Import time and our gpio librariesConnect as BCM pins

Define a function “readadc” where we pass in the analog channel (0-7) what channel to read, the clockpin, mosi pin, miso pin, and cspin:If we are reading a channel below 0 or above 7, bomb outSet our cspin to HIGHSet our clock to lowSet our cspin to low*** complicated stuff – manually advancing the clock to read a message:

read message by flicking clock on/offCompare 12 bit response to get the channel we want

Call the readadc to retrieve CH0Use the 10 bits to determine the level (requires calibration)

Page 11: IoT Aquarium 2

Sensors – Water Level• Calculating the water level requires

initial calibration to find the slope of a line between two known points• Numpy can find m (slope) and c (y

intercept) if you don’t remember how

• Avoid false readings by checking a known resistor and using median• Fixed resistor on CH1 and throw out results

off by more than “3” (>99.99%)• Use a median function to avoid false

readings by reading 100 times

Page 12: IoT Aquarium 2

Sensors – pH, Flow meter, and temperature• Atlas Scientific• Crème de la crème – worth every penny but not cheap

• Reliable, high quality, and best of all - prebuilt serial circuits [no circuit skill required]• Use PWR-ISO module ($36) for power isolation: avoid ground loops and electrical noise• Serial Expander ($11) – Control up to 4 separate serial devices even though your

Raspberry Pi only has one serial connection• Digital Temperature ($25) > cheaper analog probes: easier to calibrate, easier

to integrate, high quality• Flow Meter sensor ($38) “just works” with a cheap (adafruit $9) flow meter• pH probe kit ($150) is high quality, easy to calibrate, easy to read

Page 13: IoT Aquarium 2

Sensors – Data sheets• pH Circuit: http://atlas-scientific.com/_files/_datasheets/_circuit/pH_EZO_datasheet.pdf

• Flow Circuit: http://atlas-scientific.com/_files/_datasheets/_circuit/flow_EZO_Datasheet.pdf

• Temperature Circuit: http://atlas-scientific.com/_files/_datasheets/_probe/ENV-TEMP-D.pdf

• Power isolation: http://atlas-scientific.com/_files/_datasheets/_circuit/pwr-iso.pdf

• Serial expander: https://www.atlas-scientific.com/_files/_datasheets/_circuit/serial_port_expander_datasheet.pdf

Page 14: IoT Aquarium 2

Sample Code – Atlas Scientific SensorsEnglish:

We need to create a serial connection We need two pins to tell the serial expander which device to connect to:

Which requires two output pins to flip between 4 devices:(00, 01, 10, and 11)

We open our serial connection at 9600kbps with /dev/ttyAMA0

Use a TextIOWrapper to io buffer until a newline character is returned

Forever:Set the device to 00 (flow sensor)Read in a few lines (to make sure we clear out garbage)Read the line and split the volume and flow to variablesIf we fail, just output “unknown”

Set the device to 01 (pH meter)Read in a few lines, return lineetc…

Python:

Serial Expander Mapping

Page 15: IoT Aquarium 2

Video• Raspivid is incredible – 1080p live video streaming to Youtube• Requires ~3-5Mbps of traffic

• How to make it work• Create a Live stream (free!): https://www.youtube.com/live_dashboard• Create a bash script and enter your server and key • Download optimized ffmpeg for ARM (required!):

https://www.reddit.com/r/raspberry_pi/comments/2ahzp2/raspberry_pi_encoder_live_streaming_to_youtube/• Tweak the raspivid options to fix the ISO, gain, fps, color/white balance correction.• No sound (I tried at one point to tee in some repeating mp3 audio using mpg123 but it caused issues)

Bash Example:

Page 16: IoT Aquarium 2

IoT Dashboard• https://freeboard.io/board/l1XbFY• Freeboard.io (BugLabs)

• Great dashboards out of the box• Free hosting and open source• Possible to integrate with MQTT

• MQTT using IBM IoT Foundation• Free• Extremely fast IOT message transport

• Paho MQTT client (javascript):• https://

rawgit.com/benjaminchodroff/freeboard-mqtt/paho-mqtt-default/ibm.iotfoundation.plugin.js• https://rawgit.com/benjaminchodroff/freeboard-mqtt/paho-mqtt-default/paho.mqtt.plugin.js• Integrates to Freeboard plugin through “Developer Console”

Page 17: IoT Aquarium 2

IoT Messaging – IBM Watson IoT• MQTT based• Lightweight, high performance protocol perfectly suited

for M2M messaging in a variety of IoT scenarios

• Free to set up for limited number of devices• Scales easily in the public cloud• Contact CloudOne if you need private/custom

implementations using BlueMix Local with MessageSight• Easy code examples to integrate variety of

embedded devices in many languages

Page 18: IoT Aquarium 2

Sample Code – IBM Watson IoTEnglish:

Config = my secret passwords

Import the iot foundation

Import a json wrapper for the iot foundation

Let’s send a message with 12345 in it

Try:Set our device connection configurationConnect using that configurationmyData = “hello”: “world”, and “x”: 12345Publish that message with myData

Except/finally: Some quick error publishing and disconnect if it fails

Python:

Page 19: IoT Aquarium 2

IoT Messaging – NodeRED• Visual tool for wiring together hardware devices, APIs and online service• Based on NodeJS

• Requires no javascript programming knowledge to use• Can be ran in IBM BlueMix Public or Local, or on site

• Integrates easily to the IBM IoT Foundation, Twilio/SMS/Email/Twitter• Easily construct lightweight functions to parse messages and do logical evaluations – perfect for

makers• Why?

• Replaces 100’s of lines of python code and make it easy to visualize – great for tinkering• Free BlueMix Public hosting allows me to monitor my aquarium remotely

• If the internet at home goes down, NodeRED knows device presence and can alert me• Sending control messages back to the aquarium (I performed a water change while in China)• Scales horizontally with clustering if your application requires it (I am not allowed to buy more aquariums to

test this)

Page 20: IoT Aquarium 2

NodeRED – Aquarium IOT

Page 21: IoT Aquarium 2

Project BoxBefore After

Page 22: IoT Aquarium 2

Next Steps – AnalyticsAnalytics of photosynthesis (NDVI infragram.org) response to dosing

Page 23: IoT Aquarium 2

Next Steps – You• All the source code/experimentation• https://github.com/benjaminchodroff/aquariumiot

• Think this is fun? • CloudOne is hiring developers, data scientists, and building the future of IoT• Contact me! [email protected]

Page 24: IoT Aquarium 2

Extras

Page 25: IoT Aquarium 2

CO2 Levels based on kH and pH• 6.5 is the sweet

spot at my kH 3 due to RO water• My tap water

comes from Lake Michigan and has a kH of 11+ -- too high for my plants

Page 26: IoT Aquarium 2

Dosing Guide

One day machine imaging will be good enoughBut for now this is a manual inspection with manual adjustment