Skip to content

Instantly share code, notes, and snippets.

@ianjennings
Last active June 16, 2017 00:30
Show Gist options
  • Save ianjennings/75cdce63b4b556c8054a to your computer and use it in GitHub Desktop.
Save ianjennings/75cdce63b4b556c8054a to your computer and use it in GitHub Desktop.
PubNub IOT House (Ethernet Shield)
#include <PubNub.h>
#include <SPI.h>
#include <EthernetV2_0.h>
#include <string.h>
#include <Servo.h>
Servo frontDoor;
Servo garageDoor;
// Some Ethernet shields have a MAC address printed on a sticker on the shield;
// fill in that address here, or choose your own at random:
byte mac[] = {0x68, 0x5b, 0x35A, 0xCF, 0xA6, 0x4E};
int lightLeft =8;
int lightRight = 7;
int lightRoom = 6;
int lightGarage = 5;
int i;
EthernetClient *client;
#define W5200_CS 3
#define SDCARD_CS 4
char pubkey[] = "demo";
char subkey[] = "demo";
char channel[] = "iot_house";
void setup()
{
pinMode(SDCARD_CS,OUTPUT);
digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
Serial.begin(9600);
Serial.println("Serial set up");
while (!Ethernet.begin(mac)) {
Serial.println("Ethernet setup error");
blink(1000, 999999);
delay(1000);
}
Serial.println("Ethernet set up");
PubNub.begin(pubkey, subkey);
Serial.println("PubNub set up");
frontDoor.attach(18);
garageDoor.attach(19);
pinMode(lightLeft, OUTPUT);
pinMode(lightRight, OUTPUT);
pinMode(lightRoom, OUTPUT);
pinMode(lightGarage, OUTPUT);
blink(100, 999);
reset();
while(true) {
open();
delay(1000);
close();
delay(1000);
}
}
void flash(int ledPin)
{
/* Flash LED three times. */
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
void loop() {
Ethernet.maintain();
PubSubClient *client;
Serial.println("waiting for a message (subscribe)");
client = PubNub.subscribe(channel);
if (!client) {
Serial.println("subscription error");
return;
}
String messages[10];
boolean inside_command = false;
int num_commands = 0;
String message = "";
char c;
while (client->wait_for_data()) {
c = client->read();
if(inside_command && c != '"') {
messages[num_commands] += c;
}
if(c == '"') {
if(inside_command) {
num_commands = num_commands + 1;
inside_command = false;
} else {
inside_command = true;
}
}
message += c;
}
client->stop();
for (i = 0; i < num_commands; i++){
int colonIndex = messages[i].indexOf(':');
String subject = messages[i].substring(0, colonIndex);
String valueString = messages[i].substring(colonIndex + 1, messages[i].length());
boolean value = false;
if(valueString == "1") {
value = true;
}
if(subject == "door") {
door(value);
}
if(subject == "garage") {
garage(value);
}
if(subject == "lightLeft") {
light(lightLeft, value);
}
if(subject == "lightRight") {
light(lightRight, value);
}
if(subject == "lightRoom") {
light(lightRoom, value);
}
if(subject == "lightGarage") {
light(lightGarage, value);
}
if(subject == "blink") {
blink(100, valueString.toInt());
}
if(subject == "pingpong") {
pingpong(valueString.toInt());
}
Serial.println(subject);
Serial.println(value);
}
Serial.print(message);
Serial.println();
delay(2000);
}
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);
light(lightLeft, false);
light(lightRight, false);
light(lightRoom, false);
light(lightGarage, false);
}
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() {
blink(100, 5);
delay(1000);
open();
delay(1000);
close();
}
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);
}
<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 = "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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment