Skip to content

Commit

Permalink
Fetch informations from other services
Browse files Browse the repository at this point in the history
  • Loading branch information
macbury committed Feb 22, 2018
1 parent cf94c52 commit 9cfc9ad
Show file tree
Hide file tree
Showing 16 changed files with 439 additions and 111 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -31,3 +31,6 @@ sudo systemctl start lam.py
* https://customer.cloudmqtt.com/login

https://github.com/RidersDiscountCom/HypChat/blob/master/TYPES.rst

## TODO
* something with arcade button led? maybe if online then fade in and fade out
36 changes: 22 additions & 14 deletions config.yaml.example
@@ -1,28 +1,36 @@
jenkins:
url: "http://"
topic: 'build'
url: ""
password: ""
user: ""
jobs:
- job_name
- proj

coffee:
endpoint: 'https://api.thingspeak.com/channels/125608/fields/1.json?sum=daily&timezone=Europe/Warsaw&days=1'
topic: 'coffee'

lunching:
endpoint: 'https://api.lunching.pl/api/account/order-list'
topic: 'food'
username: ''
password: ''

mqtt:
host: ''
username: ''
password: ''
port:
port: 0

topics:
jenkins_status: 'build'
button: 'button'
presence: 'presence'
button:
topic: 'button'
recipient: 0
messages:
- "."
- ".."

hipchat:
topic: 'presence'
token: ''
team:
- email@gmail.com
- email22gmail.com
button_action:
recipient: email@gmail.com
messages:
- "."
- "Jeże"
- emails@gmail.com
19 changes: 19 additions & 0 deletions device/button.h
@@ -0,0 +1,19 @@
int buttonAccumulator = 0;
boolean actionButtonPressed() {
int actionButtonState = digitalRead(PIN_ACTION_BUTTON);

if (actionButtonState == HIGH) {
return false;
}

digitalWrite(PIN_STATUS_LED, HIGH);
int buttonAccumulator = 0;
while(actionButtonState == LOW) {
delay(100);
buttonAccumulator += 1;
actionButtonState = digitalRead(PIN_ACTION_BUTTON);
}
digitalWrite(PIN_STATUS_LED, LOW);

return buttonAccumulator >= 5;
}
93 changes: 93 additions & 0 deletions device/connection.h
@@ -0,0 +1,93 @@
void printWifiInfo() {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void setupOTA() {
Serial.println("Configuring ArduinoOTA");
ArduinoOTA.setPort(OTA_PORT);
ArduinoOTA.setHostname(OTA_HOST);
ArduinoOTA.setPassword(OTA_PASSWORD);

ArduinoOTA.onStart([]() {
Serial.println("Starting");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});

ArduinoOTA.begin();
}

void setupWifi() {
delay(1000);
Serial.println("----------");
Serial.println("Connecting to: ");
Serial.println(WIFI_NAME);
WiFi.mode(WIFI_STA);

while (WiFi.waitForConnectResult() != WL_CONNECTED){
Serial.print(".");
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);

delay(500);
}
Serial.println("OK!");
randomSeed(micros());
printWifiInfo();
setupOTA();
}

void ensureWifiConnection() {
if (WiFi.status() != WL_CONNECTED) {
delay(1);
Serial.print("WIFI Disconnected. Attempting reconnection.");
setupWifi();
}
}

bool ensureMqttConnection() {
ensureWifiConnection();
while (!client.connected()) {
delay(1000);
Serial.println("Attempting MQTT connection...");
// Create a random client ID
String clientId = "lam.py-";
clientId += String(random(0xffff), HEX);
Serial.print("Client id: ");
Serial.println(clientId);
if (client.connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD)) {
printWifiInfo();
return true;
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}

return false;
}

void onConnect() {
Serial.print("Subscribing: ");
Serial.println(MQTT_TOPIC_BUILD);
client.subscribe(MQTT_TOPIC_BUILD);
Serial.println(MQTT_TOPIC_PRESENCE);
client.subscribe(MQTT_TOPIC_PRESENCE);
}
19 changes: 17 additions & 2 deletions device/credentials.h.example
Expand Up @@ -5,7 +5,22 @@
#define ENDPOINT_USER ""
#define ENDPOINT_PASSWORD ""

#define PIX_COUNT 44
#define LED_PIN D5
#define PIXEL_COUNT 44
#define PIN_LED_STRIP D5
#define PIN_STATUS_LED D2
#define PIN_ACTION_BUTTON D3

#define LIGHT_BRIGHTNESS 100

#define MQTT_HOST ""
#define MQTT_PORT 0
#define MQTT_USER ""
#define MQTT_PASSWORD ""

#define MQTT_TOPIC_BUILD "build"
#define MQTT_TOPIC_PRESENCE "presence"
#define MQTT_TOPIC_BUTTON "button"

#define OTA_HOST ""
#define OTA_PORT 0
#define OTA_PASSWORD ""
104 changes: 54 additions & 50 deletions device/device.ino
@@ -1,30 +1,47 @@
#include <ArduinoHttpClient.h>
#include <EEPROM.h>

#include <Adafruit_NeoPixel.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include "credentials.h"
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

WiFiClient wifi;
HttpClient client = HttpClient(wifi, ENDPOINT_HOST, 80);
#include "credentials.h"

//https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino
WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIN_LED_STRIP, NEO_GRB + NEO_KHZ800);

int accumulator = 0;
boolean actionButtonPressed() {
int actionButtonState = digitalRead(PIN_ACTION_BUTTON);
#include "connection.h"
#include "effects.h"
#include "button.h"
#include "state.h"

if (actionButtonState == HIGH) {
return false;
void on_mqtt_message(char* topic, byte* payload, unsigned int length) {
char rawMessage[length + 1];
for (int i = 0; i < length; i++) {
rawMessage[i] = (char)payload[i];
}
rawMessage[length] = '\0';

digitalWrite(PIN_STATUS_LED, HIGH);
int accumulator = 0;
while(actionButtonState == LOW) {
delay(100);
accumulator += 1;
actionButtonState = digitalRead(PIN_ACTION_BUTTON);
}
digitalWrite(PIN_STATUS_LED, LOW);
String message = String(message);
String topic = String(topic);

return accumulator >= 5;
if (topic == MQTT_TOPIC_PRESENCE) {
turnOnOff(message);
} else if (topic == MQTT_TOPIC_PRESENCE) {
turnOnOff(message);
} else if (topic == MQTT_TOPIC_BUILD) {
switchToState(message)
} else {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.println("] ");
Serial.print("Message:");
Serial.println(message);
}
}

void setup() {
Expand All @@ -35,40 +52,27 @@ void setup() {
Serial.begin(115200);
delay(10);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_NAME);
strip.begin();
strip.setBrightness(0);
strip.show();

WiFi.begin(WIFI_NAME, WIFI_PASSWORD);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(MQTT_HOST, MQTT_PORT);
client.setCallback(on_mqtt_message);
colorWipe(strip.Color(0, 0, 0), 50);
}

void loop() {
if (actionButtonPressed()) {
Serial.println("Pressed a button!");
}

Serial.println("Performing request");
client.beginRequest();
client.get("/");
client.sendBasicAuth(ENDPOINT_USER, ENDPOINT_PASSWORD);
client.endRequest();
Serial.println("Ending request");

Serial.println("available: ");
Serial.println(client.available());
while(client.available() > 0) {
Serial.println("Left: ");
Serial.println(client.available());
if (client.connected()) {
client.loop();
ArduinoOTA.handle();
if (actionButtonPressed()) {
Serial.println("Pressed a button!");
colorWipe(strip.Color(255, 0, 0), 50);
theaterChase(strip.Color(127, 127, 127), 50);
}
} else {
if (ensureMqttConnection()) {
onConnect();
}
}
}
28 changes: 28 additions & 0 deletions device/effects.h
@@ -0,0 +1,28 @@
byte defaultBrightness = 0;
void colorWipe(uint32_t c, uint8_t wait) {
strip.setBrightness(defaultBrightness);
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
strip.setBrightness(defaultBrightness);
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();

delay(wait);

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}

0 comments on commit 9cfc9ad

Please sign in to comment.