MQTT in pure data

Aus hyperdramatik
Zur Navigation springen Zur Suche springen

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

/*
  SimpleMQTTClient.ino
  The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
*/

#include "EspMQTTClient.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!

EspMQTTClient client( MYSSID, MYPASS, BROKER_IP, MQTT_USERNAME, MQTT_PASS, CLIENT_NAME, 1883 );

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

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


void setup()
{
  Serial.begin(115200);

  // Optionnal functionnalities of EspMQTTClient :
  // client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  // 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)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
  connected = true;

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

void loop()
{
  // this loop is a routine for the MQTT, do not add delay in main loop
  client.loop();

  //publish a message roughly every x miliseconds.
  if (millis() - lastMillis > 50) {
    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(myTopic, 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]);
  }



}