Last active
November 25, 2021 10:02
-
-
Save yostane/d91041fc9044b24a8b4354cc7ab077b8 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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Client mqtt</title> | |
| <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> | |
| <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script> | |
| <script> | |
| window.addEventListener("DOMContentLoaded", () => { | |
| const dps = []; | |
| const data = []; | |
| var updateInterval = 1000; | |
| const chart = new CanvasJS.Chart("chartContainer", { | |
| title: { | |
| text: "Dynamic Data" | |
| }, | |
| data: [{ | |
| type: "line", | |
| dataPoints: dps | |
| }] | |
| }); | |
| const updateChart = function (data) { | |
| dps.length = 0; | |
| for (var i = 0; i < data.length; i++) { | |
| dps.push({ | |
| x: i, | |
| y: data[i] | |
| }); | |
| } | |
| chart.render(); | |
| }; | |
| updateChart(data); | |
| const client = mqtt.connect('wss://xxxx:xxxx@xxxx.cloud.shiftr.io', { | |
| clientId: 'xxxxx' | |
| }); | |
| client.on('connect', function () { | |
| console.log('connected!'); | |
| client.subscribe('game_room/ps4'); | |
| }); | |
| client.on('message', function (topic, message) { | |
| console.log(topic + ': ' + message.toString()); | |
| // le message a le format suivant: {"temp": nombre} | |
| payload = JSON.parse(message); | |
| data.push(payload.temp); | |
| updateChart(data); | |
| }); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>Client MQTT</h1> | |
| <div id="chartContainer" style="height: 300px; width: 100%;"></div> | |
| </body> | |
| </html> |
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
| import paho.mqtt.client as mqtt | |
| import time | |
| def on_connect_mqtt(client, userdata, flags, rc): | |
| print(f"rc : {rc}, {userdata}") | |
| client.publish("chambre1/ordi", "Ah !") | |
| res = client.subscribe("chambre1/tv") | |
| print(f"subscribe res : {res}") | |
| res = client.subscribe("chambre1/tv2") | |
| print(f"subscribe res : {res}") | |
| def on_subscribe_mqtt(client, obj, mid, granted_qos): | |
| print(f"Subscribed: {mid} - qos {granted_qos}") | |
| def on_message_mqtt(client, obj, msg): | |
| print(f"{msg.topic} - {msg.qos} - payload {msg.payload}") | |
| def on_publish_mqtt(client, obj, mid): | |
| print(f"client: {client} - obj: {obj} - mid: {mid}") | |
| mqttClient = mqtt.Client() | |
| mqttClient.on_connect = on_connect_mqtt | |
| mqttClient.on_publish = on_publish_mqtt | |
| mqttClient.on_subscribe = on_subscribe_mqtt | |
| mqttClient.on_message = on_message_mqtt | |
| # mqttClient.username_pw_set(login, mot de passe) | |
| mqttClient.username_pw_set("xxxx", "xxxx") | |
| mqttClient.connect("xxxx.cloud.shiftr.io", 1883) | |
| # mqttClient.loop_forever() | |
| rc = mqtt.MQTT_ERR_SUCCESS | |
| reference_time = time.time() | |
| is_on = True | |
| while rc == mqtt.MQTT_ERR_SUCCESS: | |
| if time.time() - reference_time >= 3: | |
| if is_on: | |
| mqttClient.publish("game_room/ps4", "{'status': 'on'}") | |
| else: | |
| mqttClient.publish("game_room/ps4", "{'status': 'off'}") | |
| reference_time = time.time() | |
| is_on = not is_on | |
| rc = mqttClient.loop() | |
| mqttClient.disconnect() | |
| print("finished") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment