MQTT in pure data
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
/*
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]);
}
}