Code Esclave

This commit is contained in:
boutrie.eytan 2023-06-26 15:10:20 +02:00
parent 3b23142fc1
commit f99cb0c0bb

View file

@ -1,96 +1,66 @@
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include "MPU9250.h"
#else
#include <WiFi.h> #include <WiFi.h>
#endif
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <OSCMessage.h> #include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
char ssid[] = "cohabit"; // your network SSID (name)
char pass[] = "lewifidecohabit"; // your network password
char ssid[] = "cohabit"; // your network SSID (name)
char pass[] = "lewifidecohabit"; // your network password
int update_rate = 1000000;
WiFiUDP Udp;
// A UDP instance to let us send and receive packets over UDP // A UDP instance to let us send and receive packets over UDP
const IPAddress outIp(192, 168, 0, 210); // remote IP of your computer WiFiUDP Udp;
const unsigned int outPort = 16403; // remote port to receive OSC const unsigned int localPort = 16403; // local port to listen for UDP packets (here's where we send the packets)
const unsigned int localPort = 16384; // local port to listen for OSC packets (actually not used for sending) int stepPin = 14;
int dirPin = 12;
void setup() { void setup()
Serial.begin(115200); {
Serial.begin(115200);
// initialize LED digital pin as an output.
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
// Connect to WiFi network while (WiFi.status() != WL_CONNECTED) {
Serial.println(); delay(500);
Serial.println(); Serial.print(".");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
#ifdef ESP32
Serial.println(localPort);
#else
Serial.println(Udp.localPort());
#endif
} }
void ledtoggle(OSCMessage &msg) { Serial.println("");
switch (msg.getInt(0)) {
case 0: Serial.println("WiFi connected");
digitalWrite(LED_BUILTIN, LOW); Serial.println("IP address: ");
break; Serial.println(WiFi.localIP());
case 1:
digitalWrite(LED_BUILTIN, HIGH); Serial.println("Starting UDP");
break; Udp.begin(localPort);
} Serial.print("Local port: ");
Serial.println(localPort);
digitalWrite(dirPin, HIGH);
} }
void receiveMessage() { void step(OSCMessage &msg) {
OSCMessage inmsg; switch (msg.getInt(0)) {
int size = Udp.parsePacket(); case 0:
digitalWrite(stepPin, LOW);
if (size > 0) { break;
while (size--) { case 1:
inmsg.fill(Udp.read()); digitalWrite(stepPin, HIGH);
} break;
if (!inmsg.hasError()) { }
inmsg.dispatch("/led", ledtoggle);
}
//else { auto error = inmsg.getError(); }
}
} }
void loop() { void loop()
receiveMessage(); {
OSCMessage msg("/step"); OSCMessage msg;
msg.add(1); int size = Udp.parsePacket();
Udp.beginPacket(outIp, outPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
delayMicroseconds(update_rate);
OSCMessage msg2("/Step");
msg2.add(0);
Udp.beginPacket(outIp, outPort);
msg2.send(Udp);
Udp.endPacket();
msg2.empty();
delayMicroseconds(update_rate);
if (size > 0) {
while (size--) {
msg.fill(Udp.read());
}
//step(msg);
step(msg);
}
} }