From 2b70725f6f4cbf08473f9b5a1a3195325a74932e Mon Sep 17 00:00:00 2001 From: Dooho Yi Date: Mon, 9 Sep 2024 04:55:20 +0900 Subject: [PATCH] wifi works (wip) - problem: not good fast enough to process all the messages (100ms messages) --- .python-version | 1 + faa_roller/faa_roller.ino | 219 ++++++++++++++++++++++++-------------- faa_roller/sketch.yaml | 2 + 3 files changed, 142 insertions(+), 80 deletions(-) create mode 100644 .python-version create mode 100644 faa_roller/sketch.yaml diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..7ce6d68 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +pio diff --git a/faa_roller/faa_roller.ino b/faa_roller/faa_roller.ino index c792068..8373d4a 100644 --- a/faa_roller/faa_roller.ino +++ b/faa_roller/faa_roller.ino @@ -29,12 +29,37 @@ //arduino #include +//network credentials +char ssid[] = "KT_GiGA_D565"; // ssid +char pass[] = "bhc1dd4971"; // password + +////udp +#include +#include +WiFiUDP Udp; + +// destination IP & Port +// const IPAddress dest_ip(192,168,43,255); +const IPAddress dest_ip(255,255,255,255); +const unsigned int dest_port = 5555; + +// - Local broadcast address (255.255.255.255) +// - Directed broadcast address (192.168.25.255 or 192.168.255.255 depends on your subnet) + +// difference between 'Directed broadcast' vs 'Local broadcast' +// ==> https://serverfault.com/a/219767 +// ==> https://www.sysnet.pe.kr/2/0/11368 + +// listening Port +const unsigned int listening_port = 5555; // listening port + //message types #include "message.h" -//espnow -#include -#include +//osc +#include +#include +OSCErrorCode error; //task #include @@ -125,40 +150,9 @@ void watcher2() { Task watcher2_task(1000, TASK_FOREVER, &watcher2, &runner, true); //*-*-*-*-*-*-*-*-*-*-*-*-* -//task #0 : blink led -#define LED_PERIOD (11111) -#define LED_ONTIME (1) -#define LED_GAPTIME (222) -#define LED_PIN 2 -extern Task blink_task; -void blink() { - // - static int count = 0; - count++; - // - switch (count % 4) { - case 0: - digitalWrite(LED_PIN, LOW); // first ON - blink_task.delay(LED_ONTIME); - break; - case 1: - digitalWrite(LED_PIN, HIGH); // first OFF - blink_task.delay(LED_GAPTIME); - break; - case 2: - digitalWrite(LED_PIN, LOW); // second ON - blink_task.delay(LED_ONTIME); - break; - case 3: - digitalWrite(LED_PIN, HIGH); // second OFF - blink_task.delay(LED_PERIOD - 2* LED_ONTIME - LED_GAPTIME); - break; - } -} -Task blink_task(0, TASK_FOREVER, &blink, &runner, true); // -> ENABLED, at start-up. - // on 'Note' void onNoteHandler(Note & n) { + Serial.println(n.to_string()); //is it for me? if (n.id == MY_GROUP_ID || n.id == MY_ID) { // @@ -193,36 +187,102 @@ void onNoteHandler(Note & n) { } } -// on 'receive' -void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) { - +//*-*-*-*-*-*-*-*-*-*-*-*-* +//common task #0 : blink led +#define LED_PERIOD (11111) +#define LED_ONTIME (1) +#define LED_GAPTIME (222) +#define LED_PIN 2 +extern Task blink_task; +void blink() { // - Serial.write(incomingData, len); - - // open => identify => use. - if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) { - Hello hello(""); - memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello)); - // - Serial.println(hello.to_string()); + static int count = 0; + count++; + // + switch (count % 4) { + case 0: + digitalWrite(LED_PIN, LOW); // first ON + blink_task.delay(LED_ONTIME); + break; + case 1: + digitalWrite(LED_PIN, HIGH); // first OFF + blink_task.delay(LED_GAPTIME); + break; + case 2: + digitalWrite(LED_PIN, LOW); // second ON + blink_task.delay(LED_ONTIME); + break; + case 3: + digitalWrite(LED_PIN, HIGH); // second OFF + blink_task.delay(LED_PERIOD - 2* LED_ONTIME - LED_GAPTIME); + break; } +} +Task blink_task(0, TASK_FOREVER, &blink, &runner, true); // -> ENABLED, at start-up. - // open => identify => use. - if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { - Note note; - memcpy((uint8_t *) ¬e, incomingData + 1, sizeof(Note)); +//common task #1 : listen on osc messages +// - osc processing +void route_note(OSCMessage& msg, int offset) { + // Serial.println("got route_note!"); + // matches will happen in the order. that the bundle is packed. + static Note note; + // (1) --> /onoff + if (msg.fullMatch("/onoff", offset)) { + // + note.clear(); + // + note.onoff = msg.getFloat(0); + // if (note.onoff != 0) note.onoff = 1; + } + // (2) --> /velocity + if (msg.fullMatch("/velocity", offset)) { + note.velocity = msg.getFloat(0); + } + // (3) --> /pitch + if (msg.fullMatch("/pitch", offset)) { + note.pitch = msg.getFloat(0); + } + // (4) --> /id + if (msg.fullMatch("/id", offset)) { + note.id = msg.getInt(0); + } + // (5) --> /x + if (msg.fullMatch("/x", offset)) { + note.x1 = msg.getFloat(0); + note.x2 = msg.getFloat(1); + note.x3 = msg.getFloat(2); + note.x4 = msg.getFloat(3); + note.ps = msg.getFloat(4); + // onNoteHandler(note); // - Serial.println(note.to_string()); } } +// - osc task +extern Task osc_task; +void osc() +{ + //osc + OSCBundle bundleIN; + int size = Udp.parsePacket(); -// on 'sent' -void onDataSent(uint8_t *mac, uint8_t sendStatus) { - char buff[256] = ""; - sprintf(buff, "Delivery failed! -> %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - if (sendStatus != 0) Serial.println(buff); + if (size > 0) { + Serial.println(size); + while (size--) { + bundleIN.fill(Udp.read()); + } + if (!bundleIN.hasError()) { + // on '/note' + bundleIN.route("/note", route_note); + } else { + error = bundleIN.getError(); + Serial.print("error: "); + Serial.println(error); + } + } } +Task osc_task(0, TASK_FOREVER, &osc, &runner, false); // -> ENABLED, at start-up. +//*-*-*-*-*-*-*-*-*-*-*-*-* // void setup() { @@ -237,6 +297,29 @@ void setup() { Serial.begin(115200); delay(100); + //wifi + 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(listening_port); + Serial.print("Local port: "); + Serial.println(Udp.localPort()); + Udp.flush(); + + //start osc processing + osc_task.restartDelayed(10); + //info Serial.println(); Serial.println(); @@ -244,36 +327,12 @@ void setup() { Serial.println("-"); Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\""); Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL)); - Serial.println("-"); - - //wifi - disabled - system_phy_set_max_tpw(0); - WiFiMode_t node_type = WIFI_STA; - WiFi.mode(node_type); - - //esp-now - if (esp_now_init() != 0) { - Serial.println("Error initializing ESP-NOW"); - return; - } - esp_now_set_self_role(ESP_NOW_ROLE_COMBO); - esp_now_register_send_cb(onDataSent); - esp_now_register_recv_cb(onDataReceive); - // - Serial.println("- ! (esp_now_add_peer) ==> add a 'broadcast peer' (FF:FF:FF:FF:FF:FF)."); - uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - esp_now_add_peer(broadcastmac, ESP_NOW_ROLE_COMBO, 1, NULL, 0); - Serial.println("-"); Serial.println("\".-.-.-. :)\""); Serial.println(); //random seed randomSeed(analogRead(0)); - - //tasks - rest_task.restartDelayed(500); - rest2_task.restartDelayed(500); } void loop() { diff --git a/faa_roller/sketch.yaml b/faa_roller/sketch.yaml new file mode 100644 index 0000000..ee52428 --- /dev/null +++ b/faa_roller/sketch.yaml @@ -0,0 +1,2 @@ +default_fqbn: esp8266:esp8266:nodemcuv2:baud=460800 +default_port: /dev/tty.SLAB_USBtoUART