From f99cb0c0bb4b22871f242a4bba9155f797f9ee84 Mon Sep 17 00:00:00 2001 From: "boutrie.eytan" Date: Mon, 26 Jun 2023 15:10:20 +0200 Subject: [PATCH] Code Esclave --- Rotabator.ino | 138 ++++++++++++++++++++------------------------------ 1 file changed, 54 insertions(+), 84 deletions(-) diff --git a/Rotabator.ino b/Rotabator.ino index d49722a..81a7996 100644 --- a/Rotabator.ino +++ b/Rotabator.ino @@ -1,96 +1,66 @@ -#if defined(ESP8266) -#include -#include "MPU9250.h" -#else #include -#endif -#include -#include +#include +#include +#include +#include +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 -const IPAddress outIp(192, 168, 0, 210); // remote IP of your computer -const unsigned int outPort = 16403; // remote port to receive OSC -const unsigned int localPort = 16384; // local port to listen for OSC packets (actually not used for sending) +WiFiUDP Udp; +const unsigned int localPort = 16403; // local port to listen for UDP packets (here's where we send the packets) +int stepPin = 14; +int dirPin = 12; -void setup() { - Serial.begin(115200); +void setup() +{ +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 - Serial.println(); - Serial.println(); - 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 +while (WiFi.status() != WL_CONNECTED) { +delay(500); +Serial.print("."); } -void ledtoggle(OSCMessage &msg) { - switch (msg.getInt(0)) { - case 0: - digitalWrite(LED_BUILTIN, LOW); - break; - case 1: - digitalWrite(LED_BUILTIN, HIGH); - break; - } +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: "); +Serial.println(localPort); +digitalWrite(dirPin, HIGH); } -void receiveMessage() { - OSCMessage inmsg; - int size = Udp.parsePacket(); - - if (size > 0) { - while (size--) { - inmsg.fill(Udp.read()); - } - if (!inmsg.hasError()) { - inmsg.dispatch("/led", ledtoggle); - } - //else { auto error = inmsg.getError(); } - } +void step(OSCMessage &msg) { +switch (msg.getInt(0)) { +case 0: +digitalWrite(stepPin, LOW); +break; +case 1: +digitalWrite(stepPin, HIGH); +break; +} } -void loop() { - receiveMessage(); - OSCMessage msg("/step"); - msg.add(1); - 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); - +void loop() +{ +OSCMessage msg; +int size = Udp.parsePacket(); - +if (size > 0) { +while (size--) { +msg.fill(Udp.read()); +} +//step(msg); +step(msg); +} } \ No newline at end of file