Archive

Archive for January, 2019

#Arduino Packet Sniffer using w5100 ethernet shield.

packet-sniffer

It is often surprising how busy your home network is, even when you think nothing much is happening on it, our homes are filled with smart devices that chat constantly on the network, constantly announcing technical details about themselves. If you run Wireshark on your computer, you can see just how noisy the network is.

This bit of code sniffs packets off the network, using a Arduino and a W5100 based ethernet shield. Note that this isĀ  will only pick up packets directed at the arduino, or broadcast packets, and since the serial interface is way slower than your network connection, expect plenty of dropped packets.

I’ve got the output to show human-readable text where possible, – filtering out the null character (0), which interfered with copy and pasting the output. There’s not much intellegence going on here, so there’s going to be IPv4 mixed with IPv6 and ARP frames here.

The code is based on Nicolas Humfrey’s W5100 Mac Raw project here https://github.com/njh/W5100MacRaw

#include “w5100.h”

const byte mac_address[] = {
0xae, 0x03, 0xf3, 0xc7, 0x08, 0x78
};

Wiznet5100 w5100;

void setup() {
// Setup serial port for debugging
Serial.begin(115200);
w5100.begin(mac_address);
}
uint8_t buffer[800];
void loop() {
uint16_t len = w5100.readFrame(buffer, sizeof(buffer));
if ( len > 0 ) {
for(uint16_t i=0; i<len; i++)
{
if (buffer[i]>0)
{
Serial.print((char)buffer[i]);
}
}
Serial.println();
}
}

For more information on Packet Sniffing, check out chapter 10 in my book

http://webtropy.com/articles/book-network-programming-art6-10.asp

Categories: Uncategorized

24 Million car database in the #UK

Word Cloud "Big Data"

The UK has 30 Million cars, and this database covers 24.5 Million of them. Cars under 3 years old are not available, nor are Northen Irish cars.

Fields available are

  • registration
  • make
  • model
  • firstUsedDate
  • fuelType
  • primaryColour
  • registrationDate
  • manufactureDate
  • engineSize
  • LastMOT
  • LastTestResult
  • LastOdometerValue
  • LastOdometerUnit

Anyone interested should contact via https://www.regcheck.org.uk/

Categories: Uncategorized

Getting started with the Ethernet Shield in #Arduino @rdegges

img_5474

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 https://www.arduino.cc/en/Tutorial/WebClient

#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[] = “api.ipify.org”; // name address for ipify.com (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:
Serial.begin(9600);
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 “);
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print(“connecting to “);
Serial.print(server);
Serial.println(“…”);

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print(“connected to “);
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println(“GET /?format=json HTTP/1.1”);
client.println(“Host: api.ipify.org”);
client.println(“Connection: close”);
client.println();
} 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;
client.read(buffer, 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.println();
Serial.println(“disconnecting.”);
client.stop();
Serial.print(“Received “);
Serial.print(byteCount);
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(rate);
Serial.print(” kbytes/second”);
Serial.println();

// do nothing forevermore:
while (true) {
delay(1);
}
}
}

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;

ipify

Categories: Uncategorized

HelloWorld on #Arduino

img_5473

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 https://www.arduino.cc/en/Main/Software 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

hello-world

Categories: Uncategorized

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

661763-636519532411453000-16x9

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))
{
reader.MoveToContent();
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!”);
Response.End();
}
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())
{
lClubs.Add(xeClub);
}
}
}
var strJson = JsonConvert.SerializeObject(lClubs, Newtonsoft.Json.Formatting.Indented);
Response.ContentType = “application/json”;
Response.AddHeader(“Access-Control-Allow-Origin”, “*”);
Response.Write(strJson);

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