Last active
January 4, 2018 19:45
-
-
Save andreareginato/2f2a8aa71b94bb8ffc17 to your computer and use it in GitHub Desktop.
Code for the MakerFaire Lamp
This file contains 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 <SPI.h> | |
#include <YunClient.h> | |
#include <PubSubClient.h> | |
char* deviceId = "<DEVICE-ID>"; | |
char* deviceSecret = "<DEVICE-SECRET>"; | |
char* outTopic = "devices/<DEVICE-ID>/set"; | |
char* inTopic = "devices/<DEVICE-ID>/get"; | |
byte server[] = { 96, 126, 109, 170 }; | |
char* clientId = "makerfaire"; | |
char* payloadOn = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"off\"}]}"; | |
char* payloadOff = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"on\"}]}"; | |
byte mac[] = { 0xA0, 0xA0, 0xBA, 0xAC, 0xAE, 0x12 }; | |
YunClient yun; | |
void callback(char* topic, byte* payload, unsigned int length); // subscription callback | |
PubSubClient client(server, 1883, callback, yun); // mqtt client | |
int inPin = 8; // button | |
int outPin = 5; // led | |
int state = HIGH; // current state of the output pin | |
int reading; // current reading from the input pin | |
int previous = LOW; // previous reading from the input pin | |
long time = 0; // the last time the output pin was toggled | |
long debounce = 200; // the debounce time, increase if the output flickers | |
void setup() { | |
Serial.begin(9600); | |
delay(500); | |
Bridge.begin(); | |
lelylanConnection(); // MQTT server connection | |
pinMode(inPin, INPUT); // button pin setup | |
pinMode(outPin, OUTPUT); // led pin setup | |
} | |
void loop() { | |
lelylanConnection(); | |
char* value; | |
reading = digitalRead(inPin); // read the button state | |
if (reading == HIGH && previous == LOW && millis() - time > debounce) { | |
if (state == LOW) { | |
lelylanPublish("on"); | |
state = HIGH; | |
} else { | |
lelylanPublish("off"); | |
state = LOW; | |
} | |
time = millis(); | |
} | |
digitalWrite(outPin, state); | |
previous = reading; | |
} | |
void lelylanConnection() { | |
if (!client.connected()) { | |
if (client.connect(clientId, deviceId, deviceSecret)) { | |
Serial.println("[PHYSICAL] Successfully connected with MQTT"); | |
lelylanSubscribe(); // topic subscription | |
} | |
} | |
client.loop(); | |
} | |
void lelylanPublish(char* value) { | |
if (value == "on") | |
client.publish(outTopic, payloadOn); // light on | |
else | |
client.publish(outTopic, payloadOff); // light off | |
} | |
void lelylanSubscribe() { | |
client.subscribe(inTopic); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char* json; | |
json = (char*) malloc(length + 1); | |
memcpy(json, payload, length); | |
json[length] = '\0'; | |
if (String(payloadOn) == String(json)) { | |
lelylanPublish("on"); | |
state = HIGH; | |
} else { | |
lelylanPublish("off"); | |
state = LOW; | |
} | |
digitalWrite(outPin, state); | |
free(json); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment