Skip to content

Instantly share code, notes, and snippets.

@tclem

tclem/front_lights.ino Secret

Last active Oct 14, 2020
Embed
What would you like to do?
Particle Photon front lights
// This #include statement was automatically added by the Particle IDE.
// You'll have to search for and manually add the MQTT library in 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, callback);
int led2 = D7; // led on pin7
int lights = D3; // outdoor lights
bool lights_on = false;
const char* on = "ON";
const char* off = "OFF";
const char* command_topic = "home/particle/lights/control";
const char* state_topic = "home/particle/lights/status";
void setup()
{
pinMode(led2, OUTPUT);
pinMode(lights, OUTPUT);
// Couple of particle functions so that we can use the console to toggle the lights and reset this device.
Particle.function("light", ledToggle);
Particle.function("reset", resetDevice);
// For good measure, let's also make sure both LEDs are off when we start:
digitalWrite(led2, LOW);
digitalWrite(lights, LOW);
setupMQTT();
}
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 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);
}
}
void loop() {
if (client.isConnected()) {
client.loop();
} else {
Particle.publish("mqtt-connection-lost", PRIVATE);
setupMQTT();
}
}
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
if(!strcmp(topic, command_topic)) {
if (!strcmp(p, on)) {
turnOn();
} else if (!strcmp(p, off)) {
turnOff();
}
}
delay(500);
}
int turnOn() {
digitalWrite(lights, HIGH);
digitalWrite(led2, HIGH);
lights_on = true;
client.publish(state_topic, on);
return 1;
}
int turnOff() {
digitalWrite(lights, LOW);
digitalWrite(led2, LOW);
lights_on = false;
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
You can’t perform that action at this time.