Love2d Air Hockey Game

I’ve been playing around some more with the Love2d game framework and made a little 2 player vs air hockey game using the touchscreen. It was neat to experiment with the physics engine using some simple shapes.

It was pretty easy to jump in to using, but creating more complex shapes seems difficult. I haven’t found any better ways than to build them from primitives, like circles and rectangles.

You can download the .love or an Android apk on github.

Love2d Magic Shapes

Simon likes to grab at my cellphone. I saw there a few baby touch games on the playstore, like Frankie’s Baby Fireworks! which he enjoys, so I thought it would be fun to make one of my own using Love2d.

Github Page

This game makes sounds as long as you touch the screen and shows a random shape in random colors with a unique pattern, which follow your fingers around. The shapes are animated using the particle effects api that comes with the Love2d framework.

You can run the .love file if you have love framework installed on Linux/Windows/Mac/Android or use the Android apk I created. I don’t know if I will put it up on the Play store, but you can download it and view the source on Github. It’s more fun on my touchscreen laptop since there is more space to touch.

Love2d is fun and easy to learn for making 2d games. The particle system is neat, but it also includes a physics engine I want to play with some more.

Spark for ESP8266 Wifi Display

Last year I created a simple ESP8266 based Wifi display using a Wemos D1 Mini and a 4×20 character LCD – Video 1 – Video 2. I used a php script for the backend because I wanted it show the time without having to add an RTC, and I didn’t want to format messages on the board. The php script will generate a preformatted message for the board, so it just has to split out the 4 separate lines. This was great, but I want to change the messages a little bit easier.

ESP8266 WiFi Display

Instead of using php this time, I went with Java which I’m much more familiar with, and will run the server on a Raspberry Pi. I have also set the wifi board to fallback to the old php script anytime the new Java server is unavailable. Since I’m using a Pi, it will be up and down a lot as I work on other projects, and don’t want the time on the display to be too far off. The message will change when it falls back, but I don’t mind.

Change message page

Spark

I created a Java app using Spark, which is a Java web app micro framework based on statics and Lambdas. It starts up super fast, typically only a couple hundred millis, and is very lightweight. This works great on something like a Pi or shared environments where resources are limited.

Spark Helloworld

import static spark.Spark.*;

public class HelloWorld {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "Hello World");
    }
}

Thymeleaf

Spark also works with template engines so in addition to services, you can generate html pages. I used Thymeleaf to create a basic message editing page that will display the current message.

To send a Thymeleaf template response with Spark, create a Thymeleaf TemplateEngine and return the result of the engine.render method in your response:

private static final TemplateEngine thymeleaf = new ThymeleafTemplateEngine();
...
Spark.get("/yourPath", (req, res) -> {
  Map<String, Object> model = new HashMap<>();
  model.put("templateVariableName", value);
  model.put("variable2", message2);
  return thymeleaf.render(
      new ModelAndView(model, "templateFileName")
  );
});

build.gradle Dependencies

compile group: 'com.sparkjava', name: 'spark-core', version: '2.6.+'
compile group: 'com.sparkjava', name: 'spark-template-thymeleaf', version: '2.5.5'

Basic Auth

I didn’t want just anyone to be able to change the message, so I added an HTTP Basic Auth filter using QMetrics Spark Authentication library to require a username and password. This was the easiest route to get a minimal level of security, but please note that basic auth is not safe for plain text http connections and should only be used over https. Basic auth actually sends the username and password as a Base64 encoded header string, so without https anyone can decode them. In this case though, there is minimal risk.

Spark.before(new BasicAuthenticationFilter(
		"/*", new AuthenticationDetails(user, pass)));

build.gradle Dependencies

compile 'com.qmetric:spark-authentication:1.4'

Fatjar

I wanted this app to be easy to run. Spark uses Jetty as an embedded http server, so it starts fast and there are only library dependencies, with nothing extra to install. A standard build will generate one jar with your code in it, but all the other libraries still need to be packaged together somehow. In the past, these would have been bundled together in a folder inside of a zip file, but a better approach is to use a fat jar.

Gradle helps make this easy. You need to create a new build task to tell Gradle to include all the dependencies in the built jar, and to set the main class in the Manifest to make it a runnable jar. Now there is just one self contained jar to distribute which could be run with java -jar your-fat-jar.jar

Gradle fatJar task

task fatJar(type: Jar) {
	manifest {
        attributes 'Implementation-Title': 'ESP8266 Wifi Display',
          'Implementation-Version': version,
          'Main-Class': 'com.mikelduke.java.wifidisplay.WifiDisplay'
    }
    baseName = 'spark-wifi-display-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

Gradle Build Command

./gradlew fatJar

Run it

I created a shell script to start the spark-wifi-display on my pi which can be run on startup. This script starts the java app in the background so it doesn’t need a user session and sets the property the app uses to set what port the server runs on.

#!/bin/bash

nohup java -Dserver.port=8081 -jar git/ESP8266WifiDisplay/spark-wifi-display/build/libs/spark-wifi-display-all-1.0.0.jar &

Run on Heroku

The spark-wifi-display app can even be deployed and run on Heroku. It’s probably not a good choice for a free dyno though, because the wifi module will hit it repeatedly. Heroku will sleep (turn off) free dynos for 6 hours a day, so it won’t be always on and will eat up the free pool of hours doing something which is fairly pointless, but it is possible.

To enable running on Heroku, I had to add a stage task to the gradle build and create a Procfile to hold the run command.

Gradle Build Task

task stage(dependsOn: ['build', 'clean'])
build.mustRunAfter clean
stage.dependsOn(fatJar)

Procfile

web: java -Dsecured=true -Dserver.port=$PORT -jar spark-wifi-display/build/libs/spark-wifi-display-all-1.0.0.jar

The secured property is set to true since this will be available on the internet after it’s deployed. The username and password can be set in the Heroku settings page as environment variables.

Use it

The edit message page is on the root ‘/’ for example http://your-ip:8080/ and the message formatted for the display is on ‘/message’ or as an example http://192.168.1.1:8080/message

Docker Jekyll Builder

I created a new project on Githib docker-jekyll-builder-git for a Docker containerized Jekyll project builder that I can run on a Heroku free Dyno. I put it together for use as part of a publishing pipeline instead of relying on the Github builder. This way I can use whatever plugins I want and can scp the built _site to my existing webhost. This lets me use my existing content and avoids having to change the DNS for my domain name.

Most of the other Jekyll builders I’ve found for Heroku rely on using either Rack or the Jekyll embedded server for serving content. Neither of these are really neccessary since it is just static content and any http server should do. You could instead use Apache or NGINX.

Running a static blog off an app dyno seems like a waste of resources unless you really need to scale it out dynamically. The content could just as easily be hosted on any other webhost or combined with other static sites. You would also need to upgrade to at least a hobby dyno ($7/mo) to avoid dyno sleeping for 6hrs/day, and the costs will add up with several sites.

I haven’t pushed the Docker image to Docker hub yet, but will once I’m happy with it and make things a little more flexible.

The image requires several environment variables and these can all be configured through Heroku’s web ui. Basically it will check out a git repo, run a bundle install and jekyll build, the scp the result somewhere else. You need to set the URLs to use and and SSH key for the transfers.

It can run as a bare docker image for use locally or on Heroku, instructions are in the readme or below. Next step is I want to make it run automatically after a push to github. I’ll make a php script on my webhost that can be hit from a webhook on Github that will then make the rest api call to Heroku and trigger a build.


jekyll-builder-git

Docker image to checkout a git repo, run a jekyll build, and the SCP the generated _site folder to somewhere else.

This lets you use a short lived free Heroku one-off dyno to build a Jekyll app that is hosted somewhere else.

Uses docker image jekyll/builder from https://github.com/jekyll/docker

Environment Variables

Set these variables in a run command or elsewhere:

  • GIT_HOST – Hostname for git repo
  • GIT_REPO – Path to git repo
  • SCP_HOST – Hostname for SCP destination
  • SCP_DEST – SCP Copy Destination
  • KEY – Private key for Git over SSH and SCP

Heroku

  • Create a new app heroku create jekyll-builder
  • Push the Docker Image heroku container:push builder --app jekyll-builder
  • Create the Environment variables above in the Heroku admin console
  • Run the builder heroku run -a jekyll-builder --type builder bash build.sh

Heroku Trigger Script

The script run-heroku-jekyll-build.sh is an example Heroku API curl request to trigger the jekyll builder.

  • Set your app name in the APP_ID_OR_NAME variable
  • Get a Heroku API Token by running heroku auth:token
  • Set TOKEN= to the token
  • Run ./run-heroku-jekyll-build.sh to trigger a build.
  • Logs can be viewed from the Heroku Dev console log viewer. Make sure you have it up when running the build, the logs are not saved.

Docker

  • Docker Build docker build -t jekyll-builder-git .
  • Sample Docker Run Command:
  • docker run \ --rm \ -it \ -e "GIT_HOST=github.com" \ -e "SCP_HOST=yoursite.com" \ -e "GIT_REPO=https://something" \ -e "SCP_DEST=you@yoursite.com:/path/to/dest/" \ -e "KEY=-----BEGIN RSA PRIVATE KEY----- 1234567890 -----END RSA PRIVATE KEY-----" \ jekyll-builder-git

phpGPS on Github

phpGPS Map

phpGPS Map

I have uploaded one of my recent projects phpGPS to Github in case there is any interest. It is a php/MySQL based web app for managing GPS entries and generating a Google Maps map which can easily be embedded on webpages.

GPS coordinates can be added to the database by any device that can send an HTTP GET request, which is pretty much anything with internet access. The GPS entries can then be moved, deleted, or have extra details added like a name and comments through the web interface.

I am currently working on a simple Tasker script so that I can easily add entries on a timer or with a button press from my Android phone.

Check it out here

phpGPS Index Page

phpGPS Index Page

phpGPS Edit Markers

phpGPS Edit Markers

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Test Map Embed: