The Internet of Things (IoT) is fundamentally changing how we interact with the digital world. In this session we’ll explore the implementation of real examples which bridge the gap between the physical and digital world using PHP: asking Alexa for information within a PHP application; displaying API data on an Arduino-powered display; using PHP to control LEDs on a Raspberry Pi to monitor application uptime; and connecting IR sensors to Slack to see whether a conference room is in use.
2. Colin O’Dell
• Lead Web Developer at Unleashed Technologies
• PHP League Leadership Team
• Baltimore PHP Co-Organizer
• Arduino & Raspberry Pi Enthusiast
@colinodell
3. Goals
• Introduce the basics of building custom IoT devices
• Understand where web technologies fit in
• Four real-world example projects
@colinodell
4. Internet of Things
The Internet of Things is the internetworking of physical devices,
vehicles, buildings and other items—embedded with electronics,
software, sensors, actuators, and network connectivity that enable
these objects to collect and exchange data.
- https://en.wikipedia.org/wiki/Internet_of_things
@colinodell
5. Internet of Things
The Internet of Things is the internetworking of physical devices,
vehicles, buildings and other items—embedded with electronics,
software, sensors, actuators, and network connectivity that enable
these objects to collect and exchange data.
- https://en.wikipedia.org/wiki/Internet_of_things
@colinodell
6. Connectivity
• Ethernet / WiFi
• NFC / RFID
• Bluetooth LE
• Zigbee / Z-Wave
• Cellular (LTE / CDMA / GSM)
• …and many others
@colinodell
7. 0
10
20
30
40
50
60
2003 2010 2015 2020
(Billions) Population vs Connected Devices
People Connected Devices
@colinodellSource: https://www.cisco.com/c/dam/en_us/about/ac79/docs/innov/IoT_IBSG_0411FINAL.pdf
16. Why Raspberry Pi?
• Internet connectivity
Handles HTTPs out-of-the-box
• Runs Linux (Raspian – a derivative of Debian)
PHP easily installed
• Has GPIO pins
• Raspberry Pi Zero only $5
• Why not?
@colinodell
17. Hardware
@colinodell
• Raspberry Pi (any variant)
• Micro USB cable (power)
• USB WiFi dongle
• Micro SD card
• RGB LED
• Two resistors
• Wires
23. Recap
• PHP on a Raspberry Pi
• PHP checks if site is up
• PiPHP/GPIO library used to control output pins (LEDs)
Other uses for output pins:
• Control motors/servos
• Control relays (turn higher-voltage things on/off)
• Drive digital displays (LCD screens, LED number displays, etc.)
@colinodell
25. Why Raspberry Pi?
• Runs Linux (Raspian – a derivative of Debian)
• Has GPIO pins
• PHP easily installed
• Raspberry Pi Zero only $5
• Handles HTTPs out-of-the-box
• Why not?
@colinodell
TL;DR: Same reasons as before!
32. @colinodell
<?php
// Configuration:
const GPIO_PIN = 17;
const DECLARE_ROOM_EMPTY_AFTER = 60*5; // 5 minutes
const SLACK_INCOMING_WEBHOOK_URL = 'https://hooks.slack.com/services/xxxx/xxxx/xxxx';
// End configuration
require_once 'vendor/autoload.php';
use ColinODellPHPIoTExamplesPHPPIRSensorRoom;
use PiPHPGPIOGPIO;
use PiPHPGPIOPinInputPinInterface;
// HTTP client for communicating with Slack
$http = new GuzzleHttpClient();
// Instantiate a new room object to keep track of its state
$room = new Room();
$room->setRoomEmptyTimeout(DECLARE_ROOM_EMPTY_AFTER);
$room->setOnRoomChangeCallback(function($isOccupied) use ($http) {
$message = 'The conference room is now ' . ($isOccupied ? 'occupied.' : 'vacant.');
$http->postAsync(SLACK_INCOMING_WEBHOOK_URL, [
34. Recap
• PHP on a Raspberry Pi
• PiPHP/GPIO library monitors GPIO pin for signal changes
• PHP tracks duration between rising edges (low-to-high signal changes)
• Notifications pushed to Slack via webhook
Other uses for input pins:
• Switches & buttons
• Sensors (temperature, humidity, motion, accelerometer, GPS)
• Receiving data from other devices
@colinodell
35. Alexa Custom Skill
Use PHP + Laravel to handle request for ZendCon session information
@colinodell
39. Invoking Custom Skills
• Ask [skill name] [action] Ask ZendCon which sessions are next
• Tell [skill name] [action] Tell ZendCon this talk is great
• [action] using [skill name] Rate this talk 5 stars using ZendCon
@colinodell
40. Actions
• What sessions are next?
• What session is next in {room}?
• What sessions are at {time} in {room}?
• Who is speaking in {room} {day} at {time}?
@colinodell
41. Defining Actions
GetSessions what sessions are next
GetSessions what talks are next
GetSessions who is talking next
GetSessions who is speaking next
GetSessions which sessions are next
GetSessions which talks are next
GetSessions what session is next in {room}
GetSessions what talk is next in {room}
GetSessions who is talking next in {room}
GetSessions who is speaking next in {room}
GetSessions which session is next in {room}
GetSessions which talk is next in {room}
GetSessions what sessions are at {time}
GetSessions what talks are at {time}
GetSessions who is talking at {time}
GetSessions who is speaking at {time}
GetSessions which sessions are at {time}
GetSessions which talks are at {time}
GetSessions what sessions are at {time} {day}
GetSessions what talks are at {time} {day}
GetSessions who is talking at {time} {day}
GetSessions who is speaking at {time} {day}
GetSessions which sessions are at {time} {day}
GetSessions which talks are at {time} {day}
GetSessions what session is in {room} at {time}
GetSessions what talk is in {room} at {time}
GetSessions who is talking in {room} at {time}
GetSessions who is speaking in {room} at {time}
GetSessions which session is in {room} at {time}
GetSessions which talk is in {room} at {time}
GetSessions what session is in {room} at {time} {day}
GetSessions what talk is in {room} at {time} {day}
GetSessions who is talking in {room} at {time} {day}
GetSessions who is speaking in {room} at {time} {day}
GetSessions which session is in {room} at {time} {day}
GetSessions which talk is in {room} at {time} {day}
GetSessions what session is {day} in {room} at {time}
GetSessions what talk is {day} in {room} at {time}
GetSessions who is talking {day} in {room} at {time}
GetSessions who is speaking {day} in {room} at {time}
GetSessions which session {day} is in {room} at {time}
GetSessions which talk {day} is in {room} at {time}
@colinodell
42. Intent Schema
@colinodell
{
"intents": [
{
"intent": "GetSessions",
"slots": [
{
"name": "day",
"type": "AMAZON.DATE"
},
{
"name": "time",
"type": "AMAZON.TIME"
},
{
"name": "room",
"type": "ROOM"
}
]
},
{
"intent": "AMAZON.HelpIntent"
}
]
}
The Joint
Artist C
Artist D
Artist E
Artist F
Artist G
Artist H
43. @colinodell
$ composer require develpr/alexa-app
AlexaRoute::intent('/alexa-end-point', 'GetDeveloperJoke', function(){
Alexa::say("A SQL query goes into a bar, walks up to two tables and asks, "Can I join you?"");
});
49. Particle Photon
@colinodell
• WiFi built in
• Small footprint
• Can be powered via MicroUSB
• Enough digital GPIO pins
• Programmable via web-based IDE
• OTA flashing
• Built-in webhook support
56. Problem!
• JSON document is 32.5 KB
• Photon webhook responses are 512-byte chunks spaced 250ms apart
@colinodell
57. Problem!
• JSON document is 32.5 KB
• Photon webhook responses are 512-byte chunks spaced 250ms apart
@colinodell
Solution!
• Use Yahoo YQL to reduce response size
58. Problem!
• JSON document is 32.5 KB
• Photon webhook responses are 512-byte chunks spaced 250ms apart
@colinodell
Solution!
• Use Yahoo YQL to reduce response size
• Use PHP to parse JSON, return just one number
64. Recap
• Particle Photon + 8 digit LED display
• Particle webhook fetches data from packagist-counter.php
• packagist-counter.php fetches from packagist.org; trims out the fat
@colinodell
66. Summary
• IoT is everywhere!
• PHP can bridge the gap
• Run PHP:
On the device (RasPi examples)
In “the cloud” (Alexa; Particle Photon)
Or both!
• Anyone can build web-connected devices
@colinodell
Please leave feedback on Joind.in!
67. Questions?
Project Source Code / Documentation:
https://github.com/colinodell/php-iot-examples
Even More Projects:
https://www.hackster.io
Building Robots with PHP:
Christopher Pitt @ The Joint (next session)
@colinodell
Please leave feedback on Joind.in!