maanantai 5. lokakuuta 2015

Coding Quickie: NodeMCU Lua humidity detector.

Ok I have gotten little bit more familiar with my NodeMCU, I'll show and tell little bit more about my latest experiments.

I have order lots of stuff from Ebay recently.
One was this Long name Rain Sensor Module Sensitivity Weather Humidity Detect Module for Arduino. So it's Arduino module but it works fine with NodeMCU. Also there isn't many NodeMCU modules outhere so you have to be creative.

Attaching module to NodeMcu was pretty easy. I used NodeMCU it self as a power source. Just put VCC and GND in from available GND and VCC pin. Notice that Ebays description says that nominal voltage for this module is 5v. Yes it worked well with 5v but I also tried 3.3v pin. It works as well.

Then the part where a data cable is attached to NodeMCU, There is DC and AC pins in module. Those are Digital pin and Analog pin. First I used digital pin.
I tried to connected it to available GPIO pin. I got something out but nothing readable. It also crashed my NodeMCU repeatedly.
Then I searched little bit and found out that there is analog pin in NodeMCU. Nice! ADC pin is first pin from right in NodeMCU right under NodeMCU logo cloud.

I attached AC pin to ADC and voila I got something. Value 1024. Testing works well with wet cloth or paper.

Here is little bit of lua code that I used.
print('Humidity v0.1')
dly = 500

function getAnalog()
id = 0
print("Reading value " ..


And it prints when something wet is on the sensor.  
Reading value 376
Reading value 373
Reading value 370
Reading value 369
Reading value 368
Reading value 370

Reading value 1024

Next I'll make little script that connects to server and send data there. Why? Dunno. It is just nerdishly fun to collect data.

More stories coming....

torstai 10. syyskuuta 2015

Coding Quickie: Node MCU Lua

It has been a long time since i wrote anything. Well new nerdy thing interest me and I bought this Node MCU. What I'll do with it, I don't have a glue.

Getting started wasn't plug &play for me. So I gathered few things here that you need to even get this piece to work.

Step 1.
First if you are using Win7 you may need this driver.
From my Copy
Or from here
(Both seem fishy but both are legit links without any virus or malware.)

If you wonder where I got my Node MCU it was from here:

Step 2.
Now plug in your Node MCU.
Instantly you should see that it worked. Before the driver install there was only COM1 port on a Device Manager list.

Step 3.
Does it really work? Well you can test it with this little program.
From my Copy
Or from here

Step 4.
Run LuaLoader program. Go to Settings -> Comm Port Settings.
Change the Port if needed to correspond port that you see in Device Manager. In this case I had to change it to COM4. Click ok and go to Settings again and click Open COM4.
Program will write to console:
Connected to COM4 at 9600 baud

Step 5.
Set up that WIFI there as well. Input SSID and password click image on right hand side of Get IP button.
Program will write to console:
¶Eü|vñ¤l, xÉE¬tð4C4èðØdà•lb8XìÁH``$:IdprüÿÏ}ŸÏþžÏþþÜ?½

Tadaa! It works.

For more info go to

Moment ago I downloaded this
It seems to say so I think that everything as it should be.

This was guide for total dummys like my self. Now I'm going to investigate more this little device. Lets see if it leads to anything.

Little bit later after some disc golfing.

I found out that I did it little wrong. I should have run Nodemcu firmfire programmer first. So disconnect LuaLoader(it's important otherwise COM4 port is occupied and doesn't work). You dont have to shutdown LuaLoader though. Then press Flash(F) button on Nodemcu firmfire programmer.

Wait for a bar to complete and voila.
Now if you check Wifi connection on LuaLoader it doesn't print out nonsense anymore.


perjantai 27. syyskuuta 2013

Coding quickie: Unbind click function. WHY?

If you create dynamically content to a website you may have encountered this problem.


I have a web page that makes content dynamically. The content includes some clickable content like a button. The button that was made looks good but if you click it nothing happens.

Look at example one.

Most important parts of the code looks like this:

Example 1

Of course second button won't work because it's created after it's click function. Ok it's easily fixed as picture below presents.
Example 2.

Just move the highlighted click function inside the function that creates a button.

The point

Examples above are the basics what every jQuery using coder should already know. Point of this blog text is what if button is created twice? Click functions are also created twice at same time. So when button creation is fired twice the same function runs twice although it's unwanted behaviour. 

Example 3.

In example three, two dynamic buttons are created with for loop. This means that if last button is clicked the alert box will appear twice. 
There is away to prevent this. I have faced this problem many times and appears that I haven't searched the answer enough from jQuery documentation. Now I found the answer and it's as the title says "unbind()".

Example 4.

Highlighted part on a picture shows the function. When function runs again and again you just have to unbind the old click-function every time the function runs. This way as example shows only one alert box appears when the last dynamically created button is clicked.

Hopefully this tutorial how will help you. It certainly will ease my life.

tiistai 6. marraskuuta 2012

IPTV recording is piracy?

Finnish TTVK (Tekijänoikeuden tiedotus- ja valvontakeskus ry) is planning to sue two Finland's biggest internet service providers Elisa and TeliaSonera. This is because TeliaSonera and Elisa provide their users a recording possibility in their IPTV-service.

At last the IPTV seems to become a basic thing in Finland. IPTV’s advantages are that all tv-channels can be watched whether you were in cable or antenna home. Almost any IPTV program can be recorded to the service providers cloud. Users that have IPTV don’t have a need for big digiboxes with big and slow hard drives. Another advantage is that user can record any amount of programs at the same time in any amount of channels. Yours truly has a Wbox HD2 that can record three channels at the same time as you watch a fourth channel. Turning from IPTV to this normal digibox was first challenging because I have had a habit to record any program from EPG that interested me.

Well to the point. IPTV is the best thing since turning the analog tv to digital. It’s a breakthrough that encourages service providers to enhance their outdated ADSL-connections. If you wonder why, I’ll explain. Let’s think that family has 24/1mb ADSL-connection and someone from the family watches HD-channel through IPTV and one surfs the Internet and watches videos from YouTube. It doesn’t take too long to notice that lame 24/1mb connection isn’t just enough. HD-channels will start to make loud cracking noises and it will be more like watching a cartoon than watching a movie. YouTube user notices that YouTube won’t buffer fast enough.

The family that has IPTV is probably pleased with the service and just don’t want to give up because the connection isn’t fast enough. This means that family needs faster DSL-connection and they probably will contact their service provider to get the faster connection. The main point of this post. Finnish service providers don’t currently have any better connections to offer to the general public than 24/1mb. This is because of ADSL2+ can’t offer faster connections and it’s currently only technique that is available almost for everyone in Finland. I think that if customers need faster connections, service providers will find the way to provide the connection for customer sooner or later. If it doesn’t exist now, it doesn’t mean that it won’t exist in the future. It will improve infrastructure and will help Finland to keep its place at cutting edge of technology. TTKV, which was mentioned in the beginning of this blogpost is now planning to slow this improvement.

It’s very short sighted to think that IPTV-recording services will harm anyone. It’s very far from piracy and is very equivalent to recording with digibox or even with VHS. Well, it does little harm. Let’s say that Expendables comes from tv and I record it with my IPTV. It will stay there for 90 days. TeliaSoneras service allows user to have recordings 90 days stored until those will be erased. That little harm is that I won’t probably buy or rent the movie for these 90 days because of my awesome IPTV-service. It’s a small harm comparing to piracy that happens in the internet for example by Piratebay like services. Almost all movies that come from tv are at least three years old. Not many people will rent or buy those movies or series. Notice also that TeliaSoneras service won’t allow user to record payed CMORE, MTV nordic, Discovery or any other cable like channels.

What TTKV is doing is almost criminal. IPTV-recording is an easy target while they can’t do anything to REAL piracy. I really suggest that people at TTKV consider to get a real job or at least try to do their current job the way that it won’t hurt general public.

tiistai 27. maaliskuuta 2012

Coding quickie: jQuery Mobile and dynamically created content.

I think I don't have to start telling how good jQuery Mobile(JQM) framework is. I think it's the fastest way to create mobile-webpage for newer smartphones. I have tested this framework with iPhone 4 (iOS), iPad 1 and 2 (iOS), HTC Desire HD (Android), Samsung Galaxy Tab (Android), HTC Mozart (WP7), Nokia Lumia 800 (WP7) and amazing Nokia 5230 (Symbian).
I even got my hands on a Blackberry tablet for few minutes. Unfortunately my mobile detection didn't count it as a mobile device so I can't tell how well JQM works on it.
Devices that understand JQM well and run it smoothly are iOS and Android devices. Amazingly JQM works in Nokia 5230's own browser. It doesn't look as good as on iOS or Android devices, but it’s functional. JQM works with WP7 also. Full list of supported devices can be seen here.

#Skip this part if you are not interested in my opinions#
I'm from Finland, the land of Nokia. The great Nokia has employed Finns for decades and is the most known brand from Finland.
Actually it's not too long time ago that I admired Nokia myself. I was very proud of that we had something like Nokia in this little cold country. This is the time when Nokia had just published E63 and E71 "smartphones". I still think that those were one of the best business smartphones of that time. Those were little bit like Blackberry rip-offs but what can you do if you can’t buy Blackberry from Finland.
To the point. For a long time now I haven’t had faith in Nokia. Finland or Finnish shareholders own only a little fraction of Nokia. Well, head office of Nokia is still located in Finland, but I think that not for long anymore. Deal (or alliance) with Microsoft was the last nail in the coffin.
As a developer, WP7 operating system looked promising. I had my first glance of it at Geeks On Wheels-seminar at Tampere. It's promising but I think that it's too raw operating system for Nokia. Android or Maemo would have been better choices. Personally I think that engineers that have worked at Nokia have that kind of knowledge that when they leave Nokia, we will soon have lots of new innovative companies at Finland. These companies will do the same for the technology world that Angry Birds did for the gaming world.
These are just my own opinions and the point of this blog text was not to tell how sad I'm because of Nokia.
#Skip this part if you are not interested of my opinions#

The problem that this blog-post solves is explained next. If content like list items is fetched with ajax the JQM-theme won’t work on it. This is because the JQM does different kind of changes at start to the content that is inside the pages.

JQM will change next button from this:
<a href="#" class="notheme_button" data-role="button">No theme button</a>
in to this:
<a class="notheme_button ui-btn ui-btn-up-c ui-btn-corner-all ui-shadow ui-btn-up-undefined" data-role="button" href="#" data-corners="true" data-shadow="true" data-iconshadow="true" data-inline="false" data-wrapperels="span">
<span class="ui-btn-inner ui-btn-corner-all">
<span class="ui-btn-text">No theme button</span>

If the content is fetched with ajax and placed to some JQM-page it won't do these changes. Thats why I'm introducing .trigger("create").

This is the normal JQM page:
<div data-role="page"  data-theme="c" id="main">
    <!-- header -->
    <div data-role="header" data-theme="a">          
    <!-- header -->
    <!-- content -->
    <div data-role="content">
        <a href="#" class="notheme_button" data-role="button">No theme button</a>
        <a href="#" class="button" data-role="button">Refresh theme button</a>
        <div id="job_list">
    <!-- content -->

Let's say that we want to put content inside to div#job_list with ajax. Content that I want is links styled with listview.
Let’s fetch the content like this:

Simple javascript ajax function(You can use jQuery .ajax() or .get() too).
function ajax_procedure(ajax_url,style){
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        if(style == "theme"){

Here is the php file that gives the list of links as a response:
    if($_GET['giveLinks'] == "yes"){
        echo '<ul data-role="listview" data-inset="true" data-theme="d">';
            echo "<li><a href='' rel='external'>Google</a></li>";
            echo "<li><a href='' rel='external'>Yahoo</a></li>";
        echo '</ul>';

and the functions that put response to div#job_list.
function theme(response){

function notheme(response){

This solution is not big secret and it can be found from stackoverflow also. I just explained and simplified the procedure a little. 
Why? If you haven't encountered this problem yet and you are building JQM based apps you are about to struggle with this problem.

Here's the working example of.trigger("create").

torstai 23. helmikuuta 2012

Coding quickie: How to make hitbox in canvas with arrays.

This is a tutorial how to make a hitbox with javascript. This code can be used to detect if objects in canvas were hitted. My way is using arrays to store the objects properties.
First understand that object is a block that starts and ends somewhere in canvas in both x- and y-axel.

This shows what data is needs to be stored

Let's define arrays for objects x- and y-axels start and end points. Let's also define array that defines what was hitted.
var hit_areaxs = new Array(),hit_areaxe = new Array(), hit_areays = new Array(),hit_areaye = new Array(),hit_what = new Array();

Next we put "Cow" to array.
hit_what[0] = "Cow"; //defines hitboxes name
hit_areaxs[0] = cow_x; //defines startpoint
hit_areaxe[0] = cow_x + cow_size; //defines endpoint
hit_areays[0] = cow_y; //defines startpoint
hit_areaye[0] = cow_y + cow_size; //defines endpoint

After that create a function that detects if mouse hit the hitbox.
Function needs two more arrays.
var x_hit= new Array(),y_hit=new Array();
These arrays store the name of the object that was hit.

Hit test happens in for loop:
for(var i = 0; i < hit_areaxs.length; i++) { //loop can use any hit_areas x or y start or end point
x_hit[i]="x"; // define something to x_hit and y_hit arrays to ensure that
y_hit[i]="y"; // x_hit and y_hit have diffirent values

if(hit_areaxs[i] < mouseX && hit_areaxe[i] > mouseX){ //check if any object in x-axel were hitted
x_hit[i] = hit_what[i]; //store the hitted objects name
if(hit_areays[i] < mouseY && hit_areaye[i] > mouseY){ //check if any object in y-axel were hitted
y_hit[i] = hit_what[i]; //store the hitted objects name

if(x_hit[i] == y_hit[i]){ //if x and y hit values are the same print hitted objects name in console.log
console.log("You hitted: " + hit_what[i]);

This shows what click does

perjantai 20. tammikuuta 2012

Coding quickie: JQuery and CSS3 animation.

You may have struggled with JQuery .animate and CSS3 shadow. Shadow can be applied for text and boxes like divs. Because .animate() can’t (or at least I haven’t got it working) animate box-shadow animation, I’ll show you a way to do it another way.
Instead of using .animate() I use .css(). Function .css() itself doesn’t have the capability of animating things. CSS3 has this capability. It’s called transition. Transition can be applied to text- and box-shadow. First you need to tell where you want to apply transition effect (in example it’s: all) then how long transition lasts (0.5s) and last the type of timing (ease-in-out).
Here’s how to make box glow when you put your mouse over:


<div class="apply_to_this">
This is div

'box-shadow':'0px 1px 5px 2px white,0px 0px 2px black inset',
'-moz-transition': 'all 0.5s ease-in-out',
'-webkit-transition': 'all 0.5s ease-in-out',
'-o-transition': 'all 0.5s ease-in-out',
'transition': 'all 0.5s ease-in-out'
'box-shadow':'0px 1px 5px black,0px 0px 2px black inset',
'-moz-transition': 'all 0.5s ease-in-out',
'-webkit-transition': 'all 0.5s ease-in-out',
'-o-transition': 'all 0.5s ease-in-out',
'transition': 'all 0.5s ease-in-out'

Notice that you can do the same with css by using :hover.

Working example