MQTT in pure data: Unterschied zwischen den Versionen

Aus hyperdramatik
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
 
Zeile 13: Zeile 13:


<source lang="js" line start="2" highlight="4-6">
<source lang="js" line start="2" highlight="4-6">
/*
/*
   SimpleMQTTClient.ino
   a_MQTT_send6AnalogAsString_forPD
  The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
*/
*/


#include "EspMQTTClient.h"
#include <WiFi.h>
 
#include <MQTT.h>
#define MYSSID "ladenlokal"
#define MYPASS "puppe2010"
 
#define BROKER_IP "192.168.236.100" //this depends on your brokera
 
#define MQTT_USERNAME "" // username, can be omitted if not needed
#define MQTT_PASS "" // password, can be omitted if not needed


#define CLIENT_NAME  "esp_group0_person1" // this name NEEDS TO BE UNIQUE for each device connecting to the broker!
const char ssid[] = "ladenlokal";
 
const char pass[] = "puppe2010";
EspMQTTClient client( MYSSID, MYPASS, BROKER_IP, MQTT_USERNAME, MQTT_PASS, CLIENT_NAME, 1883 );


#define totalLegs 6
#define totalLegs 6
Zeile 38: Zeile 28:
int avgs[totalLegs];
int avgs[totalLegs];
int samplesize = 5;
int samplesize = 5;
String str;
WiFiClient net;
MQTTClient client;


unsigned long lastMillis = 0;
unsigned long lastMillis = 0;
String myTopic = "/group0/person1/";
bool connected = false;
String str;


void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.print("\nconnecting...");
  while (!client.connect("arduino", "", "")) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nconnected!");


void setup()
  client.subscribe("/hello");
{
   // client.unsubscribe("/hello");
   Serial.begin(115200);
}


  // Optionnal functionnalities of EspMQTTClient :
void messageReceived(String &topic, String &payload) {
  // client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
   Serial.println("incoming: " + topic + " - " + payload);
  // client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
   client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
}
}


// This function is called once everything is connected (Wifi and MQTT)
void setup() {
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
  Serial.begin(115200);
void onConnectionEstablished()
  WiFi.begin(ssid, pass);
{
 
   connected = true;
  // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
  // You need to set the IP address directly.
  client.begin("192.168.236.90", 2883, net);
   client.onMessage(messageReceived);


   // Publish a message to "mytopic/test"
   connect();
  //client.publish(myTopic, "This is a message"); // You can activate the retain flag by setting the third parameter to true
}
}


void loop()
void loop() {
{
  // this loop is a routine for the MQTT, do not add delay in main loop
   client.loop();
   client.loop();
  delay(10);  // <- fixes some issues with WiFi stability


   //publish a message roughly every x miliseconds.
  if (!client.connected()) {
   if (millis() - lastMillis > 50) {
    connect();
  }
 
   // publish a message roughly every second.
   if (millis() - lastMillis > 1000) {
     lastMillis = millis();
     lastMillis = millis();


    for (int i = 0; i < totalLegs; i++) {
for (int i = 0; i < totalLegs; i++) {
       // read analog pins
       // read analog pins
       vals[i] = analogRead(analogLegs[i]);
       vals[i] = analogRead(analogLegs[i]);
Zeile 84: Zeile 93:


     // after you cycle through all the pins, publish the readings as list to the MQTT
     // after you cycle through all the pins, publish the readings as list to the MQTT
     client.publish(myTopic, str);
     client.publish("/group0/person1/", str);
     str = "";
     str = "";


Zeile 98: Zeile 107:
     Serial.print(vals[4]);
     Serial.print(vals[4]);
     Serial.print(" ");
     Serial.print(" ");
     Serial.println(vals[5]);
     Serial.println(vals[5]);  
   }
   }
}
}
</source>
</source>

Aktuelle Version vom 17. Mai 2022, 18:24 Uhr

Course Documentation "Communicating Bodies"

https://www.kobakant.at/DIY/?p=9133

http://hyperdramatik.net/mediawiki/index.php?title=Communicating_Bodies


PD Patch

xxx


Arduino Code for Spaghettimonster

/*
  a_MQTT_send6AnalogAsString_forPD
*/

#include <WiFi.h>
#include <MQTT.h>

const char ssid[] = "ladenlokal";
const char pass[] = "puppe2010";

#define totalLegs 6
int analogLegs[] = {36, 39, 34, 35, 32, 33};
int vals[totalLegs];
int avgs[totalLegs];
int samplesize = 5;

String str;

WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.print("\nconnecting...");
  while (!client.connect("arduino", "", "")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected!");

  client.subscribe("/hello");
  // client.unsubscribe("/hello");
}

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);

  // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
  // You need to set the IP address directly.
  client.begin("192.168.236.90", 2883, net);
  client.onMessage(messageReceived);

  connect();
}

void loop() {
  client.loop();
  delay(10);  // <- fixes some issues with WiFi stability

  if (!client.connected()) {
    connect();
  }

  // publish a message roughly every second.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();

 for (int i = 0; i < totalLegs; i++) {
      // read analog pins
      vals[i] = analogRead(analogLegs[i]);
      // take avarage of the sample size
      avgs[i] = (avgs[i] * (samplesize - 1) + vals[i]) / samplesize;

      str = str + " " + String(104);
    }

    // after you cycle through all the pins, publish the readings as list to the MQTT
    client.publish("/group0/person1/", str);
    str = "";

    //// communicate from the serial
    Serial.print(vals[0]);
    Serial.print(" ");
    Serial.print(vals[1]);
    Serial.print(" ");
    Serial.print(vals[2]);
    Serial.print(" ");
    Serial.print(vals[3]);
    Serial.print(" ");
    Serial.print(vals[4]);
    Serial.print(" ");
    Serial.println(vals[5]);   
  }
}