add support for IR commands and publish them on MQTT
This commit is contained in:
parent
8f6f100fbb
commit
bab484f357
@ -1,17 +1,20 @@
|
||||
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
|
||||
#include <Adafruit_GFX.h> // Adafruit's graphics library
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
#include <SPI.h>
|
||||
#include "ntp_time.h"
|
||||
|
||||
#define DISPLAY_ADDRESS 0x70
|
||||
#define BRIGHTNESS 1
|
||||
|
||||
uint8_t brightness = BRIGHTNESS;
|
||||
|
||||
// Create display object
|
||||
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
||||
|
||||
void setupDisplay() {
|
||||
clockDisplay.begin(DISPLAY_ADDRESS);
|
||||
clockDisplay.setBrightness(BRIGHTNESS);
|
||||
clockDisplay.setBrightness(brightness);
|
||||
}
|
||||
|
||||
void displayTime() {
|
||||
@ -33,7 +36,21 @@ void displayTime() {
|
||||
}
|
||||
}
|
||||
|
||||
void changeBrightness(bool increase) {
|
||||
increase ? brightness = (brightness+2) % 15 : brightness = (brightness-2) % 15;
|
||||
}
|
||||
|
||||
void adjustBrightness() {
|
||||
int currentHour = hour();
|
||||
if (currentHour > 9 && currentHour < 17) {
|
||||
brightness = 11;
|
||||
} else {
|
||||
brightness = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void displayColon(bool on) {
|
||||
clockDisplay.setBrightness(brightness);
|
||||
clockDisplay.drawColon(on);
|
||||
clockDisplay.writeDisplay();
|
||||
}
|
||||
|
||||
57
include/ir.h
Normal file
57
include/ir.h
Normal file
@ -0,0 +1,57 @@
|
||||
#include <queue>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <IRremoteESP8266.h>
|
||||
#include <IRrecv.h>
|
||||
|
||||
#define IR_INPUT_PIN 12
|
||||
|
||||
#define MQTT_HOST IPAddress(192, 168, 5, 138)
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
IRrecv irrecv(IR_INPUT_PIN);
|
||||
decode_results results;
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
|
||||
std::queue<uint8_t> irCommands;
|
||||
uint8_t lastIrCommand = 0x9F;
|
||||
|
||||
void setupIr() {
|
||||
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
||||
Serial.println("Connecting to MQTT...");
|
||||
mqttClient.connect();
|
||||
|
||||
irrecv.enableIRIn(); // Start the receiver
|
||||
}
|
||||
|
||||
void sendCommand() {
|
||||
if (!irCommands.empty() && mqttClient.connected()) {
|
||||
char message[32];
|
||||
sprintf(message, "%X", irCommands.front());
|
||||
if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) {
|
||||
Serial.print(irCommands.front(), HEX);
|
||||
Serial.println();
|
||||
irCommands.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool readIrCommand() {
|
||||
bool newCommand = false;
|
||||
if (irrecv.decode(&results)) {
|
||||
if (results.decode_type == NEC) {
|
||||
Serial.print(F(" C=0x"));
|
||||
Serial.print(results.command, HEX);
|
||||
Serial.println();
|
||||
lastIrCommand = results.command;
|
||||
irCommands.push(results.command);
|
||||
newCommand = true;
|
||||
}
|
||||
irrecv.resume(); // Receive the next value
|
||||
}
|
||||
return newCommand;
|
||||
}
|
||||
|
||||
uint8_t getCurrentCommand() {
|
||||
return irCommands.empty() ? 0 : irCommands.front();
|
||||
}
|
||||
@ -11,7 +11,8 @@ private:
|
||||
public:
|
||||
void setup() {
|
||||
ESP8266WiFiMulti wifiMulti;
|
||||
wifiMulti.addAP("IoT", "***REMOVED***");
|
||||
wifiMulti.addAP("OpenWrt", "***REMOVED***");
|
||||
// wifiMulti.addAP("IoT", "***REMOVED***");
|
||||
wifiMulti.addAP("Miracle", "***REMOVED***");
|
||||
|
||||
Serial.println("Connecting to WiFi netowrk.");
|
||||
|
||||
@ -15,12 +15,14 @@ framework = arduino
|
||||
lib_deps =
|
||||
arduino-libraries/NTPClient@^3.1.0
|
||||
adafruit/Adafruit LED Backpack Library@^1.1.8
|
||||
adafruit/Adafruit GFX Library@^1.10.7
|
||||
adafruit/Adafruit BusIO@^1.6.0
|
||||
jchristensen/Timezone@^1.2.4
|
||||
ottowinter/AsyncMqttClient-esphome@^0.8.5
|
||||
crankyoldgit/IRremoteESP8266@^2.7.18
|
||||
|
||||
[env:laptop_home]
|
||||
upload_port = /dev/ttyUSB0
|
||||
|
||||
[env:ota_home]
|
||||
upload_port = 192.168.6.191
|
||||
upload_port = 192.168.5.191
|
||||
upload_protocol = espota
|
||||
|
||||
@ -3,29 +3,67 @@
|
||||
#include "ota.h"
|
||||
#include "ntp_time.h"
|
||||
#include "display.h"
|
||||
#include "ir.h"
|
||||
|
||||
#define STAY_CONNECTED_AFTER_BOOT 1*60
|
||||
|
||||
int currentHour = -1;
|
||||
time_t timeAtStartup;
|
||||
bool avrOn = false;
|
||||
|
||||
Wifi wifi;
|
||||
|
||||
void setup() {
|
||||
setupDisplay();
|
||||
displayTime();
|
||||
displayColon(false);
|
||||
|
||||
Serial.begin(9600); // Start the serial console
|
||||
|
||||
wifi.setup();
|
||||
|
||||
setupOTA();
|
||||
|
||||
setupDisplay();
|
||||
|
||||
setupTime();
|
||||
timeAtStartup = updateTime();
|
||||
adjustBrightness();
|
||||
|
||||
setupIr();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
void loop() {
|
||||
if (readIrCommand()) {
|
||||
switch (lastIrCommand)
|
||||
{
|
||||
case 0x9F:
|
||||
avrOn = false;
|
||||
timeAtStartup = now();
|
||||
break;
|
||||
case 0xC4:
|
||||
case 0xD0:
|
||||
case 0xC0:
|
||||
avrOn = true;
|
||||
if (WiFi.status() == WL_DISCONNECTED) {
|
||||
wifi.reconnect();
|
||||
// connect on wifi connected
|
||||
mqttClient.connect();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!avrOn && getCurrentCommand() == 0xC7) {
|
||||
changeBrightness(true);
|
||||
irCommands.pop();
|
||||
}
|
||||
if (!avrOn && getCurrentCommand() == 0xC8) {
|
||||
changeBrightness(false);
|
||||
irCommands.pop();
|
||||
}
|
||||
sendCommand();
|
||||
if ((currentHour != hour())) {
|
||||
adjustBrightness();
|
||||
wifi.reconnect();
|
||||
wifi.printStatus();
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
@ -35,7 +73,7 @@ void loop() {
|
||||
}
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
ArduinoOTA.handle();
|
||||
if (difftime(now(), timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) {
|
||||
if ((difftime(now(), timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !avrOn) {
|
||||
wifi.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user