From 428ebd1c2901dfbe166ec327a1ec7035a0c48cb5 Mon Sep 17 00:00:00 2001 From: msp Date: Sat, 1 Oct 2022 17:29:06 -0400 Subject: [PATCH] wifi version set up for export --- README.rst | 77 +++++++++++++++++++++++++++++++--------- main/espd.h | 6 ++-- test-patch/esp-patch.pd | 25 +++++++++++++ test-patch/host-patch.pd | 52 +++++++++++++++++++++++++++ wifi/net.c | 62 +++++++++++++++++++++++++------- 5 files changed, 191 insertions(+), 31 deletions(-) create mode 100644 test-patch/esp-patch.pd create mode 100644 test-patch/host-patch.pd diff --git a/README.rst b/README.rst index 2d70efe..8725920 100644 --- a/README.rst +++ b/README.rst @@ -1,34 +1,79 @@ +Commands I issue to shell to compile (customize to your own installation): + export IDF_TOOLS_PATH=/home/msp/bis/work/esp/toolchain export ADF_PATH=/home/msp/bis/work/esp/esp-adf export IDF_PATH=/home/msp/bis/work/esp/esp-idf-v4.4.2 . $ADF_PATH/esp-idf/export.sh -configuring: -enter menuconfig "Component config", choose "Bluetooth" -enter menu Bluetooth, choose "Classic Bluetooth" and "SPP Profile" - idf.py menuconfig idf.py build idf.py -p /dev/ttyUSB0 flash monitor idf.py -p /dev/ttyUSB0 monitor -docs: -https://gitdemo.readthedocs.io/en/latest/build-system.html +**** START READING HERE **** + +This is a very sketchy description of the "espd" version of Pd, which runs +on Espressif LyraT or LyraT-mini boards. To use these you will almost certainly +have to compile your own version, at least to specify the WIFI settings which +are set in the file main/espd.h . + +The instructions here _should_ work on macintoshes and PCs with appropriate +changes (in the shell commands for instance). In addition to a linux machine +you'll need an Espressif LyraT or LyraT mini board. + +To run espd, you must compile and load it on the board, biit the board, and then +run a patch on the host computer that waits for the board to make a TCP +connection to it. Once connected, the host patch then sends Pd messages to +load a different patch on the esp. The two patches then can communicate over +the same RCP connection. + +If the connection is ever broken the board reboots itself and tries to establish +a new connection. Each time the host patch gets a new connection it then has to +reload the ESP patch. + +As it stands all the vanilla Pd objects are compiled into espd except for the +FFT objects and (oddly) netsend/netreceive. You can rebuild it with a different +xhoice of objects compiled in, including your own objects. + +Steps to get this running: first install the espd compile chain and the +"ADF" audio development platform. The ESP documentation is excellent. Start +here: + https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/linux-macos-setup.html -file:///home/msp/Downloads/WBT101-06A-ClassicBluetooth-Basic-1.pdf -https://people.csail.mit.edu/albert/bluez-intro/c404.html +another (third-party) URL that might be useful: + +https://gitdemo.readthedocs.io/en/latest/build-system.html + +The compilation chain depends on an "sdkconfig" file - samples for the two +boards are included as "sdkconfig.lyrat" and "sdkconfig.lyratmini" - you can +rename one of these as "sdkconfig" before invoking the compiler. + +In addition to the sources youre looking at you'll need Pd, preferably the +latest version, although I'm testing this with Pd commit +05bf346fa32510fd191fe77de24b3ea1c481f5ff . You can "git" clone pd into a +subdirectory "pd" of this directory. + +Then you must apply four small patches to the Pd source, found in the +subdirectory "patches". + +To compile, set up your envoronment variables and issue commands to compile, +flash, and run the monitor program to see debugging output (see top of this page +to see what I typw on my system). This should be done from a shell window that +is in this (espd) directory. + +Then set up a host patch (examples in the subdirectory host). This patch +listens on port 4498 (by default). When a connection os made, it loads a test +patch on the esp, by sending the message "pd begin-new poodle .", then the contents +of the patch, then "pd end-new" DOLIST -adapt commector extern to act as TCP server -starting server after ESP fails (can't connect - restart?) -alive message to send count starting w/0 -server patch (on host) uploads patch when "alive 0" arrives -message to Pd to restart -restart when recvfrom fails - +toggles to indicate connectedness +why is first message after restarting card dropped? +move patch-loader out of tcpconnect +sleeping between attempts to connect to host +sleep message to pd object later: fold in the network Pd objects? adapt to non-lyraT boards - diff --git a/main/espd.h b/main/espd.h index cc4afb9..787c2bc 100644 --- a/main/espd.h +++ b/main/espd.h @@ -6,9 +6,9 @@ #define PRIORITY_WIFI 2 #if !defined(CONFIG_LOCALE_NUMBER) || (CONFIG_LOCALE_NUMBER==0) -#define CONFIG_ESP_WIFI_SSID "Fios-3BR2s" -#define CONFIG_ESP_WIFI_PASSWORD "aid24rushed63fin" -#define CONFIG_ESP_WIFI_SENDADDR "192.168.1.177" +#define CONFIG_ESP_WIFI_SSID "network" +#define CONFIG_ESP_WIFI_PASSWORD "password" +#define CONFIG_ESP_WIFI_SENDADDR "192.168.1.153" #define CONFIG_ESP_WIFI_SENDPORT 4498 #define CONFIG_ESP_WIFI_LISTENPORT 4499 #endif diff --git a/test-patch/esp-patch.pd b/test-patch/esp-patch.pd new file mode 100644 index 0000000..7d3752d --- /dev/null +++ b/test-patch/esp-patch.pd @@ -0,0 +1,25 @@ +#N canvas 332 97 403 426 12; +#X obj 53 351 dac~ 1; +#X obj 44 130 r a; +#X obj 52 326 *~; +#X obj 158 87 loadbang; +#X msg 103 130 50; +#X obj 49 178 line~; +#X obj 49 203 *~; +#X obj 49 228 *~ 0.0001; +#X msg 156 123 dsp 1; +#X obj 156 147 s pd; +#X obj 72 295 osc~ 440; +#X obj 82 262 r f; +#X connect 1 0 5 0; +#X connect 2 0 0 0; +#X connect 3 0 8 0; +#X connect 3 0 4 0; +#X connect 4 0 5 0; +#X connect 5 0 6 0; +#X connect 5 0 6 1; +#X connect 6 0 7 0; +#X connect 7 0 2 0; +#X connect 8 0 9 0; +#X connect 10 0 2 1; +#X connect 11 0 10 0; diff --git a/test-patch/host-patch.pd b/test-patch/host-patch.pd new file mode 100644 index 0000000..05e31b8 --- /dev/null +++ b/test-patch/host-patch.pd @@ -0,0 +1,52 @@ +#N canvas 182 66 791 556 12; +#X msg 373 294 listen 4498; +#X obj 374 331 netreceive -u -f; +#X listbox 481 365 26 0 0 0 - - - 0; +#X listbox 374 398 26 0 0 0 - - - 0; +#X obj 374 355 list; +#X obj 374 426 bng 15 250 50 0 empty empty empty 17 7 0 10 #fcfcfc #000000 #000000; +#X obj 373 268 loadbang; +#X msg 55 38 listen 4498; +#X msg 62 65 listen 0; +#X floatatom 124 422 5 0 0 0 - - - 0; +#X obj 47 424 list; +#X listbox 47 448 20 0 0 0 - - - 0; +#X obj 227 75 text define patch; +#X obj 129 289 text sequence patch; +#X msg 130 263 line 0 \, auto; +#X obj 148 314 print; +#X obj 48 398 netreceive; +#X msg 111 199 send pd foo 1; +#X msg 127 231 send pd begin-new poodle .; +#X msg 119 341 send pd end-new; +#X obj 55 12 loadbang; +#X msg 101 119 send a 0; +#X msg 96 95 send a 70; +#X msg 102 144 send f 440; +#X msg 106 170 send f 660; +#X obj 228 15 loadbang; +#X msg 229 44 read esp-patch.pd; +#X connect 0 0 1 0; +#X connect 1 0 4 0; +#X connect 1 1 2 0; +#X connect 3 0 5 0; +#X connect 4 0 3 0; +#X connect 6 0 0 0; +#X connect 7 0 16 0; +#X connect 8 0 16 0; +#X connect 10 0 11 0; +#X connect 13 0 15 0; +#X connect 13 0 16 0; +#X connect 14 0 13 0; +#X connect 16 0 10 0; +#X connect 16 1 9 0; +#X connect 17 0 16 0; +#X connect 18 0 16 0; +#X connect 19 0 16 0; +#X connect 20 0 7 0; +#X connect 21 0 16 0; +#X connect 22 0 16 0; +#X connect 23 0 16 0; +#X connect 24 0 16 0; +#X connect 25 0 26 0; +#X connect 26 0 12 0; diff --git a/wifi/net.c b/wifi/net.c index 205444d..018aeae 100644 --- a/wifi/net.c +++ b/wifi/net.c @@ -21,7 +21,7 @@ int tcp_socket; void udpreceivertask(void *z) { char rx_buffer[4000]; - int ip_protocol = 0, err; + int ip_protocol = 0, err, newsocket; struct sockaddr_in dest_addr; ESP_LOGI(TAG, "udpreceivertask starting..."); @@ -30,26 +30,32 @@ void udpreceivertask(void *z) dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(CONFIG_ESP_WIFI_SENDPORT); - tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); - if (tcp_socket < 0) { + newsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); + if (newsocket < 0) { ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); return; } ESP_LOGI(TAG, "connecting..."); - while (err = connect(tcp_socket, &dest_addr, sizeof(dest_addr)) < 0) + while (err = connect(newsocket, &dest_addr, sizeof(dest_addr)) < 0) { - ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno); + ESP_LOGE(TAG, "Socket unable to bind: errno %d - retrying", errno); + close(newsocket); vTaskDelay(2000 / portTICK_PERIOD_MS); + newsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); + if (newsocket < 0) { + ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); + return; + } } + tcp_socket = newsocket; while (1) { - int len = recv(tcp_socket, rx_buffer, sizeof(rx_buffer) - 1, 0); + int len = recv(newsocket, rx_buffer, sizeof(rx_buffer) - 1, 0); if (len < 0) { - ESP_LOGE(TAG, "recvfrom failed: errno %d", errno); - vTaskDelay(5000 / portTICK_PERIOD_MS); - continue; + ESP_LOGE(TAG, "recvfrom failed: errno %d -- restarting", errno); + esp_restart(); } else if (len == 0) { @@ -96,26 +102,58 @@ void net_sendudp(void *msg, int len, int port) if (errorcount < 10 || (errorcount < 100 && !(errorcount%10)) || !(errorcount%100)) { - ESP_LOGE(TAG, "net send error count %d", errno); + ESP_LOGE(TAG, "udp net send error %d, count %d", errno, errorcount); } } else whensent = esp_timer_get_time(); } +void net_sendtcp(void *msg, int len) +{ + int err; + while (!tcp_socket) + { + ESP_LOGE(TAG, "sendtcp: waiting for socket"); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + err = send(tcp_socket, msg, len, 0); + if (err < 0) + { + static int errorcount; + errorcount++; + if (errorcount < 10 || (errorcount < 100 && !(errorcount%10)) + || !(errorcount%100)) + { + ESP_LOGE(TAG, "tcp net send error %d, count %d", + errno, errorcount); + } + } +} + extern char wifi_mac[]; /* esp_err_t esp_base_mac_addr_get(uint8_t *mac); */ void net_alive( void) { int elapsed = esp_timer_get_time() - whensent; + char buf[80]; + uint8_t mac[6]; + static int notfirst; + if (!notfirst) + { + notfirst = 1; + esp_base_mac_addr_get(&mac); + sprintf(buf, "hello %02x:%02x:%02x:%02x:%02x:%02x;\n", + mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); + net_sendtcp(buf, strlen(buf)); + } if (elapsed > 1000000) { - char buf[80]; - uint8_t mac[6]; esp_base_mac_addr_get(&mac); sprintf(buf, "alive %02x:%02x:%02x:%02x:%02x:%02x;\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); net_sendudp(buf, strlen(buf), CONFIG_ESP_WIFI_SENDPORT); + net_sendtcp(buf, strlen(buf)); } } #endif /* PD_USE_WIFI */