Getting started with the Ethernet Shield in #Arduino @rdegges

January 17, 2019 Leave a comment


In order to extend the capabilities of the Arduino, you add what are known as Shields. which are stackable boards that plug into the top of the Arduino board.

Ethernet support is one key feature missing in the Arduino board, so I bought a HiLetGo Ethernet shield from Amazon for £8. It’s based on the W5100 chip, and pretty standard kit.

You plug the shield into the top of the arduino, then connect the USB to the computer, and the ethernet cable to your router.

My project was to get the public IP address of my router – using the ipify API. The code is based on the TCP/IP code listed on

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don’t want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = “”; // name address for (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement

void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet

// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only

// start the Ethernet connection:
Serial.println(“Initialize Ethernet with DHCP:”);
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println(“Ethernet shield was not found. Sorry, can’t run without hardware. :(“);
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println(“Ethernet cable is not connected.”);
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(” DHCP assigned IP “);
// give the Ethernet shield a second to initialize:
Serial.print(“connecting to “);

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print(“connected to “);
// Make a HTTP request:
client.println(“GET /?format=json HTTP/1.1”);
client.println(“Connection: close”);
} else {
// if you didn’t get a connection to the server:
Serial.println(“connection failed”);
beginMicros = micros();

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
byteCount = byteCount + len;

// if the server’s disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.print(“Received “);
Serial.print(” bytes in “);
float seconds = (float)(endMicros – beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(“, rate = “);
Serial.print(” kbytes/second”);

// do nothing forevermore:
while (true) {

Note, that the Mac address listed in the code is really just a random number, as long as it’s unique on your network, it doesn’t cause a problem… and it’s extremely likely to be unique.

The output appears in the serial window such as the following;


Categories: Uncategorized

HelloWorld on #Arduino

January 16, 2019 Leave a comment


This is a really beginers guide to the most simple thing that you can possibly do with an Arduino – Say Hello World.

So, first thing is, go to and download the Windows IDE. This installs all the drivers, and gives you a nice coding interface.

Once loaded, connect the Arduino to your PC using a USB cable. You don’t need a power cable, the USB will provide the power.

Now, edit the code to say the following

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // open the serial port at 9600 bps:

void loop() {
// put your main code here, to run repeatedly:
Serial.print(“Hello World\n”); // prints a label

Now, press Verify and Upload.

Once Uploaded, then press Tools > Serial Monitor, and you should see a long line of “Hello World”‘s


Categories: Uncategorized

Creating an #API from an #XML file in C#

January 10, 2019 Leave a comment


Let’s say you’ve been given a large XML file – Let’s say, it’s too large to be stored in memory, and it contains data that you want to show wihin an App. Perhaps you don’t want to package the XML file with your app, because perhaps it’s big, or perhaps you don’t want someone extracting all your data wholesale, if they decompile your app.

You don’t want to have the app download the XML file, since it’s huge, and is going to be slow to download, and once again, could allow someone who decompiles your app, the ability to copy all your data,  Instead, you want to have your app send search queries to your server, and have your server read the XML file in an efficient way, and return snippets in JSON back to the App.

So, lets create an API in C# to do that, using ASP.NET

First off, you need a way to read an XML file node by node in C#, without reading the entire document into memory, as I said, in this example, imagine the XML file is too big to load into memory at once – or you don’t want to unduly use too much memory on your server.

Here’s a code snippet to do that;

private static IEnumerable<XElement> SimpleStreamAxis(string inputUrl,
string elementName)
using (var reader = XmlReader.Create(inputUrl))
while (reader.Read())
if (reader.NodeType != XmlNodeType.Element) continue;
if (reader.Name != elementName) continue;
var el = XNode.ReadFrom(reader) as XElement;
if (el != null)
yield return el;

Now, assuming I have my XML file in a folder called /Data and it’s called clubs.xml. The Node I want to iterate is called GolfClubDTO and the element I want to search on is called ClubCity – so here’s my code

var strSearch = Request.QueryString[“Search”];
if (string.IsNullOrEmpty(strSearch))
Response.Write(“Pass a Search Param!”);
var strXmlPath = Server.MapPath(“~/data/Clubs.xml”);
var xeClubs = SimpleStreamAxis(strXmlPath, “GolfClubDTO”);
var lClubs = new List<XElement>();
foreach (var xeClub in xeClubs)
// ClubCity
var xClubCity = xeClub.Elements().FirstOrDefault(c => c.Name.LocalName == “ClubCity”);
if (xClubCity != null)
var strClubCity = xClubCity.Value;
if (strClubCity.ToLower() == strSearch.ToLower())
var strJson = JsonConvert.SerializeObject(lClubs, Newtonsoft.Json.Formatting.Indented);
Response.ContentType = “application/json”;
Response.AddHeader(“Access-Control-Allow-Origin”, “*”);

Here I am also writing out the data with the mime type application/json and allowing CORS, to make life easier for my clients.




Categories: Uncategorized

New tech to cut 50% off IOT bandwidth usage.

December 21, 2018 Leave a comment


IOT devices are often connected to the cloud via costly mobile data network connections, where every MB of data transmitted can quickly add up. Quite often an IOT device calls a HTTP endpoint in the cloud, and there is quite alot of needless overhead in the transmission of HTTP headers, and TCP/IP handshakes.

UDP/IP is a protocol designed for transmitting data that can forgive a certain level of dropped packets and out-of-order transmissions, but it has half the overhead of TCP/IP. However, you can’t just send a UDP packet to an HTTP server, and expect it to respond.

This is where comes in, you can define a UDP Port, and a HTTP endpoint, and this gateway will forward data received via the UDP protocol on that port to your defined HTTP endpoint, thus saving you up to 50% on your bandwidth costs.

UDP can half the bandwidth useage of your IOT device with respect to the more normal TCP/IP. If your IOT device is communicating over expensive cellular data networks, and is particularly ‘chatty’ then this can save you significantly on your costs. All this without having to re-write your back-end code, or alter firewall rules.

With, you modify the code in your IOT device to send a UDP packet to our server at a port that we provide you with, and we will forward the data via HTTP-POST to a URL endpoint of your choosing. We monitor for errors, and keep track of your bandwidth usage, so that you can keep an eye on problems in real-time. is free for early adopters, and will remain free for ever for those first registered users. – Give it a go, and send us feedback!

Categories: Uncategorized

Synchronizing #Resx files within a .NET project

December 18, 2018 Leave a comment


If you are developing a multi-lingual website or application in .NET, then you undoubtedly use resx files. Over time, it’s likely that these files go out of sync, when you add text to the english version, and forget to send the french version for translation.

Here’s some code in C# that can compare a master resx file (english) with a candidate file (french), and determine what keys are missing, and what keys are present, but not translated.

To start off with, lets create an XSD model for the resx xml;

<?xml version=”1.0″ encoding=”UTF-8″?>
<xs:schema xmlns:xs=”; elementFormDefault=”qualified”>
<xs:import namespace=”; schemaLocation=”xml.xsd”/>
<xs:element name=”root”>
<xs:element maxOccurs=”unbounded” ref=”resheader”/>
<xs:element maxOccurs=”unbounded” ref=”data”/>
<xs:element name=”resheader”>
<xs:extension base=”value”>
<xs:attribute name=”name” use=”required” type=”xs:NCName”/>
<xs:element name=”data”>
<xs:extension base=”value”>
<xs:element minOccurs=”0″ ref=”comment”/>
<xs:attribute name=”name” use=”required”/>
<xs:attribute ref=”xml:space”/>
<xs:element name=”comment” type=”xs:string”/>
<xs:complexType name=”value”>
<xs:element ref=”value”/>
<xs:element name=”value” type=”xs:string”/>

From this, I create resx.cs by running xsd.exe, and include it in my project

Two helper functions I’m going to need is some code to serialise and deserialise the XML into objects as follows;

public static T ParseXmlData<T>(string xml)
var xsT = new XmlSerializer(typeof(T));
var srT = new StringReader(xml);
return (T)xsT.Deserialize(srT);
public static string Serialize<T>(T o)
var swOutput = new StringWriter();
var xs = new XmlSerializer(typeof(T));
xs.Serialize(swOutput, o);
return swOutput.ToString();

Now, lets create the main function

private static void CompareResx(string masterFile, string testFile, string outputFile)
var strMasterXml = File.ReadAllText(masterFile);
var strTestXml = File.ReadAllText(testFile);
var rootMaster = ParseXmlData<root>(strMasterXml);
var rootTest = ParseXmlData<root>(strTestXml);
if ( == null) return;
var lMissingNodes = new List<data>();
foreach (var entry in
var testEntry = => ==;
if (testEntry != null)
if (testEntry.value1 == entry.value1)
Console.WriteLine(“English?” +;
else if (string.IsNullOrEmpty(testEntry.value1))
Console.WriteLine(“Blank entry:” +;
Console.WriteLine(“Missing entry:” +;
var output = new root(); = lMissingNodes.ToArray();
var strOutputXml = Serialize(output);
File.WriteAllText(outputFile, strOutputXml);

Pass in the location of the master resx, the candidate resx, and an output file, and then run this code.

Categories: Uncategorized

Image recognition using #Azure Computer Vision with C#

December 17, 2018 Leave a comment


Assuming you have an Azure account, if you open up a Computer vision service under Cognitive Services, and grab an API key, you can use Azure to recognise the content of images. – Great for tagging images in blogs, or making sure nobody uploads a nude pic as their profile image on your website – For which you should check out for this!

So, lets see some code – I’m using C#, and Newtonsoft to parse the Json;

const string strUrl = “;;
var wc = new WebClient();
wc.Headers[“Ocp-Apim-Subscription-Key”] = “xxxxxxx”;
var jPost = new { url = Url };
var strPost = JsonConvert.SerializeObject(jPost, Formatting.Indented);
var strJson = wc.UploadString(strUrl, “POST”, strPost);
var jResult = JObject.Parse(strJson);

Depending on your image of course, this will return JSON similar to the following;

  "tags": [
      "name": "car",
      "confidence": 0.99999725818634
      "name": "outdoor",
      "confidence": 0.99585741758347
      "name": "transport",
      "confidence": 0.82641708850861
      "name": "blue",
      "confidence": 0.51219713687897
      "name": "roof",
      "confidence": 0.091790720820427
      "name": "automotive",
      "confidence": 0.01434036432286
      "name": "summer",
      "confidence": 0.0078773201327271
  "requestId": "e0010f8b-189f-4481-ad02-5b71b3ac2b2b",
  "metadata": {
    "width": 1600,
    "height": 1200,
    "format": "Jpeg"


Categories: Uncategorized

Simple #UDP/IP client/server in C#

December 13, 2018 Leave a comment



You can grab the code for this demo here

Internet traffic can be thought of as layers, normally the stack for viewing a website is IP at the bottom, TCP/IP in the middle, and HTTP at the top.

IP just gets data from A to B , TCP/IP makes sure that data doesn’t get jumbled or dropped in between, and HTTP does things specific to serving websites, like HTTP redirects, HTTP error messages etc.

But, instead of TCP/IP you can also use UDP/IP or simply “UDP”, which is TCP’s messy little brother. It still has the concept of ports like TCP has, but offers no guarantee of jumbled or dropped data.

For this reason, UDP is only used in situations where low latency is much more important than data integrity, like audio / video streaming or gaming. It’s terrible for file transfer.

One other key use of UDP is it’s lower bandwidth usage, which makes it useful for IOT applications over expensive mobile data networks.

If you want to read more about Network programming, check out my book- which you can Buy at Amazon US or Buy at Amazon UK


Categories: Uncategorized