I used the Feather Huzzah ESP8266 board to create my game controller. Here is an overview of what happens when using the controller.
- Controller will connect to the itpsandbox WiFi network.
- User hits a button to activate the connection to the server.
- The LED inside the button will light up when the controller is connected to the server.
- Rotary encoder dials on the top and side let the user scroll their paddle up/down and left/right.
- When you’re done playing the game, you can hit the button again to disconnect from the server and the LED will turn off.
Read more for a more detailed breakdown of the build & code.
When designing the controller, I tried to take advantage of materials I already had, and also I wanted to make something that was hand held. I initially had the idea to have dials that the user would rotate like in this sketch.
I had a lot of trouble securing the rotary encoders during construction. When you pushed too hard on the dials, it would bend and wobble around. Unfortunately, I forgot to photograph the first enclosure I made ): Anyways, this wasn’t great for giving the user reliable control when playing the game, so I panel mounted the dials instead.
I had my breadboard set up like this. At one point, I spent an hour figuring out why my rotary encoders + buttons weren’t working. I had even moved everything to an Arduino Uno to make sure it was all working (it was), only to realize that I had never connected my ESP8266 board to ground ))))):
With the power/ground issue solved, everything was working well.
Finally, I moved things inside my enclosure. You can see the discarded first attempts in the background.
Here’s the finished controller!
Connecting to the Wifi Network
I used the ESP8266WiFi.h library, but it is similar to the wifi101.h library. The first part of the code deals with connecting to the wifi network. When the device is connected, its IP address will print to the serial monitor.
Connecting to the Server
Next, I have some code that allows the user to connect and disconnect from the server that is hosting the game when they press a button. It sends ‘x’ to disconnect or it runs the “login()” function to connect.
Here is the login function that is connecting my device (the client) to the server and saying hello. The device will try to keep connecting until it is successful.
And when the device has successfully connected to the server, it will print any messages sent to the client from the server.
I used the “encoder.h” library to get inputs from my rotary encoder. I’m only showing the code for one encoder. I used a modulo function, so it only sends a signal when the encoder has been moved by more than a value of 4. This helps to make it more reliable since rotary encoders can be noisy.