* Connecting plants to the internet

25. July 2017, Jakub Horák. Categorized as IoT.


I have a couple of pelargonium plants on my balcony and I’m always struggling to determine, when to water them. By chance I also have a Raspberry Pi 3 lying around, so I’ve decided to do a little IoT project, turn the plants into cyborgs and let them tell me, when they need watering.

The connected plant

The connected plant

The basic setup is inspired by the Moisture Sensor Tutorial. But instead of sending emails and needing an SMTP server, I just submit the data to ThingSpeak, a very useful IoT analytics platform. An event in ThingSpeak can be configured every time a differing value is received in the input data stream. I’ve configured such an event to trigger sending a tweet, check the plant’s Twitter account here. On my phone, I’ve then tweaked the notifications in the Twitter app using my personal account to notify me on every tweet on the plant’s account.

Raspberry Pi 3 with Witty Pi 2 mounted on top

To provide electric energy, I bought the Ravpower powerbank with capacity 26800mAh. However even the massive battery doesn’t last more than three days in this setup. That’s when I found a nice extension board for Raspberry Pi called Witty Pi 2, which will handle turning the Pi on and off based on a schedule, thus conserving huge amounts of energy. See the photo on the left. I set the schedule to turn the Pi on for 2 minutes once every 2 hours. The whole thing can keep running like this for about a month without recharging.

To summarize, here is a complete list of used components:

  • Raspberry Pi 3
  • Witty Pi 2
  • Ravpower powerbank with capacity 26800mAh
  • Moisture sensor
  • Breadboard
  • 5 jumper wires

 

Placement in a drawer on my balcony

Placement in a drawer on my balcony

Raspberry Pi 3 with Witty Pi 2 on top and a powerbank

Raspberry Pi 3 with Witty Pi 2 on top and a powerbank

ThingSpeak chart

The hardware running smoothly, finally, let’s discuss the code. I started off with the tutorial’s Python sample and extended on that. I ended up with Ansible scripts. Ansible takes care of the configuration deployment, so whenever I’d format the Pi, I just need to install Raspbian, replay the Ansible playbook and I will get the same software installed and configured on it. Check the repo on Github. Pretty sweet!

Tags: , .



* Alexa, frage Feiertag, wann der nächste ist

16. April 2017, Jakub Horák. Categorized as AWS.


The topic of AI assistants is recently one of the hottest in technology. With Apple’s Siri and “Ok, Google” now available in every smartphone, Amazon is not behind and has its own Alexa. As Amazon is not a phone maker, the assistant’s main place to live is a special speaker with integrated microphone array known as Echo.

In December 2016, Amazon was just introducing Alexa in Germany and came up with a promotion, under which everyone, who publishes three Alexa skills in the Alexa store within one month, will recieve a free Echo Dot device. That and curiosity were good motivation for me to start developing. I made a first primitive skill quite fast. It took just two hours. I wanted my second skill to be more advanced though. An idea came to me to make a skill, which tells you, the user, when the next public holiday is. That is actually quite useful in Germany, as different Bundeslände have different public holidays.

Alexa skill’s main part consists of an AWS Lambda function, which takes input from the Alexa speech model and outputs a text which will be told to the user. Lambda currently supports Java, Python and Node.js. As the developer gets charged by the used memory, I believe it’s best to use Node.js. Most of the examples online are implemented in Node.js too, and Alexa’s Node.js SDK  provides more features than the Java SDK.

My new skill, which I call Feiertag, requires you to link your Amazon profile and fetches the postal code from your primary Amazon postal address. The postal code is then used to determine the  Bundesland in which you’re residing and subsequently the public holidays that affect you. One of the ways to can interact with the skill is:

Alexa, frage Feiertag, wann der nächste ist.

As it took me a bit longer to finish the skill, I missed Amazon’s promotion for a free device. So when an Echo Dot became available for pre-order, I bought one myself. Unfortunately while testing my skill, I discovered, that the Alexa itself can tell you the holidays without the need to install any skill. You just ask:

Alexa, wann ist der nächste Feiertag?

This sentence sounds much more natural than the previous one, right? The difference is that it doesn’t contain invocation of my “Feiertag” skill. Too bad, that my skill is not bringing any new cool features, but at least I got to do a nice programming exercise.

See the source code of Feiertag skill on Github.

Tags: , , , , .



* Spaong, the space pong game

14. March 2017, Jakub Horák. Categorized as Game development.


Last weekend we got together with Mark and Merten for a hackathon weekend with a goal to make a game. We have succeeded and the result is called Spaong. It’s about two alien robot races, Vulgarians and Profanians, who both hate humans. However a human astronaut seems to have gotten lost in the empty space between their planets! What will happen? The game requires two players and at least one of you needs to bring a gamepad.

Play the game at spaong.mortal.host to find out or visit the game website!

Tags: , , , , , .



* Feud.eu – Analysing Twitter hashtags

18. November 2016, Jakub Horák. Categorized as Twitter.


Expanding on the idea of my previous project MrTweety, I wanted to provide better real-time data and also try out big-data processing software. That’s when I discovered Apache Spark, toolbox used to manage distributed batch processing. My chosen project was not too complex, I was thinking of it being more like a proof of concept. The basic idea is to analyze a real-time feed of tweets posted globally on Twitter. From each individual tweet, occurrences of hashtags are counted and summed in 10 second intervals together. From these intervals a 15 minute sliding window is generated and hashtag counts again summed to form the final table. Top five most occurring hashtags are shown as a result.

To start, I checked out the Twitter API and saw that they offer streaming API. After some research, I’ve found an official Twitter Hosebird Client library and an example of how to use it to publish messages to Kafka. Kafka is  messaging system that is also a perfect source of data for Spark. That means we have one subproject for taking the Twitter stream and “producing” it into Kafka. A Spark subproject is then receiving the data and processing it. While working with Spark, I got a little derailed, when I found that you can use Twitter receiver directly with Twitter DStream. However when I actually tried this library, it didn’t work. I suspect that the DStream receiver tries to connect and read Twitter API from multiple threads and that forces Twitter to block the connections altogether. The Twitter DStream project got deleted from Github, while I was using it, so I’ve decided to go back to the Kafka approach. The official Hosebird Client claims to implement the limits and backing-off mechanisms correctly, so I believe it’s better anyway.

After that. Spark does all the hard computing work for us. We just specify, what we want to do with the data. Spark is made to be distributed, but for this small example we use just one node. Everything is running on Amazon ECS, which is a nice “Docker as a service” platform. To make it work, we just need an EC2 instance to run the Docker container. The basic t2.micro instance’s 1GB of memory was not enough for the all the included hungry JVMs, so I had to get a “spot instance” m3.medium with 3.75GB RAM. In the current market the costs should be around 7.5$ per month.

I’ve developed the whole project from scratch on Github as open-source, so check it out there. The production site is feud.eu. Here is a screenshot:

Screenshot of feud.eu

Note: A similar project that I’ve found and that could be interesting to check out is Twitter Analytics with Spark.

Tags: , , .



* Discovering Akka

25. April 2016, Jakub Horák. Categorized as Frameworks.


I came across this interesting piece of technology called Akka and I’ve decided to write a few words about it. So what is Akka? In short it is a toolkit and runtime for concurrent and distributed applications. To describe in more detail, we have to first talk about the motivation, why would someone use it.

Today we live in the era of distributed systems, billions of people use internet for their everyday life and successful online services have to plan for massive traffic. As soon as a service becomes popular, it is not economically viable to serve it using one large super-computer, but rather use a cluster of cheaper machines. As a programmer, who comes from a Java background, I have implemented concurrent thread-safe programs too many times to know, that it’s not easy. Concurrent systems introduce a new dimension of difficulty. To simplify the concurrent programing and introduce a common standard, company called Lightbend (previously Typesafe) came up with the Reactive manifesto.

Point of the Reactive manifesto is that distributed systems built following it’s guidelines will be easier to develop, scale, maintain and enhance at a later time. They are flexible, loosely-coupled, tolerant of failure and can handle unexpected behavior gracefully. Last but not least they they provide user with interactive responsive feedback.

 

Reactive traits

Reactive traits. Arrows show, how they depend on each other.

So what are then the problems of concurrency? Distributed code by default behaves non-deterministically. Without any locking, every time you run the program, you get a different result. It is caused by multiple threads sharing a mutable state, e.g. a common global variable. That leads to race conditions, when things are happening in a different order than the programmer expected. Another problem is thread starvation, when a thread will never get to work and advance the states, because others have higher priority. This happens for example in an OS process scheduler, when there are many processes with high priority and a low priority process will never get to run on CPU.

Akka is a toolkit and runtime for simplifying the construction of concurrent and distributed applications on the JVM. Reasons to write Akka were practical. First simple actor implementation (not yet Akka) was written as a part of Scala in 2006. By 2008 Scala was used in complex server applications and need for a better library support was growing. That motivated developers to come up with Akka. It was released in 2010 and drew inspiration from Erlang’s libraries. Today it is a part of Scala standard library. Java programmers don’t have to be sad though, as there are also language bindings for Java. The library is open-source (Apache License 2.0) and developed by Lightbend, which is well known for its Play framework.

Akka has many features. These are the main ones:

  • actor system, which introduces a concept of actor as a unit of execution,
  • remoting allows messages to be sent between machines,
  • routing defines how are the messages sent to the routees,
  • clustering allows cluster nodes to run a part of a bigger application,
  • Akka Persistence introduces actors with state, that remains even after restart, and
  • Akka Streams provides asynchronous stream processing with non-blocking back pressure.

Actors encapsulate state and behavior. They are isolated and lightweight, meaning that they don’t have hard references to each other, only so called ActorRef. To communicate, they pass serializable messages back and forth. Advantage of this approach is location transparency – actors can communicate locally or over network, it’s only a matter of configuration. Akka is distributed by default, meaning you can only pass messages and everything is asynchronous. It hides all thread-related code behind its actor model and allows programmers to write code easily scalable and fault-tolerant.

We already mentioned you can use Akka in Java. To do that, you just have to make sure that all messages are immutable and serializable, because they could be sent over network. Immutability is achieved in Java by making all attributes final. Akka also requires toString(), equals() and hashCode() methods to be implemented.

When building reactive applications in Java, it is often convenient to use Lambdas. Lambdas are small blocks of code, that can be executed in separate threads. Typically these applications use many callbacks and Lambdas make it easy to write them. Before Java 8 we had to use inner classes and that was not as nice.

To see how can Akka be used, I recommend trying out these two examples:

https://www.lightbend.com/activator/template/hello-akka-java8
https://www.lightbend.com/activator/template/akka-sample-remote-java

Go to akka.io to read up on the documentation or to lightbend.com/activator/templates for more samples. Also check out Apache Spark, a fast and general engine for large-scale data processing built on Akka or Apache Spark Streaming, which can do the same for near real-time small batches of data.

Tags: , , , , .



* Introducing the English version of the blog

13. March 2016, Jakub Horák. Categorized as Unassigned.


Hello dear visitor and welcome to the English version of my blog. The main purpose of this blog is to store memories of projects I did or experiences I had. You can find here mainly scripts, code samples and solutions to problems. Because of that, do not expect me to write posts every day, week or month. The point is to have a central and public location for my documentation.

The older articles are available only in Czech. Feel free to browse through them too!

.



* Aktualizace WordPressu podruhé

22. November 2014, Jakub Horák. Categorized as Scripts.


V létě 2011 jsem na tomto blogu popisoval, jak vzdáleně aktualizovat WordPress. O tři roky později znovu řeším stejný problém, situace se však změnila. Dříve jsem měl na server přístup pouze přes FTP, nyní mám SSH.

Myšlenka nového skriptu spočívá v tom, že udržuje zdrojový kód WordPressu i jeho pluginů v SVN pracovních kopiích. Jak se to dělá pro samotný WordPress je popsáno zde. Poprvé je nutno provést SVN checkout nejnovějších tagů jádra WordPressu a jeho pluginů z repozitářů na http://core.svn.wordpress.org resp. http://plugins.svn.wordpress.org. Pro kompletní aktualizaci pak už vždy stačí spustit skript. Sám si zjistí tagy odpovídající nejnovějším verzím a pracovní kopie do nich přepne.

Skript si můžete prohlédnout:

#!/bin/bash
##
## Updates Wordpress in this directory to maximum release version.
##
 
LCYAN="\033[1;35m"
NOCOL="\033[0m"
 
update() {
  svninfo=`svn info 2>/dev/null`
  # check that directory is a working copy
  if [ "$?" -ne 0 ]; then
    echo -e "$LCYAN$(readlink -f .)$NOCOL is not a working copy."
    return
  fi
 
  oldurl=`echo "$svninfo" | grep '^URL:'`
  oldurl=${oldurl##* }
  oldversion=${oldurl##*/}
  tagurl=${oldurl%/*}
  newversion=`svn ls "$tagurl" | sed -e 's/\/$//' | sort --version-sort | tail -n 1`
 
  if [ "$oldversion" == "$newversion" ]; then
    echo "$(readlink -f .) is up to date with version $newversion."
  else
    echo -e "Update $LCYAN`readlink -f .`$NOCOL from version '$LCYAN$oldversion$NOCOL' to '$LCYAN$newversion$NOCOL'?"
    select prompt in "Yes" "No but continue" "Abort"; do
      case $prompt in
        Yes ) break;;
        "No but continue" ) return;;
        Abort ) exit;;
      esac
    done
 
    echo ---
    svn switch "$tagurl/$newversion"
    echo ---
  fi
}
 
if [ "$#" -ne 0 ]; then
  echo "Usage: $0"
  exit
fi
 
# main
update
 
# plugins
pushd wp-content/plugins/ >/dev/null
for plugindir in *; do
  if [ -d "$plugindir" ]; then
    pushd "$plugindir" >/dev/null
    update
    popd >/dev/null
  fi
done
popd >/dev/null

Také si skript můžete stáhnout.

Tags: .



* Plugin do Vimu na odstranění diakritiky

24. September 2014, Jakub Horák. Categorized as Vim.


Při kopírování textu se mi občas stává, že potřebuji z původního textu odstranit českou diakritiku. Jako textový editor používám Vim, rozhodl jsem se tedy napsat do něj plugin, který mi takovou funkcionalitu poskytne. Zasvětil jsem se do tajů Vimscriptu. V mnohém jsem se inspiroval skvělou knížkou Learn Vimscript the Hard Way od Steva Loshe.

Plugin včetně dokumentace je k dispozici na GitHubu.

Hurá, už žádné ruční přepisování písmen z české abecedy do latinky!

Tags: , , .



* Populární trendy na Twitteru

24. August 2013, Jakub Horák. Categorized as Twitter.


Spustil jsem aplikaci zobrazující grafy populárních trendů na Twitteru.

Začalo to vymýšlením tématu semestrální práce na předmět Enterprise Java, kdy jsem potřeboval napsat aplikaci, která by splňovala technologické podmínky vyučujícího a zároveň dělala něco zajímavého. Zrodil se nápad a v rámci předmětu byl úspěšně dotažen do konce, nebo to tak alespoň vypadalo 🙂 Bohužel, to co fungovalo při předvádění na testovacích datech nefungovalo na o několik řádů mohutnějších reálných datech. Nicméně jsem se poučil, překopal architekturu aplikace a odstranil funkčnosti, které jsem implementoval pro pobavení vyučujícího. Voilà a dnes je z toho prima hračka.

Rozhodl jsem se implementovat pouze denní a týdenní grafy, protože kvůli proměnlivosti trendů nedává pro tento typ grafu větší časový rozsah smysl. Trendy většinou trvají jen několik hodin, maximálně několik dní. Pro větší rozsahy by se dala udělat statistika např. nejvíce používaných trendů, ale vyžadovalo by to jiný způsob zobrazení. Vidím to zatím jen jako nápad na rozšíření.

Co se týče dat, zajímavý je např. včerejší denní graf, kde lze vidět, jak se na Twitteru vzedmula vlna odporu proti účinkování Bena Afflecka v roli Batmana v nadcházejícím filmu Batman vs. Superman (viz #BetterBatmanThanBenAffleck). Z historických dat lze pozorovat např. vývoj trendů při dubnovém útoku v Bostonu (viz #PrayForBoston) nebo při květnovém tornádu Moore v Oklahomě (viz #PrayForOklahoma).

Prohlížejte a sdílejte zde: mrtweety.zee.cz

Tags: , , , .



* Simulátor agentů v Lispu

21. June 2013, Jakub Horák. Categorized as Unassigned.


V rámci mé bakalářské práce jsem implementoval engine na simulaci agentů.  Agenti mohou hrát hry na schovávanou, honěnou nebo slepou bábu. Prostředí je reprezentováno pomocí šestiúhelné mřížky. Konkrétní vlastnosti jsou dokumentované přímo v souborech projektu, který je k dispozici na Githubu.

Jedná se o můj první projekt v jazyku Common Lisp a musím říct, že se mi Lisp hodně zalíbil. Dovedu si představit jeho využití v situacích, kdy chce člověk vytvořit algoritmus, ale neví ještě, co ten algoritmus bude přesně dělat a jaké bude používat datové struktury. Díky dynamickému typování a principu “všechno je funkce” se kód refaktoruje výjimečně snadno. Člověk tak získá výsledný algoritmus postupnou evolucí původní myšlenky.

V síle Lispu ale zároveň spočívá i jeho slabina. Volání funkce musí být obalené závorkami. Málokdy se mi podařilo trefit ukončující závorky tak, aby program fungoval napoprvé. S přibývajícími zkušenostmi se to naštěstí zlepšilo. Když si člověk zvolí nějaký konkrétní styl odřádkování a psaní závorek,  lze tento typ chyby omezit. Hodně pomáhá barevné zvýraznění jednotlivých úrovní závorek, jako to umí např. Vim plugin Rainbow Parentheses.

Více o Lispu se můžete dozvědět online v knize Practical Common Lisp.

Tags: , , .