Operácie

Thingsboard.io: Rozdiel medzi revíziami

Z SensorWiki

(Vytvorená stránka „1. Arduino so senzorom vysiela data Pozri program na cvičení č. 2: MEMS cvičenie 3#Vysielač - Arduino 2. Processing prijíma data zo sériovej linky Pozri pr…“)
 
 
Riadok 10: Riadok 10:
  
 
Pozri program na cvičení MQTT: [[Protokol MQTT]]
 
Pozri program na cvičení MQTT: [[Protokol MQTT]]
 +
 +
 +
Vzorový príklad
 +
 +
<source lang="cpp">
 +
import processing.serial.*;
 +
import mqtt.*;
 +
 +
int adcValue = 0;        // value received from Serial
 +
String Unit="[-]";      // Unicode codes may be entered as well
 +
 +
Serial myPort;
 +
PFont Segment, Units;
 +
 +
 +
MQTTClient client;
 +
JSONObject message;
 +
float temperature = 0.0;
 +
int status = 0;
 +
 +
void setup()
 +
{
 +
  message = new JSONObject();
 +
  client = new MQTTClient(this);
 +
  client.connect("mqtt://dOVph0fe2HybffoMHjN3@demo.thingsboard.io");
 +
 
 +
  size(480, 180);        // Size of the window
 +
  Segment = createFont("Segment7", 150);  // Assign fonts and size
 +
  Units = createFont("Arial", 40);
 +
  textFont(Segment);
 +
  textAlign(RIGHT);        // Text align
 +
  fill(250,250,0);        // Font color is yellow = red + green
 +
 +
  println(Serial.list());    // List all the available serial ports
 +
  // Then open the port  you're using, my is the first, i.e. '0'
 +
  myPort = new Serial(this, Serial.list()[1], 9600);
 +
// don't generate a serialEvent() unless you get a newline character:
 +
  myPort.bufferUntil('\n');
 +
}
 +
 +
 +
void draw()
 +
{
 +
 
 +
  background(0,0,0);  // set the background color black
 +
 +
  textFont(Segment);
 +
  text(adcValue, 400, 150);
 +
 
 +
  textFont(Units);
 +
  text(Unit,465,65);
 +
 +
}
 +
 +
void serialEvent(Serial myPort)
 +
{
 +
String inString = myPort.readStringUntil('\n');    // get the ASCII string:
 +
if (inString != null)
 +
    {
 +
    inString = trim(inString);    // trim off any whitespace
 +
    adcValue = int(inString);      // convert into an integer
 +
    adcValue =int(map(adcValue, 0, 1023, 0, 1023));  // possible range adjusting
 +
 +
    message.setFloat("XXXY-Temp", adcValue);
 +
    println(message.toString());
 +
    client.publish("v1/devices/me/telemetry", message.toString());
 +
    }
 +
 +
}
 +
 +
void messageReceived(String topic, byte[] payload)
 +
{
 +
  println("new message: " + topic + " - " + new String(payload));
 +
}
 +
 +
 +
void clientConnected()
 +
{
 +
  println("client connected");
 +
}
 +
 +
void connectionLost()
 +
{
 +
  println("connection lost");
 +
}
 +
 +
</source>

Aktuálna revízia z 09:08, 1. máj 2022

1. Arduino so senzorom vysiela data

Pozri program na cvičení č. 2: MEMS cvičenie 3#Vysielač - Arduino

2. Processing prijíma data zo sériovej linky

Pozri program na cvičení s Processingom: MEMS cvičenie 3#Prijímač - Processing

3. Ak funguje (1) a (2), doplňte prenos do ThingsBoard.IO

Pozri program na cvičení MQTT: Protokol MQTT


Vzorový príklad

import processing.serial.*;
import mqtt.*;

int adcValue = 0;        // value received from Serial 
String Unit="[-]";       // Unicode codes may be entered as well
 
Serial myPort;
PFont Segment, Units; 


MQTTClient client;
JSONObject message;
float temperature = 0.0;
int status = 0;

void setup() 
{
  message = new JSONObject(); 
  client = new MQTTClient(this);
  client.connect("mqtt://dOVph0fe2HybffoMHjN3@demo.thingsboard.io");
  
  size(480, 180);         // Size of the window
  Segment = createFont("Segment7", 150);  // Assign fonts and size 
  Units = createFont("Arial", 40);
  textFont(Segment);
  textAlign(RIGHT);        // Text align 
  fill(250,250,0);         // Font color is yellow = red + green

  println(Serial.list());    // List all the available serial ports
  // Then open the port  you're using, my is the first, i.e. '0'
   myPort = new Serial(this, Serial.list()[1], 9600);
 // don't generate a serialEvent() unless you get a newline character:
   myPort.bufferUntil('\n');
}


void draw() 
{
   
  background(0,0,0);   // set the background color black

  textFont(Segment);
  text(adcValue, 400, 150);
  
  textFont(Units);
  text(Unit,465,65);

}

void serialEvent(Serial myPort) 
{
 String inString = myPort.readStringUntil('\n');    // get the ASCII string:
 if (inString != null) 
    {
     inString = trim(inString);     // trim off any whitespace
     adcValue = int(inString);      // convert into an integer
     adcValue =int(map(adcValue, 0, 1023, 0, 1023));  // possible range adjusting
 
     message.setFloat("XXXY-Temp", adcValue);
     println(message.toString());
     client.publish("v1/devices/me/telemetry", message.toString());
    }

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


void clientConnected() 
{
  println("client connected");
}

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