Compare commits
18 Commits
abbf6a8546
...
ota-builds
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27e8d25fac | ||
|
|
e7b9398d04 | ||
|
|
bb087d10c4 | ||
|
|
2920809d23 | ||
|
|
ae5a9d572a | ||
|
|
27c323f1e1 | ||
|
|
e9511999e7 | ||
|
|
4c90c1075c | ||
| 94a6929fea | |||
| c382ae2888 | |||
| 7a493759ba | |||
| 5ccd15dde3 | |||
|
|
f7ab7f61a2 | ||
|
|
9a128704ff | ||
|
|
150a9aae20 | ||
|
|
2fb449522d | ||
|
|
79d440646d | ||
|
|
c23cf033de |
140
.gitea/workflows/ota-build.yml
Normal file
140
.gitea/workflows/ota-build.yml
Normal file
@@ -0,0 +1,140 @@
|
||||
name: OTA build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '**'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-ota:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.11"
|
||||
|
||||
- name: Install PlatformIO
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install platformio
|
||||
|
||||
- name: Build OTA firmware binaries
|
||||
run: pio run
|
||||
|
||||
- name: Prepare direct OTA files
|
||||
run: |
|
||||
set -eu
|
||||
rm -rf dist/ota
|
||||
project_name="${GITHUB_REPOSITORY#*/}"
|
||||
project_name="${project_name#PlatformIO_}"
|
||||
timestamp="$(date -u +%Y-%m-%dT%H-%M-%SZ)"
|
||||
mkdir -p dist/ota
|
||||
found=0
|
||||
for fw in .pio/build/*/firmware.bin .pio/build/*/firmware.hex; do
|
||||
[ -f "$fw" ] || continue
|
||||
env_name="$(basename "$(dirname "$fw")")"
|
||||
ext="${fw##*.}"
|
||||
cp "$fw" "dist/ota/${project_name}_${env_name}_${timestamp}.${ext}"
|
||||
found=1
|
||||
done
|
||||
if [ "$found" -eq 0 ]; then
|
||||
echo 'No firmware.bin or firmware.hex artifacts were produced.' >&2
|
||||
exit 1
|
||||
fi
|
||||
find dist/ota -maxdepth 1 -type f | sort
|
||||
|
||||
- name: Upload backup OTA artifact ZIP
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: ota-files
|
||||
path: dist/ota/*
|
||||
if-no-files-found: error
|
||||
|
||||
- name: Publish raw OTA files to rolling release
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: |
|
||||
python - <<'PY'
|
||||
import json
|
||||
import mimetypes
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from urllib.request import Request, urlopen
|
||||
from urllib.error import HTTPError
|
||||
|
||||
server = os.environ['GITHUB_SERVER_URL']
|
||||
repo = os.environ['GITHUB_REPOSITORY']
|
||||
token = os.environ.get('GITEA_TOKEN')
|
||||
if not token:
|
||||
print('GITEA_TOKEN missing', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
api = f"{server}/api/v1/repos/{repo}"
|
||||
headers = {
|
||||
'Authorization': f'token {token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': 'Hermes-Agent'
|
||||
}
|
||||
tag = 'ota-builds'
|
||||
release_name = 'OTA Builds'
|
||||
body = 'Rolling OTA files from the latest successful push build.'
|
||||
|
||||
def req(url, method='GET', data=None, extra_headers=None):
|
||||
h = dict(headers)
|
||||
if extra_headers:
|
||||
h.update(extra_headers)
|
||||
payload = None if data is None else json.dumps(data).encode()
|
||||
r = Request(url, data=payload, headers=h, method=method)
|
||||
with urlopen(r, timeout=120) as resp:
|
||||
body = resp.read()
|
||||
if not body:
|
||||
return None
|
||||
text = body.decode()
|
||||
try:
|
||||
return json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
return text
|
||||
|
||||
try:
|
||||
release = req(f"{api}/releases/tags/{tag}")
|
||||
except HTTPError as e:
|
||||
if e.code != 404:
|
||||
raise
|
||||
release = req(f"{api}/releases", method='POST', data={
|
||||
'tag_name': tag,
|
||||
'target': os.environ.get('GITHUB_SHA', ''),
|
||||
'name': release_name,
|
||||
'body': body,
|
||||
'draft': False,
|
||||
'prerelease': True,
|
||||
})
|
||||
|
||||
release_id = release['id']
|
||||
existing = req(f"{api}/releases/{release_id}/assets") or []
|
||||
for asset in existing:
|
||||
rid = asset['id']
|
||||
req(f"{api}/releases/{release_id}/assets/{rid}", method='DELETE')
|
||||
|
||||
for path in sorted(Path('dist/ota').glob('*')):
|
||||
if not path.is_file():
|
||||
continue
|
||||
mime = mimetypes.guess_type(path.name)[0] or 'application/octet-stream'
|
||||
upload_headers = {
|
||||
'Authorization': f'token {token}',
|
||||
'Content-Type': mime,
|
||||
'Accept': 'application/json',
|
||||
'User-Agent': 'Hermes-Agent'
|
||||
}
|
||||
url = f"{api}/releases/{release_id}/assets?name={path.name}"
|
||||
data = path.read_bytes()
|
||||
r = Request(url, data=data, headers=upload_headers, method='POST')
|
||||
with urlopen(r, timeout=240) as resp:
|
||||
print(resp.read().decode())
|
||||
PY
|
||||
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"CodeGPT.apiKey": "CodeGPT Plus Beta"
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
[env:esp32doit-devkit-v1]
|
||||
platform = espressif32
|
||||
board = esp32doit-devkit-v1
|
||||
monitor_speed = 115200
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
knolleary/PubSubClient@^2.8
|
||||
|
||||
115
src/main.cpp
115
src/main.cpp
@@ -6,8 +6,7 @@
|
||||
const int PIR_PIN=23;
|
||||
const char* ssid = "JoNelsDarlings";
|
||||
const char* password = "Paris2016";
|
||||
const char* mqtt_server = "mqtt.ddns.kawomi.com";
|
||||
|
||||
const char* mqttServerName = "mqtt.ddns.kawomi.com";
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient mqttClient(espClient);
|
||||
@@ -16,7 +15,6 @@ unsigned long lastMsg = 0;
|
||||
char msg[MSG_BUFFER_SIZE];
|
||||
|
||||
void setup_wifi() {
|
||||
|
||||
delay(10);
|
||||
// We start by connecting to a WiFi network
|
||||
Serial.println();
|
||||
@@ -39,37 +37,19 @@ void setup_wifi() {
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void send_mqtt(String status, long minutes=0) {
|
||||
Serial.print("MQTT Send status ");
|
||||
Serial.print(status);
|
||||
Serial.print(" mins ");
|
||||
Serial.println(minutes);
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!mqttClient.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Create a random client ID
|
||||
String clientId = "ESP32Client-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
// Attempt to connect
|
||||
if (mqttClient.connect(clientId.c_str())) {
|
||||
Serial.println("MQTT connected");
|
||||
// Once connected, publish an announcement...
|
||||
} else {
|
||||
Serial.print("MQTT failed, rc=");
|
||||
Serial.print(mqttClient.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void send_mqtt(String value) {
|
||||
const size_t CAPACITY = JSON_OBJECT_SIZE(1);
|
||||
StaticJsonDocument<CAPACITY> doc;
|
||||
|
||||
// create an object
|
||||
JsonDocument doc;
|
||||
JsonObject object = doc.to<JsonObject>();
|
||||
object["status"] = value.c_str();
|
||||
object["status"] = status.c_str();
|
||||
if (minutes > 0) {
|
||||
object["minutes"] = minutes;
|
||||
}
|
||||
|
||||
// serialize the object and send the result to Serial
|
||||
static char sensorStatus[150];
|
||||
serializeJson(doc, sensorStatus);
|
||||
Serial.println(sensorStatus);
|
||||
@@ -77,28 +57,89 @@ void send_mqtt(String value) {
|
||||
mqttClient.publish("Address/Livorno/Kitchen/PIR", sensorStatus, true);
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
int numRetries=0;
|
||||
|
||||
while (!mqttClient.connected()) {
|
||||
numRetries++;
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
|
||||
// Create a random client ID
|
||||
String clientId = "ESP32Client-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
|
||||
if (mqttClient.connect(clientId.c_str())) {
|
||||
Serial.println("MQTT connected");
|
||||
} else {
|
||||
Serial.print("MQTT failed, rc=");
|
||||
Serial.print(mqttClient.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
|
||||
if (numRetries>5){
|
||||
Serial.println("Too many retries, restarting the ESP...");
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
send_mqtt("ready");
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN,OUTPUT);
|
||||
pinMode(PIR_PIN,INPUT);
|
||||
pinMode(PIR_PIN,INPUT_PULLUP);
|
||||
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
|
||||
const int mqttPort=1883;
|
||||
mqttClient.setServer(mqttServerName, mqttPort);
|
||||
}
|
||||
|
||||
const int millisPerSecond=1000;
|
||||
long previousMotionMillis=millis();
|
||||
short previousState=-1;
|
||||
long previousNoMoNotifyMinutes=0;
|
||||
|
||||
void loop() {
|
||||
if (!mqttClient.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
mqttClient.loop();
|
||||
|
||||
int pir = digitalRead(PIR_PIN);
|
||||
if (pir == HIGH) {
|
||||
short currentState = digitalRead(PIR_PIN);
|
||||
// This out pin triggering is dependent not only on movement, but also on light levels which can be configured in the app.
|
||||
// I set it to 255 and "below" - this triggered it for me.....
|
||||
//send_mqtt("occupancy_state_debug", currentState);
|
||||
|
||||
// When occupancy is detected, reset the timer
|
||||
if (currentState == HIGH) {
|
||||
previousMotionMillis=millis();
|
||||
}
|
||||
|
||||
// Send messages only on status change, we don't want to flood the network...
|
||||
if (currentState != previousState) {
|
||||
Serial.print("Status changed; currentState: ");
|
||||
Serial.print(currentState);
|
||||
Serial.print(" previousState: ");
|
||||
Serial.println(previousState);
|
||||
if (currentState == HIGH) {
|
||||
send_mqtt("motion");
|
||||
digitalWrite(LED_BUILTIN,HIGH);
|
||||
delay(1000);
|
||||
} else {
|
||||
send_mqtt("no_motion_immediate");
|
||||
digitalWrite(LED_BUILTIN,LOW);
|
||||
}
|
||||
previousState = currentState;
|
||||
}
|
||||
|
||||
// Send no_motion messages every minute, indicating how long we didn't see occupancy
|
||||
const int secondsPerMinute=60;
|
||||
long minutesSinceLastMotion=(millis()-previousMotionMillis)/(millisPerSecond*secondsPerMinute);
|
||||
if ((previousNoMoNotifyMinutes != minutesSinceLastMotion) && (0 != minutesSinceLastMotion)) {
|
||||
send_mqtt("no_motion", minutesSinceLastMotion);
|
||||
previousNoMoNotifyMinutes = minutesSinceLastMotion;
|
||||
}
|
||||
|
||||
delay(50);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user