Operácie

Protokol MQTT: Rozdiel medzi revíziami

Zo stránky SensorWiki

Balogh (diskusia | príspevky)
Vytvorená stránka „== MQTT cez Shiftr.IO == <source lang="cpp"> import mqtt.*; MQTTClient client; void setup() { client = new MQTTClient(this); client.connect("mqtt://try:try@broke…“
 
Balogh (diskusia | príspevky)
Riadok 1: Riadok 1:
'''Úloha'''
Vyskúšajte si posielanie protokolom MQTT.
Pošlite jednoduchú správu
* Najprv na
** MQTT server mqtt://try:try@broker.shiftr.io (username vase priezvisko)
** do topicu /feistu/misa/2020/XXX
* a potom na
** MQTT server mqtt://9RYd7rPhakMm9CCwPBJG@demo.thingsboard.io
** do topicu v1/devices/me/telemetry
Správa vo formáte JSON má vyzerať takto:
{"XXX-Lat": 49.1634, "XXX-Lon": 20.1349, "XXX-Temp": 18.2}
kde
* XXX su prve tri pismena vasho priezviska
* Lat je zemepisna sirka na styri desatinne miesta
* Lon je zemepisna dlzka na styri desatinne miesta a
* Temp aktualna vonkajsia teplota
V Processingu treba pridat kniznicu Joela Gaehwillera MQTT:
Sketch -> Import Library -> Add Library -> MQTT -> Install
  payload.setFloat("XXX-Lat", 49.1634);
  ...
  client.connect("mqtt://try:try@broker.shiftr.io", "Ferdinand");
  ...
  client.publish("/feistu/misa/2020/XXX", "{\"XXX-Lat\": 49.1634, \"XXX-Lon\": 20.1349, \"XXX-Temp\": 18.2}");
  client.publish("/feistu/misa/2020/XXX", payload.toString());
 
ALEBO
 
  client.connect("mqtt://9RYd7rPhakMm9CCwPBJG@demo.thingsboard.io");
  ...
  client.publish("v1/devices/me/telemetry", payload.toString());
 
 
== MQTT cez Shiftr.IO ==
== MQTT cez Shiftr.IO ==


Riadok 20: Riadok 62:
   client.publish("/feistu/BlokB/humidity", "38.1");
   client.publish("/feistu/BlokB/humidity", "38.1");
   client.publish("/feistu/BlokA", "InReconstruction...");
   client.publish("/feistu/BlokA", "InReconstruction...");
  // client.publish("/feistu/misa/2020/XXX", "{\"XXX-Lat\": 49.1634, \"XXX-Lon\": 20.1349, \"XXX-Temp\": 18.2}");
   
 
}
}


Riadok 41: Riadok 82:
}
}
</source>
</source>


== JSON ==
== JSON ==

Verzia z 08:03, 27. apríl 2020

Úloha

Vyskúšajte si posielanie protokolom MQTT. Pošlite jednoduchú správu

  • Najprv na
    • MQTT server mqtt://try:try@broker.shiftr.io (username vase priezvisko)
    • do topicu /feistu/misa/2020/XXX
  • a potom na
    • MQTT server mqtt://9RYd7rPhakMm9CCwPBJG@demo.thingsboard.io
    • do topicu v1/devices/me/telemetry

Správa vo formáte JSON má vyzerať takto:

{"XXX-Lat": 49.1634, "XXX-Lon": 20.1349, "XXX-Temp": 18.2}

kde

  • XXX su prve tri pismena vasho priezviska
  • Lat je zemepisna sirka na styri desatinne miesta
  • Lon je zemepisna dlzka na styri desatinne miesta a
  • Temp aktualna vonkajsia teplota



V Processingu treba pridat kniznicu Joela Gaehwillera MQTT:

Sketch -> Import Library -> Add Library -> MQTT -> Install


 payload.setFloat("XXX-Lat", 49.1634);
 ...
 client.connect("mqtt://try:try@broker.shiftr.io", "Ferdinand");
 ...
 client.publish("/feistu/misa/2020/XXX", "{\"XXX-Lat\": 49.1634, \"XXX-Lon\": 20.1349, \"XXX-Temp\": 18.2}");
 client.publish("/feistu/misa/2020/XXX", payload.toString());
 
ALEBO 
 
 client.connect("mqtt://9RYd7rPhakMm9CCwPBJG@demo.thingsboard.io");
 ...
 client.publish("v1/devices/me/telemetry", payload.toString());
 
 

MQTT cez Shiftr.IO

import mqtt.*;

MQTTClient client;

void setup() 
{
  client = new MQTTClient(this);
  client.connect("mqtt://try:try@broker.shiftr.io", "Ferdinand");
}

void draw() { /* nerob nic, len cakaj na klavesu */}


void keyPressed() 
{
  client.publish("/feistu/BlokB/temperature", "27.0");
  client.publish("/feistu/BlokB/humidity", "38.1");
  client.publish("/feistu/BlokA", "InReconstruction...");
 
}


void clientConnected() {
  println("client connected");
  client.subscribe("/feistu/#");
  client.subscribe("/hello/#");
}


void messageReceived(String topic, byte[] payload) {
  println("new message: " + topic + " - " + new String(payload));
}


void connectionLost() {
  println("connection lost");
}

JSON

JSONObject message;

float temperature = 0.0;
int status = 0;

void setup() 
{
  message = new JSONObject();
  message.setFloat("temperature", 10.0);
  message.setInt("state",2);
  message.setString("name", "Lion");

  saveJSONObject(message, "data/new.json");

  int aktualnyStav = message.getInt("state");
  float aktualnaTeplota = message.getFloat("temperature");
  String realName = message.getString("name");

  println("Stav: " + aktualnyStav 
  + ", Teplota: " + aktualnaTeplota + ",  Meno: " + realName);
}

void draw() { /* nic nekreslime */ }
   
void keyPressed() {
  temperature = random(-10, 32.5);
  message.setFloat("temperature", temperature);
  println(message.toString()); 
}