Skip to content

Instantly share code, notes, and snippets.

@ianjennings
Last active January 29, 2016 18:25
Show Gist options
  • Save ianjennings/b88d9c64816629fdf54a to your computer and use it in GitHub Desktop.
Save ianjennings/b88d9c64816629fdf54a to your computer and use it in GitHub Desktop.
Home Automation with PubNub
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.js"></script>
<script src="http://cdn.pubnub.com/pubnub.min.js"></script>
<style type="text/css">
* {
font-family: 'Roboto', sans-serif !important;
font-weight: 300;
}
.ui-header {
background-color: #ce1126 !important;
color: #fff !important;
text-shadow: 0px 2px 1px #88000F !important;
border-top: 1px solid #DB5362 !important;
border-bottom: 1px solid #88000F !important;
}
a {
color: #ce1126 !important;
text-decoration: none;
}
.ui-bar-a {
background-color: #f2f2f2 !important;
}
.ui-header h1.ui-title {
font-weight: bold;
margin: 0px !important;
font-size: 24px;
}
h3.ui-bar {
padding-top: 15px;
padding-bottom: 15px;
}
.ui-block-b {
text-align: right;
}
.ui-bar:first-of-type {
margin-top: 0px;
}
.ui-body .ui-grid-a {
border-bottom: 1px solid #efefef;
padding-bottom: 10px;
padding-top: 6px;
}
.ui-body .ui-grid-a:last-child {
border-bottom: none;
}
legend {
padding-top: 19px !important;
}
.ui-block-b {
font-size: 0px;
}
.toggle .ui-block-b button:first-of-type {
border-top-right-radius: 0px;
border-bottom-right-radius: 0px;
margin-right: 0px !important;
border-right: 0px;
}
.toggle .ui-block-b button:last-of-type {
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
margin-lefT: 0px !important;
}
.ui-grid-a>.ui-block-a {
width: 25% !important;
}
.ui-grid-a>.ui-block-b {
width: 75% !important;
}
</style>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>The PubNub IOT House</h1>
</div>
<div data-role="content">
<h3 class="ui-bar ui-bar-a ui-corner-all">Lights</h3>
<div class="ui-body ui-body-a ui-corner-all">
<div class="ui-grid-a toggle" id="lightLeft">
<legend class="ui-block-a">Left:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">On</button>
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="0">Off</button>
</div>
</div>
<div class="ui-grid-a toggle" id="lightRight">
<legend class="ui-block-a">Right:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">On</button>
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="0">Off</button>
</div>
</div>
<div class="ui-grid-a toggle" id="lightRoom">
<legend class="ui-block-a">Kitchen:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">On</button>
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="0">Off</button>
</div>
</div>
<div class="ui-grid-a toggle" id="lightGarage">
<legend class="ui-block-a">Garage:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">On</button>
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="0">Off</button>
</div>
</div>
</div>
<h3 class="ui-bar ui-bar-a ui-corner-all">Doors</h3>
<div class="ui-body ui-body-a ui-corner-all">
<div class="ui-grid-a toggle" id="door">
<legend class="ui-block-a">Front:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">Open</button>
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="0">Close</button>
</div>
</div>
<div class="ui-grid-a toggle" id="garage">
<legend class="ui-block-a">Garage:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">Open</button>
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="0">Close</button>
</div>
</div>
</div>
<h3 class="ui-bar ui-bar-a ui-corner-all">Functions</h3>
<div class="ui-body ui-body-a ui-corner-all">
<div class="ui-grid-a" id="blink">
<legend class="ui-block-a">Blink:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="5">Blink</button>
</div>
</div>
<div class="ui-grid-a" id="demo">
<legend class="ui-block-a">Demo:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">Demo</button>
</div>
</div>
<div class="ui-grid-a toggle" id="async">
<legend class="ui-block-a">Async:</legend>
<div class="ui-block-b">
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="1">On</button>
<button type="button" class="ui-shadow ui-btn ui-corner-all ui-btn-inline" value="0">Off</button>
</div>
</div>
</div>
</div>
<div data-role="footer">
<h4>Powered by <a href="http://pubnub.com">PubNub</a></h4>
</div>
</div>
<script>
$(function() {
var channel = "pubnub_iot_house";
var pubnub = PUBNUB.init({
publish_key: 'demo',
subscribe_key: 'demo'
});
$('button').click(function(){
var value = $(this).val();
var module = $(this).parent().parent().attr('id');
pubnub.publish({
channel: channel,
message: {
name: module,
value: value
}
});
});
});
</script>
</body>
#include <JsonArray.h>
#include <JsonHashTable.h>
#include <JsonObjectBase.h>
#include <JsonParser.h>
#include <Bridge.h>
#include <HttpClient.h>
#include <Servo.h>
HttpClient client;
Servo frontDoor;
Servo garageDoor;
int lightLeft = 11;
int lightRight = 10;
int lightRoom = 9;
int lightGarage = 8;
int servoDoor = 7;
int servoGarage = 6;
bool asyncdemo = false;
void setup() {
// Bridge takes about two seconds to start up
// it can be helpful to use the on-board LED
// as an indicator for when it has initialized
Bridge.begin();
client.setTimeout(2000);
pinMode(lightLeft, OUTPUT);
pinMode(lightRight, OUTPUT);
pinMode(lightRoom, OUTPUT);
pinMode(lightGarage, OUTPUT);
blink(300, 5);
frontDoor.attach(servoDoor);
garageDoor.attach(servoGarage);
reset();
}
String timetoken = "0";
void loop() {
Serial.println("subscribe called");
String sub = "demo";
String pub = "demo";
String chan = "my_channel";
String url = "http://pubsub.pubnub.com/subscribe/" + sub + "/" + chan + "/0/" + timetoken;
char sub_buff[200];
char next_char;
String thejson;
Serial.println(url);
client.getAsynchronously(url);
// Wait for the http request to complete
while (!client.ready()) {
if(asyncdemo) {
pingpong(1);
off();
}
}
while (client.available()) {
next_char = client.read();
Serial.print(next_char);
if(String(next_char) == '\0') {
break;
} else {
thejson += next_char;
}
}
Serial.println("the json is");
Serial.println(thejson);
int firstParen = thejson.indexOf('(');
int lastParen = thejson.lastIndexOf(')');
String thejson2 = "";
for(int i = 0; i < thejson.length(); i++){
if(i == lastParen) {
Serial.println("last paren");
break;
}
if(i > firstParen) {
thejson2 += thejson[i];
}
}
Serial.println(thejson2);
thejson2.toCharArray(sub_buff, 200);
JsonParser<32> parser;
JsonArray root = parser.parseArray(sub_buff);
if (!root.success()) {
Serial.println("fail");
} else {
timetoken = root.getString(1);
JsonArray messages = root.getArray(0);
Serial.print("array len ");
Serial.print(messages.getLength());
Serial.println();
if(messages.getLength() < 0) {
Serial.println("no data");
}
for(int i = 0; i < messages.getLength(); i++){
JsonHashTable message = messages.getHashTable(i);
if (!message.success()) {
Serial.println("fail");
}
String name = message.getString("name");
String valueString = message.getString("value");
Serial.println(name + ":" + valueString);
boolean value = false;
if(valueString == "1") {
value = true;
}
if(name == "door") {
door(value);
}
if(name == "garage") {
garage(value);
}
if(name == "lightLeft") {
light(lightLeft, value);
}
if(name == "lightRight") {
light(lightRight, value);
}
if(name == "lightRoom") {
light(lightRoom, value);
}
if(name == "lightGarage") {
light(lightGarage, value);
}
if(name == "blink") {
blink(100, valueString.toInt());
}
if(name == "pingpong") {
pingpong(valueString.toInt());
}
if(name == "demo") {
demo();
}
if(name == "async") {
asyncdemo = value;
}
}
}
Serial.flush();
}
void light(int ledPin, boolean on) {
Serial.println("led");
if(on) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
void garage(boolean open){
Serial.println("garage");
if(open) {
garageDoor.write(30);
} else {
garageDoor.write(80);
}
}
void door(boolean open){
Serial.println("door");
if(open) {
frontDoor.write(80);
} else {
frontDoor.write(20);
}
}
void reset() {
garage(false);
door(false);
off();
}
void on() {
light(lightLeft, true);
light(lightRight, true);
light(lightRoom, true);
light(lightGarage, true);
}
void off() {
light(lightLeft, false);
light(lightRight, false);
light(lightRoom, false);
light(lightGarage, false);
}
void blink(int delayn, int count) {
for (int j=0; j <= count; j++){
on();
delay(delayn);
off();
delay(delayn);
}
}
void pingpong(int count) {
for (int j=0; j <= count; j++){
ping(100);
pong(100);
}
}
void demo() {
pingpong(1);
open();
delay(500);
close();
delay(500);
blink(100, 5);
}
void open() {
door(1);
garage(1);
}
void close() {
door(0);
garage(0);
}
void ping(int delayn) {
off();
light(lightLeft, true);
delay(delayn);
light(lightRight, true);
light(lightLeft, false);
delay(delayn);
light(lightRight, false);
light(lightRoom, true);
delay(delayn);
light(lightRoom, false);
light(lightGarage, true);
delay(delayn);
delay(delayn);
}
void pong(int delayn) {
off();
light(lightGarage, true);
delay(delayn);
light(lightGarage, false);
light(lightRoom, true);
delay(delayn);
light(lightRoom, false);
light(lightRight, true);
delay(delayn);
light(lightRight, false);
light(lightLeft, true);
delay(delayn);
delay(delayn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment