Skip to content

Instantly share code, notes, and snippets.

@tstellanova
Created January 4, 2022 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstellanova/bc4d5f1e8fcc13c6f22cd9553442af49 to your computer and use it in GitHub Desktop.
Save tstellanova/bc4d5f1e8fcc13c6f22cd9553442af49 to your computer and use it in GitHub Desktop.
Simple Particle BSOM example that uses the Ethernet capability of the M.2 SOM Eval Board (EVB)
/*
* Project etherecho
* Description:
* Author:
* Date:
* Can use eg `telnet 10.0.1.47 80` to connect to this echo server
* Ensure your EVB has ETH_* jumpers connected.
*/
#include "Particle.h"
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);
SerialLogHandler logHandler(115200, LOG_LEVEL_INFO);
TCPServer server = TCPServer(80); //setup server on port 80
TCPClient client;
void setup() {
delay(4000);
Log.info("setup start");
System.enableFeature(FEATURE_ETHERNET_DETECTION);
Ethernet.on();
Ethernet.connect();
Log.info("setup done");
}
void loop() {
static bool server_started = false;
if (Ethernet.ready()) {
if (!server_started) {
uint8_t addr[6] = {};
Ethernet.macAddress(addr);
Log.info("mac: %02x-%02x-%02x-%02x-%02x-%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
Log.info("subnetMask=%s", Ethernet.subnetMask().toString().c_str());
Log.info("gatewayIP=%s", Ethernet.gatewayIP().toString().c_str());
Log.info("dnsServerIP=%s", Ethernet.dnsServerIP().toString().c_str());
Log.info("localIP=%s", Ethernet.localIP().toString().c_str());
server.begin();
server_started = true;
}
else {
if (client.connected()) {
// echo all available bytes back to the client
while (client.available()) {
server.write(client.read());
}
}
else {
// if no client is yet connected, check for a new connection
client = server.available();
delay(500);
}
}
}
else {
delay(2000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment