Compare commits
43 Commits
56f65b1a96
...
v.0.5.1
Author | SHA1 | Date | |
---|---|---|---|
d0f3845dc5 | |||
e9e0c17a8a | |||
2966170165 | |||
6be0baf301 | |||
cee415446a | |||
91a29db8ed | |||
7a37df2fff | |||
b5498b7173 | |||
0287456f1e | |||
019592f1ad | |||
c1c07a4003 | |||
|
cf380443a7 | ||
|
b8e68bed7c | ||
8387a0610b | |||
764b18e539 | |||
e5a64c13ed | |||
338c90fb8e | |||
85c57daf91 | |||
5cb5300d15 | |||
d5ffd87f90 | |||
0cff4d019a | |||
10b337d43f | |||
d19bd16561 | |||
36ce6f976b | |||
a4c2d49fb0 | |||
bad20a7f58 | |||
c00d2afb27 | |||
|
0ae3824e88 | ||
|
847dda81be | ||
|
dc91211889 | ||
58bcd731fc | |||
6efa5bd0b8 | |||
2a3bbc3628 | |||
|
e322b5ddcd | ||
795043a639 | |||
b0a485d578 | |||
3d4399c33d | |||
a33780a3a7 | |||
d2cd7f3445 | |||
b0f6691a7d | |||
d510cdd50e | |||
9b4dd040c1 | |||
53ed60151c |
23
.drone.yml
23
.drone.yml
@ -3,21 +3,30 @@ name: default
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: build
|
- name: build
|
||||||
image: python:2
|
image: python:3
|
||||||
commands:
|
commands:
|
||||||
- apt-get -y update && apt-get -y install git && pip install platformio
|
- pip install platformio
|
||||||
- pio run
|
- pio run
|
||||||
- name: upload
|
- name: upload
|
||||||
image: python:2
|
image: python:3
|
||||||
commands:
|
commands:
|
||||||
- make otaupload
|
- make otaupload
|
||||||
|
when:
|
||||||
|
event: tag
|
||||||
- name: release
|
- name: release
|
||||||
image: plugins/gitea-release
|
image: plugins/gitea-release
|
||||||
settings:
|
settings:
|
||||||
api_key: ef6a0baa30f8994719e236370290ac9f8bbbdb1d
|
api_key:
|
||||||
|
from_secret: DRONE_ACCESS_TOKEN
|
||||||
base_url: https://code.jcktrue.dk
|
base_url: https://code.jcktrue.dk
|
||||||
files:
|
files:
|
||||||
- .pioenvs/nodemcu/firmware.bin
|
- .pio/build/nodemcu/firmware.bin
|
||||||
- .pioenvs/nodemcu/firmware.elf
|
checksum:
|
||||||
|
- md5
|
||||||
|
- sha1
|
||||||
|
- sha256
|
||||||
|
- sha512
|
||||||
|
- adler32
|
||||||
|
- crc32
|
||||||
when:
|
when:
|
||||||
event: tag
|
event: tag
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,6 @@
|
|||||||
.pioenvs
|
.pioenvs
|
||||||
.piolibdeps
|
.piolibdeps
|
||||||
|
.pio
|
||||||
data/homie/config.json
|
data/homie/config.json
|
||||||
nbproject
|
nbproject
|
||||||
|
.vscode
|
||||||
|
41
Jenkinsfile
vendored
41
Jenkinsfile
vendored
@ -1,41 +0,0 @@
|
|||||||
pipeline {
|
|
||||||
agent {
|
|
||||||
docker {
|
|
||||||
image 'debian:stretch-slim'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stages {
|
|
||||||
stage('Requirements') {
|
|
||||||
steps {
|
|
||||||
sh 'apt-get -y update && apt-get -y install git python-pip cloc && pip install platformio'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stage('Build') {
|
|
||||||
steps {
|
|
||||||
sh 'pio run'
|
|
||||||
}
|
|
||||||
post {
|
|
||||||
always {
|
|
||||||
archiveArtifacts '.pioenvs/nodemcu/firmware.bin, .pioenvs/nodemcu/firmware.elf'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stage('Coverage') {
|
|
||||||
steps {
|
|
||||||
sh 'cloc --by-file --xml --out=.test/cloc.xml src include'
|
|
||||||
}
|
|
||||||
post {
|
|
||||||
always {
|
|
||||||
recordIssues(tools: [taskScanner(ignoreCase: true, includePattern: 'src/*.*', normalTags: 'TODO')])
|
|
||||||
sloccountPublish encoding: '', pattern: '.test/cloc.xml'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stage('Upload') {
|
|
||||||
steps {
|
|
||||||
sh 'make otaupload'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
4
Makefile
4
Makefile
@ -2,5 +2,5 @@ build:
|
|||||||
pio run
|
pio run
|
||||||
|
|
||||||
otaupload:
|
otaupload:
|
||||||
pip install -r .piolibdeps/Homie/scripts/ota_updater/requirements.txt
|
pip install -r .pio/libdeps/nodemcu/Homie/scripts/ota_updater/requirements.txt
|
||||||
python .piolibdeps/Homie/scripts/ota_updater/ota_updater.py -l mqtt.jcktrue.dk -i 18fe34f2f987 .pioenvs/nodemcu/firmware.bin
|
python .pio/libdeps/nodemcu/Homie/scripts/ota_updater/ota_updater.py -l mqtt.jcktrue.dk -i 18fe34f2f987 .pio/build/nodemcu/firmware.bin
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
|
[![Build Status](https://build.jcktrue.dk/api/badges/jct/deskcontrol/status.svg)](https://build.jcktrue.dk/jct/deskcontrol)
|
||||||
# README #
|
# README #
|
||||||
edit data/homie/config.json
|
* edit data/homie/config.json
|
||||||
platformio run -t upload
|
* platformio run -t upload
|
||||||
platformio run -t uploadfs
|
* platformio run -t uploadfs
|
15
data/homie/config.example.json
Normal file
15
data/homie/config.example.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"name": "deskcontrol",
|
||||||
|
"wifi": {
|
||||||
|
"ssid": "<SSID>",
|
||||||
|
"password": "<PASS>"
|
||||||
|
},
|
||||||
|
"mqtt": {
|
||||||
|
"host": "<Domain>",
|
||||||
|
"port": 1883,
|
||||||
|
"auth": false
|
||||||
|
},
|
||||||
|
"ota": {
|
||||||
|
"enabled": true
|
||||||
|
}
|
||||||
|
}
|
15
include/config.h
Normal file
15
include/config.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef CONFIG_H
|
||||||
|
#define CONFIG_H
|
||||||
|
|
||||||
|
#ifndef VERSION
|
||||||
|
//SEMVER style version
|
||||||
|
#define VERSION "0.4.5-Dev"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CONFIG_SERIAL_BAUDRATE 115200
|
||||||
|
|
||||||
|
#define CONFIG_IO_RELAY D1
|
||||||
|
#define CONFIG_IO_DHT11 D2
|
||||||
|
|
||||||
|
#define CONFIG_TEMPERATURE_SEND_INTERVAL 60
|
||||||
|
#endif
|
@ -1,27 +1,12 @@
|
|||||||
#
|
[env]
|
||||||
# Project Configuration File
|
|
||||||
#
|
|
||||||
# A detailed documentation with the EXAMPLES is located here:
|
|
||||||
# http://docs.platformio.org/en/latest/projectconf.html
|
|
||||||
#
|
|
||||||
|
|
||||||
# A sign `#` at the beginning of the line indicates a comment
|
|
||||||
# Comment lines are ignored.
|
|
||||||
|
|
||||||
# Simple and base environment
|
|
||||||
# [env:mybaseenv]
|
|
||||||
# platform = %INSTALLED_PLATFORM_NAME_HERE%
|
|
||||||
# framework =
|
|
||||||
# board =
|
|
||||||
#
|
|
||||||
# Automatic targets - enable auto-uploading
|
|
||||||
# targets = upload
|
|
||||||
|
|
||||||
[env:nodemcu]
|
|
||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board = nodemcu
|
|
||||||
upload_speed=921600
|
upload_speed=921600
|
||||||
|
|
||||||
build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
|
build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY -DVERSION=${sysenv.DRONE_SEMVER}
|
||||||
lib_deps = https://github.com/homieiot/homie-esp8266.git#develop, Syslog
|
lib_deps = https://github.com/homieiot/homie-esp8266.git#develop, DHT sensor library for ESPx, HomieLoggerNode
|
||||||
|
|
||||||
|
[env:nodemcu]
|
||||||
|
board = nodemcu
|
||||||
|
|
||||||
|
194
src/main.cpp
194
src/main.cpp
@ -1,116 +1,136 @@
|
|||||||
|
#include "config.h"
|
||||||
|
|
||||||
#include <Homie.h>
|
#include <Homie.h>
|
||||||
#include <Syslog.h>
|
|
||||||
#include <WiFiUdp.h>
|
|
||||||
WiFiUDP udpClient;
|
|
||||||
Syslog syslog(udpClient, SYSLOG_PROTO_IETF);
|
|
||||||
|
|
||||||
const int PIN_RELAY = D1;
|
#include <LoggerNode.h>
|
||||||
int timer = 0;
|
LoggerNode LN;
|
||||||
HomieNode powerNode("power", "switch");
|
|
||||||
|
#include "DHTesp.h"
|
||||||
|
DHTesp dht;
|
||||||
|
|
||||||
|
|
||||||
|
#define OUTPUT_SET(x) digitalWrite(CONFIG_IO_RELAY, x ? LOW : HIGH)
|
||||||
|
unsigned int timer = 0;
|
||||||
|
unsigned int next_timer_update = 0;
|
||||||
|
HomieNode powerNode("power", "Power", "switch");
|
||||||
|
HomieNode temperatureNode("temperature", "Temperature", "temperature");
|
||||||
|
|
||||||
bool powerOnHandler(const HomieRange& range, const String& value) {
|
bool powerStateHandler(const HomieRange& range, const String& value)
|
||||||
if (value != "true" && value != "false") return false;
|
{
|
||||||
bool on = (value == "true");
|
if (value != "on" && value != "off")
|
||||||
digitalWrite(PIN_RELAY, on ? LOW : HIGH);
|
{
|
||||||
powerNode.setProperty("on").send(value);
|
return false;
|
||||||
Homie.getLogger() << "Power is " << (on ? "on" : "off") << endl;
|
}
|
||||||
|
powerNode.setProperty("state").send(value);
|
||||||
|
powerNode.setProperty("timer").send("0");
|
||||||
|
timer = 0;
|
||||||
|
|
||||||
|
if(value == "on")
|
||||||
|
{
|
||||||
|
OUTPUT_SET(true);
|
||||||
|
LN.logf(__PRETTY_FUNCTION__, LoggerNode::INFO, "Power ON");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OUTPUT_SET(false); LN.logf(__PRETTY_FUNCTION__, LoggerNode::INFO, "Power OFF");
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupHandler() {
|
bool powerTimerHandler(const HomieRange& range, const String& value)
|
||||||
pinMode(PIN_RELAY, OUTPUT);
|
{
|
||||||
digitalWrite(PIN_RELAY, HIGH);
|
int settimer = value.toInt();
|
||||||
|
if(settimer == 0 || settimer > 120)
|
||||||
|
return false;
|
||||||
|
|
||||||
powerNode.setProperty("on").send("false");
|
timer = millis() + (settimer * 1000 * 60);
|
||||||
|
next_timer_update = millis() + 60*1000;
|
||||||
|
powerNode.setProperty("timer").send(String(settimer));
|
||||||
|
powerNode.setProperty("state").send("on");
|
||||||
|
LN.logf(__PRETTY_FUNCTION__, LoggerNode::INFO, "Power on for %d minutes", settimer);
|
||||||
|
OUTPUT_SET(true);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onHomieEvent(const HomieEvent& event) {
|
|
||||||
switch(event.type) {
|
|
||||||
case HomieEventType::STANDALONE_MODE:
|
|
||||||
// Do whatever you want when standalone mode is started
|
|
||||||
break;
|
|
||||||
case HomieEventType::CONFIGURATION_MODE:
|
|
||||||
// Do whatever you want when configuration mode is started
|
|
||||||
break;
|
|
||||||
case HomieEventType::NORMAL_MODE:
|
|
||||||
// Do whatever you want when normal mode is started
|
|
||||||
break;
|
|
||||||
case HomieEventType::OTA_STARTED:
|
|
||||||
// Do whatever you want when OTA is started
|
|
||||||
break;
|
|
||||||
case HomieEventType::OTA_PROGRESS:
|
|
||||||
// Do whatever you want when OTA is in progress
|
|
||||||
|
|
||||||
// You can use event.sizeDone and event.sizeTotal
|
void setupHandler()
|
||||||
break;
|
{
|
||||||
case HomieEventType::OTA_FAILED:
|
|
||||||
// Do whatever you want when OTA is failed
|
|
||||||
break;
|
|
||||||
case HomieEventType::OTA_SUCCESSFUL:
|
|
||||||
// Do whatever you want when OTA is successful
|
|
||||||
break;
|
|
||||||
case HomieEventType::ABOUT_TO_RESET:
|
|
||||||
// Do whatever you want when the device is about to reset
|
|
||||||
break;
|
|
||||||
case HomieEventType::WIFI_CONNECTED:
|
|
||||||
// Do whatever you want when Wi-Fi is connected in normal mode
|
|
||||||
syslog.deviceHostname(Homie.getConfiguration().name);
|
|
||||||
syslog.appName("deskcontrol");
|
|
||||||
syslog.defaultPriority(LOG_ERR);
|
|
||||||
syslog.log(LOG_INFO, "WiFi Connected, software version: 1.0");
|
|
||||||
// You can use event.ip, event.gateway, event.mask
|
|
||||||
break;
|
|
||||||
case HomieEventType::WIFI_DISCONNECTED:
|
|
||||||
// Do whatever you want when Wi-Fi is disconnected in normal mode
|
|
||||||
|
|
||||||
// You can use event.wifiReason
|
pinMode(CONFIG_IO_RELAY, OUTPUT);
|
||||||
break;
|
OUTPUT_SET(false);
|
||||||
case HomieEventType::MQTT_READY:
|
powerNode.setProperty("state").send("off");
|
||||||
// Do whatever you want when MQTT is connected in normal mode
|
powerNode.setProperty("timer").send("0");
|
||||||
syslog.log(LOG_INFO, "MQTT connected");
|
|
||||||
break;
|
|
||||||
case HomieEventType::MQTT_DISCONNECTED:
|
|
||||||
// Do whatever you want when MQTT is disconnected in normal mode
|
|
||||||
syslog.log(LOG_INFO, "MQTT disconnected");
|
|
||||||
// You can use event.mqttReason
|
|
||||||
break;
|
|
||||||
case HomieEventType::MQTT_PACKET_ACKNOWLEDGED:
|
|
||||||
// Do whatever you want when an MQTT packet with QoS > 0 is acknowledged by the broker
|
|
||||||
|
|
||||||
// You can use event.packetId
|
dht.setup(CONFIG_IO_DHT11, DHTesp::DHT11); // Connect DHT sensor to GPIO 17
|
||||||
break;
|
}
|
||||||
case HomieEventType::READY_TO_SLEEP:
|
|
||||||
// After you've called `prepareToSleep()`, the event is triggered when MQTT is disconnected
|
void loopHandleTimer()
|
||||||
break;
|
{
|
||||||
case HomieEventType::SENDING_STATISTICS:
|
if(timer)
|
||||||
// Do whatever you want when statistics are sent in normal mode
|
{
|
||||||
break;
|
if(timer < millis())
|
||||||
|
{ //Timer has expired. (Ignore the wraparound...)
|
||||||
|
timer = 0;
|
||||||
|
OUTPUT_SET(false);
|
||||||
|
powerNode.setProperty("timer").send("0");
|
||||||
|
powerNode.setProperty("state").send("off");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(next_timer_update && millis() > next_timer_update)
|
||||||
|
{
|
||||||
|
next_timer_update = millis() + 60*1000;
|
||||||
|
unsigned int time_remaining = timer - millis();
|
||||||
|
unsigned int min_left = time_remaining / (60*1000);
|
||||||
|
powerNode.setProperty("timer").send(String(min_left));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void loopHandleTemperature()
|
||||||
syslog.server("192.168.1.100", 514);
|
{
|
||||||
|
static unsigned long lastTemperatureSent = 0;
|
||||||
|
if (millis() - lastTemperatureSent >= CONFIG_TEMPERATURE_SEND_INTERVAL * 1000UL || lastTemperatureSent == 0)
|
||||||
|
{
|
||||||
|
float humidity = dht.getHumidity();
|
||||||
|
float temperature = dht.getTemperature();
|
||||||
|
LN.logf(__PRETTY_FUNCTION__, LoggerNode::DEBUG, "Temperature: %f C Humidity: %f", temperature, humidity);
|
||||||
|
temperatureNode.setProperty("temperature").send(String(temperature));
|
||||||
|
temperatureNode.setProperty("humidity").send(String(humidity));
|
||||||
|
lastTemperatureSent = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Serial.begin(115200);
|
void loopHandler()
|
||||||
|
{
|
||||||
|
loopHandleTimer();
|
||||||
|
loopHandleTemperature();
|
||||||
|
}
|
||||||
|
|
||||||
Serial << endl << endl;
|
void setup()
|
||||||
Serial << "Firmware 0.0.1" << endl;
|
{
|
||||||
Serial << endl << endl;
|
|
||||||
|
|
||||||
Homie_setFirmware("deskcontrol", "1.0.0");
|
Homie_setFirmware("deskcontrol", VERSION);
|
||||||
Homie_setBrand("FuryFire");
|
Homie.setLoggingPrinter(&Serial);
|
||||||
|
Serial.begin(CONFIG_SERIAL_BAUDRATE);
|
||||||
|
|
||||||
powerNode.advertise("on").settable(powerOnHandler);
|
Homie.disableResetTrigger();
|
||||||
|
|
||||||
|
//Power
|
||||||
|
powerNode.advertise("state").settable(powerStateHandler);
|
||||||
|
powerNode.advertise("timer").settable(powerTimerHandler);
|
||||||
|
|
||||||
|
//Temperature
|
||||||
|
temperatureNode.advertise("humidity");
|
||||||
|
temperatureNode.advertise("temperature");
|
||||||
|
|
||||||
Homie.setSetupFunction(setupHandler);
|
Homie.setSetupFunction(setupHandler);
|
||||||
Homie.onEvent(onHomieEvent); // before Homie.setup()
|
Homie.setLoopFunction(loopHandler);
|
||||||
|
|
||||||
Homie.setup();
|
Homie.setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop()
|
||||||
|
{
|
||||||
Homie.loop();
|
Homie.loop();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user