Tuesday, 28 June 2016

R Install Required Packages

When using R, if there is a script with multiple packages that might not be installed, it can take some time to go through the list and check they are all installed.

Instead of using the "require(packagename)" command, I used the following script to load packages

pkgTest <- function(x)
{
  if (!require(x,character.only = TRUE))
  {
    install.packages(x,dep=TRUE)
    if(!require(x,character.only = TRUE)) stop("Package not found")
  }
}

This script tries to load the package using the "require" command, and if this is not successful then will try to install it. It then tries the "require" command once more, and if not successful will stop the code execution.

Monday, 6 June 2016

MATLAB Error - Warning: Executing startup failed in matlabrc. In matlabrc at 212

After reinstalling MATLAB I saw the following error:

Warning: Executing startup failed in matlabrc.
This indicates a potentially serious problem in your MATLAB
setup,
which should be resolved as soon as possible.  Error detected
was:
MATLAB:FileIO:InvalidFid
Invalid file identifier.  Use fopen to generate a valid file
identifier.
> In matlabrc at 212 

This was due to a problem in my "startup.m" file which was still persistent from a previous installation, which tried to load a toolbox that was no longer installed. This was located in the following folder:

%UserProfile%\Documents\MATLAB

As I had no files there that I needed, I renamed the folder to MATLAB_old, then tried opening MATLAB again. This time it started up fine so I deleted the folder completely as it is not needed for my version (R2012a).

Saturday, 9 April 2016

Problems when programming the ESP8266-12E using Arduino software/IDE

I followed this instructables tutorial to program the ESP8266-12E. I was able to program the board the first time correctly, however trying to make changes and update the code didn't work. When I tried to update the code using the arduino IDE, I got the following messages:

warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed

So I tried changing some settings with no luck. By trial and error I found the solution. On the board is a button called "Flash". This button needs to be held down when connecting the USB cable between the board and your computer. If not, it is not possible to program using the default arduino settings. I expect there is some way to enable this however I haven't yet dug deeper in to this problem, as I just wanted to get some basic functionality working.

Friday, 26 February 2016

R View more than 100 columns when exploring variable data.

I normally use R Studio when working with R. I found there is a problem when double clicking the variable name in the environment view, as the window which is displayed only shows the first 100 rows. Double clicking the variable name is the same as entering the following command:

View(data)

Instead, use the following command:

utils::View(data)

These examples assume the name "data" is the one that you want to view.

Friday, 5 February 2016

Scan WiFi networks on the raspberry pi using essid and the grep command

First of all, find the name of the wireless network adapter. Enter the command ifconfig to show each network interface along with its details. The IP address and further information is also shown, but we just need the name shown in the left most column. There will be an entry for lo, and probabaly one for eth0 if you have a connection by ethernet also. My WiFi dongle showed up as wlan0 which is typical. If you have more than one WiFi adapter, it will probabaly show up as wlan1, wlan2, etc.

Then use the following command:

sudo iwlist wlan0 scan

I have used wlan0 as the interface name, if you found something earlier in a previous step then obviously you should change wlan0 to the interface name.

I ended up getting quite a log list of networks which went off the top of the screen, with a lot of unneeded information. So it is possible to reduce the amount of information shown. To do that, we pipe the output to the grep command, which searches within the data and only displays data that matches what we are searching for. Each network contains information about the network, including the field "ESSID" which is the network ID. To only display these lines, use the following command:

sudo iwlist wlan0 scan | grep ESSID

The command is case sensitive, alternatively we can use the -i flag at the end to make it case insensitive, as follows:

sudo iwlist wlan0 scan | grep essid -i

Thursday, 28 January 2016

Connecting SKU193431 I2C RTC To a Raspberry Pi

The I2C module came in a bag with "SKU193431 EVU" on the sticker, which Googling finds the banggood page from which it was ordered, but not much else.

http://www.banggood.com/RTC-Real-Time-Clock-Module-For-Raspberry-Pi-p-965976.html

There is no schematic or any further details, also there is no markings on the RTC module to indicate even which pin is positive. I found the following image on the internet showing the GPIO header pinout:



I took a guess that I should connect it to point 01, 03, 05, 07, 09. This is the inner most set of connectors. Then I carried on with a guide that I had found online. Luckily my guess was correct. There are no other pins on the GPIO header with an I2C label, but I was mostly concerned about the power connections - if connected the wrong way with 3.3V the wrong way across the battery it could cause some damage. Anyway, luck was on my side and it seemed to all work fine. So on to the software setup.

The first thing to do is to enable I2C in the raspi-config tool. To do this, open the configuration tool:

sudo raspi-config

Go in "Advanced Options" > "I2C" > "Yes"

When asked if the default module should be loaded by default, select "Yes", and then reboot the raspberry pi.

Then I followed steps from here: http://www.drewkeller.com/blog/adding-hardware-clock-raspberry-pi-ds3231

# Comment out the blacklist entry so the module can be loaded on boot
sudo sed -i 's/blacklist i2c-bcm2708/#blacklist i2c-bcm2708/' /etc/modprobe.d/raspi-blacklist.conf
# Load the module now
sudo modprobe i2c-bcm2708
# Notify Linux of the Dallas RTC device (use -0 for Model A or -1 for Model B)
echo ds3231 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
# Test whether Linux can see our RTC module.
sudo hwclock

The response was the time that the RTC thought it was, which hadn't yet been set. If you get this, then that looks good ad the hardware aspect is correctly connected and working.

The raspberry pi was connected to the internet and got the time and date automatically, as can be checked by the "date" command.

If the date is wrong, this can be set with the following command:

sudo date -s "Jan 27 2016 12:41:01"

I have previously had some problems when setting the time and date together, and found I had better luck setting first the date, and then the time, rather than both together in the same command. I think this was due to problems with the formatting, but this method of splitting the time and date helped. Then write the current date and time to the RTC module using the following command:

sudo hwclock -w

Then edit the /etc/rc.local file with this command:

sudo nano /etc/rc.local

and add the following two lines before the line "exit 0":

echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s

Now disable the fake hardware clock with the following line:

sudo update-rc.d fake-hwclock disable

The network time can also be disabled, but I wanted to leave this in place. To disable this, use the following command:

sudo update-rc.d ntp disable

But if it is left in place, then the clock should be updated each time that the raspberry pi connects to the internet, but will keep time when powered off, and when rebooted without power.



Python base64 decode Problem

I was struggling with a problem in Python, trying to use the base64 package to decode some base64 data, using the base64.decode() function. I was passing my string to the function, so my code looked link the following:

import base64
#code to load base64 data from file
encoded='ZGF0YSB0byBiZSBlbmNvZGVk'
outputdata=base64.decode(encoded)

Well I obviously had different code to handle the loading of the data from the file, but I have just changed the code to use a string with encoded data for this example.

If you try and run this code, you will get the following error:

Traceback (most recent call last):
  File "removed", line 4, in <module>
    outputdata=base64.decode(encoded)
TypeError: decode() takes exactly 2 arguments (1 given)

This was confusing as I couldn't find any more information about what should be in the second argument, so I tried a few different things, nothing works.  If you do this, the error will instead change to the following:

Traceback (most recent call last):
  File "removed", line 4, in <module>
    outputdata=base64.decode(encoded, '')
  File "C:\Python27\lib\base64.py", line 303, in decode
    line = input.readline()
AttributeError: 'str' object has no attribute 'readline'

The problem was that I was infact calling the wrong function name. There are 2 separate functions, and the function I thought I was using, takes just a string, and the function that I was actually using takes 2 parameters, and I think is intended to read from a file on this disk.

So this is quite an easy fix, which is to instead use the function base64.b64decode(encoded_data), and just means I wasn't reading the manual well enough. I am documenting it here because Googling the previous error messages along with some keywords about base64 and decoding didn't turn up anything. It was only when I went back to the very basic example that I noticed my error. Thus the final working code is as follows:

import base64
#code to load base64 data from file
encoded='ZGF0YSB0byBiZSBlbmNvZGVk'
outputdata=base64.b64decode(encoded)
print(outputdata)