MCP7940 – RTC with Pi

As I’m building my own IQ house control system, I need to have RTC in my system. So started playing with MCP7940N and i2c interface. I’m using Raspberry Pi for my experiments with i2c/SPI.

Construction is pretty simple, just use MCP7840N Datasheet.

mcp7940n-schema

 

Then you can start checking with Pi. In my case, I’m using RevB, so my bus has number 1 and RTC got 0x6f address.

what next? Check address 0x00, if the onboard oscilator is running

0x00 means NO, it’s NOT running. So, turn it on

and check few times address 0x00

Increments! Great. That means – our oscilator is working = our clock is working too. Next step is to set current time and date. But wait. How are values stored and read/written? RTC uses BCD encoding for values, that means, eg. number 94 is stored as 9 and 4, both in 4 bits = 9 is stored as 1001 and 4 as 0100, in hexadecimal 0x94. Easy? Eg. day is using 6 bits, upper two for 0123 values, bottom bits for 0-9, so 31th will be stored as 0x31. For more details please read the PDF – RTCC Memory map. So back to setting the date & time:

Fine. But if we’re setting ’00’ as seconds – address 0x00 – why value 0x80? 7th bit = ST – onboard oscilator enabled.

I’ve written simple Python code to read from RTC and print out the value:

Just run by

Voila! :) RTC is up and running. I’d like to check tomorrow (resp. today) morning, if everything is still working correctly, and can create next module for my IQ house control system :)

capistrano3: run gem binary

I need to setup deploy task to run eye. Tried to deal with gem-wrappers, but had no success. As capistrano3 is using non-interactive ssh, without loading user’s environment (.profile, .bashrc etc), then command, which is not in PATH, it’s not workin.

So, after searching and reading capistrano (capistrano/rvm) source, then sshkit source, I got into this simple solution.

It’s not dependent on any other settings nor knowing what, where rvm is installed.

before change in deploy.rb (not working)

after change in deploy.rb

original code from capistrano is

Index? Yes, please

When altering some tables, I got into optimization adding indexes.

I have one table – users

and doing one query:

and slow-log complains

from his you can see it did full table scan (Rows_examined: 389894) which is kinda wrong.

so, let’s explain

Hmm, “type” shows no index has been used and ALL table scanned. WRONG

Let’s add an index – we have two fields, last_active_at and last_logoff, let’s try with last_logoff only

hmm, still using full table scan. Let’s drop and create combined index

and what our explain friend shows now?

Oops. Explain shows it’s using index (bit better than ALL), but still fetches 385646 rows. Still wrong. Let’s switch columns in index.

What explain says now?

Great! and select?

0.20secs improvement.

Explanation? It’s simple. last_active_at is doing range scan against a value and against second column, thus first referenced. If last_logoff is first in combined index, it can’t be used for last_active_at > part of the query.

redis sentinel with ruby (on rails)

In the last article I introduced how to install and use redis sentinel. As I’m using ruby, I need to use this new redis configuration with ruby (on rails).

For ruby on rails use redis-sentinel gem.

Then your redis initializer will look like

You can use your redis then as usual.

When using sidekiq, configuration is pretty simple too

You can test your configuration. Run rails console and test with

if you see “127.0.0.1:6379”, something is probably wrong. Then try to set/get some key and check Redis.current once again.

redis sentinel setup

Prerequisities

  • multiple clients with redis 2.8.2+ installed

Do I need sentinel? If you want to have some kind of redis failover (there’s no cluster yet) – yes. Sentinels continuously monitor every redis instance and change configuration of given redis node(s) – if specified number of sentinels decided whether master is down, then they elect and promote new master and set other nodes as a slave of this master.

Looks interesting? Yes. It is. But. There’s a little time gap between electing and switching to the new master. You have to resolve this on application level.

Basically. Initial setup expects all nodes running as a master, with manual set slaveof ip port in redis-cli on meaned redis slaves. Then run sentinel and it does the rest.

sample redis configururation files follow:

and sentinel configuration file:

Start all of your redis nodes with redis config and choose master. Then run redis console and set all other nodes as a slave of given master, using command slaveof 10.0.0.1 6379. Then you can connect to your master and verify, if there are all of your slave nodes, connected and syncing – run info command in your master redis console. Output should show you something like this

To test, if your sentinel works, just shutdown your redis master and watch sentinel log. You should see something like this

explained line by line

master is subjectively down (maybe)

master is objectively down (oh, really), two of four sentinels have the same opinion

so we switch to another master – chosen 10.0.0.2

reconfigure 10.0.0.3 as a slave of new master 10.0.0.2

sorry, former master, you have to serve as a slave now

+sdown, -odown? + means ‘is’, – means ‘is no longer’. Then “+sdown” can be translated as “is subjectively down” and “-odown” like “is no longer objectively down”. Simple, huh? :)

PS: take my configuration files as a sample. Feel free to modify to match your need and check redis/sentinel configuration docs to get deeper knowledge about configuration options.

Kaštanový koláč Gordona Ramseye

Ingredience

  • 250g loupaných, vařených kaštanů
  • 250g kvalitní hořké čokolády
  • 250g másla, nesoleného
  • 125g cukru (krupice, krystal)
  • 4 vejce
  • 125ml smetany
  • 125ml plnotučného mléka

Příprava je primitivní. Ale musí se dělat pečlivě :)

Máslo a čokoládu rozehřejeme ve vodní lázni. Cukr utřeme se žloutky. Smetanu, mléko a nakrájené kaštany dáme vařit – jakmile směs přejde do varu, sundáme a rozmixujeme. Ušleháme sníh ze 4 bílků. Čokoládovou směs, cukr a kaštany smícháme a vymícháme dohladka. Opatrně vmícháme ušlehaný sníh. Dáme péct do vyhřáté trouby (170oC) na 20-30minut.

Po pečení vyndáme, necháme chvíli chladnout – dort se stáhne a popraská – a podáváme buď ještě teplé nebo necháme úplně vychladnout.

Pár tipů:
Pokud máte vejce v lednici, nechte je pár hodin ohřát a stabilizovat se. Sníh musí být lesklý a pevný – ideálně šlehat v kovové misce, přidat špetku soli.

20131219-222524.jpg

rails + passenger + nginx maintenance mode

I need to add maintenance page to some rails app, running with passenger and nginx. Here’s some config and steps.

You just need to add static html file to app_root/public/maintenance.html – and I assume css files on /assets url.

so, here’s nginx config:

setting maintance mode is really simple – set app_root/tmp/maintenance.txt file – when escaping, just remove that file.

Need to split big SQL dump into separate databases?

I do. So I’ve written small bash script to do so. I think it’s self explanatory and does it’s job well :)

I do NOT have CREATE DATABASE in sql file, thus set to USE..

Change image in UIImageView – RubyMotion

I needed to change image in my UIImageView, but simply setting new image using myImageView.setImage didn’t work. There’s simple workaround

and now set the image

you can remove resizableImageWithCapInsets – I’m using square images to make UIImageView of any size.