Wednesday, 26 October 2016

Hacking an analog clock to sync with NTP - Part 5


This is how it looks after I have put everything together. The Arduino sketch is available here.

The 2 jumper wires soldered to the clock mechanism are connected to pins D0 and D1 on the ESP-12 (in any order). When the device first boots up, it presents an access point which can be connected to via the PC or smartphone. Once connected, the captive portal redirects the web browser to the configuration page:

  

A custom field has been added to the WiFi configuration page to enter the current clock time in HHMMSS format. Try to set the clock time to as close to the current time as possible using the radial dial at the back of the clock so the clock will have less work to do catching up.


In the config page, the HTML5 Geolocation API is also used to obtain your current location (so if your web browser asks if you would like to share your location, answer "yes").

This is then passed to the Google Time Zone API to obtain the time and DST offset of your time zone. This allows us to convert the UTC time obtained via NTP to the local time.

Clicking "Save" will make the device connect to the configured AP. Once that is successful, the configuration portal is disabled. The next time the device boots up, it will automatically connect to the configured AP without starting the portal. If you wish to start the portal instead, simply connect pin D0 to GND before starting up the device. The program will force start the configuration portal if it detects that D0 is low.

The following videos show the ESPCLOCK in action. If the clock time is behind NTP time, it will fastforward to catch up:


If NTP time is behind clock time by a couple of minutes, the clock will pulse and wait for NTP time to catch up:


With every tick, the clock time is persisted to the EEPROM so even if the device loses power, it will not lose the current time setting, unless that is changed using the radial clock dial, in which case the configuration portal should be used again to enter the new clock time.

Unfortunately, this clock can only be practically used when connected to the mains. When connected to a 2400mha battery, it lasted slightly more than a day (27.5 hours to be exact). A typical analog quartz clock will run for a year or more on a single AA battery!

Part 1 - Part 2 - Part 3 - Part 4

Saturday, 22 October 2016

Hacking an analog clock to sync with NTP - Part 4

Assuming the jumper wires from the clock are connected to pins D1 and D2 on the ESP-12, the following Arduino sketch will make it function as a normal clock that ticks every second:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
tickPin = D1;
os_timer_t secTimer;

// Triggered by system timer every second
void timerCallback(void *pArg) {
  tickOccured = true;
}

void setup() {
  // Setup GPIO outputs
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  digitalWrite(D1, LOW);
  digitalWrite(D2, LOW);

  // Start system timer which triggers every second
  os_timer_setfn(&secTimer, timerCallback, NULL);
  os_timer_arm(&secTimer, 1000, true);
}

void loop() {
  if (tickOccured) {
    tickOccurred - false;
    tickPin = (tickPin == D1 ? D2: D1);
    digitalWrite(tickPin, HIGH);
    delay(100);
    digitalWrite(tickPin, LOW);
    delay(100);
  }  
  delay(250);
}

To be continued...

Thursday, 20 October 2016

Hacking an analog clock to sync with NTP - Part 3

Most battery-operated analog quartz clock on the market today are quite similarly internally. They are usually based on an oscillator driving a Lavet type stepper motor, Movement is made by alternating the current running through a copper coil in every step, followed by an interval without current so as to provide time for the magnetic gear to turn to its new position.

I am going to disassemble the Ikea clock and connect two jumper wires to the clock mechanism. These two wires will then be connected to the GPIO pins of the ESP-12 for ours to control.

To remove the plastic cover over the clock face, turn the clock over and disengage the 3 plastic tabs holding the cover using a flathead screwdriver.


Once that is done, pull out the second hand, followed by the minute then the hour hands.


With a butter knife (I used a plastic pry bar), gently pry the clock mechanism out of its holding cavity.


There are tabs on both sides of the clock mechanism holding the front and back halves together, which is easy to take apart.


When pulling the front and back halves apart, it is best to do it with the spindle always facing up to minimize the gears falling out all over the place and having to spend time fitting them back together. Also, take photos every step of the way to make it easier to fit the parts back later if needed.



The cluster on the right containing the copper coil is what we are interested in. If you look closely, the coil is actually connected to the two solder points at the front. When powered by a battery, alternating pulses will be applied by the osciallator (the metal tube) at those points, powering the copper coil and causing the tiny magnetic gear to rotate in steps.


What we want to do is to solder two jumper wires at those points so that we can power the copper coil at will.

I also used the soldering iron to create a small hole in the casing for one of the jumper wires to pass through.

Now it is time to put everything back together.I find it easier to place the magnetic gear upside down with the other gears:

then carefully invert the other half containing the coil onto this half, after carefully lining up all the holes and connectors.

At this point, it is best to check if everything has been assembled properly by fitting the second hand into the spindle and inserting an AA battery. If the second hand starts to tick as normal, the operation is a success!



Once the testing has been done, I can insert the clock mechanism back into the holding cavity. In order to accommodate for the new wires, I used the soldering iron to melt two holes at the side:



Then reinsert the plastic cover over the clock face, and we are done!

To be continued...


Hacking an analog clock to sync with NTP - Part 2

Since I am more familiar with C/C++, it makes sense for me to use the Arduino IDE (rather than Lua, the other alternative).

Everything was surprisingly easy to setup. I first installed the Arduino IDE, Then I followed the instructions here to setup the board manager within the IDE. I really wanted to base the WiFi configuration part of the project on WiFi Manager, so I added that to the IDE using the library manager.


I also setup the board type under "Tools". There is no entry for ESP-12F, so I went with the ESP-12E since the two should be virtually identical as mentioned previously.

With all the setup done, I went with the simplest possible WiFi Manager sample code, which looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

void setup() {
  WiFiManager wifiManager;
  wifiManager.autoConnect("ESPCLOCK");
}

void loop() {
}

Then I click on "Sketch, Upload" and let the magic happen...


Lo and behold, a new WiFi network appears!


Connecting to it automagically brings me to the front page of the captive portal.


To be continued...

Wednesday, 19 October 2016

Hacking an analog clock to sync with NTP - Part 1

Inspired by this blog post detailing how to control the ticking mechanism on an analog clock with an Arduino, and having recently come across this gem of a WIFI-enabled micro-controller called ESP8266,  I decided to try and see if I can tether an analog clock to the ESP8266 and make it sync with a time server so that it is always on time (and is able to deal with daylight saving as well).

Not being much of a hardware geek myself, I opted to get the dev board for the ESP8266, the ESP-12E. That way, I can connect it straightaway with my laptop via a USB cable and start coding right away. Cost: ~$6.



Note: What I received turns out to be the ESP-12F, which appears to be virtually identical to ESP-12E.

For the analog clock, I chose to use the $2 RUSCH wall clock from Ikea. Now this is a cheap and nasty clock, made using the cheapest possible material. The second hand doesn't even line up with some of the tick marks as it makes its rounds. But cheap is good for experimentation, and an Ikea clock is useful as a reference point since everyone has access to Ikea.


To be continued...

Sunday, 24 July 2016

Cheaper to salvage 2TB laptop HDD from Seagate portable HDD


As widely discussed since 2014, this trick still works in 2016, which is insane price arbitrage if you ask me.

Bought a 2TB Seagate Expansion Portable HDD for $117. The 2.5" SATA HDD inside, if purchased as a stand-alone unit, costs upwards of $165.



Prying this open requires a thin sturdy Swiss army knife blade (or something equivalent). A guitar pick was not rigid enough to do the job for me.



The way these newer casings are designed, the plastics catches on the end that you start prying are bound to break.


The other end should be OK if you are careful enough.


I didn't need the casing anyway, so I didn't care. But you can always put it together with a little creative application of black electrical tape, so I don't think it's too much of a problem, unless you are really picky about cosmetics.

After putting it into my laptop, SeaTools detects the HDD as ST2000LM007. Not much of a surprise here.


Pros:

  • $50 savings for 5 minutes' work
Cons:
  • You can never be 100% sure which brand/model HDD you are getting inside
  • You void the warranty
This approach is not for everyone, but the bang-for-buck ratio is really high for the adventurous to take a dive.

Wednesday, 25 May 2016

Lossless concatenation of MP4 video files

Source: http://stackoverflow.com/questions/7333232/concatenate-two-mp4-files-using-ffmpeg


ffmpeg -i myfile1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts int1.ts
ffmpeg -i myfile2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts int2.ts
ffmpeg -i "concat:int1.ts|int2.ts" -c copy -bsf:a aac_adtstoasc output.mp4