AWSIOT
Raspberry Pi, Node.js and Python scripts for AWS IoT, used in Temasek Polytechnic Smart IoT Applications course
Install / Use
/learn @lupyuen/AWSIOTREADME
AWS IoT with Raspberry Pi, Node.js and Python
Raspberry Pi, Node.js and Python scripts for AWS IoT, used in Temasek Polytechnic Smart IoT Applications course. See also:
- https://github.com/lupyuen/RaspberryPiImage
- https://www.facebook.com/photo.php?fbid=10203864039081512&set=a.1222080012259.25950.1836747147&type=3&theater
Preparing the SD Card for Raspberry Pi 2 and 3:
-
Download full version of Raspbian Jessie: https://www.raspberrypi.org/downloads/raspbian/
-
Download full version of Noobs: https://www.raspberrypi.org/downloads/noobs/
-
Burn the Rasbian image to SD card: https://www.raspberrypi.org/documentation/installation/installing-images/README.md
-
Boot and update the OS
sudo apt-get update sudo apt-get upgrade
When prompted to replace lightdm.conf, respond 'Y'
0. Attach the GrovePi+ Shield and sensors: http://www.seeedstudio.com/depot/GrovePi-Starter-Kit-for-Raspberry-Pi-ABB23-CE-certified-p-2572.html?cPath=122_154_151
0. Connect a USB keyboard, mouse and HDMI monitor. Boot and connect to wifi.
0. Alternatively, boot with console cable, open a serial port connection from your Mac (or PC):
screen /dev/tty.usbserial 115200
And connect to wifi manually:
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
Add:
network={ ssid="YOUR_SSID" psk="YOUR_PASSWORD" key_mgmt=WPA-PSK }
Restart the network:
sudo ifdown --exclude=lo -a && sudo ifup --exclude=lo -a sudo dhclient -r sudo dhclient
0. Click Menu -> Preferences -> Raspberry Pi Configuration. Click Interfaces. Enable SSH, SPI, I2C and Serial. Set time zone to GMT+8. Set keyboard to US. Set locale to English US UTF-8 (en_US.UTF-8). From the advanced menu, change the memory split for the GPU to “16”.
Reboot.
0. Install latest "Latest Features" Node.js from https://nodejs.org/en/download/ (ARMv7)
wget https://nodejs.org/dist/v6.9.0/node-v6.9.0-linux-armv7l.tar.xz tar -xvf node-v6.9.0-linux-armv7l.tar.xz sudo cp -r node-v6.9.0-linux-armv7l /opt sudo mv /usr/bin/nodejs /usr/bin/nodejs.v0.10.29 sudo rm /usr/bin/node sudo ln -s /opt/node-v6.9.0-linux-armv7l/bin/node /usr/bin/node sudo ln -s /opt/node-v6.9.0-linux-armv7l/bin/node /usr/bin/nodejs sudo ln -s /opt/node-v6.9.0-linux-armv7l/lib/node_modules/npm/bin/npm-cli.js /usr/bin/npm
0. Download the GrovePi+ software:
git clone https://github.com/DexterInd/GrovePi.git
0. Run:
cd ~/GrovePi/Script chmod +x grovepi_python3_install.sh sudo ./grovepi_python3_install.sh
Reboot. (Note: Don't use install.sh because it caused my Raspberry Pi 3 to boot with a black screen.)
0. Connect the Grove buzzer to port D8. Test by running:
cd ~/GrovePi/Software/Python python3 grove_buzzer.py
0. Check that Python supports TLS. Otherwise MQTT requests to AWS IoT will fail.
python3 import ssl ssl.OPENSSL_VERSION
Ensure that OpenSSL version >=1.0.1
0. Tell Node.js to use python2 instead of python3 for building NPM packages
sudo npm config -g set python /usr/bin/python2.7
0. Install common tools
sudo apt install htop sudo apt install telnet sudo apt install cmake sudo apt install zsh curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
0. Set the system default to python3.4 instead of python2.x. Python 2.x does not support TLS.
nano ~/.bash_aliases
Change to
Some more alias to avoid making mistakes:
alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'
For TP IoT: Alias python to python3 because python2 doesn't support TLS needed for AWS IoT.
alias python=python3
Do the same for `nano ~/.zshenv`
0. Install paho, the MQTT library for Python
sudo pip3 install paho-mqtt
0. Remove unnecessary packages so we can clone the NOOBS image easily:
rm -rf ~/Documents/* rm -rf ~/python_games/ rm -rf ~/GrovePi/Firmware rm -rf ~/GrovePi/Hardware rm -rf ~/GrovePi/Software/CSharp rm -rf ~/GrovePi/Software/Scratch
0. Copy /home/pi/TP-IoT from
https://github.com/lupyuen/RaspberryPiImage to /home/pi/TP-IoT on the Raspberry Pi
0. Run the sample script to send data to AWS IoT MQTT
cd /home/pi/TP-IoT python send_sensor_data.py
You should see
pi@raspberrypi:~/TP-IoT $ python send_sensor_data.py Connecting to AWS IoT... Log: Received CONNACK (0, 0) Connected to AWS IoT Subscribing to MQTT topic $aws/things/g88_pi/shadow/update/accepted Log: Received SUBACK Sending sensor data to AWS IoT... { "state": { "reported": { "sound_level": 320, "light_level": 410, "temperature": NaN, "humidity": 0.0, "timestamp": "2016-04-17T14:55:17.812605" } } } Log: Sending PUBLISH (dFalse, q0, r0, m2, '$aws/things/g88_pi/shadow/update', ... (145 bytes) Sent to AWS IoT
0. Install pigpio, which is needed by the Python script for reading data from the DHT22 temperature+humidity sensor directly without using resistors: http://abyz.co.uk/rpi/pigpio/examples.html
Instructions from http://abyz.co.uk/rpi/pigpio/download.html:
cd /tmp rm pigpio.zip sudo rm -rf PIGPIO wget abyz.co.uk/rpi/pigpio/pigpio.zip unzip pigpio.zip cd PIGPIO make -j4 sudo make install
0. Install GrovePi for Node.js
sudo npm install -g node-grovepi
0. Assign hostname: https://github.com/adafruit/Adafruit-Pi-Finder#adafruit-raspberry-pi-finder
sudo apt-get install avahi-daemon sudo apt-get install netatalk
Assign local domain e.g. g88pi.local where g88 is the group name: http://www.howtogeek.com/167190/how-and-why-to-assign-the-.local-domain-to-your-raspberry-pi/
0. Change hostname to g88pi where g88 is the group name: http://www.howtogeek.com/167195/how-to-change-your-raspberry-pi-or-other-linux-devices-hostname/
sudo vi /etc/hosts sudo vi /etc/hostname sudo /etc/init.d/hostname.sh sudo reboot now
Change raspberrypi to g88pi where g88 is the group name.
0. Share Pi filesystem: http://raspberrywebserver.com/serveradmin/share-your-raspberry-pis-files-and-folders-across-a-network.html
sudo apt-get install samba samba-common-bin sudo vi /etc/samba/smb.conf
Change to
workgroup = WORKGROUP wins support = yes
Must use WORKGROUP not TP-IOT!
Change
[homes] comment = Home Directories browseable = no
to
[homes] ; comment = Home Directories ; browseable = no
Add:
[pihome] comment= Pi Home path=/home/pi browseable=Yes writeable=Yes valid users = pi only guest=no create mask=0777 directory mask=0777 public=no read only = no force user = root
Set the SMB password:
sudo smbpasswd -a pi raspberry sudo service smbd restart
Must use "force user = root".
Must use “valid users = pi".
0. Allow users to set WiFi password from Windows by running /boot/set_wifi_password_from_windows. We should check at startup whether there is a pending update to the WiFi config.
crontab -e
Add these lines:
At every reboot, check whether there are pending updates to the wifi config set by set_wifi_password.
@reboot /home/pi/WiFi/check_wifi_updates.sh
0. Copy the following files from https://github.com/lupyuen/RaspberryPiImage/blob/master/boot/ to /boot. These programs allow the user to set the Raspberry Pi wifi credentials by inserting the SD card into Windows, Mac and Linux computers.
set_wifi_password_from_mac set_wifi_password_from_pi.py set_wifi_password_from_windows.exe
0. set_wifi_password_from_windows.exe was created by running the following command in Windows:
pyinstaller --onefile set_wifi_password_from_pi.py
set_wifi_password_from_mac was created by running the above command in Mac OS.
## Install Ajenti Web Console
0. Install Ajenti: http://support.ajenti.org/topics/1121-installing-on-ubuntu/
sudo bash wget -O- https://raw.github.com/ajenti/ajenti/1.x/scripts/install-ubuntu.sh | sudo sh exit sudo service ajenti restart
0. Update Ajenti config to start at port 80 instead of 8080. Edit /etc/ajenti/config.json. Change
"port": 8000
to
"port": 80
Change
"ssl": { "enable": true,
to
"ssl": { "enable": false,
0. Browse to
http://raspberrypi/.
Login in as root, password admin
Change the user authentication to sync with local users:
Configure -> User -> Authentication -> Sync with OS Users
Ensure pi has all permissions.
Restart Ajenti, log in as pi, password raspberry.
0. Copy `index.html, auth.html` from
https://github.com/lupyuen/RaspberryPiImage/tree/master/usr/share/pyshared/ajenti/plugins/main/content/static
to
`/usr/share/pyshared/ajenti/plugins/main/content/static` and
`/usr/lib/pymodules/python2.7/ajenti/plugins/main/content/static`
This enables Font Awesome to support icons in the text widget, and hides the SSL warning messages. Also it allows launching of tty.js as our web terminal.
0. Copy `main.py` from
https://github.com/lupyuen/RaspberryPiImage/tree/master/usr/share/pyshared/ajenti/plugins/main
to
`/usr/share/pyshared/ajenti/plugins/main` and
`/usr/lib/pymodules/python2.7/ajenti/plugins/main`
The allows launching of tty.js as our web terminal by passing the authentication token from Ajenti to tty.js.
0. Add text widget for tty.js web terminal:
<b>Welcome to the Ajenti Web Console</b><br> For monitoring and controlling your Raspberry Pi<br> <a target='_blank' href='/terminal.html'><span class="fa-stack fa-lg"><i class="fa fa-square fa-stack-2x"></i><i class="fa fa-terminal fa-stack-1x fa-inverse"></i></span></a> <a target='_blank' href='/terminal.html'>Open Raspberry Pi Web Terminal</a>
0. Add text w
