Thursday, 10 August 2017

Powershell Receive-Jobs Failing on scheduled tasks


Since get-wmiobject doesn't have a timeout parameter a favorite snippet of mine is to use something like this

$WMIjob = Get-WMIObject -class win32_operatingsystem -ComputerName $somecomputer -ErrorAction SilentlyContinue -AsJob | Wait-Job -Timeout 3
$win32OS = $WMIjob | Receive-Job -erroraction silentlycontinue
if($win32OS){
      do something...
 
This creates a 3 second timeout for WMI, which is very handy when querying hundreds of systems



This works fine in the ISE, but when using it in a scheduled task I get no result


There are lots of ways to output script errors. One way I like is to add a line to output the error object to a powershell xml file into my scheduled script.

$error | Export-Clixml C:\Scripts\errorcli.xml

Then load it into the ISE to have a look

$myerror = import-clixml  C:\Scripts\errorcli.xml
$myerror

There is a 'jobstatefailed' error, what gives?

Invalid parameter
    + CategoryInfo          : InvalidResult: (:) [], ManagementException
    + FullyQualifiedErrorId : JobStateFailed

My google-fu doesn't reveal much information about this on the interwebs.
Turns out you need to run the scheduled task with highest privilages. Then it is happy




Saturday, 14 May 2016

Kindle weather display with RF temperature sensor





I've seen projects on the web about:
  1. Turning old kindles into internet data weather displays.
  2. Using RF temperature sensors on a Raspberry Pi and displaying the data on a Pi served web page or simple LCD display.
Combining these two projects seemed like a great project idea to learn about the Pi and electronics. I haven't seen this particular combination published on the web yet.


To explain what the kindle display shows:

  1. 'House Temperature Sensor' Section: 
    • Inside temperature as measured by a DHT22 sensor attached to the Pi
    • Outside temperature as measured by a remote RF temperature sensor.
  2. 'Local Weather Forecast' - API call to weatherunderground.com to show 7 day descriptive forecast and graphs of local temperature, wind, cloud cover, and rain chance.  

Or to put it in a more visio kinda way:



Full tutorials take a lot of time to write, plus it's no fun following cut and paste steps. The real fun of tinkering is reading stuff, trying stuff and adapting it to your own needs. So here are some basic tips and pointers for anyone interested.


Hardware

1) Raspberry Pi. 
I got a version 3. There is a slight hack to get the Uart pins working the same way as the Pi2 as the slice of radio expects.
     sudo nano /boot/config.txt
and add
     #  Change device tree to enable slice of radio
     dtoverlay=pi3-miniuart-bt
(Thanks goes to this site )

2) RF receiver (Slice of Radio) and remote temperature sensor

http://projects.privateeyepi.com/home/home-alarm-system-project/wireless-projects/wireless-temperature-sensor-2-0
This remote temperature sensor is good as it doesn't require any configuration over serial port (unlike xbee devices). The bad news is that you have to solder the components together yourself. Price is good, you can buy the receiver and sensor as part of one kit.

3) Local temperature sensor. 

Any local temperature sensor would work. I got a Adafruit DHT22 but lots of others would do the same thing. You could also buy another RF remote sensor and just use that indoors too.


SQLite database

There's tons of tutorials and info and how to create databases and use insert and select statements.
The only things I would add is

1)
use 'detect_types=sqlite3.PARSE_DECLTYPES' in your sqlite3.connect command. This makes python convert the millisecond values directly into python datetime objects.
e.g. "conn = sqlite3.connect('/home/pi/yourDB.sqlite',detect_types=sqlite3.PARSE_DECLTYPES)"

2)
install iceweasel on your pi (which is firefox) and add the addon SQLiteManager. Managing your DB just got a whole lot easier.

Saving sensor data to a SQLite database

The adafruit local sensor comes with its own library and examples. Create a cronjob for every 5 minutes to poll the sensor and insert it into the database.

The Slice of Radio works differently in that a bash script needs to be running all the time to capture incoming serial messages. Again use the example code provided with it and modify it to insert the data into the DB.

Setting up the Apache webserver and CGI scripts

This website is a great resource for CGI scripts, SQLite and Apache.

He also has a excellent full tutorial on reading temperature sensors and displaying this via a google charts webpage. I ended up using flot charts in my project as they have the shading capability which I use to denote night times.

Flot charts

This site is a great tutorial on logging temperature sensors to a Pi database and then displaying this via a webpage using flot charts. Especially useful is his code to create shaded areas on the graph to denote weekends, I adapted this code to denote nightime areas on my graphs.

Converting the webpage to PNG

The kindle needs a small PNG file to display, it can't show a webpage.
wkhtmltoimage with a javascript delay of 2000ms will create your PNG file
pngcrush will compress the file to a size agreeable with the kindle.

Kindle

This site will give you all you need to know about hacking the kindle.

  1. Jailbreak
  2. Install KUAL - this give you a nice menu where you can disable your screensaver and turn on usbnet
  3. Install USBnet - this allows you to access the kindle over putty. Your read about some kindles not needing this but do it anyway, my kindle technically didn't need it but I had problems running cron without connecting via usbnet. 
  4. setup a cron job to call your bash script every 5 minutes (more on this script below)

Some tutorials out there setup much more (e.g. functionality for enabling/disabling the cron job) but I didn't bother with that.

Weather display script

This site is one of the first internet resources on create a kindle weather display using internet forecast data.
I used his display-weather.sh bash script. This is the file that is executed by the cron job on the kindle. The commands fetches the PNG file served from the Pi and display it on the kindle.

Internet Weather Forecast

Weather Underground provide free access to weather forecast via API calls and JSON responses.


Monday, 2 May 2016

breaking out a slice of radio on a raspberry pi cobbler


Using a slice of radio RF transmitter will take over the GPIO pins on your raspberry pi.
Sometimes you want your leftover GPIO pins back so you can use them for other things. (e.g. a DHT22 temperature sensor in this picture below)

A slice of radio only needs Tx, Rx, 3+ and GND.
You can cobble your slice of radio on a 26 pin cobbler and then put this on a breadboard.

I had the unfortunate surprise that the breadboard for my 40 pin cobbler and the 28 pin cobbler are slightly different dot alignment for the power and ground rails. So I had to daisy chain them together with one on top of the other