Compare commits

...

4 commits
main ... wifi

Author SHA1 Message Date
d965a971d8 more observation & testing... 2024-09-10 01:20:23 +09:00
d2766b2f44 test log - typo 2024-09-09 05:10:41 +09:00
4051f6b17d test log 2024-09-09 05:03:28 +09:00
2b70725f6f wifi works (wip)
- problem: not good fast enough to process all the messages (100ms 
messages)
2024-09-09 04:55:20 +09:00
4 changed files with 314 additions and 80 deletions

1
.python-version Normal file
View file

@ -0,0 +1 @@
pio

View file

@ -29,12 +29,37 @@
//arduino
#include <Arduino.h>
//network credentials
char ssid[] = "KT_GiGA_D565"; // ssid
char pass[] = "bhc1dd4971"; // password
////udp
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
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 <ESP8266WiFi.h>
#include <espnow.h>
//osc
#include <OSCMessage.h>
#include <OSCBundle.h>
OSCErrorCode error;
//task
#include <TaskScheduler.h>
@ -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 *) &note, 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(1, 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() {

2
faa_roller/sketch.yaml Normal file
View file

@ -0,0 +1,2 @@
default_fqbn: esp8266:esp8266:nodemcuv2:baud=460800
default_port: /dev/tty.SLAB_USBtoUART

172
faa_roller/test.log Normal file
View file

@ -0,0 +1,172 @@
with : Task osc_task(0, TASK_FOREVER, &osc, &runner, false);
( id=4001, pitch=0.00, velocity=96.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=98.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:98
156
( id=4001, pitch=0.00, velocity=99.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=1.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:1
156
( id=4001, pitch=0.00, velocity=2.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=4.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:4
156
( id=4001, pitch=0.00, velocity=5.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=7.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:7
156
( id=4001, pitch=0.00, velocity=8.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:8
156
( id=4001, pitch=0.00, velocity=9.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:9
156
( id=4001, pitch=0.00, velocity=10.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:10
156
( id=4001, pitch=0.00, velocity=11.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:11
156
( id=4001, pitch=0.00, velocity=12.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:12
156
( id=4001, pitch=0.00, velocity=13.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:13
156
( id=4001, pitch=0.00, velocity=14.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=16.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:16
156
( id=4001, pitch=0.00, velocity=17.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=19.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:19
156
( id=4001, pitch=0.00, velocity=20.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=22.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:22
156
( id=4001, pitch=0.00, velocity=23.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=25.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:25
156
( id=4001, pitch=0.00, velocity=26.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=28.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:28
156
( id=4001, pitch=0.00, velocity=29.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=31.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:31
156
( id=4001, pitch=0.00, velocity=33.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=35.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:35
156
( id=4001, pitch=0.00, velocity=36.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:36
156
( id=4001, pitch=0.00, velocity=37.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:37
156
( id=4001, pitch=0.00, velocity=38.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:38
156
( id=4001, pitch=0.00, velocity=39.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:39
156
( id=4001, pitch=0.00, velocity=40.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:40
156
( id=4001, pitch=0.00, velocity=41.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:41
156
( id=4001, pitch=0.00, velocity=42.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:42
156
( id=4001, pitch=0.00, velocity=43.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:43
156
( id=4001, pitch=0.00, velocity=44.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:44
156
( id=4001, pitch=0.00, velocity=44.00, onoff=0.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
(pio) doohoyi@Doohos-MacBook-Pro:~/Documents/Arduino/faa/faa_roller (wifi-interrupt
)$ arduino-cli compile && arduino-cli upload -v -p /dev/tty.SLAB_USBtoUART && pio device monitor -p /dev/tty.SLAB_USBtoUART -b 115200
with : Task osc_task(1, TASK_FOREVER, &osc, &runner, false);
156
( id=4001, pitch=0.00, velocity=45.00, onoff=0.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=46.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=48.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:48
156
( id=4001, pitch=0.00, velocity=50.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=52.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:52
156
( id=4001, pitch=0.00, velocity=53.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=55.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:55
156
( id=4001, pitch=0.00, velocity=56.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=58.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:58
156
( id=4001, pitch=0.00, velocity=59.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=61.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:61
156
( id=4001, pitch=0.00, velocity=62.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=64.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:64
156
( id=4001, pitch=0.00, velocity=65.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=67.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:67
156
( id=4001, pitch=0.00, velocity=68.00, onoff=1.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
156
( id=4001, pitch=0.00, velocity=69.00, onoff=0.00, x1=0.00, x2=0.00, x3=0.00, x4=0.00, ps=2000.00 )
set_speed:69
osc msg parsing is lost + sometimes, set_speed also doesn't fire.
- when no set_speed, subsequent 1-2-3 msg lost..
when i change scheduler speed slower. becomes worse. (loose more)
- then scheduling too slow?
- but then, why no set_speed? isn't it being streamed? (buffered?=saved?)
- at some cases, only set_speed doesn't fire. => msg. is there. but set_speed not.