DIY Dynamic DNS with DreamHost


I recently got a new internet connection from Hyperoptic… which is night and day compared to the rubbish Sky ADSL2+ I had before (6/1mbit vs 1000/1000mbit). As such, it is now practical to run servers from my home connection. (for personal use only, if you’re reading this hyperoptic ūüėČ )

When I was living in Australia and had a decent internet connection, I typically used DynDNS as it was free (it no longer is) and was supported by my router.

Now that DnyDNS isn’t free, nor does my new router support it, I had to find another solution.

I use DreamHost for my web hosting (i.e this site), so I wondered if there was a way to leverage their DNS dynamically.

As it turns out, DreamHost have an API, which has support for creation/deletion/etc of DNS records, and there is some documentation on this on their wiki.

From this, I decided to build my own Dynamic DNS daemon in Java. This daemon will run on my Raspberry Pi3.

The daemon itself is pretty simple, at a set interval it checks my external IP (using this amazon service) and then checks the DNS record for the domain I specify. If it’s the same, nothing is done. If the IP is different, the old record is removed and a new one added. If there is no record there, a new one is added.

It uses a simple JSON file for storing preferences. This version doesn’t have much in the way of logging or error handling… But it was written in a few hours and is very much a work in progress.

If you want to take a look at the source/fork it, it is available on my github here.

If you just want to run it, you can download it from here and then use: java -jar dh-dyn-dns.jar to start it.

Adding and changing a record
Adding and changing a record, as you can see, it’s pretty basic.

If you want to run it as a service on *nix, download this and¬†follow the instructions below; (this service *might* also work on windows, however I haven’t tested it)

  1. You will need a Dreamhost API key, which you can get here, when setting it up, only chose the ‘All DNS functions’ option
  2. Then you will need to install Apache Jsvc, if you are using ubuntu/raspbian/debian/etc run the following command;
    sudo apt-get install Jsvc
  3. Extract the zip to your home directory
  4. Edit the ‘preferences.json’¬†file¬†to match your confiuration
  5. If you are using a Raspberry Pi, you should be good to go. You can start the service by running the below command;
    ./dh-dyn-dns start
  6. If you are using another configuration, you will need to edit the dh-dyn-dns file and set the correct $JAVA_HOME path.

You can then add the service to init.d (for raspbian/ubuntu/etc) if you want it to start automatically on boot.

Feel free to leave a comment below if you have any questions/comments/ideas ūüôā


SOQL to CSV Utility

Recently, I needed to come up with a way for some simple case metrics to be available to users, these metrics need to contain cases that the user may not necessarily have visibility too.

Now, there are a couple of options here;

  • Dashboard
  • Scheduled report
  • Something custom

Now the first two options are the ones I would normally use, both dashboards and report can be scheduled to be run as a user that has access to all cases, and emailed to those who need to see them, dashboards having the added advantage that they can be viewed any time.

But what if you need the data in excel? Well, you could copy/paste from the scheduled email, or get someone to export it for you. But neither are a great solution.

So you could build something custom, which is what I have done here;

Screen Shot 2016-03-14 at 11.46.46 AM

Now, this tool, on its own isn’t much use to your average end user, as a working knowledge of SOQL is required to use it. However, this can very easily be extended¬†to store frequently used ‘exports’ in a custom setting, use custom permissions¬†to grant access to the tool, attach the resulting CSV to a record, or be scheduled to send the csv via email at a set interval using scheduleable apex.

Here is how it works;

The SOQL query entered in the text box is run against the database, returning a list of SObjects. Once we have this list we need to construct a CSV.

The first line of the CSV is the column headings, taken from the text box on the page (or alternatively, the API names of the field entered as part of the query string). For the sake of simplicity, I have simply extracted these fields from the SOQL query (the fields in the list are always in the same order as in the query), however you could use the SObjectDescribe to do fancier things here.

The code then loops through the list of SObjects, and subsequently the list of field names, each loop writing a line of the CSV and then adding a new line at the end

 for(Sobject s :sobjects) {
            for(String fn :fieldNames) {
                csvReport += '"' + s.get(fn) + '"' + ',';
            csvReport += '\r\n';

We then return the CSV as a string to the VisualForce¬†controller, and display it on a page. In order to make the page ‘render as’ a CSV, rather than HTML or text, we need to set the content type attribute of <apex: page>, this also allows us to set the filename of the downloaded CSV.

<apex:page controller="SOQLCSVController" 
contentType="application/csv#{!fileName}.csv"cache="true" showheader="fase">
    <apex:outputText value="{!stringCSV}" />

That really is it, on pressing the Download button, the user is sent to the Download VisualForce page, which renders as a CSV and the browser downloads.

If you wanted to send the generated CSV via email, or attach it to a record you could simply call the Blob class to and attach the resulting blob to an email or record.

The complete code for this example is sitting on my github, feel free to use it and extend it as you wish.

Siri, turn on the light (Part 1)


As you may or may not know, Apple has a technology called HomeKit. Put simply, it allows you to control devices (lights, heating, etc) from your iPhone, and it integrates with Siri.

Several companies make HomeKit enabled devices (e.g Phillips Hue) that you can easily install to take advantage of this… but what is the fun of that?

Luckily, there exists a very good HomeKit Java library, so armed with that, an ESP8266 I set out to see if I could make my own HomeKit enabled devices.

The rough plan was, have the ESP8266 handle the hardware side of things (e.g actually turn things on and off, via a relay) and built a Java application that acts as a HomeKit Bridge.

The first version of the application I built was very basic, it essentially used the example code provided with the library, but one thing I quickly learnt is that HomeKit is like a database, rather than an actual application. You need an app to add entries into the database in order to use it.

Elgato Eve is one such app (which is designed to work with their devices) or there is Home¬†that is designed to work with any HomeKit device (commercial or otherwise). Another option is Apple’s own ‘example code’ called HomeKit Catalog, if you have a Mac with Xcode installed, you can install this on your device (see here for instructions)

I went with HomeKit Catalog, so with that installed and running I was able to test my application. Initially, it worked but would not maintain pairing if the Java application was stopped and re-started, to spite me persisting the data that was required for this. I solved that issue (a method returning weather a device has been paired was not implemented correctly) and was able to move onto the next problem…

How to communicate with the ESP8266 (and actually turn things on and off!) I decided to go with the MQTT protocol for this, its a lightweight and fairly secure publish/subscribe protocol that is often used in low bandwidth/low power applications. The ESP8266 is a very powerful piece of kit, so by using this I have plenty of headroom for other things.

MQTT uses a client/server architecture, meaning I would need a MQTT Broker (server) running, and both my Java application and my ESP8266 would be clients, with the ESP8266 listening for publications from the Java application in order to know when to turn the attached relay on and off. Eclipse make a very good MQTT client library (and application) called Paho that I used in my project. For my MQTT Broker I used Moquette (which I could integrate in to my Java application at a later date)

Next, I had to program the ESP8266. I have done a lot of tinkering in the past with Arduinos, and since you can now program an ESP8266 with the Arduino IDE, I used that. The sketch is fairly simple, it just listens to a topic (A, or B) and then for a value of 1 or 0 (on or off) and sets a GPIO pin high or low, triggering a relay.

So after adding the Paho library to my Java application, I had a means to communicate easily between the Java application and the ESP8266, so time to put it all together and try it out.

And it worked! The Java application is running on my MacBook, so the next step is to get this running on my raspberry pi (and perhaps integrate control of the GPIO pins) as well as support for devices other than simple switches.

I am also working on a design for a ESP8266 relay board that I will talk about in more detail in a future post.

If you want to try this for yourself, the code is available on my github, click here for the Java application and here for the ESP8266 sketch.

A word of warning: working with mains electricity (in this case 240v) is dangerous! Do not attempt this unless you know what you are doing, regardless I take no responsibility for your actions and any damage caused by them.

Australia Day 2015: Slip ‘n Slide + Salesforce

NOTE: This is a previously unpublished post from 2015

When you think of Australia day what would normally come to mind? Celebrating with mates? A BBQ and (often too many) beers? All of these things are pretty standard when it comes to celebrating Australia’s national holiday… but one thing that probably doesn’t come to mind is Salesforce.

For those of you outside of Australia, Australia Day is a national holiday celebrating the foundation of Australia. Like most things here, it’s a pretty relaxed affair. A newer tradition is to listen to the Triple J Hottest 100, a crowd sourced music countdown on Australia’s national alternative radio station. Something else a lot of Aussies do, is setup a backyard slip-n-slide.

So, what does this have to do with Salesforce, you ask. Well… every year a couple of my mates who are electrical engineers host an Australia day party… and being engineers, they couldn’t just settle for any old backyard slip-n-slide… No, their slip-n-slide had a full timing system, leaderboards and a drag racing style Christmas tree! The previous iterations of the slide lacked analytics and a way to track who performed each ride… enter Salesforce.

This year I jumped in on the project and using Salesforce, provided a way to track each person’s runs, display all run-times easily and allow for data from other sources (e.g. the currently playing song on the Triple J Hottest 100) to merge with our data.

Every time a person wanted to use the slide, they’d scan their unique barcode, wait for the traffic light  to go green, and then do their run. The christmas tree would not go green until the course was clear. A series of 6 sensors, placed along the slide run would report data back to a microcontroller (in this case, a teensy 3.0). This data was calculated into times and pushed into Salesforce by a processing sketch. The processing sketch also handled scanning of barcodes and control of the Christmas tree.

Each run down the slide was recorded inside Salesforce, including sector times, total time and speed, with each run being related to the current rider’s contract record as well as the currently playing song on Triple J.

So how did we achieve this? Each time a person scanned their barcode the data was recorded by the processing sketch, and once a run had been completed the barcode number, along with all of the timing data, was pushed in to Salesforce via the processing sketch. As processing is derived from java, It was easy to write a Salesforce connector using existing libraries leveraging the salesforce REST API. This connector created a slide run record in a custom object from this data each time a run was completed.

Upon creation of this record a trigger would fire that called a custom web service class. This web service made a callout to retrieve an XML file that Triple J updated each time a song is played. The class retrieves the file, parses it and stores the currently playing song information against the record.

To tie this all together, a custom app appropriately called ‘Straya Day was created, which consisted of the object tabs and several visualforce tabs to serve as leader boards. This resulted in three main visualforce pages;

  • Last 10 Runs – Displays the last 10 runs down the slide, with the last run in a larger font at the top of the page, as well as the currently playing track on Triple J
  • Top 10 Runs – Displays two tables, the 10 fastest runs by ET and the 10 fastest runs by total time. Also displays the last run and currently playing track in large font at the top of the page.
  • Top 10 by Gender – Displays two tables, 10 fastest runs by girls sorted by total time, and 10 fastest runs by guys sorted by total time. Also displays the last run and currently playing track in large font at the top of the page.

The ‚ÄėStraya Day app was then displayed on a laptop, with the page set to refresh every 10 seconds, so that when a rider had completed their run, they could view their time and the currently playing song on the laptop.

Now for some photos.

IMG_20150126_115745 IMG_20150126_115707

The slip and slide itself, running down the side of the house and round into a
catchment area at the end. You can see the yellow timing beams beside the slide.

IMG_20150126_124327 IMG_20150126_125915

The VisualForce page and people checking their recent runs.
IMG_20150126_115907 IMG_20150126_132200

The big mess-o-wires (teensy3, etc) collecting data from the sensors and reporting back to the processing sketch.

If you are keen to get started on something like this yourself? I have placed some example code for the processing sketch, as well as the JJJ fetch web service on my github.