Platform.IO, Visual Studio Code and Arduino – Bye Bye Arduino IDE

Was looking around at some code examples and I stumbled on to someones comments around Platform.IO as a replacement to the Arduino IDE.  I really don’t like the Arduino IDE so was keen to have a look at it.  I tried Visual Micro (allows Arduino development in Visual Studio) once, but found it a bit buggy and slow.  It may be fine these days?

Any way, i though’t i’d put in the time and learn Platform.IO, and I am very glad i did!.  It took a little bit of messing around but it was worth it.  Never going back to Arduino IDE now I am setup 🙂

Here’s the process I went through to get going on Windows 10:

Installation

Download Visual Studio Code – Its only 43mb – i was thinking Visual Studio, It will take 2 hours to install, but Visual Studio Code is really lightweight.

Run Visual Studio as Admin (right click, run as Admin – needed to do the next bit).

I changed the color scheme to light as I am old school 🙂   – File > Preferences > Color Scheme

Install Platform IO extension in to Visual Studio code – follow these instructions here (this is why running as Admin as it failed for me the first time without running as Admin)

 

All going well, you should be ready to go and when you open Visual Studio Code, you should see the PlatformIO home screen, and a little Home icon on the bottom left of the bottom status bar.

 

Create a Project

Click on + New Project

Give it a name

Select a board – you can search here.. this is what i really liked, heaps of support for modern boards.  I initially tried the WEMOS LOLIN board – ESP32 with an OLED Screen on it.  Have also tried with just basic NodeMCU 1.0 boards too.

For this example, using a Wemos D1 Mini.

Set Arduino as the platform.

Press Finish

Once you press Finish, if this is your first project, it can take some time to open and install all the libraries and toolchains etc.

I thought mine had locked up, but after maybe 7-8 mins, it kicked into life.  Now every time i start a new project it’s fast.

Had the same thing when i started a new ESP32 project – it has to go and get all the bits and bobs to make it go so it’s slow the first time.

Moral – let it do its thing.

 

Getting your head around it

Here’s my notes from my initial – getting my head around it and how it works like Arduino IDE.

First off, the main ‘sketch’ is under the src > main.cpp file

We can get something really basic happening here just writing to the terminal.  Use this code:

Now before we upload our basic sketch, configure the board port and speed.

Open the platformio.ini file from the left hand nav at the bottom

If you need to find what port your board is connected on, check under Device Manager in Windows:

Start > type Device Manager

Expand out Ports (COM & LPT)

Once you have set your correct port in the INI file, you can build and upload.

The bottom nav bar is where you find the build, upload and show terminal buttons, similar to Arduino IDE.

The tick = build, the right arrow = upload, and the plug icon = show terminal window.

Press the tick button to build.

You should now have a compiled version of our simple sketch.  If you have any error, you will see in the Terminal window what’s wrong.  Hopefully you should have no errors with basic sketch above.

Now upload (presuming plugged in) your sketch the the board using the right arrow on bottom Nav.

 

Once you are uploaded, you can then switch on the terminal monitor by pressing the plug icon in the bottom nav:

Here you can see our simple app displaying ‘Hello Universe’ in a loop every second:

Yay, that’s the basics of Platform IO… now for the next bit.. handling libraries.

 

Library Management

This is the bit i like about Platform IO, took my head a bit to get.

First off, there’s a library manager system built in like the later versions of Arduino IDE.

Let’s say we want to add Blynk support.

Switch the PIO Home tab, and click on the Libraries button on the left hand side.

Here you can search for a library.

Click in to the Library to read more, get examples, and Install.

Once the library is installed, it will be put in to your file system here:

This is the folder where libraries are stored.

You should be able to then just add a reference to your library you added:

I found though when you include manual libraries, sometimes they get a green squiggle under them.

In this case, you have to go add a reference to the libraries path.

When you see a green squiggle under the #include line, go check the c_cpp_properties.json file in your project.

Make sure the path to the library you want to use is in this file in the top section.

NOTE: the folder path is separated by forward slashes instead of back slashes.. you need to follow this format.

In this example below, i can see the Blynk library was automatically added when we added the library through the library manager search/install process.

 

If i wanted to download a manual library from the internet and use it in my sketch, i would follow these steps:

For example, lets say i want to use this WifiManager library that has been modified to support ESP32 and isn’t necessarily available in the Library Manager.

I’d download the ZIP file from here: https://github.com/zhouhan0126/WIFIMANAGER-ESP32

I’d right click and unblock the ZIP, then i’d extract it to here:

C:\Users\paul.obrien\.platformio\lib\WIFIMANAGER-ESP32-master

I’d go and insert a reference line in the bottom of the includePath section of my c_cpp_properties.json file (changing the \’s for /’s) to the path of the library.

Now i’d add my #include as normal to my sketch and build.

 

Hope that helps someone get started quickly.

The intellisense, problems tab (so you can immediately see issues), and general workflow is so much nicer than the Arudino IDE.

Plastic Micky – My IoT Robot to entertain the toddler

My 2 1/2 year old is robot mad.  I played him an old youtube clip of Metal Micky from the early 1980’s that was on TV when I was a kid and he has been an addict ever since.  There’s some more youtube vids of a guy in the USA retrofitting OnmniBot (again from the 80’s) with modern tech as well and he watches it over and over..

So I thought, why not build him his own one.  He’s fully into the parts and ‘how does it work’ buzz and fizzes when the AliExpress parcels turn up from China with servo’s and bits and pieces.

I wanted the thing to be a similar size to him, be able to move around, wave its arms, move its head left and right and control various LED lights and strips.  A project we can build up to do different things and tinker around with on Sunday afternoons.

Key things to sus:

  • Body – what to use? (plastic rubbish bins :))
  • Head – what to use – how to make move left and right (servo, bracket etc.)
  • Movement – Tank Tracks on the bottom for movement – Ultrasonic sensor so it can avoid stuff – like Roomba 🙂
  • Lights /Buttons to play with
  • Camera/Screen – got an old android tablet with forward facing camera to stick on the front.
  • Brain – ESP8266 Wifi Micro Controller connected to motors/servo’s/relays to control everything
  • Control – C++ Code on the ESP8266 (Arduino style) and something like Blynk for mobile phone control
  • Semi resemble Metal Micky – we call him ‘Plastic Micky’.

 

So this post is going to be the process of building it up, physically, sourcing all the bits and the electronics (as simply and cheaply as possible) to bring Micky to life..

I aim to build up the electronics using simple cheap readily available parts.. and maybe when the design is ‘stable’ – make in to a single PCB – maybe others can contribute to the design?? I’m going ‘open source’ on the electronics design and C++ code.

Here’s a link to GitHub project for the source code.. https://github.com/paulobriennz/plasticmicky

 

I’m just a beginner really with C++ and micro controllers, but I’ll have a crack.. if any one wants to make the code better – please feel free 🙂  I was even thinking, this could be a project to build in school’s for kids to learn electronics.. there’s ton’s of ‘instructable’ type articles around on the net about how to use an Arduino to make some wheels move, or lights flash, or a servo do things but not really any one stop guide that combines all the components to build a walking talking robot with mobile control and a bit of intelligence to combine movement with ‘personality’.

Also going to build an Alexa skill.. so Micky can do things by voice command as well as mobile phone control.. I’ll post the code to this as well.

 

High level, I’ve come up with an electronics design based trying a few different bits and pieces – :

  • 12v 7A Alarm Battery for Power Source (need to source a battery charging module)
  • NodeMCU/Wemos Development board – ESP8266 based – easy to ‘swap out’ and try different firmware’s etc
  • L298N Motor Controller (2A motor rating) to drive the tracks for movement (initially tried a ESP12E Motor Controller board (600ma motor rating) but not enough guts to handle robot’s weight – chip kept overheating).
  • PCA9685 16 Channel I2c Servo Control Board to move heads, arms, ears, mouth etc..
  • PCF8574 8 Channel I2c GPIO Extender – as we’re going to need a few pins to make this all happen – 6 alone for the L298N Motor Controller
  • Adjustable Buck Regulators to get 5V for Relays and electronics, LCD etc., 6-7v for Solenoids – 12v for Motors, LED Strips etc
  • OLED I2c Screen to help ‘communicate’ what’s going on.
  • Tank Tracks for movement – brought a kit with small ESP12E Motor Shield and NodeMCU – but the chip on the motor driver board is pretty gutless and just overheated trying to drive any weight on the tracks – hence the L298N controller above.  Motors connected to 12v battery directly and it was grunty as so the 12v motors and track mechanism have def got enough guts to move the thing.
  • Pan and Tilt Servo bracket for head – just using a Servo to spin the head left and right (although this area of the design needs more (mechanical) work..

 

Plan is to be able to control via Blynk (or some IoT control app) to move about and make head move, control lights etc. and have an autonomous mode (like a Roomba) that can move around and use its ultrasonic sensor (radar) to avoid objects and change course.  Also expose a ‘web service’ so we can create an Alexa skill to get it to do things.  Simple Wi-Fi Setup so you don’t have to mess round with C++ code to get it to connect – plug and play!

 

Procurement

First mission – what is the body and head going to be… after watching Metal Micky on YouTube for the 407th time with toddler, I thought – rubbish bins like you see in cafeteria’s.

I went to the Warehouse (general everything store we have in NZ – like Home Depot) and got 2 rubbish bins.

1 is a mini ‘wheelie’ bin and the other was a round bin with a push flap

 

https://www.thewarehouse.co.nz/p/living-co-wheelie-bin-black-60l/R2120555.html#start=1

https://www.thewarehouse.co.nz/p/taurus-rad-bin-50l-assorted/R638533.html#start=1

I’ll use the top off the round one for Micky’s head, and turn the wheelie bin upside down, remove the lid and casters and make a wooden base for the wheelie bin to sit on upside down.

This base can have the tracks screwed to it so its relatively ‘stable’.

 

NodeMCU Pin Map (and what ones you can actually use for stuff)

I found this picture on the internet, and it’s gold.. thank you to whoever made it – I always struggle with which pins you can actually use for stuff, and which pins will stop the thing from booting or flashing if you try and use or do weird stuff (like SD2)

For translation – Pins D10 and D9 are RX/TX for the onboard USB Serial port – so if you are using the USB Serial, you can’t use these pins

D1/D2 – normally I2c (GPIO 4,5) – tested this as well with a Servo Controller Board and I2C Scan tool

So D3 and D4 can only be used for Digital Write’s

D10 and D9 are RX/TX on the USB Serial – so if you want to debug in console, you can’t really use these easily

D1 and D2 work nice for I2c but also work for Digital Read or Write

SD3 works as GPIO10

SD2 is evidently no go – I did an I2c test on GPIO 9 (SD2) and 10 (SD3) and the I2c Scan found the device connected, but trying to use just made the NodeMCU freak and reboot

D5,D6,D7,D8 are all fully usable read and write

D0 – messes with booting up/flashing so I generally stay clear of it.

A0 is Analog Pin

Dynamics 365 – Connecting your app using MFA

MFA is becoming a common thing, as joyous as it is to use 🙂

I have a C# app that connects to Dynamics CRM/365 and I had to update it to support Microsoft Azure MFA.

I couldn’t really find any definitive guide out there, i had to cobble all different things together to get a working solution.

I hope this guide helps out some other poor sucker like me.

 

1. I had to update my application to use the modern CRM Tooling method of connection.

I added the following in Nuget to my solution – the key being the CrmTooling which supports the new connection string method of connecting.

In my code, i changed the way i obtained an IOrganizationService to the below (simplified):

 

This gets me a connection using the new Tooling DLL and a CRM Connection string.

 

Next step, you need to create an application in Azure AD.  I followed this guide.

The trick is the Redirect URI – i wasn’t working with a web app – I ended up using http://localhost

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/walkthrough-register-dynamics-365-app-azure-active-directory

 

Finally, constructing a connection string that would work with the new registered Azure App.

AuthType=OAuth;Url=https://yourcrm.crm.dynamics.com;AppId=yournewappid;RedirectUri=http://localhost;

 

Now when you go to connect, the Microsoft Sign In assistant pops up and handles the authentication to the CRM Instance.

And, if you have MFA turned on, you are also prompted with MFA.

 

Happy Days!

 

Dynamics CRM 365 – On Prem – Invalid Trace Directory

Looks like another piece of CRM team awesomeness.

The Tracing directory should be:

C:\Program Files\Microsoft Dynamics CRM\Trace

 

But some update somewhere changes it to:

c:\crmdrop\logs

 

That’s not very helpful.

I initially tried to change the trace directory back to the right place using CRM PowerShell, but that failed with authentication errors (that i have also posted on here – http://paulobrien.co.nz/2018/03/07/get-crmsetting-powershell-the-caller-was-not-authenticated-by-the-service-the-request-for-security-token-could-not-be-satisfied-because-authentication-failed/).

 

This is the guide i tried using the powershell method – makes sense, if powershell crm wasn’t broken as well.

How to fix ‘Invalid Trace Directory’ errors

 

So ended up changing in the CRM Database and registry:

 

 

And in the MSCRM_CONFIG database:

 

 

Get-CrmSetting powershell – The caller was not authenticated by the service / The request for security token could not be satisfied because authentication failed.

Had a need to run CRM powershell on an On-premise CRM Server.

I’ve had this same issue before where any powershell command you run against the Microsoft.Crm.PowerShell provider fails with authentication errors.

In this example, i was trying to run this and it barfed on Get-CrmSetting ….

 

Turned out to fix it, i needed to add a registry key.  After i did this it worked straight away (no need to reboot or reopen the PS window).

 

Method 2 in this article:

https://support.microsoft.com/en-us/help/896861/you-receive-error-401-1-when-you-browse-a-web-site-that-uses-integrate

ESP32 programming with Arduino on Windows

Today, I had an ESP32 development board arrive.  I have been eagerly awaiting this new ESP32 chip as from what I can understand, it has a lot more I/O pins (which you run out of pretty quickly on a ESP8266 dev board like a Wemos) with some more dedicated rs232 interfaces, and the big thing I have been waiting for – 2 cores!  This means in theory you can design your IoT code so one core can handle a web server and communications requests while the other core does time consuming work like blasting RF433 signals and the like – well, so I believe anyway..

So I ordered this chip – https://www.aliexpress.com/item/New-Wireless-module-NodeMcu-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266-with-pcb-Antenna/32266249427.html?spm=2114.13010608.0.0.jnMoB6

Was around $20 delivered to me here in NZ and took just over a month to arrive.  Package turned up all OK.

So I started looking around to see how to setup my Arduino IDE to work with it, but i found a couple of guides that didn’t work so well for me and I had to play around to get it all to work.

Here are the steps I followed.

Make sure i am running Arduino IDE 1.6.12 – maybe if you have an older version, you want to upgrade.

 

Install Python – https://www.python.org/downloads/release/python-2712/ – I installed the 64 bit Windows one.
When the installer runs, all the options are selected except the bottom – Add to Windows Path option – tick this option too as it makes some of the next steps easier having Python in your system path

 

Install a tool called pyserial – https://pypi.python.org/pypi/pyserial
Download the pyserial-3.3.tar.gz file.  I used WinRAR to unzip it in to a folder off my Downloads folder.

 

Open a CMD prompt as Administrator.  Change directory (and drive) to the folder you extracted pyserial-3.3 to.

 

 

Type python setup.py install

You’ll see a whole load of guff on the screen as things get unpacked and installed.  Check through there is no errors.
Keep the CMD window open as we need it again later

.

 

Find your Arduino IDE folder – mine was at C:\Program Files (x86)\Arduino.
Go to the hardware folder, then create a new folder called espresif, then go in to that folder and create another new folder called esp32.

 

Download the Arduino-ESP32 code from https://github.com/espressif/arduino-esp32/archive/master.zip

Extract the contents of this download in to the folder you created above.  Should look like below

 

Go back to the CMD window, change directory to the folder you created above off your Arduino IDE folder.
Once your in the directory, type GET and press Enter

 

Install is complete! 

 

Testing time.

 

Open Arduino IDE.

I went to File > Examples and you should now see an ESP32 section.  I opened Wifi > Wifi Scan

 

Go to Tools and then the Boards sub menu.  Select the ESP32 Dev Module.  I checked the port was right too by plugging and unplugging the ESP32 in to my USB board and going into the Port > sub menu to see it was in there, then assigned – in my case COM6

 

I learnt two things with uploading the image to the board.  One, my trusty USB cable I have used on Wemos and other chips and things just wouldn’t work with the chip.  Once you plugged in the ESP32 board you just heard every 20 seconds or so the USB plug in/unplug sound on the PC.  I ended out swapping for a shorter more ‘robust’ looking cable and plugged in and then had no issues.  Second thing is – Hold down the BOOT button on the ESP32 dev board when uploading from Arduino IDE or the upload fails.  I know there is probably a pin to pull low (I think its pin 9 but I haven’t got to that yet) that will put the chip in to ‘flash’ mode.

 

With the BOOT button held down, press the -> Arrow in Arduino IDE to push the sketch to the chip.

This is a successful upload for me below:

 

With the sketch uploaded to the ESP32, I was away!  Open up the Serial Window and my simple WiFi Scan sketch was running and could see WLANs around me.

 

Now on to figure out what this chips can do.. I know the clever awesome people working on the Arduino stuff for the ESP32 are adding stuff all the time with more and more of the stuff we’ve come to expect on Arduino and ESP8266 chips is ported over.  I see OTA Updates and mDNS have recently been added.

I’m very keen to see a webserver running on one core/task/thread while something else can be consuming the loop () { } and holding it up if it needs to without affecting the performance of the webserver thread…  I’ve tried different ESP8266 web servers, async and interrupt tricks and queuing etc., but if your chip has to go off and do a long time operation, the web server doesn’t respond like if it was only being a web server.  When you’re working with some other IoT Gateway or bridge that is not very forgiving in its timeouts, this can make or break a design.
Or even better yet, the core threaded so you can just call tasks and let the chip assign them to the cores… this will allow for far more complex solutions without having to connect multiple micro controllers together and then talk on a bus like i2c between the chips.

The other big plus I see with these ESP32 chips from what I read is that all the pins are assignable in software, so you can make any logical pin physically route to another pin and it has more than one hardware serial TX/RX so you get hardware performance serial ports in case the ‘thing’ you are talking to is not so easily done with software serial without messing around with the standard development serial connection on RX/TX that is generally wired to the USB RS232 chip on the little dev board.

Anyway, now on to building something worthwhile with this chip and see what I can get it to do.

 

Azure DNS via PowerShell

In my working world, I recently have discovered the relatively new feature in Azure – DNS.

From my early playing round it seems great! Very fast to update and easy to control with scripting.

You will need the Azure PowerShell provider.  Very simple to get.

You basically open up PowerShell and type:

And then you wait 5 mins while it gets and installs a whole bunch of stuff..

 

These two links are handy to give you insight in to how to connect and running different commands:

https://docs.microsoft.com/en-us/powershell/azureps-cmdlets-docs/

https://docs.microsoft.com/en-us/azure/dns/dns-operations-dnszones

 

 

Below is a script i wrote that that adds an A record.  It does a few checks and creates the dependent bits if they don’t exist – basically you need a resource group to put the DNS zone in, then you need a DNS zone and then you can create a record.

Couple of things to note from the script – you will want to set your location – my closest is “australiasoutheast” – you will want to find your preferred location to create your resource groups.

Second thing, I found this out by accident but very handy.. you can sign in to AzureRM and then ‘export’ your sign in details to a JSON file where the password etc is encrypted.. then in your script you can just use the Select-AzureRmProfile command and point it at the file, and it takes care of establishing a session with AzureRM.

Export your login details:

 

Here’s the script – you can see it uses the saved credentials above:

Google Places API in C# .NET

I recently had a requirement to look in to using the Google Places API in some code i was writing.

I found this library here that made the whole thing a lot easier.

https://github.com/azizmb9494/Google-Places 

 

I ended up writing a quick C# application to test the different searches by lon/lat and by text.

Download Places –  a quick and dirty C# application that implements the API.. you’ll need to get a Google API Key to run it.

https://developers.google.com/places/web-service/get-api-key

 

Paste your key in to the app and then you can search by lon/lat, text or a combination of both

The basic guts of the code is below.  Having never had anything to do with Google API’s before, it was pretty simple to get some programmatic searching going on…

Example method using the API above: