Thursday, 12 May 2016

Using Python, MySQL and UTF-8

I have been spending way to long time figuring out how I can encode my data to utf8 when fetching records from MySQL database. All the time I was focusing on encoding the output, resulting in error like this one.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 1: 
ordinal not in range(128)
The solution I finally found is to set the character set when I prepare the MySQL connection
con.set_character_set('utf8')
Ending up in my code like this.
!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb as mdb

def getname(p):
    con = mdb.connect('hostname', 'username', ' password', 'databasname')
    con.set_character_set('utf8')
    sql = "SELECT cellname1 from tablename where cellname2='%s'"%(str(p))
    cur = con.cursor()
    cur.execute(sql)
    row = cur.fetchone()
    if not row:
        return 'n/a'
    else:
        return str(row[0])
    cur.close()
Now everything works like a charm.
Tuesday, 15 December 2015

How To Allow Usb To Serial Adapters In Osx “el Capitan


In my line of work I use serial port on some devices frequently and most new computers do not have serialport. In Linux I found out that it’s easy to connect using usb to serial and screen, but now when I’ve got mac it looks like there is no driver for the usb to serial by default.
I bought PL-2303 Driver (€6.17/$7.36) and all my problems are solved. It'a easy and fast installation.
After I connect the USB serial converter I simply type in:
screen /dev/tty.Repleo-PL2303-00001014
-cheers
Monday, 23 November 2015

Windows is blocking random websites


My gf is running Windows and some time after she got Windows 8 some DNS issue occurred and random webpages started to get blocked. After update to Windows 10 this problem was still ongoing. To "fix" this she had to use
ipconfig /flushdns
But that was just a temporary fix because later same day you could need to do this again.
I did multiple web searches and in the end I found this method that seems to fix this issue.

Run CMD as admininstrator.
In the command line type:
netsh winsock reset
Press Enter then restart your PC.

Netsh winsock reset is a helpful command that you can use in Windows to reset the winsock catalog back to default setting or clean state.
In computing, the Windows Sockets API (WSA), later shortened to Winsock, is a technical specification that defines how Windows network software should access network services, especially TCP/IP.



Tuesday, 9 June 2015

Terminal won't launch after upgrade to Ubuntu 15.04

After upgrade to Ubuntu 15.04 there is know bug where you can't get the terminal to open with Ctrl+Alt+T
Apparently the problem has to do with custom locale. You can fix this by changing the locale /etc/default/locale
LANG="en_US.UTF-8
"LANGUAGE="en_US"
Or you can add the LANG=en_US.utf8 to your ~/.bashrc file by adding following line to it
export LANG=en_US.utf8
If you have any improvement to this or a better method, please feel free to share it here in the comments.
Monday, 8 June 2015

How to embed a Google Calendar in a responsive site

I'm using embedded google calendar on a responsive website and in my opinion it works quite well on a mobile device. Well it works on my LG G3 with Android 5.0 Lollipop.
But there seems to be a problem as I had some complaints from some iPhone users where the calendar would display fine but not populate any actual events.

So, what to do. One solution was brought to my attention where two versions of the calender code is used in two div's. One div for normal view and one div for mobile view and some css to hide or show based on the width of the screen. Yes, it works. But the calender view in this solution looses part of it's functions like the week view. I think it is a bad solution where you "fix" one thing by making it worse for those where it works.

My solution might not be the best in the world, but I'm trying to fix only what is broken. The iPhone view. That's why I use php to find out what device or browser is viewing the page and then I select which calender code I use. And I use a bit from fhe CSS from the other solution.

The main difference is the URL to the calender is "embed" vs. "htmlembed"
Original:
https://www.google.com/calendar/embed?
The fixed version:
https://www.google.com/calendar/htmlembed?

The web page has a Wordpress cms and to use php code on a page or a post we need to install some plugin for that, I choose PHP Code for posts for this job, but you can use anything that works for you.

Here is the code I use to select how I display the calendar
<?php
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'ipod'))
 {
 
 ?>
<div class="responsive-iframe-container">
<iframe style="border-width: 0;" src="https://www.google.com/calendar/htmlembed?***"></iframe>
</div>
 <?
}else {
 
 ?>
<div class="responsive-iframe-container-normal">
<iframe src="https://www.google.com/calendar/embed?***"></iframe>
</div>
 <?
}
?>
Here is the CSS I added, it might need some adjustments.
.responsive-iframe-container-normal iframe,   
.vresponsive-iframe-container-normal object,  
.vresponsive-iframe-container-normal embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
.responsive-iframe-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 30px;
    height: 400px;
    overflow: hidden;
}
.responsive-iframe-container iframe,   
.vresponsive-iframe-container object,  
.vresponsive-iframe-container embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
If you have any improvement to this or a better method, please feel free to share it here in the comments.
Sunday, 26 April 2015

Sublime text in "command mode"

 
I really love Sublime Text 3, it's very easy to use and powerful tool at the same time. One of the things that recently started to bother me is "command mode". Some how doing things I have done many times before like selecting word or string multiple times CRTL + D, edit them and quit the selection with ESC which is enabling "command mode". Something has changed in my setup I think, because I'm pretty sure that this did not happen before and for some time I just closed the file and reopened to get back in "insert mode". But after all, the solution was quite simple. i takes you back to "insert mode". Even better, at least in my case where I'm not using this "command mode", simply go to preferences -> settings-user and add following.
"ignored_packages":
[
    "Vintage"
]
Monday, 13 April 2015

Taking and tweeting a photo with raspberry pi


At first I was going to create a timelapse of my cherry tree while blooming, then I decided to make a tweet once a day with a image.

Timelapse

Based on older post on how to make a Timelapse with Rapberry Pi camera module, I made small change. Rather than do a loop of x images I choose to use cronjob to run the script every 15 minutes. The script is simple and short.
#!/bin/bash

DIR=/home/pi/cherrytree
/usr/bin/raspistill -w 1920 -h 1080 -rot 180 -n -t 1000 -o $DIR/$(date -u +"%Y%m%d-%H%M-%S").jpg

Next add to your cronjob your interval, I use every 15 min.
*/15 * * * * /bin/bash /home/pi/cherry.sh >/dev/null 2>&1

Twitter bot

There's more than one way to cook an egg... There are also many ways to make a twitter bot.
I installed Twitter CLI
sudo apt-get install ruby-dev
gem install t
You will also have to create or have access to an twitter app to authorize the CLI interface.


Here is a simple script I use to take picture and tweet it. Every time I run this script, I just overwrite the image, but you can collect them by naming the file like in the script above.
#!/bin/bash
DIR=/home/pi/autotwitter
DATE=$(date -u +"%A %d %B %Y")
/usr/bin/raspistill -w 800 -h 460 -rot 180 -n -t 1000 -o $DIR/twitter.jpg
/usr/local/bin/t update 'Image of the day, '"${DATE}"'. #CherryBlossoms #raspberrypi #cli ' --file=$DIR/twitter.jpg
Once a day this script runs by cronjob at 12:01
1 12 * * * /bin/bash /home/pi/twitter.sh >/dev/null 2>&1
In addition to that, you can auto follow your followers in the same script by adding following line to the twitter script.
/usr/local/bin/t groupies | xargs /usr/local/bin/t follow