Skip to content

Instantly share code, notes, and snippets.

@rickbassham
Last active April 25, 2023 18:33
Show Gist options
  • Save rickbassham/2912a558ec9cc8e0f86aa776503eb25c to your computer and use it in GitHub Desktop.
Save rickbassham/2912a558ec9cc8e0f86aa776503eb25c to your computer and use it in GitHub Desktop.
Alpaca Discover Example - Arduino
#define USE_THIS_SS_PIN 17
#define RESPONSE_BUFFER_SIZE 8192
#define _ETG_LOGLEVEL_ 0
#define BOARD_ID 0xDB
#define HARDWARE_VERSION_STRING "2.0.1"
#define HARDWARE_VERSION_MAJOR 2
#define HARDWARE_VERSION_MINOR 0
#define HARDWARE_VERSION_PATCH 1
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet_Generic.h>
bool ethernetConnected = false;
EthernetUDP alpacaDiscovery;
byte *mac;
String macAddress;
bool isConnected = false;
#include <pico/unique_id.h>
byte *generateMacAddress(byte boardId)
{
static byte mac[6];
pico_unique_board_id_t id;
pico_get_unique_board_id(&id);
mac[0] = 0x02; // Self Defined Mac
mac[1] = boardId;
mac[2] = id.id[4];
mac[3] = id.id[5];
mac[4] = id.id[6];
mac[5] = id.id[7];
return mac;
}
String getMac(byte boardId)
{
byte *mac = generateMacAddress(boardId);
char macStr[18];
memset(&macStr, 0, 18);
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr);
}
String getBoardSerial()
{
static String serial;
if (serial.length() > 0)
{
return serial;
}
char buffer[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
pico_get_unique_board_id_string(buffer, sizeof(buffer));
serial = String(buffer);
return serial;
}
String generateHostname(String prefix)
{
return prefix + getBoardSerial();
}
unsigned long lastEthernetCheck = 0;
void initializeEthernet()
{
if (millis() - lastEthernetCheck > 1000)
{
lastEthernetCheck = millis();
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.print(".");
return;
}
if (Ethernet.begin(mac, 5000, 2000) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
}
else
{
Serial.print(F("Connected! IP address: "));
Serial.println(Ethernet.localIP());
Serial.print(F("MAC Address: "));
Serial.println(macAddress);
#ifdef MDNS_FEATURE
mdns.begin(Ethernet.localIP(), hostname.c_str());
mdns.addServiceRecord(DEVICE_NAME "._http",
80,
MDNSServiceTCP);
#endif
ethernetConnected = true;
}
}
}
void commonSetup()
{
Serial.println("commonSetup");
mac = generateMacAddress(BOARD_ID);
macAddress = getMac(BOARD_ID);
Serial.println(macAddress);
Ethernet.setHostname("testboard");
Ethernet.init(USE_THIS_SS_PIN);
initializeEthernet();
Serial.print(F("Serial Number: "));
Serial.println(getBoardSerial());
}
void checkForAlpacaDiscovery()
{
int packetSize = alpacaDiscovery.parsePacket();
if (packetSize > 0)
{
char message[64];
memset(message, 0, sizeof(message));
int ret = alpacaDiscovery.read(message, sizeof(message));
if (strncmp("alpacadiscovery1", message, 16) == 0)
{
alpacaDiscovery.beginPacket(alpacaDiscovery.remoteIP(), alpacaDiscovery.remotePort());
alpacaDiscovery.print("{\"AlpacaPort\":80}");
alpacaDiscovery.endPacket();
}
}
}
void loop()
{
watchdog_update();
if (!ethernetConnected)
{
initializeEthernet();
}
else
{
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
ethernetConnected = false;
lastEthernetCheck = millis();
return;
}
Ethernet.maintain();
#ifdef MDNS_FEATURE
mdns.run();
#endif
checkForAlpacaDiscovery();
}
}
void setup()
{
Serial.begin(9600);
sleep_ms(1000);
Serial.println("Starting up");
commonSetup();
alpacaDiscovery.begin(32227);
watchdog_enable(0x7fffff, false);
}
[env:pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
board_build.core = earlephilhower
board_build.variant = wiznet_5500_evb_pico
board_upload.maximum_size = 16777216
board_build.filesystem_size = 1024k
lib_deps =
SPI
khoih-prog/Ethernet_Generic@^2.6.2
lib_ignore =
WiFiNINA_Generic
WiFiMulti_Generic
WiFi101
WiFiEspAT
QNEthernet
HTTPClient
WiFi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment