Hey guys,
I’m pretty new to photon (and this kind of hardware environment in general). I have code written for some home automation stuff, but the problem is that my photon goes on and offline at semi-regular intervals. This is what I have in my console:
and my code:
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
// This #include statement was automatically added by the Particle IDE.
#include <FastLED.h>
#include "math.h"
FASTLED_USING_NAMESPACE;
#define PIXEL_COUNT 15
#define PIXEL_PIN D6
#define PIXEL_TYPE WS2812B
CRGB leds[PIXEL_COUNT];
float delta = 0.5f;
float t = 0;
boolean blinkStove = false;
boolean stoveOn = false;
boolean motionDetected = false;
int stoveStart = 0;
int waterStart = 5;
int doorStart = 10;
int numPixelsPerPanel = 5;
const int RED_HUE = 0;
const int GREEN_HUE = 96;
// Broker hosted on test.mosquitto.org
byte server[] = {37, 187, 106, 16};
MQTT client(server, 1883, 30, callback);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
handleMessage(message.toInt());
}
void setup() {
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, PIXEL_COUNT);
client.connect("a");
if (client.isConnected()) {
Particle.publish("Connected to mqtt broker");
} else {
Particle.publish("Failed to connect to mqtt broker");
}
client.subscribe("xxxx");
fill_solid(&(leds[0]), PIXEL_COUNT, CRGB::Black);
}
void loop() {
if (client.isConnected()) {
client.loop();
} else {
client.connect("a");
}
if (blinkStove) {
if (motionDetected) {
blinkPanel(stoveStart, GREEN_HUE);
} else {
blinkPanel(stoveStart, RED_HUE);
}
}
}
void blinkPanel(int start, int hue) {
t += delta;
int value = constrain((int) triwave8(abs(t)), 50, 255);
fill_solid(&(leds[start]), numPixelsPerPanel, CHSV(hue, 255, value));
FastLED.show();
}
void handleMessage(int message) {
switch (message) {
case 0:
fill_solid(&leds[doorStart], numPixelsPerPanel, CRGB::Black);
break;
case 1:
fill_solid(&leds[doorStart], numPixelsPerPanel, CRGB::Red);
break;
case 2:
fill_solid(&leds[stoveStart], numPixelsPerPanel, CRGB::Black);
stoveOn = false;
break;
case 3:
fill_solid(&leds[stoveStart], numPixelsPerPanel, CRGB::Red);
stoveOn = true;
break;
case 4:
if (stoveOn) {
fill_solid(&leds[stoveStart], numPixelsPerPanel, CRGB::Red);
}
motionDetected = false;
break;
case 5:
if (stoveOn) {
fill_solid(&leds[stoveStart], numPixelsPerPanel, CRGB::Green);
}
motionDetected = true;
break;
case 6:
blinkStove = false;
stoveOn = false;
break;
case 7:
blinkStove = true;
stoveOn = false;
break;
default:
fill_solid(&leds[0], PIXEL_COUNT, CRGB::Black);
break;
}
FastLED.show();
}
Any ideas what could be wrong? Input/help would be greatly appreciated.