Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish on MQTT #17

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ ENABLE_BUTTONS=
TELEGRAM_TOKEN=
TELEGRAM_CHATS=
OPENAI_API_KEY=
TRIGGER_SENSOR_HOST=
TRIGGER_SENSOR_HOST=
MQTT_HOST=
MQTT_USERNAME=
MQTT_PASSWORD=
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ sudo yarn collect &
sudo yarn manager &
```

### Output generated example
## Outputs

Developed to provide updated data in 4 different stacks:

- Publish to external REST/HTTP
- Publish to MQTT
- Expose with a REST/HTTP
- Send with Socket.IO

### Output managed example

#### Realtime data

Expand Down Expand Up @@ -131,4 +140,4 @@ https://api.giona.tech/domotica/battery

<img src="res/img/dashboard.png" alt="web app" width="100%" />

https://sacconazzo.altervista.org/#battery
https://app.giona.tech/#battery
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpio",
"version": "1.1.3",
"version": "1.2.0",
"description": "rpi battery signals",
"scripts": {
"db:prepare": "npx knex migrate:latest",
Expand All @@ -18,6 +18,7 @@
"dotenv": "^16.4.5",
"express": "^4.19.2",
"knex": "0.15.2",
"mqtt": "^5.10.1",
"mysql": "^2.18.1",
"node-cron": "^3.0.3",
"openai": "^4.49.0",
Expand Down
10 changes: 10 additions & 0 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Gpio = require("pigpio").Gpio;
const http = require("http");
const app = require("express")();
const { Server } = require("socket.io");
const mqtt = require("mqtt");
const { exec, execSync } = require("child_process");
const db = require("./db");
const telegram = require("./telegram");
Expand Down Expand Up @@ -56,6 +57,10 @@ const buttonShutDown = new Gpio(pinShutDown, {
const server = http.createServer(app);
const io = new Server(server);
server.listen(3000);
const mqttClient = mqtt.connect(process.env.MQTT_HOST, {
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
});

const share = async () => {
pwmShare.hardwarePwmWrite(frequency, 20000);
Expand Down Expand Up @@ -90,6 +95,11 @@ const share = async () => {

io.emit("data", app._data);

mqttClient.publish("data", JSON.stringify(app._data), {
qos: 1,
retain: true,
});

console.log("stored data");

pulseConnection.servoWrite(pulseWidth);
Expand Down
Loading