Created
September 13, 2016 21:41
-
-
Save timigod/dde27fec5f80af1a4eadc29b7c97b0c1 to your computer and use it in GitHub Desktop.
Arduino code for home automation model project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 lightDining = 11; | |
int lightOutside = 10; | |
int lightKitchen = 9; | |
int lightLivingroom = 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(lightDining, OUTPUT); | |
pinMode(lightOutside, OUTPUT); | |
pinMode(lightKitchen, OUTPUT); | |
pinMode(lightLivingroom, OUTPUT); | |
blink(300, 5); | |
frontDoor.attach(servoDoor); | |
garageDoor.attach(servoGarage); | |
reset(); | |
} | |
String timetoken = "0"; | |
void loop() { | |
Serial.println("subscribe called"); | |
String sub = "sub-blahblah"; | |
String pub = "pub-blahblah"; | |
String chan = "pubnub_iot_house"; | |
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 == "frontDoor") { | |
door(value); | |
} | |
if (name == "garageDoor") { | |
garage(value); | |
} | |
if (name == "lightDining") { | |
light(lightDining, value); | |
} | |
if (name == "lightOutside") { | |
light(lightOutside, value); | |
} | |
if (name == "lightKitchen") { | |
light(lightKitchen, value); | |
} | |
if (name == "lightLivingroom") { | |
light(lightLivingroom, 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(150); | |
} 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(lightDining, true); | |
light(lightOutside, true); | |
light(lightKitchen, true); | |
light(lightLivingroom, true); | |
} | |
void off() { | |
light(lightDining, false); | |
light(lightOutside, false); | |
light(lightKitchen, false); | |
light(lightLivingroom, 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(lightDining, true); | |
delay(delayn); | |
light(lightOutside, true); | |
light(lightDining, false); | |
delay(delayn); | |
light(lightOutside, false); | |
light(lightKitchen, true); | |
delay(delayn); | |
light(lightKitchen, false); | |
light(lightLivingroom, true); | |
delay(delayn); | |
delay(delayn); | |
} | |
void pong(int delayn) { | |
off(); | |
light(lightLivingroom, true); | |
delay(delayn); | |
light(lightLivingroom, false); | |
light(lightKitchen, true); | |
delay(delayn); | |
light(lightKitchen, false); | |
light(lightOutside, true); | |
delay(delayn); | |
light(lightOutside, false); | |
light(lightDining, true); | |
delay(delayn); | |
delay(delayn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment