MQTT in pure data: Unterschied zwischen den Versionen
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"> | ||
/* | /* | ||
a_MQTT_send6AnalogAsString_forPD | |||
*/ | */ | ||
#include | #include <WiFi.h> | ||
#include <MQTT.h> | |||
# | |||
const char ssid[] = "ladenlokal"; | |||
const char pass[] = "puppe2010"; | |||
#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; | ||
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() | void loop() { | ||
{ | |||
client.loop(); | client.loop(); | ||
delay(10); // <- fixes some issues with WiFi stability | |||
//publish a message roughly every | if (!client.connected()) { | ||
if (millis() - lastMillis > | connect(); | ||
} | |||
// publish a message roughly every second. | |||
if (millis() - lastMillis > 1000) { | |||
lastMillis = millis(); | lastMillis = millis(); | ||
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( | 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]);
}
}