esp32 support

This commit is contained in:
Dooho Yi 2023-05-30 11:02:21 +09:00
parent 5f2b731367
commit eb49d7c51c
3 changed files with 413 additions and 2 deletions

View file

@ -0,0 +1,214 @@
//
// configurablefirmata_esp32
//
// D. Yi @ 2023 5 30
//
#define WIRELESS
#if defined(WIRELESS)
//wi-fi
#include <WiFi.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
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Connected to AP successfully!");
}
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(2, LOW);
}
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Disconnected from WiFi access point");
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.wifi_sta_disconnected.reason);
Serial.println("Trying to Reconnect");
WiFi.begin(ssid, password);
digitalWrite(2, HIGH);
}
#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(23, INPUT_PULLUP);
pinMode(22, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
//digital outputs
pinMode(2, OUTPUT); //build-in LED && also used as wifi indicator!
#if defined(WIRELESS)
digitalWrite(2, HIGH); //HIGH ==> LED ON ==> 'wifi. disconnected.'
#else
digitalWrite(2, LOW); //LOW ==> LED OFF
#endif
//serial (for debug & info)
Serial.begin(57600);
#if defined(WIRELESS)
// delete old config
WiFi.disconnect(true);
delay(1000);
WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
/* Remove WiFi event
Serial.print("WiFi Event ID: ");
Serial.println(eventID);
WiFi.removeEvent(eventID);*/
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.println("Wait for WiFi... ");
#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 |= (digitalRead(23) ? 0x01 : 0x00);
port |= (digitalRead(22) ? 0x02 : 0x00);
port |= (digitalRead(21) ? 0x04 : 0x00);
port |= (digitalRead(19) ? 0x08 : 0x00);
port |= (0 ? 0x10 : 0x00);
port |= (0 ? 0x20 : 0x00);
port |= (0 ? 0x40 : 0x00);
port |= (0 ? 0x80 : 0x00);
if (port0_prev != port) Firmata.sendDigitalPort(0, port);
port0_prev = port;
//send out alive counter every 1sec. (50 counts x 20ms == 1 sec.)
if (counter % 50 == 0) {
static int alivecounter = 0;
Firmata.sendAnalog(0, alivecounter);
alivecounter++;
if (alivecounter == 1000) alivecounter = 0;
}
//analog reads
Firmata.sendAnalog(1, analogRead(36));
Firmata.sendAnalog(2, analogRead(39));
Firmata.sendAnalog(3, analogRead(34));
Firmata.sendAnalog(4, analogRead(35));
//touch reads
Firmata.sendAnalog(5, touchRead(32)); //touch 9
Firmata.sendAnalog(6, touchRead(33)); //touch 8
Firmata.sendAnalog(7, touchRead(27)); //touch 7
Firmata.sendAnalog(8, touchRead(13)); //touch 4
}
//process firmata stream
while (Firmata.available()) {
Firmata.processInput();
}
#if defined(WIRELESS)
//firmata over wi-fi
serverStream.maintain();
//
ArduinoOTA.handle();
#endif
//for wdt
yield();
}

View file

@ -26,14 +26,14 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println("Mac address: " + WiFi.macAddress()); Serial.println("Mac address: " + WiFi.macAddress());
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
digitalWrite(2, HIGH); digitalWrite(D4, HIGH);
} }
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi, trying to reconnect..."); Serial.println("Disconnected from Wi-Fi, trying to reconnect...");
WiFi.disconnect(); WiFi.disconnect();
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
digitalWrite(2, LOW); digitalWrite(D4, LOW);
} }
#endif #endif

View file

@ -0,0 +1,197 @@
#N canvas 221 33 923 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 4 0 0 empty empty gpio36 10 -9 0 12 #dfdfdf #000000 #000000 0 1;
#X floatatom 356 338 5 0 0 0 - - - 0;
#X floatatom 25 113 5 0 0 0 - - - 0;
#X text 97 112 D4 == GPIO2;
#X obj 25 508 tgl 20 0 empty empty D23 0 30 0 12 #ffc7c6 #000000 #000000 0 1;
#X obj 55 508 tgl 20 0 empty empty D22 0 30 0 12 #ffe3c6 #000000 #000000 0 1;
#X text 74 15 ==== digital OUTPUT ===;
#X text 74 415 ==== digital INPUT ===;
#X text 385 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 4.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 275 479 pd scope;
#N canvas 0 0 361 269 net 0;
#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 obj 28 88 route 0;
#X connect 0 0 2 0;
#X connect 1 0 0 0;
#X connect 2 0 6 0;
#X connect 2 0 7 0;
#X connect 4 0 9 0;
#X connect 5 0 8 0;
#X connect 6 0 5 0;
#X connect 9 0 1 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.24 27016;
#X obj 356 313 route 1 2 3 4, f 30;
#X obj 416 389 vsl 20 170 0 4 0 0 empty empty gpio39 10 -9 0 12 #bbbbbb #000000 #000000 0 1;
#X floatatom 416 338 5 0 0 0 - - - 0;
#X obj 476 389 vsl 20 170 0 4 0 0 empty empty gpio34 10 -9 0 12 #9f9f9f #ffffff #000000 0 1;
#X floatatom 476 338 5 0 0 0 - - - 0;
#X obj 536 389 vsl 20 170 0 4 0 0 empty empty gpio35 10 -9 0 12 #7c7c7c #ffffff #000000 0 1;
#X floatatom 536 338 5 0 0 0 - - - 0;
#X obj 25 470 route 0 1 2 3, f 18;
#X obj 86 508 tgl 20 0 empty empty D21 0 30 0 12 #feffc6 #000000 #000000 0 1;
#X obj 117 508 tgl 20 0 empty empty D19 0 30 0 12 #c6ffc7 #000000 #000000 0 1;
#X obj 666 389 vsl 20 170 0 0.1 0 0 empty empty gpio32 10 -9 0 12 #c6feff #000000 #000000 0 1;
#X floatatom 666 338 5 0 0 0 - - - 0;
#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 4.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 585 479 pd scope;
#X obj 726 389 vsl 20 170 0 0.1 0 0 empty empty gpio33 10 -9 0 12 #c7c6ff #000000 #000000 0 1;
#X floatatom 726 338 5 0 0 0 - - - 0;
#X obj 786 389 vsl 20 170 0 0.1 0 0 empty empty gpio27 10 -9 0 12 #e3c6ff #000000 #000000 0 1;
#X floatatom 786 338 5 0 0 0 - - - 0;
#X obj 846 389 vsl 20 170 0 0.1 0 0 empty empty gpio13 10 -9 0 12 #ffffff #0400ff #000000 0 1;
#X floatatom 846 338 5 0 0 0 - - - 0;
#X text 695 287 ==== touch INPUT ===;
#X obj 666 313 route 5 6 7 8, f 30;
#X connect 0 0 3 1;
#X connect 1 0 2 0;
#X connect 2 0 24 0;
#X connect 3 0 6 0;
#X connect 3 1 4 0;
#X connect 4 0 5 0;
#X connect 6 0 32 0;
#X connect 6 1 19 0;
#X connect 6 1 25 0;
#X connect 7 0 11 0;
#X connect 8 0 3 0;
#X connect 9 0 18 0;
#X connect 10 0 9 0;
#X connect 11 0 8 0;
#X connect 19 0 20 0;
#X connect 19 1 22 0;
#X connect 22 0 23 0;
#X connect 24 0 3 1;
#X connect 25 0 10 0;
#X connect 25 1 27 0;
#X connect 25 2 29 0;
#X connect 25 3 31 0;
#X connect 25 4 45 0;
#X connect 27 0 26 0;
#X connect 29 0 28 0;
#X connect 31 0 30 0;
#X connect 32 0 13 0;
#X connect 32 1 14 0;
#X connect 32 2 33 0;
#X connect 32 3 34 0;
#X connect 35 0 37 0;
#X connect 36 0 35 0;
#X connect 39 0 38 0;
#X connect 41 0 40 0;
#X connect 43 0 42 0;
#X connect 45 0 36 0;
#X connect 45 1 39 0;
#X connect 45 2 41 0;
#X connect 45 3 43 0;