I had two design goals: first, I wanted the gadget to be standalone and not need a companion app on a phone or a computer. So it needs to have wifi and talk directly to the Internet. Second, I wanted the cube to last a long time on battery, at least a few months. So I had to learn a bit about how deep sleep modes work on microcontrollers.
Here's a video that shows the cube in action. (The laptop is just there to show that time reporting works, as I said the cube talks to the Internet directly.)
These are the parts that I used to make this happen:
- ESP8266 board (Wemos D1 mini) for wifi
- MPU-6050 accelerometer to check which side the cube is flipped to
- SW-18010P vibration sensor for detecting motion
- ATtiny85 chip for watching the vibration sensor and waking up the wifi chip
- MCP1826 voltage regulator with a shutdown pin that made it easy to switch the wifi chip on and off
The idea here is that most of the time the ESP8266 chip is off and the ATtiny85 chip is in deep sleep, using almost no power from the battery. It is set to wake up when the state of one of its pins changes - a pin connected to the vibration sensor. Then the ATtiny85 chip enables the voltage regulator, waking up the ESP8266 chip, which reads the orientation of the cube from the accelerometer and makes a HTTP request to report it to a time tracking service. When it's done it signals the ATtiny85 chip on another pin. The ATtiny85 then disables the voltage regulator and goes back to sleep, waiting for another interrupt on the vibration sensor pin. An LED indicates when the chips are awake.
Here's a schematic of the connections. You may notice an extra button, I will explain its purpose in a moment.
And here's the code: the part running on the ATtiny85 is done in Arduino and the ESP8266 part in MicroPython.
I used Toggl for tracking time, but any service with a reasonable API could be used, the cube just makes HTTP requests.
Since the cube is woken up by motion, we have to consider what happens when you put it in a bag and take it with you. We don't want it to wake up constantly and deplete the battery. So here's how it works. The ESP8266 chip reads the accelerometer and waits for the readings to settle before making the HTTP request and signaling the ATtiny85 to go to sleep. So if the cube keeps moving, that signal never comes. And the ATtiny85 chip has a 30 second timeout. If it doesn't receive the signal before the timer runs out, it switches to "travel mode". Which means it goes back to sleep, but now it ignores the vibration sensor - it will only be woken up when the button is pressed. When it's pressed, it goes back to normal mode. That way we still get the long battery life, but we can take the cube with us.
Currently pressing the button is not very convenient as the cube needs to be opened up to access it.
Another improvement that comes to mind is making it work with multiple wifi networks. Currently it only remembers one, which makes it hard to use the same cube at, say, home and work.