Software apps and online services

Hand tools and fabrication machines

Story

In this project, we are going to mix water, fire, light, wind, and beer with some hardware and code and send data to Hologram to notify us if the elements are pleased.

Imagine the following scenario:

You are ready to pop a cold one only to be met with that look that happens when you there is no beer left in the fridge. Somehow managing to avoid dry-heaving you head to the nearest beer store only to be met with a closed sign. And then the light bulb goes on, you are ready to use your inner full metal alchemist.

1. Alchemy behind the project

Full Version

The RGB and ambient light sensor are used to detect the presence of beer. Light is shone on a beer bottle and the light intensity is measured. If the beer bottle is not there the reflection is minimal and the threshold if below the critical. If the space is full of beer the reflection is above the threshold and we are all happy.

This is where the Hologram functionality kicks in. Even if you're out of home and somebody else is imbibing on your ale, the Hologram server can send you an email or SMS so that you are prepared.

2. Hardware

The hardware setup is shown below. The Hologram box emulates the beer bottle.

Beer in the form of a box.

Ambient light sensor.

The main program makes use of the APDS9300 digital ambient light sensor which communicates over I2C. The sensor has two channels and based on the data sampled from two photo diodes one can calculate the amount of ambient light measured in LUX.

An RGB led is used as the active sensor. During normal operation the RGB leds are all on. The light gets reflected from the bottle and is continuously measured by the sensor every second. If the ambient light is below the programmed threshold for more than a minute then the program sends an SMS to my phone.

3. Hologram.io configuration

Open a shell and issue:

sudo hologram connect

Then log in on your account using the username and password and select the plan if you have more than one. Once the modem blue led is blinking you are ready to proceed.

At this point, also make sure to take down your device key. This will be needed to send data or SMS via the Hologram modem.

Device Key on the right corner.

4. Program

The program shines a light on a beer bottle and measures the amount of reflected light. If the measured data is below a threshold (meaning no beer bottle in front of the sensor) an SMS is sent.

First we check the sensor address via the i2cdetect package. Issue:

sudo i2cdetect -y 1

This will give the addresses below. On my setup the ambient light sensor is the one with address 0x29.

The other two are the RTC and EEPROM.

The program initializes the sensor, configures access to the hologram cloud and and shines the LED over the bottles. The while loop then continuously measures the reflected light intensity. Once the reflected light is below the threshold an SMS message is send via the Hologram Nova and the program exits.

To send an SMS we have to import the subprocess module in Python and include this line:

subprocess.call (["hologram","send","--sms","--destination","+1xxxxxxxxx","'Message from Hologram.io : Out of Beer bro!'","--devicekey","mykeybro"])

Check the main program for further details. This basically calls a command line version with admin privileges.

I wanted to simulate the beer bottle with a nice bottle of rum but I settled for the cardboard box of the modem:

Code

Beer Sensor With Hologram Io Nova Modem and SwishPi pHAT shield

Python

Issue : sudo python App.py

#!/usr/bin/sudo /usr/bin/pythonimportmathimporttimeimportsysimportAPDS9300asLuxSensimportRPi.GPIOasGPIOimportsubprocessimportjsonfromtimeimportsleepimporttimeimportdatetimefromHologram.HologramCloudimportHologramCloud## Import Hologram cloud libraryprint"=== Beer Sensor demo test ==="print"********************************"R=11# LED pinG=13B=15""" Setups the pins in BCM mode :param none: :returns none : """defInitLeds():GPIO.setwarnings(True)# suppress GPIO used messageGPIO.setmode(GPIO.BOARD)# use BCM pin numbersGPIO.setup(R,GPIO.OUT)# set LED pin as outputGPIO.setup(G,GPIO.OUT)# set LED pin as outputGPIO.setup(B,GPIO.OUT)# set LED pin as outputGPIO.output(R,GPIO.HIGH)GPIO.output(G,GPIO.HIGH)GPIO.output(B,GPIO.HIGH)defRedLEDon():GPIO.output(R,GPIO.LOW)defRedLEDoff():GPIO.output(R,GPIO.HIGH)defGreenLEDon():GPIO.output(G,GPIO.LOW)defGreenLEDoff():GPIO.output(G,GPIO.HIGH)defBlueLEDon():GPIO.output(B,GPIO.LOW)defBlueLEDoff():GPIO.output(B,GPIO.HIGH)DEVICE_ID=1# send data to Hologram's cloud through WiFiwithopen('../credentials.json')askey_file:devicekey=json.load(key_file)# hologram = HologramCloud(devicekey, enable_inbound=False)hologram=HologramCloud(devicekey,network='cellular',enable_inbound=False)result=hologram.network.connect()ifresult==False:print' Failed to connect to cell network'THRESHOLD=10globalcountTRANSMIT_CYCLE=10defmain(argv):print"=== Beer Sensor First test ==="print"********************************"InitLeds()AmbientLight=LuxSens.APDS9300()#Setup Ambient light sensor id=AmbientLight.chipID()print"Chip ID: 0x%02X. \r\n"%idcount=0while(count<TRANSMIT_CYCLE):time.sleep(3)channel1=AmbientLight.readChannel(1)#Take a reading from channel oneprint"Channel 1 value: %d."%channel1channel2=AmbientLight.readChannel(0)#Take a reading from channel twoprint"Channel 2 value: %d"%channel2Lux=AmbientLight.getLuxLevel(channel1,channel2)print"Lux output: %d."%Luxif(Lux>THRESHOLD):BlueLEDon()RedLEDon()beerCount=0subprocess.call(["hologram","send","--sms","--destination","+1xxxxxxxxxx","'Message from Hologram.io : Out of Beer bro!'","--devicekey","xxxxxxx"])# message = json.dumps({'beer': beerCount,"source_device": DEVICE_ID, "datetime": time.time()}) # response_code = hologram.sendMessage(message)# print hologram.getResultString(response_code) # Prints 'Message sent successfully'.print("out of beer message sent\r\n")hologram.network.disconnect()count=TRANSMIT_CYCLE#get out of hereelse:BlueLEDoff()GreenLEDon()RedLEDon()beerCount=1if__name__=='__main__':try:main(sys.argv[1:])time.sleep(2)exceptKeyboardInterrupt:print("")# /* Hologram IO Nova Contest Beer Sensor# * Copyright (C) 2017 (almost 2018) by Dimiter Kendri# *# *# * This Library is free software: you can redistribute it and/or modify# * it under the terms of the GNU General Public License as published by# * the Free Software Foundation, either version 3 of the License, or# * (at your option) any later version.# *# * This Library is distributed in the hope that it will be useful,# * but WITHOUT ANY WARRANTY; without even the implied warranty of# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# * GNU General Public License for more details.# *# * You should have received a copy of the GNU General Public License# * along with the code. If not, see# * <http://www.gnu.org/licenses/>.# */