...
 
Commits (2)
......@@ -6,5 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
### Added
- proximity sensor support
## [0.1.0] - 2017-04-24
### Added
- fetch color from controlcenter server
- send color to the led strip
\ No newline at end of file
- send color to the led strip
digi-iot/device-firmware
iot4i/device-firmware
========================
This repository contains the source code for the digi-iot devices.
This repository contains the source code for the iot4i devices.
You need [PlatformIO](http://platformio.org) installed to build and run the program.
......@@ -10,7 +10,7 @@ I recommend [PlatformIO IDE](http://platformio.org/platformio-ide) for developpe
## Environment variables
As the firmware must be built with the wifi ssid, the wifi key and the controlcenter server ip, you must set the build flags corresponding to your device configuration through the `PLATFORMIO_BUILD_FLAGS` env variable before running any command :
```bash
export PLATFORMIO_BUILD_FLAGS='-DWIFI_SSID=\"YOUR SSID\" -DWIFI_KEY=\"YOUR SECRET\" -DIOT_SERVER=\"192.168.1.XX\" -DLED_COUNT=50 -DPIN=D1'
export PLATFORMIO_BUILD_FLAGS='-DWIFI_SSID=\"YOUR SSID\" -DWIFI_KEY=\"YOUR SECRET\" -DIOT_SERVER=\"192.168.1.XX\" -DLED_COUNT=50 -DPIN_LED=D1 -DPIN_PROXIMITY=D2'
```
## Build
......
......@@ -3,20 +3,27 @@
#include <Adafruit_NeoPixel.h>
#include <ArduinoJson.h>
#ifndef PIN
#define PIN D4
#ifndef PIN_LED
#define PIN_LED D4
#endif
#ifndef PIN_PROXIMITY
#define PIN_PROXIMITY -1
#endif
#ifndef LED_COUNT
#define LED_COUNT 1
#endif
#define LOOP_DELAY 1000
#define LOOP_DELAY 500
#define HTTP_TIMEOUT 5000
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, PIN_LED, NEO_GRB + NEO_KHZ800);
const char* url = "/api/v1/color";
const int httpPort = 80;
void startProximity(int pin){
pinMode(pin, INPUT);
}
void setup() {
Serial.begin(115200);
delay(1000);
......@@ -40,38 +47,76 @@ void setup() {
// Wifi connected, we can start neopixel
strip.begin();
// Proximity sensor
if(PIN_PROXIMITY != -1) {
startProximity(PIN_PROXIMITY);
}
}
// String to long, skiping the first char that is '#'
int colorStringToInt(const char * color){
return (int) strtol(&color[1], NULL, 16);
}
// Set a color for all leds
// Take an hexadecimal string forlatted like : #RRGGBB
void setColorForAllLeds(Adafruit_NeoPixel* pixels, const char* hexstring){
// String to long, skipping the first char that is '#'
int number = (int) strtol(&hexstring[1], NULL, 16);
void setColorForAllLeds(Adafruit_NeoPixel* pixels, int color){
int r = color >> 16;
int g = color >> 8 & 0xFF;
int b = color & 0xFF;
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;
Serial.printf("Setting led color to %d,%d,%d\n", r,g,b);
int i;
for(i=0; i<pixels->numPixels(); i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels->setPixelColor(i, pixels->Color(r,g,b));
}
}
void loop() {
delay(LOOP_DELAY);
// Take an hexadecimal string forlatted like : #RRGGBB
void setColorForAllLeds(Adafruit_NeoPixel* pixels, const char* hexstring){
int number = colorStringToInt(hexstring);
setColorForAllLeds(pixels, number);
}
enum LED_STATE { AUTOMATIC, MANUAL, SWITCHED_TO_MANUAL };
int currentLedState = AUTOMATIC;
int getProxymityStatus(int pin){
// If pin is -1 it has not been activated on build
// That means no support for proximity sensor
if(pin == -1)
return AUTOMATIC;
int newProximityState = digitalRead(pin);
if(newProximityState == 0) {
if(currentLedState == AUTOMATIC) {
Serial.printf("PROXIMITY: activated\n");
currentLedState = SWITCHED_TO_MANUAL;
return currentLedState;
}
if(currentLedState == MANUAL) {
Serial.printf("PROXIMITY: deactivated\n");
currentLedState = AUTOMATIC;
return currentLedState;
}
}
if(currentLedState == SWITCHED_TO_MANUAL) {
currentLedState = MANUAL;
}
return currentLedState;
}
int getColorFromControlCenter(const char * host, const char * url){
int color = 0;
Serial.printf("HTTP: Connecting to %s\n", IOT_SERVER);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(IOT_SERVER, httpPort)) {
Serial.printf("HTTP: Connection to %s failed\n", IOT_SERVER);
return;
return color;
}
Serial.printf("HTTP: Requesting URL %s\n", url);
......@@ -83,7 +128,7 @@ void loop() {
if (millis() - timeout > HTTP_TIMEOUT) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
return color;
}
}
......@@ -95,7 +140,7 @@ void loop() {
}
if(line.length() == 0) {
Serial.println("HTTP: Received empty string");
return;
return color;
}
Serial.printf("HTTP: Response received : %s\n",line.c_str());
......@@ -104,16 +149,26 @@ void loop() {
JsonObject& root = jsonBuffer.parseObject(line.c_str());
if (!root.success()) {
Serial.printf("JSON: parseObject() failed: %s\n", line.c_str());
return;
return color;
}
const char * color = root["hexa"];
if(color == NULL) {
const char * colorString = root["hexa"];
if(colorString == NULL) {
Serial.printf("JSON: Unable to fetch json 'hexa' property' in: %s\n", line.c_str());
return;
return color;
}
Serial.printf("JSON: color parsed: %s\n",color);
Serial.printf("JSON: color parsed: %s\n",colorString);
return colorStringToInt(colorString);
}
setColorForAllLeds(&strip, color);
void loop() {
delay(LOOP_DELAY);
int proximityStatus = getProxymityStatus(PIN_PROXIMITY);
if(proximityStatus == SWITCHED_TO_MANUAL) {
setColorForAllLeds(&strip, "#FFFFFF");
}else if (proximityStatus == AUTOMATIC) {
int color = getColorFromControlCenter(IOT_SERVER, url);
setColorForAllLeds(&strip, color);
}
strip.show();
}