alarmclock/arduino/alarmclock/alarmclock.cpp

216 lines
4.8 KiB
C++

#include <Arduino.h>
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "alarmclock.h"
// LED
float brightness = LED_BRIGHTNESS_DEFAULT;
int color = LED_COLOR_DEFAULT;
CRGB leds[LED_NUM];
boolean ledState = false;
void setup()
{
Serial.begin(SERIAL_SPEED);
Serial.println("\nresetting");
// WIFI
setupWifi();
// LED
/*
brightness = LED_BRIGHTNESS_DEFAULT;
color = LED_COLOR_DEFAULT;
ledState = false;
*/
LEDS.addLeds<LED_CHIPSET,LED_PIN, LED_COLOR_ORDER>(leds, LED_NUM).setCorrection(TypicalSMD5050);
ledBlackAll();
FastLED.setBrightness(brightness);
// MQTT
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callbackMQTT);
testConnectMQTT();
Serial.println("Ready");
/* MQTT
* Il est important de faire un loop avant toute chose,
* afin de récupérer les valeurs provenant du broker mqtt
* et pas démarrer avec de vieilles infos.
* Il faut un certains nombres de tentative pour tout récuperer.
*/
for (short int i = 0; i < 10; i++) {
delay(200);
client.loop();
}
Serial.println("End of setup");
}
// WIFI
void setupWifi()
{
Serial.print("Connexion a ");
Serial.print(WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" OK");
Serial.print("IP : ");
Serial.println(WiFi.localIP());
}
// MQTT
void testConnectMQTT()
{
while (!client.connected()) {
Serial.print("Connexion au serveur MQTT... ");
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
Serial.print("OK\nSend Current State");
mqttSendState();
mqttSendBrightnessState();
mqttSendColorState();
Serial.print("OK\nSubscribe");
client.subscribe(MQTT_LED_COMMAND);
client.subscribe(MQTT_LED_BRIGHTNESS_COMMAND);
client.subscribe(MQTT_LED_COLOR_COMMAND);
Serial.println(" OK");
} else {
Serial.print("KO, erreur : ");
Serial.print(client.state());
Serial.println(", on attend 5 secondes avant de recommencer");
delay(5000);
}
}
}
// Déclenche les actions à la réception d'un message
void callbackMQTT(char* topic, byte* payload, unsigned int length)
{
String stopic = String(topic);
unsigned int i = 0;
for(i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
Serial.print("Received [" + stopic + "] : ");
Serial.println(msgString);
if (stopic == MQTT_LED_COMMAND) {
if (msgString == "ON") {
ledState = true;
} else {
ledState = false;
ledBlackAll();
}
mqttSendState();
} else if (stopic == MQTT_LED_BRIGHTNESS_COMMAND) {
brightness = msgString.toInt();
FastLED.setBrightness(brightness);
mqttSendBrightnessState();
} else if (stopic == MQTT_LED_COLOR_COMMAND) {
// Sample : 134,168,255
int red = msgString.substring(0, msgString.indexOf(',')).toInt();
int green = msgString.substring(msgString.indexOf(',') + 1, msgString.lastIndexOf(',')).toInt();
int blue = msgString.substring(msgString.lastIndexOf(',') + 1).toInt();
color=((red <<16)|(green <<8)|blue);
mqttSendColorState();
}
}
void mqttSendState()
{
client.publish(MQTT_LED_STATE, (ledState) ? "ON": "OFF", true);
}
void mqttSendBrightnessState()
{
char buff[4];
itoa(brightness, buff, 10);
client.publish(MQTT_LED_BRIGHTNESS_STATE, buff, true);
}
void mqttSendColorState()
{
int red = color>>16 & 0xFF;
int green = color>>8 & 0xFF;
int blue = color & 0xFF;
char buff[12];
sprintf(buff, "%i,%i,%i", red, green, blue);
client.publish(MQTT_LED_COLOR_STATE, buff, true);
}
// LED
/**
* Coupe tout le strip de led.
*/
void ledBlackAll()
{
FastLED.clear();
FastLED.show();
}
/**
* Utilise pour indiquer une erreur sur la reception de l'effet.
*/
void ledError()
{
for (int i = 0; i < LED_NUM; i++) {
if ((i % 2) == 0) {
leds[i] = CRGB::Black;
} else {
leds[i] = CRGB::Red;
}
}
FastLED.delay(1000);
}
/**
* Affiche une couleur de manière uniforme sur le strip.
* Pour éviter un éclairage basique, on applique un breath qui permet
* de faire respirer la couleur (brightness).
*/
void ledFullColor()
{
// Source : http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
// Voic la version avec la gestion du speed, mais je ne suis pas convaincu
//float breath = (exp(sin(millis() / 2000.0 * map(speed, 0, 255, 50, 300)/100 * PI)) - 0.3678794) * 108.4;
float breath = (exp(sin(millis() / 4000.0 * PI)) - 0.3678794) * 108.4;
fill_solid(leds, LED_NUM, color);
FastLED.setBrightness(breath);
FastLED.show();
}
void loop() {
// MQTT
testConnectMQTT();
client.loop();
// LED
if (!ledState) {
FastLED.delay(1000);
} else {
ledFullColor();
}
}