Android Things GPIO Callbacks

Following the blog post to get you up and running with Android Things, Visual Studio and Xamarin.  http://explorationspace.co.za/2017/02/26/using-xamarin-and-visual-studio-with-android-things/

 

Android Things has GPIO callbacks which are triggered on certain trigger events.    This is great for event handling.    I’ve added a sample to Xamarin bindings which illustrates this by means of a Grove PIR (Passive InfraRed) sensor much like what’s in your home alarm system.   If we use that in conjunction with a buzzer, you have a primitive alarm system.

 

The source can be found here:   https://github.com/apead/Xamarin-Android-Things

 

Intel Edison with PIR and Buzzer

 

 

Gpio callbacks are classes implemented inheriting from the abstract class Gpio Callback.    This call back is triggered on certain trigger types.   This are set on the gpio pin with the settriggertype method.

  • EDGE_NONE: No interrupt events. This is the default value.
  • EDGE_RISING: Interrupt on a transition from low to high
  • EDGE_FALLING: Interrupt on a transition from high to low
  • EDGE_BOTH: Interrupt on all state transitions

 

 public class AlarmCallback : Com.Google.Android.Things.Pio.GpioCallback
 {
 public Gpio BuzzerPin { get; set; }

 public override bool OnGpioEdge(Gpio p0)
 {
 if (p0.Value)
 {
 if (!BuzzerPin.Value)
 {
 BuzzerPin.Value = true;
 Thread.Sleep(1000);
 BuzzerPin.Value = false;
 }
 }

 return true;
 }

 public override void OnGpioError(Gpio p0, int p1)
 {
 Log.Info("Alarmcallback", "Error");
 }
 }

 

The class has two methods to override.   OnGpioEdge and OnGpioError.     The OnGpioEdge method is called on a trigger event.     The OnGpioError is called on an error.

 

This class is registered on the actual input gpio pin.


_callback = new AlarmCallback {BuzzerPin = _buzzerGpio};

_alarm1Gpio.RegisterGpioCallback(_callback);

 

Happy Android Things development with Xamarin!

 

Using Xamarin and Visual Studio with Android Things

Android Things is Google’s new IOT Android based platform.    In essence it’s a slightly altered version of Android which can run on Iot type devices such as the Intel Edison and the Raspberry Pi3.

 

“Android Things lets you build professional, mass-market products on a trusted platform, without previous knowledge of embedded system design.”

 

Personally I’m not yet too impressed with Android Things.   It is still very new and very raw and is lacking a lot of what you’d expect from an Iot platform.   However, having finally a standard Android implementation officially from Google running on these devices has to be a good thing.    The platform should just get better from here.

 

 

 

Running in Visual Studio using Xamarin

 

Of course being Android, Android Things works with Xamarin too.     Using Xamarin will also be a benefit for all those awesome cross platform benefits it offers, like sharing your Iot code with your backend services, Android, Ios, Windows Phone, Windows 10, OSX, XBox and whatever other devices you’d like to be supporting in your solution.

 

Getting the devices to appear in Visual Studio is exactly as you’d expect.    The devices will use the standard Android Debugging Bridge (ADB).      Plugging in an Intel Edison via USB will be reflected as “Unknown Iot_Edison”.    You can obviously also use the network debugging features of ADB.   eg.   adb connect <ip address>   The device will then also be detected in Visual Studio.   A Raspberry Pi will appear as “Unknown iot_rpi3”.

 

Using Xamarin is very simple.    All we need to do is bind to the Android Things API Jar to be able to make use of all the features provided by the Android Things SDK.   At this moment in time there isn’t an official release Nuget package from Xamarin to do this.   It is however very simple to do this yourself.     In this sample (and I’ve shared the code on GitHub) I’m binding to the new Dev Preview 2 API for Android Things (androidthings-0.2-devpreview.jar).    Once the binding project is referenced from your Android Things project, everything will work as expected.   Just of course with the benefit of using C# and not Java!

 

I’ve created some samples based on the Standard Android Things Samples

 

Samples:   https://github.com/apead/Xamarin-Android-Things

Blink Led

No Iot sample and demo is complete without a blinking LED.   This sample is a conversion of the Android Things sample.

 

 

Simple UI

This sample is a simple illustration of using a UI on an Android Things device.   It also illustrates GPIO pins and how to set the high/low values of the pins.

 

Simple UI Android Things Sample

 

 

 

Learning Resources:

 

SDK Samples:  https://developer.android.com/things/sdk/samples.html

How to install Android Things on your Device:   https://developer.android.com/things/preview/index.html

A great article on the basics of electronics and hardware:  https://riggaroo.co.za/android-things-hardware-basics/

 

Happy Android Things Development with Xamarin!

Installing Xamarin Cycle 9 side by side with Android Studio

You may need to run Android Studio and Xamarin side by side from time to time.   This may be needed if you are an Android developer experimenting with Xamarin or maybe you are migrating an Android application to Xamarin.   I run the two side by side often especially when I am binding native libraries for Xamarin.        I put together a guide to make the new Xamarin Cycle 9 release and Android Studio 2.2.3 run together without any pain and also without wasting disk space with multiple copies of Android Sdks.    I also find it’s much cleaner to install everything manually without the installers.     This guide is specifically aimed for Windows and Visual Studio 2015+ users.

 

Installing Xamarin

Install Java JDK 1.8  x64  (Use the latest update of this available)http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

 

Install the Android NDK

 

The NDK is a zip file.  Extract this to a location that’s easily accessible:   C:\AndroidSDK\android-ndk-r13b-windows-x86_64

 

Install the Android SDK

 

The simplest and cleanest is to install the command line version of the SDK

Android SDK Command line installation: https://dl.google.com/android/repository/tools_r25.2.3-windows.zip

This is just a zip file not requiring any installation.  Extract the contents to a location that’s easily accessible.  Eg.  C:\AndroidSDK\tools_r25.2.3-windows

 

Install the required SDK Tools and APIs

From command line execute <Android SDK>\tools\android  This will open the SDK manager.

 

Install Android SDK tools 25.2.5

Install Android SDK platform tools 25.0.3

Install Android Build tools 25.0.2

 

 

Install API 25 SDK Platform   (and whichever other APIs you require)

Install “Sources for Android SDK” for API 25.    Xamarin does not require this, but Android Studio will later request it be installed.    This will simplify the later setup if done now.

 

 

Install Google Repository

Install Google Play Services

Install Android Support Repository

 

 

Install Xamarin for Visual Studio 4.3.   If you installed Xamarin with Visual Studio 2015, you have it already.    To upgrade to Cycle 9 use the Updater Channel within Visual Studio.      Alternatively if you don’t have it, you can manually install it:  https://store.xamarin.com/account/my/subscription/downloads.   Download the individual installation package per platform (and not the universal installer).  At the time of writing this there isn’t a direct link to cycle 9, so download cycle 8 and update it.

 

 

Set up the SDK paths within Visual Studio.   There is a setting for both the path to the NDK and SDK previously installed.  The location of the Java SDK should be set here too.    Visual Studio will need to be restarted for this to take effect.

 

Note:   If Xamarin was previously installed via the Visual Studio or the Xamarin Universal Installer.  There will be another Android SDK installed.    This is most likely:   C:\Users\<user>\AppData\Local\Xamarin\Universal\AndroidSDK.     If the steps above were followed, this previous path can be safely removed.       This folder can be used to share the SDK with Android Studio, however personally I don’t think it’s a great location for doing that.

 

The Xamarin Cycle 9 installation is complete and should now work with the new SDK location.

 

Installing Android Studio 2.2.3

 

Download the Android Studio zip file (without installer and SDK):   https://developer.android.com/studio/index.html

 

Extract the Android Studio Zip to any desired location eg:  D:\AndroidStudio\android-studio-ide-145.3537739-windows

 

On first run, Android studio will start the wizard to set itself up (and download the SDK).   Skip the wizard.

 

 

Choose “Settings” from the Start screen.

 

 

Specify the path previously used for the Android SDK. eg:  C:\AndroidSDK\tools_r25.2.3-windows

 

Everything needed should already be in place, so Android Studio will accept the SDK without any problems.

 

 

Android Studio should now work fine with shared SDK with Xamarin.

 

Happy Xamarin and Android Coding!

 

Xamarin.Forms ContextActions, Binding and Mvvm Code Sample

How to bind a ContextAction in a Xamarin Forms ListView to a View Model?  This is a question I was asked recently.   I created a simple code sample for this, so thought I’d share it with everyone.

 

Below is a link to the sample on GitHub.    It includes an example of how to do it in code and in XAML.

 

 

https://github.com/apead/FormsMvvmContextActionSample

 

Happy Xamarin.Forms Coding!

Arduino Development with Visual Studio via Visual Micro (Overview)

Visual Studio has become a one stop shop for any type of development.  But did you know you can do Arduino development as well?    There is a Visual Studio extension called Visual Micro (http://www.visualmicro.com) which will allow Arduino development and debugging within Visual Studio IDE, with the full development experience you have become used to.   This is a high level overview of what is provided.

 

Arduino and Visual Studio

 

What’s really great about having Arduino support within Visual Studio is, you can group together all the various different projects that makes up your solution all together within a Visual Studio Solution.

 

Multi-platform Solution

 

In a world of Iot and cross platform (or multi platform) development this is perfect.   You could have one or more Arduino projects, .NET Micro framework projects,  a Xamarin Mobile client and the back-end (be it an on-premise ASP .NET WEB API solution, or in the cloud with something like Azure Mobile Services) all managed together in a single Visual Studio solution.

 

Installing Visual Micro

 

Visual Micro can be downloaded here:   http://www.visualmicro.com/page/Arduino-Visual-Studio-Downloads.aspx   It is a Visual Studio extension which will be installed within Visual Studio.   There is a version for both Visual Studio 2015 and 2017 RC.     Be sure to have the standard Arduino IDE installed as well, as it uses this tooling under the hood.   Arduino version 1.06 – 1.8 is supported.

 

The IDE Experience

 

Visual Micro Toolbar

 

 

Visual Micro Menu

 

Both a toolbar and a new menu is added to Visual Studio.    The toolbar allows access to quickly configure the connected board and COM port used (via USB).    It also allows quick access for building and debugging, however if the Arduino project is the startup project the debugging and build options work as usual direct from the Visual Studio hotkeys, toolbars and menu items.    The menu allows for deeper configuration of the compiler, debugging options and other integration options.

 

File New Dialog

Templates are provided to easily create a new Arduino Sketch or Library.    Standard Arduino INO files are supported.   These standard file types are also used when opening and saving to existing Arduino Sketch files, which ofcourse can be ported back to the Arduino IDE if need be.

 

Solution Explorer

 

The solution explorer is quite neat for the Arduino.   All the source files, header files and external dependencies are presented in much the way you have grown accustomed to in Visual Studio.

 

 

The Visual Micro Explorer provides a visualization of all libraries installed.    There is also reference material and documentation provided along with a collection of Example code.   This is great for both learning and quickly looking up something.

 

The debug experience

 

Debugging Experience

 

Debugging and conditional breakpoints are supported.   This does though require a purchase of the Pro version.   The pro version has quite a number of additional features aswell which you can read about here:  http://www.visualmicro.com/page/What-features-are-included-in-Visual-Micro-Pro.aspx

 

The live tracing works great.  There is also the standard port monitor which does live logging of the Com ports.    In the screenshot provided the codes displayed are being reported from an infrared remote control in real-time.   The ability to see everything happen, and be visible, at once in an IDE saves a whole load of time.

 

Conclusion

I’ve been using Visual Micro for a few Arduino related Iot projects and can definitely recommend using it.

 

Happy Arduino coding!  🙂

Getting Started with .NET Core

.NET core is really easy to get up and running.     Download the installer relevant to your environment from https://www.microsoft.com/net/download/core   An IDE such as Visual Studio is optional.  I’m going to start with the command line version as it’s really quick to get running.   It also doesn’t need much resources for a development environment, which is great as I’m currently using a Samsung Tablet with 2 Gig of RAM to write this blog post.

 

There is a “Current” and a “LTS” version.   Choose which version suites your support needs.   “LTS” versions are supported for 3 years.   “Current” is the latest version, and is supported for 3 months.    I’m going to use “Current” here as it contains all the new juicy bits.

 

.NET Core Installer

 

The installer will take a minute or two to run.  Once installed, the executable “dotnet” will be added to the path.  Also different version of .NET will be installed side by side.

 

C:\blog\dotnet

Microsoft .NET Core Shared Framework Host

Version : 1.1.0
Build : 928f77c4bc3f49d892459992fb6e1d5542cb5e86

Usage: dotnet [common-options] [[options] path-to-application]

Common Options:
--help Display .NET Core Shared Framework Host help.
--version Display .NET Core Shared Framework Host version.

Options:
--fx-version &lt;version&gt; Version of the installed Shared Framework to use to run the application.
--additionalprobingpath &lt;path&gt; Path containing probing policy and assemblies to probe for.

Path to Application:
The path to a .NET Core managed application, dll or exe file to execute.

If you are debugging the Shared Framework Host, set 'COREHOST_TRACE' to '1' in your environment.

To get started on developing applications for .NET Core, install the SDK from:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

C:\blog\

 

To create a new app, there’s a “new” command.   On first execution it will extract and expand, which may take a moment.

 

Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.
Telemetry
--------------
The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include commandline arguments. The data is collected by Microsoft and shared with the community.
You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.
You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.
Configuring...
-------------------
A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute to complete and will only happen once.
Decompressing 100% 2294 ms
Expanding 100% 4502 ms

 

By default, the “dotnet new” command will create a console app. Issuing a “dotnet restore” command, all required packages will be fetched.

02/14/2017 03:01 PM &lt;DIR&gt; .
02/14/2017 03:01 PM &lt;DIR&gt; ..
02/14/2017 03:01 PM 0 dotnet
11/11/2016 12:10 AM 214 Program.cs
11/11/2016 12:10 AM 367 project.json
 3 File(s) 581 bytes
 2 Dir(s) 92,367,912,960 bytes free

C:\blog\dotnet restore
log : Restoring packages for C:\blog\project.json...
log : Writing lock file to disk. Path: C:\blog\project.lock.json
log : C:\blog\project.json
log : Restore completed in 899ms.

C:\blog\

 

To run the application simply type “dotnet run”.

C:\blog\dotnet run
Project blog (.NETCoreApp,Version=v1.1) will be compiled because expected outputs are missing
Compiling blog for .NETCoreApp,Version=v1.1

Compilation succeeded.
 0 Warning(s)
 0 Error(s)

Time elapsed 00:00:00.6861620


Hello World!

C:\blog\

 

There are various project types:  console, web, lib and xunittest.   The type can be specified with the -t parameter.  The language can be specified with the -l parameter.  The available languages are C# and F#

 

To create a ASP .NET Core / web app, type:   “dotnet new -t web”

 

C:\blog\web>dotnet new -t web
Created new C# project in C:\blog\web.

C:\blog\web>dir
Volume in drive C has no label.
Volume Serial Number is 4A07-921B

Directory of C:\blog\web

02/14/2017 03:12 PM <DIR> .
02/14/2017 03:12 PM <DIR> ..
11/11/2016 12:10 AM 36 .bowerrc
11/11/2016 12:10 AM 3,889 .gitignore
11/11/2016 12:10 AM 265 appsettings.json
11/11/2016 12:10 AM 214 bower.json
02/14/2017 03:12 PM <DIR> Controllers
02/14/2017 03:12 PM <DIR> Data
11/11/2016 12:10 AM 1,193 gulpfile.js
02/14/2017 03:12 PM <DIR> Models
11/11/2016 12:10 AM 239 package.json
11/11/2016 12:10 AM 574 Program.cs
11/11/2016 12:10 AM 3,397 project.json
11/11/2016 12:10 AM 2,229 README.md
02/14/2017 03:12 PM <DIR> Services
11/11/2016 12:10 AM 3,193 Startup.cs
02/14/2017 03:12 PM <DIR> Views
11/11/2016 12:10 AM 563 web.config
02/14/2017 03:12 PM <DIR> wwwroot
11 File(s) 15,792 bytes
8 Dir(s) 92,065,746,944 bytes free

C:\blog\web>

 

Running the web application is simple: the  “dotnet run” command will make this happen.   It will even host it in web container.

 

C:\blog\web>dotnet run
Project web (.NETCoreApp,Version=v1.1) will be compiled because expected outputs are missing
Compiling web for .NETCoreApp,Version=v1.1
C:\blog\web\project.json(5,30): warning NU1007: Dependency specified was Microsoft.NETCore.App >= 1.1.0-preview1-001153-00 but ended up with Microsoft.NETCore.App 1.1.0.

Compilation succeeded.
1 Warning(s)
0 Error(s)

Time elapsed 00:00:01.3100351
info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]
User profile is available. Using 'C:\Users\apead\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Production
Content root path: C:\blog\web
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
ASP .NET core landing page

 

Browsing to the URL presented:  “http://localhost:5000” will display the ASP .NET core default landing page.   This page is also quite useful as it contains links to various learning resources.

 

.NET core development can be done using any text editor, or in the cross platform lightweight editor Visual Studio Code, or if you prefer the full featured IDE Visual Studio.

 

Happy .NET core development!