Skip to content

Commit

Permalink
Move from .h and .ino to .cpp
Browse files Browse the repository at this point in the history
This hopefully fixes many confusing errors regarding undefined
functions. Moving away from .h files makes it a lot easier for those
files to call each other.

Unfortunately, this comes at a cost of code size: link-time optimization
(inlining etc. across compilation units) hasn't been enabled for the
ESP8266 port yet. See: esp8266/Arduino#23
  • Loading branch information
aykevl committed Nov 25, 2016
1 parent 0de8a45 commit 6dc713b
Show file tree
Hide file tree
Showing 20 changed files with 541 additions and 470 deletions.
2 changes: 2 additions & 0 deletions button.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#pragma once

#include "Arduino.h"

// states in the state machine
const uint8_t RELEASED = 0;
const uint8_t RELEASED_BOUNCE = 1;
Expand Down
7 changes: 7 additions & 0 deletions colorlight.ino → colorlight.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

#include <Arduino.h>
#include <ArduinoJson.h>

#include "colorlight.h"
#include "config.h"
#include "radio.h"
#include "mqtt.h"
#include "ufloat8.h"

const uint8_t COLOR_FLAG_DISABLED = 0b10000000;
const uint8_t COLOR_FLAG_LOOPING = 0b01000000;
Expand Down
3 changes: 3 additions & 0 deletions colorlight.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

#pragma once

#include <ArduinoJson.h>

void colorLightSend(uint8_t *arg);
void colorLightReceive(JsonObject &value);
20 changes: 8 additions & 12 deletions domo-wakeup.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

const uint8_t LIGHT_PIN = D0;
const uint8_t WAKEUP_PIN = D0;
const uint8_t BUTTON_PIN = D1;

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
Expand All @@ -17,9 +14,8 @@ const uint8_t BUTTON_PIN = D1;
#include "wifi-led.h"
#include "time.h"
#include "button.h"
#include "light.h"
#include "wakeup.h"
#include "htsensor.h"
WakeupLight light;
HTSensor htsensor;
#include "server.h"

Expand All @@ -33,7 +29,7 @@ void setup() {
Serial.println(F("ESP8266 begin"));

Settings.begin();
light.begin(LIGHT_PIN);
wakeup.begin(WAKEUP_PIN);
WifiLed.begin(D2); // D4 is the onboard LED of the ESP-12E
htsensor.setup();
wifi.setup();
Expand All @@ -54,22 +50,22 @@ void loop() {
static bool buttonWasPressed = false;

button.loop();
light.loop();
wakeup.loop();

bool buttonPressed = button.pressed();
if (buttonPressed && !buttonWasPressed) {
switch (light.currentState()) {
switch (wakeup.currentState()) {
case LIGHT_OFF:
log(F("button off -> on"));
light.on();
wakeup.on();
break;
case LIGHT_WAKE:
log(F("button wake -> on"));
light.on();
wakeup.on();
break;
case LIGHT_ON:
log(F("button on -> off"));
light.off();
wakeup.off();
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions htsensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ArduinoJson.h>

#include "DHT.h"
#include "mqtt.h"

const uint8_t HT_PIN = D3;
const uint32_t HT_INTERVAL = 60000; // 1 minute
Expand Down
2 changes: 1 addition & 1 deletion http-session.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ String httpSessionGenerateToken(uint32_t ts) {
String token(ts);
// Generate a MAC over the payload, with the given key.
uint8_t hashRaw[32];
if (blake2s(hashRaw, 32, token.c_str(), token.length(), SECRET, sizeof(SECRET))) {
if (blake2s(hashRaw, 32, token.c_str(), token.length(), SECRET, SECRET_SIZE)) {
return String(F("fail"));
}
String hash = base64::encode(hashRaw, 32);
Expand Down
157 changes: 0 additions & 157 deletions light.h

This file was deleted.

77 changes: 77 additions & 0 deletions mqtt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

#include <ArduinoJson.h>

#include "mqtt.h"
#include "config.h"
#include "colorlight.h"

WiFiClient mqttClient;
PubSubClient mqtt(MQTT_HOST, MQTT_PORT, mqttCallback, mqttClient);

const char *MQTT_MSG_ONLINE = "online";
const char *MQTT_MSG_OFFLINE = "offline";

bool mqttWasConnected = false;
uint32_t mqttLastTry = 0;

void log(String line) {
Serial.println(line);
#ifdef MQTT_LOG
line = String(F(CLIENT_ID ": ")) + line;
mqtt.publish(MQTT_LOG, line.c_str());
#endif
}

void mqttLoop() {
if (!mqtt.loop()) {
// Not connected, but connection available.
// Try to reconnect, with a second delay between each try.
uint32_t currentMillis = millis();
if (mqttLastTry == 0 || currentMillis - mqttLastTry > 5000) {
mqttLastTry = currentMillis;
if (mqtt.connect(CLIENT_ID, MQTT_LOG, 1, false, MQTT_MSG_OFFLINE)) {
mqtt.publish(MQTT_LOG, MQTT_MSG_ONLINE);
if (!mqtt.subscribe(MQTT_PREFIX "a/+", 1)) {
log(F("failed to subscribe"));
}
}
}
}
}

void mqttCallback(char *topic, byte *payload, unsigned int length) {
if (length < 1) {
// Odd packet
Serial.println("MQTT: got zero-length payload");
return;
}

// Turn it into a C string (needed for the JSON parser).
char json[length+1];
json[length] = 0;
memcpy(json, payload, length);

DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
log(F("colorlight: could not parse JSON"));
return;
}
JsonObject& value = root["value"];
if (!value.success()) {
log(F("colorlight: no 'value' key"));
return;
}

const char *origin = root["origin"];
if (origin != NULL && strcmp(origin, CLIENT_ID) == 0) {
// Received my own message.
return;
}

if (strcmp(topic, MQTT_PREFIX "a/colorlight") == 0) {
colorLightReceive(value);
} else {
log(String("unknown actuator: ") + topic);
}
}
Loading

0 comments on commit 6dc713b

Please sign in to comment.