Writing /var/www/timeinventorskabinet/public/wiki/data/meta/windclocks/arduino.meta failed
 
Writing /var/www/timeinventorskabinet/public/wiki/data/meta/windclocks/arduino.meta failed

Arduino WindClock step by step Guide


This tutorial shows you how to connect your WindClock to the TAK-Server with Arduino.
It's not specific to any WindClock, so don't expect instructions for your specific model.
At the bottom of the page, you can find the code used here and the code we use for the vortex windclock model.

Prerequisites


I'm working with Arduino 22. Arduino 1 break backward compatibility, so download an older version instead.

Launch your Arduino app, select your board and serialtty from the menu. Basic Arduino set-up is beyond the scope of this tutorial, go to arduino for that.

We'll be working with the standard Ethernet shield, you can also use a Wifi board, use your computer as networking device etc. but here we do it like this.

You'll need you coordinates, easiest way to know is go to googlemaps, pinpoint your location, right click, center map. then paste this:

javascript:void(prompt('',gApplication.getMap().getCenter()));


in the location bar (where your put addresses in your browser) and hit enter. you should get a pop-up with your coordinates.

First


First we invoke the libraries required for the Ethernet shield:

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

Network Configuration

Then we move on to the network set-up. It's important to understand that we don't have DHCP, there's a library for that, but last time I checked it was not functional. This means that network configuration has to be done manually, so you need to know a few things about your local network.
Configuration goes into the script, we're going through it line by line:

the MAC address is the hardware address, you need to change the last digit if you have several on the same network. e.g. { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}, it's hexadecimal notation if you're wondering.

byte mac[] ={ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF}; 

then you need to get yourself an IP address. make sure it's not taken in your network, check your router if unsure, but a high value max 255 should be OK, here we're using 210. Make sure that the other digits match the ones in the gateway, see below.

byte ip[] = {192,168,0,210}; 

now you have to put the address of your router, often it is 1.

byte gateway[] = {192,168,0,1};	

subnet should be like this in most standard home networks:

byte subnet[] = {255,255,255,0};


on more complex networks, double check this if you're having problems, it's important.

finally we need the IP of the server, for tik.okno.be it's: 62.58.108.215

byte server[] = {62,58,108,215}; 

Now we can launch the client:

Client client(server, 30223);

The Strings


Now we need to prepare some strings. The TAK server to arduino connection works with JSON strings, the protocol is documented here, please note that there are significant changes, e.g. no more numerical id and now we have metadata!
More recently the quoting got more strict. Always double quote inside your JSON and escape with

\

. You also have to terminate your string with

\n

.

The next string will be our clock identifier that gets sent to the server to register to the TIK network.

String clock = String("{\"name\":\"esplanade\",\"longitude\":\"0.0\",\"latitude\":\"0.0\",\"altitude\":\"0.0\",\"metaDataDefinitions\":[{\"name\":\"orientation\",\"type\":\"int\"},{\"name\":\"windspeed\",\"type\":\"int\"}],\"test\":false}\n")

Lots of quotes,curly stuff and other not so readable things, but it's easy, just make sure to keep the formatting and only change the values.
First of all, pick a name, any name for your clock and type it in. DON'T USE AN EXISTING ONE, thank you : )

Then, fill in the coordinates for latitude and longitude that you got from google maps.

Next we have to define the metaDataDefinitions. These are the declarations of what extra data we will be sending with every TIK. There are three types: int, float and string. Int is a whole number, float a decimal number and string is text.
This is to ensure that the data we'll be adding with every tik is added and calculated as it should on the TAK server.
Try to keep the names as easy as possible, so they can be related in the database. All lowercase, no fancy punctuation, no alternate spelling.
For example : windspeed, not windSpeed or wind_speed.
Here you can add other metaDataDefinitions to the JSON, for example temperature would give:

String("{"name":"esplanade","longitude":"0.0","latitude":"0.0","altitude":"0.0","metaDataDefinitions":[{"name":"orientation","type":"int"},{"name":"windspeed","type":"int"}, {"name":"temperature","type":"float"}],"test":false}


You can register a clock without metadata like this:

String("{"name":"esplanade","longitude":"0.0","latitude":"0.0","altitude":"0.0","test":false}"); 


But we won't be using that here.

Metadata Variables


let's define some metaData variables that we will paste into our JSON when sending a TIK.
what we send will have to look like this:

//{\"metaDataValues\":[{\"name\":\"orientation\",\"type\":\"float\",\"value\":\"0.5\"},{\"name\":\"windspeed\", \"type\":\"float\",\"value\":\"2.965\"}]}\n
int orientation = 0;
int windspeed = 0;

Setup


This function get called once on startup.
In setup() as well as loop(), every line with Serial in it can be removed if you're not debugging with the computer.

void setup() 
{
  Serial.begin(9600);   
  Ethernet.begin(mac, ip, gateway, subnet); 
  delay(1000); // give ethernet shield time to boot

  //make the connection
  client.connect(); 
  if(client.connected())
  {
     Serial.println("connected!"); 
     client.print(clock); //first connection sends clockId to register with the TAK server
  }
  
  //give a randomSeed for our pseudoTIK, just for our example
  randomSeed(analogRead(6));
}

Loop

void loop() 
{
  //check if connection is still up and start tikking
  if(client.connected() && doesItTik) // doesItTik is a function we're defining below that has to return true for a tik to be sent
  { 
      orientation = analogRead(0); // get some data
      windspeed = analogRead(1); // other data
      
      String tik = String("{\"metaDataValues\":[{\"name\":\"orientation\",\"type\":\"int\",\"value\":") + 
              String(orientation) + 
              String("},{\"name\":\"windspeed\", \"type\":\"int\",\"value\":") +
              String(windspeed) +  String("}]}\n");

      Serial.println(tik);
      client.print(tik);
  }

This is the code that gets executed when we have both a connection and we can send a TIK.
If you don't have any metadata defined, you would just send empty JSONs, like this: {}, but this is not our case.

The types have to match the metaDataDefinition, it's a quick check to see they match.

We're concatenating a lot of String objects together, with the ”+” operator, it's very messy and prone to bugs, but in the scope of this guide it seems the best solution, especially since any libraries would exclude many models of Arduino. So, evil, but it works : )

The following code just pauses, reconnects. Note the use of client.stop() to flush the connection.

  //wait 5 seconds and reconnect
  else
  {
    Serial.println("no connection");
    delay(5000);
    client.stop();
    client.connect();
    client.print(clock);
  }
  
  delay(300); //make this smaller after debugging, this is  a delay in the loop, so it doesn't run as fast as it can
}

doesItTik


Now as you can see we didn't define any function to calculate the TIK. This greatly depends on your model, so here we use a generic function for fakeClock, I'll give more examples later.

//replace by real function connected to WindClock
int doesItTik()
{
  int tikReady = random(100) < 70; // simple probability function
  return tikReady;
}

Debugging


For debugging, a few things to consider.
The Serial.blabla statements will print information in the serial monitor, which is quite useful. Always print in parallel on the client and the serial to figure things out.
The main thing you're looking for is a connection or lack thereof.
If you get “no connection” you might consider changing the server IP to the IP of your computer, on the network locally.
To do this, open a terminal
on OSX

$ nc -l 30223


on GNU/Linux

$ nc -l -p 30223


And you should get clocks and tiks floating in nicely. If you don't, the problem's on your side.
You can run the nc command on a remote machine to test as well, to see if you're getting out, but of course you need to have a box at your disposal then.

Download

the code used in this tutorial: fakeclockmd_01.zip
(updated 20120507, conform to the Balt-flavour of the TAK server)

to see a working example, here's okno's clock script:
isjvortexokno.zip

 
windclocks/arduino.txt · Last modified: 2012/05/08 19:51 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki