This commit is contained in:
Dooho Yi 2023-05-30 01:58:02 +09:00
commit 5f2b731367
4 changed files with 1627 additions and 0 deletions

203
cfirmata_esp.ino Normal file
View file

@ -0,0 +1,203 @@
//
// configurablefirmata_esp8266
//
// D. Yi @ 2023 5 23
//
#define WIRELESS
#if defined(WIRELESS)
//wi-fi
#include <ESP8266WiFi.h>
//network!
const char* ssid = "esp1";
const char* password = "1111100000";
const int NETWORK_PORT = 27016;
// IPAddress local_IP(192, 168, 137, 111);
// IPAddress gateway(192, 168, 137, 1);
// IPAddress subnet(255, 255, 255, 0);
//wi-fi event handlers : re-connect automatically
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.println("Connected to Wi-Fi sucessfully.");
Serial.print("IP address: ");
Serial.println("Mac address: " + WiFi.macAddress());
Serial.println(WiFi.localIP());
digitalWrite(2, HIGH);
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi, trying to reconnect...");
WiFi.disconnect();
WiFi.begin(ssid, password);
digitalWrite(2, LOW);
}
#endif
//firmata over wi-fi
#include <ConfigurableFirmata.h>
#if defined(WIRELESS)
#include "utility/WiFiClientStream.h"
#include "utility/WiFiServerStream.h"
WiFiServerStream serverStream(NETWORK_PORT);
//firmware update over the air
#include <ArduinoOTA.h>
#endif
//handle digitalWrite
byte previousPORT[TOTAL_PORTS];
void digitalWriteCallback(byte port, int value)
{
byte i;
byte currentPinValue, previousPinValue;
if (port < TOTAL_PORTS && value != previousPORT[port]) {
for (i = 0; i < 8; i++) {
currentPinValue = (byte) value & (1 << i);
previousPinValue = previousPORT[port] & (1 << i);
if (currentPinValue != previousPinValue) {
digitalWrite(i + (port * 8), currentPinValue);
}
}
previousPORT[port] = value;
}
}
////
void setup()
{
//pinmodes
//digital inputs
pinMode(D1, INPUT_PULLUP);
pinMode(D2, INPUT_PULLUP);
pinMode(D3, INPUT_PULLUP);
pinMode(D5, INPUT_PULLUP);
pinMode(D6, INPUT_PULLUP);
pinMode(D7, INPUT_PULLUP);
//digital outputs
pinMode(D4, OUTPUT); //build-in LED && also used as wifi indicator!
#if defined(WIRELESS)
digitalWrite(D4, LOW); //LOW ==> LED ON ==> 'wifi. disconnected.'
#else
digitalWrite(D4, HIGH); //HIGH ==> LED OFF
#endif
//serial (for debug & info)
Serial.begin(57600);
#if defined(WIRELESS)
//wifi
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
WiFi.mode(WIFI_STA);
//Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// if (!WiFi.config(local_IP, gateway, subnet)) {
// Serial.println("STA Failed to configure");
// }
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
// Serial.print(".");
delay(500);
}
Serial.println("");
#endif
//firmata
Firmata.disableBlinkVersion();
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
#if defined(WIRELESS)
Firmata.begin(serverStream);
#else
Firmata.begin(Serial);
#endif
#if defined(WIRELESS)
//ota
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
#endif
}
void loop()
{
// loop frequency = 50hz (20 ms)
static unsigned long last = 0;
unsigned long now = millis();
if (now > last + 20) {
last = now;
//
static int counter = 0;
counter++;
if (counter == 1000) counter = 0;
//digital
//==> https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/
byte port = 0;
static byte port0_prev = 0;
port = 0x00;
port |= (0 ? 0x01 : 0x00); // EXCLUDED ==> no interrupt.. let's just leave it.
port |= (digitalRead(D1) ? 0x02 : 0x00);
port |= (digitalRead(D2) ? 0x04 : 0x00);
port |= (digitalRead(D3) ? 0x08 : 0x00); // by design, PULLED-UP, being used for the FLASH button.
port |= (0 ? 0x10 : 0x00); // EXCLUDED ==> LED output
port |= (digitalRead(D5) ? 0x20 : 0x00);
port |= (digitalRead(D6) ? 0x40 : 0x00);
port |= (digitalRead(D7) ? 0x80 : 0x00);
if (port0_prev != port) Firmata.sendDigitalPort(0, port);
port0_prev = port;
//analog 0
Firmata.sendAnalog(0, analogRead(0));
//send out alive counter every 1sec. (50 counts x 20ms == 1 sec.)
if (counter % 50 == 0) {
static int alivecounter = 0;
Firmata.sendAnalog(1, alivecounter);
alivecounter++;
if (alivecounter == 1000) alivecounter = 0;
}
}
//process firmata stream
while (Firmata.available()) {
Firmata.processInput();
}
#if defined(WIRELESS)
//firmata over wi-fi
serverStream.maintain();
//
ArduinoOTA.handle();
#endif
//for wdt
yield();
}

92
pd/arduino-serial-test.pd Normal file
View file

@ -0,0 +1,92 @@
#N canvas 492 30 602 584 12;
#X obj 25 349 route digital analog;
#X obj 25 63 tgl 20 0 empty empty LED\ ON/OFF 0 -10 0 12 #7c7c7c #ffffff #000000 0 1;
#X msg 25 137 digital 2 \$1;
#X obj 356 389 vsl 20 170 0 1 0 0 empty empty analog0 10 -9 0 12 #e3c6ff #000000 #000000 0 1;
#X floatatom 356 338 5 0 0 0 - - - 0;
#X obj 356 313 route 0;
#X obj 25 88 == 0;
#X floatatom 25 113 5 0 0 0 - - - 0;
#X text 97 112 D4 == GPIO2;
#X obj 25 470 route 1 2 3 5 6 7, f 25;
#X obj 25 508 tgl 20 0 empty empty D1 0 30 0 12 #ffc7c6 #000000 #000000 0 1;
#X obj 53 508 tgl 20 0 empty empty D2 0 30 0 12 #ffe3c6 #000000 #000000 0 1;
#X obj 82 508 tgl 20 0 empty empty D3 0 30 0 12 #feffc6 #000000 #000000 0 1;
#X obj 111 508 tgl 20 0 empty empty D5 0 30 0 12 #c6ffc7 #000000 #000000 0 1;
#X obj 139 508 tgl 20 0 empty empty D6 0 30 0 12 #c6feff #000000 #000000 0 1;
#X obj 168 508 tgl 20 0 empty empty D7 0 30 0 12 #c7c6ff #000000 #000000 0 1;
#X text 74 15 ==== digital OUTPUT ===;
#X text 74 415 ==== digital INPUT ===;
#X text 305 287 ==== analog INPUT ===;
#N canvas 836 419 450 201 scope 0;
#N canvas 0 50 450 250 (subpatch) 0;
#X array scope 200 float 0;
#X coords 0 1.5 200 -0.5 200 140 1 0 0;
#X restore 26 29 graph;
#X obj 367 76 f;
#X obj 403 76 + 1;
#X obj 272 56 t a b;
#X floatatom 367 126 5 0 0 0 - - - 0;
#X obj 272 150 tabwrite scope;
#X obj 367 101 mod 200;
#X obj 272 31 inlet;
#X connect 1 0 2 0;
#X connect 1 0 6 0;
#X connect 2 0 1 1;
#X connect 3 0 5 0;
#X connect 3 1 1 0;
#X connect 4 0 5 1;
#X connect 6 0 4 0;
#X connect 7 0 3 0;
#X restore 435 439 pd scope;
#N canvas 0 0 361 269 net 0;
#X obj 28 88 route 1;
#X floatatom 28 138 5 0 0 0 - - - 0;
#X obj 28 113 * 1023;
#X obj 28 162 bng 20 250 50 0 empty empty live! 0 30 0 12 #fcfcfc #000000 #000000;
#X text 84 20 ==== network STATUS ===;
#X obj 28 58 inlet;
#X obj 101 138 del;
#X msg 101 113 stop \, 2000;
#X obj 28 217 outlet;
#X obj 101 217 outlet;
#X connect 0 0 2 0;
#X connect 1 0 3 0;
#X connect 2 0 1 0;
#X connect 3 0 7 0;
#X connect 3 0 8 0;
#X connect 5 0 0 0;
#X connect 6 0 9 0;
#X connect 7 0 6 0;
#X restore 381 190 pd net;
#X obj 381 215 bng 20 250 50 0 empty empty (online) 0 30 0 12 #fcfcfc #000000 #000000;
#X text 380 169 connection monitoring;
#X obj 460 215 bng 20 250 50 0 empty empty disconnected! 0 30 0 12 #fcfcfc #000000 #000000;
#X obj 510 206 print X!;
#X obj 25 238 arduino;
#X msg 48 196 devices;
#X msg 38 172 devicename COM4;
#X msg 124 202 close;
#X connect 0 0 9 0;
#X connect 0 1 5 0;
#X connect 0 1 20 0;
#X connect 1 0 6 0;
#X connect 2 0 25 0;
#X connect 3 0 19 0;
#X connect 4 0 3 0;
#X connect 5 0 4 0;
#X connect 6 0 7 0;
#X connect 7 0 2 0;
#X connect 9 0 10 0;
#X connect 9 1 11 0;
#X connect 9 2 12 0;
#X connect 9 3 13 0;
#X connect 9 4 14 0;
#X connect 9 5 15 0;
#X connect 20 0 21 0;
#X connect 20 1 23 0;
#X connect 23 0 24 0;
#X connect 25 0 0 0;
#X connect 26 0 25 0;
#X connect 27 0 25 0;
#X connect 28 0 25 0;

150
pd/arduino-wifi-test.pd Normal file
View file

@ -0,0 +1,150 @@
#N canvas 221 33 602 588 12;
#X msg 154 238 disconnect;
#X obj 381 39 bng 20 250 50 0 empty empty search\ &&\ connect 0 -10 0 12 #fcfcfc #000000 #000000;
#N canvas 0 0 489 532 arduino 0;
#X obj 19 77 zconf.browse;
#X obj 19 290 zconf.resolve;
#X msg 19 249 resolve _arduino._tcp. esp8266-0b5620 local.;
#X obj 19 315 route resolve;
#X obj 59 371 list split 1;
#X obj 19 346 list split 5;
#X obj 59 412 separate .;
#X obj 59 437 t b a;
#X msg 91 462 set connect \$1.\$2.\$3.\$4 27016;
#X obj 59 493 outlet;
#X text 134 489 connect msg. builder with IP & port number;
#X obj 203 159 unpack f f f f, f 18;
#X floatatom 203 119 5 0 0 0 - - - 0;
#X floatatom 244 119 5 0 0 0 - - - 0;
#X floatatom 285 119 5 0 0 0 - - - 0;
#X floatatom 326 119 5 0 0 1 IP - - 0;
#X listbox 203 97 20 0 0 1 ident - - 0;
#X obj 19 112 route add;
#X obj 19 137 list;
#X msg 51 224 set resolve \$2 \$1 \$3;
#X obj 19 199 t b a;
#X msg 19 52 type \, type _arduino._tcp;
#X msg 139 108 set \$1;
#X obj 19 18 inlet;
#X text 171 372 depends on: external: zconf \, else;
#X text 171 324 mDNS discovery (a.k.a. zeroconf \, bonjour);
#X connect 0 0 17 0;
#X connect 1 0 3 0;
#X connect 2 0 1 0;
#X connect 3 0 5 0;
#X connect 4 0 6 0;
#X connect 5 1 4 0;
#X connect 6 0 7 0;
#X connect 6 0 11 0;
#X connect 7 0 9 0;
#X connect 7 1 8 0;
#X connect 8 0 9 0;
#X connect 11 0 12 0;
#X connect 11 1 13 0;
#X connect 11 2 14 0;
#X connect 11 3 15 0;
#X connect 17 0 18 0;
#X connect 18 0 22 0;
#X connect 18 0 20 0;
#X connect 19 0 2 0;
#X connect 20 0 2 0;
#X connect 20 1 19 0;
#X connect 21 0 0 0;
#X connect 22 0 16 0;
#X connect 23 0 21 0;
#X coords 0 -1 1 1 185 80 1 200 60;
#X restore 381 64 pd arduino mDNS discovery;
#X obj 25 258 arduino-wifi;
#X obj 106 283 tgl 20 0 empty empty connected? 23 10 0 12 #bbbbbb #000000 #000000 0 1;
#X obj 106 308 bng 20 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X obj 25 349 route digital analog;
#X obj 25 63 tgl 20 0 empty empty LED\ ON/OFF 0 -10 0 12 #7c7c7c #ffffff #000000 0 1;
#X msg 25 137 digital 2 \$1;
#X obj 356 389 vsl 20 170 0 1 0 0 empty empty analog0 10 -9 0 12 #e3c6ff #000000 #000000 0 1;
#X floatatom 356 338 5 0 0 0 - - - 0;
#X obj 356 313 route 0;
#X obj 25 88 == 0;
#X floatatom 25 113 5 0 0 0 - - - 0;
#X text 97 112 D4 == GPIO2;
#X obj 25 470 route 1 2 3 5 6 7, f 25;
#X obj 25 508 tgl 20 0 empty empty D1 0 30 0 12 #ffc7c6 #000000 #000000 0 1;
#X obj 53 508 tgl 20 0 empty empty D2 0 30 0 12 #ffe3c6 #000000 #000000 0 1;
#X obj 82 508 tgl 20 0 empty empty D3 0 30 0 12 #feffc6 #000000 #000000 0 1;
#X obj 111 508 tgl 20 0 empty empty D5 0 30 0 12 #c6ffc7 #000000 #000000 0 1;
#X obj 139 508 tgl 20 0 empty empty D6 0 30 0 12 #c6feff #000000 #000000 0 1;
#X obj 168 508 tgl 20 0 empty empty D7 0 30 0 12 #c7c6ff #000000 #000000 0 1;
#X text 74 15 ==== digital OUTPUT ===;
#X text 74 415 ==== digital INPUT ===;
#X text 305 287 ==== analog INPUT ===;
#N canvas 836 419 450 201 scope 1;
#N canvas 0 50 450 250 (subpatch) 0;
#X array scope 200 float 0;
#X coords 0 1.5 200 -0.5 200 140 1 0 0;
#X restore 26 29 graph;
#X obj 367 76 f;
#X obj 403 76 + 1;
#X obj 272 56 t a b;
#X floatatom 367 126 5 0 0 0 - - - 0;
#X obj 272 150 tabwrite scope;
#X obj 367 101 mod 200;
#X obj 272 31 inlet;
#X connect 1 0 2 0;
#X connect 1 0 6 0;
#X connect 2 0 1 1;
#X connect 3 0 5 0;
#X connect 3 1 1 0;
#X connect 4 0 5 1;
#X connect 6 0 4 0;
#X connect 7 0 3 0;
#X restore 435 439 pd scope;
#N canvas 0 0 361 269 net 0;
#X obj 28 88 route 1;
#X floatatom 28 138 5 0 0 0 - - - 0;
#X obj 28 113 * 1023;
#X obj 28 162 bng 20 250 50 0 empty empty live! 0 30 0 12 #fcfcfc #000000 #000000;
#X text 84 20 ==== network STATUS ===;
#X obj 28 58 inlet;
#X obj 101 138 del;
#X msg 101 113 stop \, 2000;
#X obj 28 217 outlet;
#X obj 101 217 outlet;
#X connect 0 0 2 0;
#X connect 1 0 3 0;
#X connect 2 0 1 0;
#X connect 3 0 7 0;
#X connect 3 0 8 0;
#X connect 5 0 0 0;
#X connect 6 0 9 0;
#X connect 7 0 6 0;
#X restore 381 190 pd net;
#X obj 381 215 bng 20 250 50 0 empty empty (online) 0 30 0 12 #fcfcfc #000000 #000000;
#X text 380 169 connection monitoring;
#X obj 460 215 bng 20 250 50 0 empty empty disconnected! 0 30 0 12 #fcfcfc #000000 #000000;
#X obj 510 206 print X!;
#X msg 106 193 connect 192.168.137.113 27016;
#X connect 0 0 3 1;
#X connect 1 0 2 0;
#X connect 2 0 31 0;
#X connect 3 0 6 0;
#X connect 3 1 4 0;
#X connect 4 0 5 0;
#X connect 6 0 15 0;
#X connect 6 1 11 0;
#X connect 6 1 26 0;
#X connect 7 0 12 0;
#X connect 8 0 3 0;
#X connect 9 0 25 0;
#X connect 10 0 9 0;
#X connect 11 0 10 0;
#X connect 12 0 13 0;
#X connect 13 0 8 0;
#X connect 15 0 16 0;
#X connect 15 1 17 0;
#X connect 15 2 18 0;
#X connect 15 3 19 0;
#X connect 15 4 20 0;
#X connect 15 5 21 0;
#X connect 26 0 27 0;
#X connect 26 1 29 0;
#X connect 29 0 30 0;
#X connect 31 0 3 1;

1182
pd/arduino-wifi.pd Normal file

File diff suppressed because it is too large Load diff