Maybe the best advent calendar 2017

Hello together.

The advent season started now and what shouldn’t be missing? Exactly, an advent calendar. Usually kids get this calendar, filled with sweets and candies. But sometimes also the big ones :-)
This year I got also confronted with the subject ‘advent calendar’. The basic idea was to make a advent calendar. Fast the ambition came fast ‘If I make a advent calendar already, then the right way’. :-D

So, what exactly is or is needed for an advent calendar. 24 ‘doors’ of course. The best is some bags or boxes, to put all the small, sweet surprises in for the 24 days till xmas. But what, how, why?
The decision was quickly made that it should be a house. A wooden frame, for the boxes, which looks like a house.
Great, a good plan so far.

And everybody knows about the bad boys and girls, which can’t wait for the day to come. This has to be prohibit! :-D And then I am just on a ‘diy lamp’ trip… so the house has to have a lot of lights.
So far the rough plan. 

The final plan was made after a few detailed thoughts:
Every box will get a window (a house has windows, isn’t it), which lighted by an LED. To prevent the cheating, the calendar need to know which date it is and which box is removed. (I don’t need to mention that there is an arduino in the game. ;-) ). To be able to do that, every box need a ‘switch’, which is checking if the box is in the shelf or not. If someone removed the wrong box (a box from a date in the future), an alarm will be set off. However, if the box is pulled out of today, then a Christmas carol should be played.
Of course the house has to be a chimney with smoke… it is winter and people are heating. And of course the smoke has to be illuminated too.
And then we need a display, to show the current date (just in case the person is not really awake in the morning and don’t know the current date). 

And since we already put so much light in the house, then should be also install some gadgets, that the calendar can also be used as light/lamp. For example, I came up with the idea to illuminate the windows randomly. Just like in a real house … here is a light on and there is a light on. And then sometimes one goes on or off here and there. The imitation of a real house, so to speak.
(If I had known what will get into … I would have thought again !! :-))

And off we went with the crafting.

First, the house had to be built. The boxes are 6.5cm x 6.5cm. In order to leave a little space, I made the openings for the boxes 7x7cm. 7cm deep, however, is not very much. This would certainly overturn the house constantly. So I extended the depth to 20cm and got a bit ‘storage space’ for slightly larger surprises. I needed a partition anyway to accommodate the wiring for the switches.
In between, I also had to solder the LEDs. For this I drilled a hole in the wood in each shelf, for the SK6811. They are round and fit exactly in a 10mm hole. The nice thing about the SK6811 is that they are ‘smart’ and only need one pin on the Arduino. You always pass on the signal to the next LED. Thus, I always needed only 3 cables, from LED to LED. And not from every LED 3 cable to the Arduino. That is very convenient.
The LEDs then fixed with hot glue and that was done.
Then the roof ridge. Here I had to cut the two windows into the wood and saw out the opening for the display. Glued in front of the window openings then the ‘window paper’ and glued the display in the opening.
And install 3 more LEDs in the chimney and fix it to the house roof.

Most tricky was the subject with the box-switches. To accomplish this, I had to cut 28 slots into the partition to sink the cables there. But only to the extent that they are just in there. (24 data lines + 1x ground per row) I had the great idea to simply take self-adhesive copper foil. Seemed easiest to me. 2 strips under the box (1x data line, 1x ground) and another strip under the box. So this would close the circle once it’s in the drawer. Unfortunately, it didn’t worked out always. A little unevenness, a small curvature in the bottom of the cardboard box … and there is no contact. So I had to put half the strips on the cardboard box with a little bit of soldering tin, so that the contact – more or less sure – was made.
Definitely it was a pretty fumble. But at some point it was done.

What was missing were the RTC (Real Time Clock, so that the Arduino knows what day it is and also rememberes when the power is gone), a receiver for the remote control (yes! Remote control for the Advent calendar! ;-) How should one otherwise switch through the light modes?) And the buzzer for the Christmas carol or the alarm.
I soldered the buzzer and the RTC to an Arduino Mega Prototyping Shield. There was already a DHT22 sensor (temperature and humidity) on it. … Well, if he’s already there, then it can be used. So the calendar also shows the temperature and humidity on the display. Why not? :-)
The receiver LED for the remote control I have installed behind the windows in the roof. So it is not visible from outside. But you have to do a bit of ‘aiming’.
In addition, I have installed a photodiode. Actually, I wanted the advent calendar to light up by itself when it gets dark. I later discarded that.

And then it was time for programming. Afterwards everything seems very logical and simple. But when you start, it’s not like that at all. You start and you always have some stumbling blocks in your way. Such as an Adruino only handles single-thread. Mostly that is not so tragic. But if you want to play a song and in the same time want to turn lights on and off, then that’s not possible.

But after many hours it was finally time. The good piece was finished … more or less.

And here is the whole thing in action:

And some more pictures

What is needed:

  • Arduino Mega 2560
  • RTC
  • Buzzer
  • SK6811 LED
  • Boxes
  • Remote control + receiver
  • Photodiode
  • Tools and wood
  • Cotton

Winter greetings
Gordon

What am I doing with Arduino and Raspberry Pi?

Hello.

My Facebook disciple know more already then my beloved blog reader. A few days ago I posted the following pictures in Facebook:

The description was:

Da bomb is in da house… and working quite fine :)
What is that, beside of a beautiful piece of art of colored wires and electric stuff.
Of course it is not exploding :) It is a small stand-alone measuring station. It contains:
1x Arduino Pro Mini -> The micro controller which is processing all the data
1x Power supply -> No cool e-stuff without a bit electricity
2x Photo resistor -> to measure the amount of light around (of course, both give different values… ^^)
1x DHT11 -> Measures temperature and humidity
1x DHT22 -> Measures temperature and humidity as well… the newer model of the DHT11
1x LM35 -> Measures temperature… I fucked it when I mistaken plus and minus :( It is still sending values… but useless ones.
1x MQ-2 -> Measures the amount of smoke/lpg/co gas in the air.
1x 1.8” TFT with SD card -> To display the values and it is also writing all data in a log file on a sd card for later processing.
So far… will be continued…  :)

So… why do I play around with it?

An Arduino is a micro controller. Actually a micro controller on a board with some more stuff, to make it easier to work with it for the people. It got invented from Massimo Banzi and David Cuartielles (and named after a pub… yeah yeah :) ). David Mellis developed the programming language for it. The whole system is open source. That means everybody can see the sources, how it works. That’s why there are also copies of the original Arduino available and even you could solder your own one. It’s a quite easy way to get in touch with the micro controller stuff.
But lets have a look to another kind. As I mentioned in the headline already, there is also a ‘Raspberry Pi’.

The Raspberry Pi is similar to Arduino, but also different. The Raspberry Pi is more a micro computer then a controller. He consist a 700MHz ARM processor (the same CPU architecture as in mobile phones), 512MB RAM, etc. As operating system is usually Linux installed. Here is one big difference, the Raspberry Pi have a operating system, the Arduino doesn’t.

So, what does it mean now?

Both boards have GPIO pins. That are connectors on which we can connect sensors and such sttuff. One sensor is for example the DHT22… a sensor which is measuring the temperature and humidity. I can connect this sensor now to the Arduino and the Raspberry Pi. A small program written and it will show me the values of the sensor.

So, one board is enough!?

In general yes. But it is always a question of what we want to do.
With the Raspberry Pi the possibilities are much bigger, because we can make a lot more with the board as just reading sensor values. We have much more storage available. We can install software and have even a GUI (graphic user interface). We can write emails, texts, we can program and much more. Its a full computer, just a bit limited in its resources. But therefore it consumes more electricity.

I mean ‘consuming more electricity’ is relative here. 3.5 Watt is usually not worth a word! But if you want to create (for example) a stand alone weather which have a battery as power supply, then is 3.5 watt a very bad consumption and the weather station will not even run 1 day. Here is a solution needed which is running with as less power as possible. And here comes the Arduino in the game. That is his strength. Doing a job (like reading sensor values) and consume almost no electricity.

Why do I get in touch with this strange stuff?

Don’t worry, I didn’t went to become a bomb builder;) (Hello NSA… and again a match with a keyword… congratulation! ;) )
Well, some know that I have a new project in my head. I want to travel around with a camper. To do so, I need to take my work with me. That means internet and electricity in the camper. But power will be very limited and my current computer have a consumption of approx 200 watt. So, this one is out of the game. I need a more battery-saving alternative is needed. Then I ‘met’ the Raspberry Pi. It was clear that this small computer can not replace a working station, but it can be used to do at least some of the job. 3.5 watt is a good number for a 24h server. Even 3.5 * 24 is also 84 watt per day – in the theory – but I hope in praxis it will be a bit less, as the RPi is slowing down in times of not usage. I just testing it with a 10.400 mAh battery.

ArduinoBut I guess the RPi will be quite busy with all the task I have in mind for it :) As following:

  • NAS with external SSD
  • Local web server for development
  • Automatic connection control system for internet sources (Sat-DSL, WLAN, GPRS, etc.)
  • Desktop for internet, watching movies, programming, etc.
  • Central data processing unit for decentralized Arduinos with their sensors (weather sation (somewhere outside), camera modul in the front of the camper, motor sensor, gas sensro, etc.)

The are the rough tasks which the poor little guy needs to do.

I also need to note that I am testing the little PRi already and I need to mention that I am absolutely positive surprised. The little guy is not that weak as I thought. (Always with the consumption of 3.5 watt in mind… CPU without cooling!). The GUI is running properly. Also movies running with VLC. Icedove (the Linux version of Mozilla Thunderbird) needs a few more seconds, until its loading my several thousand emails. But then its ready for use. LibreOffice (like MS Office) the same. There are even games for the Raspberry Pi. I tried Mindcraft for a while and it is running smoothly.
As I said, with the hardware in mind, is this little computer a technical marvel.

And the Arduino joint my ‘tool box’ when my play instinct got activated. :) This stuff is so awesome for me now, that I can’t stop playing with it :) Real addiction factor! When you made your first circuit, the first script is written and transferred to the Arduino and the LED really starts to blink… then you feel a bit like god… ‘This LED is blinking now because I wanted it!’ :D
And then further… measuring temperatures, lighting up LEDs, reading light intensity (is it night, how strong is the sun shining, etc.), reading GPS coordinates and showing on a map, flame sensor (is it burning?), noise sensor, displays to show information, … and much more. The possibilities are almost endless. With a bit of creativitiy is it possible to make some really cool projects.

And in MY endless creativity (*cough*) I have the following projects in mind (in the camper).

      • Weather station with sensor for temperatur, humidity, air pressure, light intensitiy, Geiger-Müller tube, GPS, etc.
      • Camera module in front of the camper, which is making a picture every hour and is uploading it when internet connection is online)
      • Gas sensor at the gas bottles, in the kitchen and at the heating. Also gas sensor for the gas which the thieves like to use.
      • Connction to the engine electronic. For example to read the error buffer of the engine computer. Its not with every car possible… will see.

I think to remove the doorhandles and use instead a key card / fingerprint reader to open the doors. Instead of a key. The would confuse some thieves. ;)

 

Of all this data I want to make my own little black-box. But primarily I want to store this data in a database and put it online that everybody (who is interested) can see the data. (GPS data for security reasons with a time delay, of course ;) ) But that is all still up in the air. At the moment I still play around with both boards and try to build some stable systems. To test and get experience with it.

So, if you want to have a look at the micro controller world, then check out Arduino and / or the Raspberry Pi. I can highly recommend both systems. Is fun like hell and not hard to learn at all.

Best wishes
Gordon

[PHP] Normalization of strings – Upper and lower case

Hi,

today again a small task to do in PHP. The following situation:

We have a text-string in which is (obviously) a text. Lets say the content is:
HERE you’ll get today CHEAP cucumber DIRECT from the farmer.
We would like to change this string now like we want to change all words which consist only uppercase letters in: first letter uppercase and the rest lowercase. Like it should be.
Of course we should check now as well which really start with an uppercase letter, if they are in the middle of the sentence. Else we would mess up badly the orthography.
But if you don’t have a good dictionary (database) and a good routine lying around… then we need to be happy with my routine for now like it is.

So, the task is clear. Letter only in uppercase shall be converted in first letter uppercase and the rest lowercase. Like for the example above: HERE -> Here

There is no ready solution for it. We need to make our own function. The first thought is to split each word of the text, to check each word if it is consist only capitals. If yes, then convert, if not… then not. After all putting everything together again and finish.

As far, as good. Is working very well. Just one thing I’ve mentioned. I’ve used to convert first the function ucfirst(). This is doing exactly that. First letter capital and the rest lower case. We just need to convert everything before into small letters with strtolower().
And here I got the problem. By accident I used a string with German umlauts. And the function strtolower() messed up the umlauts. Which is probably not important for the English languages, as there are no such umlauts… but maybe it is interesting as well for Turkish or Spanish or French (as example).
So, what can we do? I found the solution with: mb_strtolower($String,’UTF-8′)

As you can see you can specify the charset (in this case ‘UTF-8’). That works very well and the umlauts are correct.

And here the whole code snippet:

function NormalizeString($String) {
		$StrA = explode(" ", $String);
		$StrAc = count($StrA);
		$Out = "";
		for($i=0;$i<$StrAc;$i++) {
			$tmp = preg_replace('/[^a-zA-ZäÄöÖüÜß]/', '', $StrA[$i]);
			if(!empty($tmp) && strtoupper($tmp) == $tmp) {
				//Alles groß
				$StrA[$i] = ucfirst(mb_strtolower($StrA[$i],'UTF-8'));
			}
			$Out .= $StrA[$i]." ";
		}
		
		return $Out;
	}

First of all we create an array with all single words, using explode(). Then we pass through every single word with an For-Loop. We copy the word in a temporary variable to clean it up to compare it better. If the word is upper case, then we convert it. And finally we put the string together again and give it back. That’s it.

Best regards
Gordon

[PHP] Date and date functions

Hi,

let’s talk today about date functions in PHP.

If you are looking for an interactive clock for your homepage, then you should look into Javascript functions. That is not possible with PHP. PHP is a server side language and can’t interact direct with the browser. Ok, you could try to play around with AJAX and reloads, but this is hard and why not using easier things…

But back to the subject. Date and PHP. I’ll show you some functions of the date class of PHP. The most important is date().

Date() return an formated date. It can be the current date or a date from an variable. For example it can be a timestamp from an database field. Also can be provided a format for the date. Just date or just time or both… and in various formats.
string <strong>date</strong> ( string <code>$format [, int $timestamp = time() ] )

Another, often used, function is mktime(). This command simply returns a UNIX timestamp. This timestamp can be saved in a database or can be used for whatever. You can provide also a date to get the timestamp for it, for example 01-03-1998.
I use it mostly without, to get the current timestamp.

int <strong>mktime</strong> ([ int <code>$hour = date(“H”) [, int $minute = date(“i”) [, int $second = date(“s”) [, int $month = date(“n”) [, int $day = date(“j”) [, int $year = date(“Y”) [, int $is_dst = -1 ]]]]]]] )

And with strtotime() you get the timestamp of a date which is saved in a variable. For example you got from somewhere the string “2000-07-01T00:00:00+00:00” and you need the timestamp for this date (in a string variable). Then use strtotime() and you’ll get it and can be saved into a database or whatever.

int <strong>strtotime</strong> ( string <code>$time [, int $now ] )

These are my 3 most used date function in PHP. There are at all 50 and more. If you like to know more about them, please have a look at php.net.

I hope that could help you a bit.

Best regards
Gordon

[PHP] Automatical redirect

Hi,

today I would like to talk quick about the subject “Redirection with PHP”. Quick, because there is not much to say :)

The code for a redirect in PHP looks like that:

<?php
	header("Location: http://www.domain.de/seite.php"); 
	exit(); 
?>

header() is sending the HTML-header. With “Location:” you’re telling it that it is an redirect. After it you provide the URL to which the redirect it pointing to. And with exit(); we tell the script to abort, not to execute any other code.

But I also have to mention to thing about what you do before you set an redirect. Is it a permanent redirect, because something changed in the webpage structure (for example a page got a new URL), then you should “say” that in the redirect, else it can occur problems with the search engine rankings.

A permanent redirect looks then like that:

<?php
	header("Status: 301 Moved Permanently");
	header("Location: http://www.domain.de/seite.php"); 
	exit(); 
?>

The line header("Status: 301 Moved Permanently"); is sending the status code that the redirect is permanent. There are also other status codes, like the 302 code, which is for temporarily redirects.
If e.g. the Google bot comes around and gets an 301 status code for a URL, then it will save the URL change and is crawling next time straight the new page. Like that the new page gets the ranking of the old page and it doesn’t get lost.

The good thing on PHP redirection is that it happened on the server side and will always work. On the other side you can make it dynamically. Redirection with the .htaccess can be also dynamic, but not as good as with PHP. Redirects in the HTML-Header are totally underground. The same for Javascript redirects. They doesn’t work at all for spider and user without Javascript.

I hope it’s helping. If not, let me have a comment :)

Best regards
Gordon

[PHP] Round numbers with PHP

Hi,

today again a bit PHP. A small tip about handling with numbers in PHP. Or more explicit, round off numbers.

PHP comes already with several functions to handle numbers. Four of them are number_format, ceil, floor and round. My personal favorite is  number_format. Why?

Because this function does not just round, it is also formatting the number. The syntax is:

Syntax: string <strong>number_format</strong> (float number [, int <code>decimals [, string decimal separator, string thousands separator]])

This function can be called with 2 or 4 parameter, but not with 3. So

number_format (<code>number);
number_format (<code>number, <code>decimals)
oder
number_format (<code><code>number, <code>decimals, decimal separator, thousands separator);

An example:

number_format (15329.6956, 2, ",", ".");

The output will be: 15.329,70

The next function, resp. functions are ceil and floor. This functions simply round up (ceil) or down (floor). The syntax is float ceil (float $number) reps. float floor (float $number). In both cases will be the number rounded up- or down on the next integer.

Last but not least the round function. It’s the middle way between the last 3 functions. It rounds a floating-point number up or down and you can provide a decimal place. The syntax is:
float <strong>round</strong> ( float <code>$number[, int $<code><code><code>decimals = 0 [, int $mode]] )

An example:

round(15.1698, 2) results: 15.17

Please note that you’re working always with the English number format while you’re programming. Decimal places are separated with a dot “.” and the thousands separator (if they have a separator) with a comma “,”.

Best regards
Gordon

[PHP] Comments in PHP scripts

Hello together.

Lets talk today about comments :)
When you start coding a script then is it useful to comment your code. So a small documentation in the script. Because as long as you are working on the script and it is in a relative small size, then you know still which line of source code is doing what.

But latest when you source code is growing bigger and/or you need to work on it after a few weeks or months… then is everything gone. How was it? What is this line doing? Where do I find this and this now? Everything gone… the code is just a big pile of confusing.
Latest now you’ll kick your ass that you haven’t commented the code.

Of course it takes a bit longer if you write also comments in the code… but some when it will pay back. Also (and especially) if the code will be handed over to another programmer is it important that the code is commented.

And it is soooo simple ;) 2 slashes (//) for one line and a slash with a star for a multiple-line comment (/* at the beginning and */ at the end). And it will look like that:

<?php
echo "vor dem Kommentar";
// Hier ein Einzeiliger Kommentar
echo "nach dem Kommentar";
?>

And a multiple line comment:

<?php
echo "vor dem Kommentar";
/* Dieser Kommentar
   geht über mehrere
   Zeilen */
echo "nach dem Kommentar";
?>

Best regards
Gordon

[PHP] Delete a file with PHP

Hi together,

today I wand to answer the question how to delete a file with PHP.

First of all we have to separate. Files on the web server or files on the local hard disk. First yes, second no.
PHP doesn’t have access to files of the local file system of a visitor. No server side programming language can do it. Also client side languages (like Javascript) can’t do it. Else it would be easy for every single webpage to delete randomly files from your computer. That would be an disaster.

But on the web server is it – partly – possible to delete files. Partly means that you can’t delete every file. Also this would be a disaster.

The PHP-Script needs the right to delete files or at least specific files. Under Linux is the right system very strict.

But in the normal situation is it not so complicated like it sounds like. Because usually you delete a file which you created with the script. Like a temporary file or a file from a user upload (a picture for example). Such a file was created by the PHP script and inherit it’s user rights. And also the script has the right to delete this file again.

But how?
Very simple. All we need is the PHP command for deleting a file and the path of the file itself. It fits all in one code line.

$res = unlink("testdatei.hm");

That’s it. In $res we get a return value. 1 = successful deleted or 0 = Couldn’t delete the file.

You can use relative as well as absolute paths.

That’s it. Hope it was helpful :)

[PHP] How to add a line break in PHP

Hi,

today I’ll like to talk about the subject line breaks with PHP. I guess this article will be not very long, because it’s done with a few lines of code. :) Read more

Happy Programmers Day!

Hi,

today a quick headline. But it doesn’t mean that it is not important! Because today is Programmers Day (The day for the programmers). :-) It is a very important holiday (unfortunately not an official one… I would love to have off today)!

And who is asking himself now on which day this programmers day is… very simple… on the 256th day of the year ;-) Why 256? Because 1 bit can have 256 values (0 / 1).

This number is the basic number of all programmers. Even if a programmer know nothing else, but he has to know these number… else he is not a programmer :-)

Valentin Bald invented the programmers day in the year 2007. He even tried that it will become an official holiday in Russia. And? … Since 2009 is the programmers day an official holiday in Russia :-) (But not a day off :( )

 

So my beloved programmers all around the world: Happy Programmers Day!! And less bugs in the next year! ;-)