wifi works (wip)

- problem: not good fast enough to process all the messages (100ms 
messages)
This commit is contained in:
Dooho Yi 2024-09-09 04:55:20 +09:00
parent 83fdb707ef
commit 2b70725f6f
3 changed files with 142 additions and 80 deletions

1
.python-version Normal file
View file

@ -0,0 +1 @@
pio

View file

@ -29,12 +29,37 @@
//arduino //arduino
#include <Arduino.h> #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 //message types
#include "message.h" #include "message.h"
//espnow //osc
#include <ESP8266WiFi.h> #include <OSCMessage.h>
#include <espnow.h> #include <OSCBundle.h>
OSCErrorCode error;
//task //task
#include <TaskScheduler.h> #include <TaskScheduler.h>
@ -125,40 +150,9 @@ void watcher2() {
Task watcher2_task(1000, TASK_FOREVER, &watcher2, &runner, true); 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' // on 'Note'
void onNoteHandler(Note & n) { void onNoteHandler(Note & n) {
Serial.println(n.to_string());
//is it for me? //is it for me?
if (n.id == MY_GROUP_ID || n.id == MY_ID) { 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); static int count = 0;
count++;
// 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()); 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. //common task #1 : listen on osc messages
if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) { // - osc processing
Note note; void route_note(OSCMessage& msg, int offset) {
memcpy((uint8_t *) &note, incomingData + 1, sizeof(Note)); // 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); onNoteHandler(note);
// //
Serial.println(note.to_string());
} }
} }
// - osc task
extern Task osc_task;
void osc()
{
//osc
OSCBundle bundleIN;
int size = Udp.parsePacket();
// on 'sent' if (size > 0) {
void onDataSent(uint8_t *mac, uint8_t sendStatus) { Serial.println(size);
char buff[256] = ""; while (size--) {
sprintf(buff, "Delivery failed! -> %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); bundleIN.fill(Udp.read());
if (sendStatus != 0) Serial.println(buff); }
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() { void setup() {
@ -237,6 +297,29 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(100); 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 //info
Serial.println(); Serial.println();
Serial.println(); Serial.println();
@ -244,36 +327,12 @@ void setup() {
Serial.println("-"); Serial.println("-");
Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\""); 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("- 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("\".-.-.-. :)\""); Serial.println("\".-.-.-. :)\"");
Serial.println(); Serial.println();
//random seed //random seed
randomSeed(analogRead(0)); randomSeed(analogRead(0));
//tasks
rest_task.restartDelayed(500);
rest2_task.restartDelayed(500);
} }
void loop() { 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