-
-
Save tclem/a66a3b10fdde44b226a3cdca62f94202 to your computer and use it in GitHub Desktop.
Particle Photon front lights
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
// This #include statement was automatically added by the Particle IDE. | |
#include <MQTT.h> | |
void callback(char* topic, byte* payload, unsigned int length); | |
/** | |
* if want to use IP address, | |
* byte server[] = { XXX,XXX,XXX,XXX }; | |
* MQTT client(server, 1883, callback); | |
* want to use domain name, | |
* exp) iot.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started | |
* MQTT client("iot.eclipse.org", 1883, callback); | |
**/ | |
byte server[] = { 192, 168, 1, 78 }; | |
MQTT client(server, | |
1883 /* port */, | |
600 /* keep alive in seconds (10 mins) */, | |
callback, | |
512 /* max packet size */); | |
int led2 = D7; // led on pin7 | |
int lights = D3; // outdoor lights | |
#define LIGHTS_ON 1 | |
#define LIGHTS_OFF 2 | |
#define SHOULD_TURN_ON 3 | |
#define SHOULD_TURN_OFF 4 | |
int state = 0; | |
const char* on = "ON"; | |
const char* off = "OFF"; | |
const char* command_topic = "home/particle/lights/control"; | |
const char* state_topic = "home/particle/lights/status"; | |
const char* discovery_payload = "{\"~\":\"home/particle/lights\",\"name\":\"Walkway\",\"unique_id\":\"walkway_light\",\"state_topic\":\"~/status\",\"command_topic\":\"~/control\",\"device\":{\"identifiers\":\"dustmite\",\"model\":\"Photon\",\"manufacturer\":\"Particle\"}}"; | |
void setup() | |
{ | |
pinMode(led2, OUTPUT); | |
pinMode(lights, OUTPUT); | |
Particle.function("light", ledToggle); | |
Particle.function("reset", resetDevice); | |
digitalWrite(led2, HIGH); | |
digitalWrite(lights, LOW); | |
state = LIGHTS_OFF; | |
// RGB.control(true); | |
setupMQTT(); | |
} | |
void loop() { | |
digitalWrite(led2, LOW); | |
Particle.process(); | |
if (client.isConnected()) { | |
client.loop(); | |
if (state == SHOULD_TURN_ON) { | |
turnOn(); | |
} else if (state == SHOULD_TURN_OFF) { | |
turnOff(); | |
} | |
} else { | |
// disconnected for some reason, turn on the led to signal this. | |
digitalWrite(led2, HIGH); | |
delay(1000); | |
if (!Particle.connected()) { | |
Particle.connect(); | |
} | |
// Particle.publish("mqtt-connection-lost", PRIVATE); | |
if (setupMQTT()) { | |
// connected again, signal success by turning off the led. | |
digitalWrite(led2, LOW); | |
} | |
} | |
} | |
bool setupMQTT() { | |
// Particle.publish("connecting-to-mqtt", PRIVATE); | |
// connect to the server | |
client.connect(System.deviceID()); | |
// publish/subscribe | |
if (client.isConnected()) { | |
// This uses Home Assistant's MQTT's device discovery | |
// https://www.home-assistant.io/docs/mqtt/discovery/ | |
client.publish("homeassistant/light/dustmite/config", discovery_payload); | |
client.subscribe(command_topic); | |
client.publish(state_topic, off); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
const unsigned int on_command_size = 3; | |
const unsigned int off_command_size = 4; | |
const byte on_command[on_command_size] = "ON"; | |
const byte off_command[off_command_size] = "OFF"; | |
bool equal(const byte* a, const byte* b, unsigned int length_a, unsigned int length_b) { | |
for(int i = 0; i < length_a; i++) { | |
if (i + 1 >= length_b) | |
return false; | |
if (a[i] != b[i]) | |
return false; | |
} | |
return true; | |
} | |
// MQTT callback | |
// Important to avoid network calls or other blocking activity in here. | |
// Just set some flags and return. | |
void callback(char* topic, byte* payload, unsigned int length) { | |
if(!strcmp(topic, command_topic)) { | |
if (equal(payload, on_command, length, on_command_size)) { | |
state = SHOULD_TURN_ON; | |
} else if (equal(payload, off_command, length, off_command_size)) { | |
state = SHOULD_TURN_OFF; | |
} | |
} | |
} | |
int turnOn() { | |
digitalWrite(lights, HIGH); | |
// digitalWrite(led2, HIGH); | |
state = LIGHTS_ON; | |
client.publish(state_topic, on); | |
return 1; | |
} | |
int turnOff() { | |
digitalWrite(lights, LOW); | |
// digitalWrite(led2, LOW); | |
state = LIGHTS_OFF; | |
client.publish(state_topic, off); | |
return 0; | |
} | |
// This is a particle function (good for testing) | |
int ledToggle(String command) { | |
if (command=="on") { | |
return turnOn(); | |
} | |
else if (command=="off") { | |
return turnOff(); | |
} | |
else { | |
return -1; | |
} | |
} | |
int resetDevice(String command) { | |
System.reset(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment