Hello World with .NET Core


.NET Core is Microsoft’s new cross-platform Command Language Runtime (CLR). This post is an elementary getting started guide where I create, build and run a “Hello World!” console application with .NET Core.

Let’s begin by downloading and installing .NET Core for your platform. To create a new console applications run

dotnet new

That will create two files – project.json and Program.cs. Edit Program.cs using your favorite editor.

I recommend using the multi-platform VS Code. You can extend the capabilities of VS Code by downloading extensions from the VisualStudio Marketplace. VS Code should prompt you to download the C# extension when you open Program.cs.

To build the new program from the command line run

dotnet build

To execute the program run

dotnet run

You can also edit, and run or debug the program using VS Code, as shown in this screenshot

.NET CLR with VS Code

Console applications can be very powerful, but .NET CLR can also be used to build Web applications using ASP.NET Core.

git branch


This post lists some useful Git commands related to branching and merging.

List all branches

git branch -v

List all remote branches

git branch -r -v

See status log with branch refs

git log --decorate=full

Pull all branches from remote

git pull origin

Pull a specific branch from remote by name, as FETCH_HEAD, but continue on current branch

git pull origin name

Switch to the branch

git checkout FETCH_HEAD

Create a new branch with specified name from current start point

git checkout -b name

Switch to master branch of origin

git checkout master

Merge specified branch into the branch currently selected

git merge origin/name

Merge with result available in working tree to commit (history is not preserved)

git merge --squash origin/name

Run your favorite merge tool to resolve conflicts (I use meld, on Mac OS X)

git mergetool -t meld

Diff of file with version on HEAD (or branch name)

git diff [--cached] HEAD file

Delete a local branch

git branch -d name

Delete remote branch in cloned repo (can restore from origin with pull)

git branch -d -r origin/name

Permanently delete branch ref from remote (use caution)

git push origin --delete name
## OR
git push origin :name

Create a new branch at current start point (but continue on current branch)

git branch name

Checkout and switch to a branch (or start tracking and switch to a remote branch with same name)

git checkout name

Push branch to remote

git push origin name

Push all branches to remote

git push --all origin

Getting Started with the ESP-03


The ESP-03 is a very affordable Wi-Fi module containing the ESP8266EX chip by Espressif. The latter has become very popular among makers who want to affordably add wirelessly controlled smarts to things at home and work.

The ESP-03 has two useful modes of operation that can be initiated by controlling its GPIO pins. The normal mode connections are show in the following figure. The ESP-03 shown here is powered using SparkFun’s FTDI Basic Breakout – 3.3V – USB to serial module.

Normal Mode

In normal mode, the ESP-03 runs firmware programmed in the SPI Flash. SPI Flash is an external NOR Flash chip where program instructions can be stored, and retrieved later for execution. The ESP-03 has a 4 Mbit 25Q40BT part which allows for 512 KB of program space. Of that, about 423 KB is available for your own programs.

Serial Flash Chip

The second mode is the flash mode, in which new program instructions can be flashed to the SPI Flash, using tools such as the ESP Flash Download Tool. The connections are similar to those for normal mode, with the addition of GPIO0 connected to GND.

Flash Mode

The ESP8266 can be programmed using an SDK distributed by Espressif. Popular embedded development platforms such as the Arduino IDE and MicroPython, can also be used to develop firmware for the ESP-03.

Adafruit provides instructions for configuring the Arduino IDE for ESP8266 development. Here’re the settings I use with the ESP-03.

Arduino Settings

Try out the example project under File -> Examples -> ESP8266WiFi. With it, you’ll be controlling a GPIO pin on the ESP-03, over your Wi-Fi network, in no time at all.

wifi-web-server.gif
Wi-Fi Web Server

Run homebridge as a service upon reboot


This post shows how to run homebridge automatically upon reboot using upstart.

Install upstart

sudo apt-get install upstart

Create configuration file /etc/init/homebridge.conf with

start on stopped rc
stop on shutdown

setuid pi

script
    export HOME="/home/pi"
    export NODE_PATH=$HOME/node_modules/
    gpio -g mode 27 out
    gpio -g mode 27 down
    gpio export 27 out
    exec /usr/local/bin/homebridge
end script

start on stopped rc ensures that avahi-daemon has been started by its SysV script under /etc/init.d before homebridge is started.

Test the job by running it thus

sudo start homebridge

Use the following command to check the output of the job

sudo tail -f /var/log/upstart/homebridge.log

The following command can be used to verify that homebridge job has been started

sudo initctl list | grep homebridge

To stop the above job

sudo stop homebridge

To run job as a service that will run automatically at boot

sudo service homebridge start

To stop the service forever

sudo service homebridge stop

Toggle GPIO on Raspberry Pi using HomeKit


In this post, I take my HomeKit Raspberry Pi integration a step further, by turning on/off a LED using the homebridge-gpio-wpi plugin. With the ability to control the GPIO pins, I should be able to turn on/off much bigger things using solid state relays and such.

Install homebridge-gpio-wpi

The installation should be pretty straightforward. Assuming you are at the command line in the home folder, run

npm install homebridge-gpio-wpi

That should install all node modules under ~/node_modules/.

Configure homebridge by editing ~/.homebridge/config.json. Here’s mine

{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:32",
        "port": 51826,
        "pin": "031-45-155"
    },
    
    "description": "This has some fake accessories",

    "accessories": [
        {
            "accessory":      "FakeBulb",
            "name":           "Test lamp",
            "bulb_name":      "Lamp 1"
        },
        {
            "accessory": "GPIO",
            "name": "GPIO2",
            "pin": 27
        }
    ],

    "platforms": []
}

Configure GPIO2 using the gpio utility, and start homebridge

gpio -g mode 27 out
gpio -g mode 27 down
gpio export 27 out
export NODE_PATH=$HOME/node_modules/
homebridge

See also how to run homebridge as a service upon reboot.

Test with HomeKit

If you’ve configured the Homebridge peripheral in an iOS app such as Hesperus, it should now show you a new device called GPIO2, and allow you to switch it on/off.

LED

Hesperus allows you to create a schedule to turn on and off devices.

Schedule

Make things smart with HomeKit and Raspberry Pi


As an avid iOS user I have been keen on using HomeKit. That’s when I read about a new – and currently free – HomeKit app in the iOS App Store called Hesperus. I don’t have a HomeKit compatible thing at home, but a quick internet search revealed that I could run a HomeKit compatible service called homebridge on a Raspberry Pi. This post only goes so far as configuring a fictitious light bulb plugin that can be controlled remotely.

Setup Raspberry Pi Image

I decided to download a headless (console-only) version of Raspbian called RASPBIAN JESSIE LITE. Instructions for setting up an SD card appropriately can be found here. I tend to use the Windows based Win32 Disk Imager, I trust it and have used it – for as long as I can remember – to write Ubuntu ARM images.

Powering the Raspberry Pi

I didn’t want to use an HDMI display with the Raspberry Pi, and wanted to power it using my laptop. I have used a USB to serial adapter to do that in the past. This time, I went with Adafruit’s USB Serial TTL cable as described in this lesson. That done, I was able to power up and login to the Raspberry Pi using a serial terminal. I tend to use screen on Linux or Mac OS X

screen /dev/ttyUSB0

Configuring Wi-Fi

RASPBIAN JESSIE LITE lacks a full-fledged user interface, making Wi-Fi configuration slightly painful. I am using a Wi-Fi stick and had some issues getting the driver to work. Hopefully, you’ve got a Raspberry Pi 3, or a compatible Wi-Fi stick that does not require too much tinkering. You can also use Ethernet. The following can be used to check whether your network interface can be listed

ifconfig

Look for an interface called wlan0 if using Wi-Fi, or eth0 if using ethernet.

This is how you can create a configuration file for your Wi-Fi access point

wpa_passphrase your_SSID your_passphrase > your_SSID.conf

Copy the contents of your_SSID.conf and paste them into /etc/wpa_supplicant/wpa_supplicant.conf using any text editor. I used vi thus

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

Having done that, Wi-Fi was up and running. I had internet access, and could access the Raspberry Pi on the local network via ssh.

Installing packages

A few additional Linux packages and configuration steps are required before homebridge may be installed. Packages can be installed thus

sudo apt-get update
sudo apt install nodejs npm git libavahi-compat-libdnssd-dev

Updating Node.js

The version of Node.js installed by apt-get is rather dated, and will not work with homebridge. To update node, use the following commands

sudo npm install -g n
sudo n stable

Install homebridge

homebridge can be installed using npm thus

sudo npm install -g homebridge

Find and install plugins

To do anything interesting with homebridge you’ll require a plugin, and have it configured in ~/.homebridge/config.json. One simple plugin called homebridge-fakebulb can be installed thus

sudo npm install -g homebridge-fakebulb

Its sample configuration file can be used to create the config.json file mentioned above. This is what my config.json looks like

{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:32",
        "port": 51826,
        "pin": "031-45-155"
    },
    
    "description": "This has some fake accessories",

    "accessories": [
        {
            "accessory":      "FakeBulb",
            "name":           "Test lamp",
            "bulb_name":      "Lamp 1"
        }
    ],

    "platforms": []
}

Use with HomeKit

Apple’s HomeKit has been app-less since launch. Siri is the only way you were able to control HomeKit devices. HomeKit has a rich API and it didn’t take long for paid apps to appear in the App Store. Hesperus is a new free app that I opted to use to control homebridge.

Here’s Hesperus with the Homebridge perihperal paired and working, showing the Test lamp device’s status. I can control the Test lamp (turn it on/off) anywhere I have an internet connection because I have an Apple TV (generation 4 – but 3 should also work) at home. Apple TV needs to be signed into the same iCould account as the iOS device paired with the Homebridge peripheral.

IMG_1979