Last active
March 20, 2021 13:06
-
-
Save prohazko2/b008a5a90c273774243da266471d1a28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log("init"); | |
| const WIFI_SSID = "..."; | |
| const WIFI_PASS = "..."; | |
| const MQTT_CLIENT_ID = "..."; | |
| const MQTT_HOST = "dev.rightech.io"; | |
| let mqtt; | |
| function connectMqtt() { | |
| mqtt = require("MQTT").connect({ | |
| client_id: MQTT_CLIENT_ID, | |
| host: MQTT_HOST, | |
| }); | |
| mqtt.on("connected", () => { | |
| console.log("mqtt connected"); | |
| mqtt.publish("hello", JSON.stringify({ n: Date.now(), r: Math.random() })); | |
| }); | |
| mqtt.on("disconnected", () => { | |
| console.log("mqtt disconnected"); | |
| }); | |
| mqtt.on("publish", (pub) => { | |
| console.log(`publish received: ${pub.topic}: ${pub.message}`); | |
| }); | |
| mqtt.on("error", (message) => { | |
| console.log(`mqtt error: ${message}`); | |
| }); | |
| } | |
| require("Wifi").connect(WIFI_SSID, { password: WIFI_PASS }, (err) => { | |
| if (err) { | |
| console.log(`wifi error: ${err}`); | |
| return; | |
| } | |
| console.log("wifi connected"); | |
| connectMqtt(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.