WinRT

M2Mqtt and GnatMQ are dead ? Long life to them !

In the last months a lot of people asked me if the M2Mqtt and GnatMQ projects are dead because there were no commits for a long time.

I can say … no ! They are not absolutely dead !

I was very busy in the last months, starting my new exciting Red Hat career in the messaging and IoT team, but I want to continue to support the projects as lead and main committer. Of course, I’ll be still busy with my new job but my commitment is to continue to improve the libraries, fixing bugs and adding new features. Just remember that I’ll do it in my spare time so I could not be so quick to reply your suggestions; of course your help with useful pull requests will be great !

As you now, today the M2Mqtt client library is under the official Paho GitHub repo and I have finally deleted the repo on my personal GitHub account.

The GnatMQ, based on M2Mqtt, will continue to live where it’s now here.

I’ll do my best to check the issues and the pull requests provided by the community, happy to know that my projects have a lot of followers and there are a lot of professional solutions using them out there !

As always I invite you to share with me your experience using my libraries in order to mention your solution in the “case studies” section of the official web site.

Thanks !

Windows IoT Core and M2Mqtt … a simple marriage !

“Hello, Windows IoT Core” as Steve Texeira wrote few days ago !

The last stable release of the new IoT (embedded ?) OS for makers (and professionals ?) is out but of course I couldn’t wait the last days to start using it so I already played with it in a lot of demos and sessions code.

Who follows me knows that other than interacting with real world using sensors from Raspberry Pi 2, my great love is share these data and send them to the Cloud ! I already spoke and coded about using Pi 2 to send data to the awesome Event Hubs service (in the Microsoft Azure Service Bus family) and you can find my material on GitHub and SlideShare related to my “Telemetry with Windows 10 IoT : from Raspberry Pi2 to Event Hubs” session.

Of course, who follows me knows that I’m currently developing and updating (since about 2 years) the M2Mqtt library as part of the Eclipse Paho project (starting from October 2014) as the main MQTT client implementation for all .Net platforms (full, compact and micro) and WinRT (Windows 8.1 and Windows Phone 8.1) but … what about Windows 10 and Windows IoT Core ?

Don’t worry guys ! The last stable release (4.2.0.0) in its WinRT version works great on the new awesome Microsoft OS from the PC to the Raspberry Pi 2 thanks to the UWP (Universal Windows Platform) programming model. You can use the last Nuget package for sending telemetry data from your embedded system, your PC or smartphone writing only one application !

In the next few lines, I’ll describe how to use this package to send simple temperature value from my home. I’d like only to show how simple is to use the library and give you the right tools to create you IoT solution MQTT based for a Windows IoT Core system !

The hardware : Raspberry Pi2 and TMP102

Of course, it’s possible to develop an Internet of Things solution without an embedded system. Yes, of course ! I’m not crazy but I don’t want to discuss about this scenario (IoT is very huge you could think) during this article that will be focused on using an embedded hardware like the Raspberry Pi 2 with latest release of Windows IoT Core. Online, you can find a lot of article and official documentation on how to setup your board so I can consider this step already done for you !

Regarding the telemetry scenario … what is the easiest data to send … a temperature value of course ! 🙂

For this, I chose a temperature sensor I already had … the TMP102 from Texas Instruments in the related breakout board from Sparkfun. The connections to the Pi2 are so simple because the sensor supports I2C protocol and referring to the Pi 2 pinout we have (left Pi 2 pin and right TMP102 pin) :

  • PIN 1 (3.3V PWR) –> V+ (power)
  • PIN 3 (I2C1 SDA) –> SDA (I2C data)
  • PIN 5 (I2C1 SCL) –> SCL (I2C clock)
  • PIN 6 (GND) –> GND (ground)
  • PIN 9 (GND) –> ADD0 (for I2C address selection)

You can leave the ALT pin of TMP102 not connected to Pi 2 because it’s for alert notifications from the sensor if temperature is higher or lower than a specific threshold. If you chose to use it, you need to connect it to a GPIO pin on the Pi 2 to receive an interrupt (Gpio Value Changed event) on alert.

Create UWP app and add the M2Mqtt package

Using Visual Studio 2015, it’s so simple to create a new UWP application from “File –> New Project –> Windows –> Universal” as showed in the following picture.

01_pi2mqtt

To develop on Raspberry Pi 2 and using all IoT features like Gpio pins, I2C and SPI devices, you need to add the Windows IoT Extensions for the UWP under references and selecting “Select Universal –> Windows –> Extensions”.

02_pi2mqtt

Now you are able to interact with all your Pi 2 pins and sensors you can connect to it but what about the M2Mqtt library for sending data to the Cloud using MQTT protocol. As I said, the library is available as Nuget package without need to recompile it from source code (it’s open source ! you can do it if you want !). Adding the package to your project is so simple under “References -> Manage NuGet Packages”, search for “M2Mqtt” and click on “Install”.

03_pi2mqtt

At the end of the installation you can find the package under the “Solution Explorer” window.

04_pi2mqtt

Time to code !

Now it’s time to code our application, reading from the temperature sensor and publish data to an MQTT broker. The first step is to interact with TMP102 sensor to get the temperature value. To do this I already wrote the driver you can find on GitHub here and include in your project.

To be much simpler as possible I wrote my code in the MainPage constructor to instantiate the TMP102 class and the MqttClient class from M2Mqtt library and to launch a task that reads the temperature and send it to the broker every 5 seconds. Of course, you have to write better code (for example using a Backgroud IoT Task instead of an UWP app) … but this sample is useful to show you how to integrate M2Mqtt in your project and how it’s simple to use it.

public sealed partial class MainPage : Page
{
    private TMP102 tmp102;
    private MqttClient client;

    public MainPage()
    {
         this.InitializeComponent();

         this.tmp102 = new TMP102();
         this.client = new MqttClient("test.mosquitto.org");

         this.client.Connect(Guid.NewGuid().ToString());

         Task.Run(async () =>
         {
              bool isOpened = await this.tmp102.OpenAsync();

              while (true)
              {
                   float temperature = this.tmp102.Temperature();
                   string json = "{ temp : " + temperature + " }";

                   this.client.Publish("/pi2mqtt/temp", Encoding.UTF8.GetBytes(json));

                   await Task.Delay(5000);
              }
         });
    }
}

To simplify the solution I decided to use the publically available Mosquitto broker at this address : test.mosquitto.org

The above example is very simple. First you have to create TMP102 and MqttClient instances (for the last one you need to specify the broker host name). The second step is to connect to the MQTT broker and start the task for reading and sending data. Inside the task, every 5 seconds, the temperature value is read from the TMP102 instance and it’s published to the broker on the “/pi2mqtt/temp” topic in JSON format (as payload of the MQTT message).

Now that your project is sending value to the broker, the simplest way to see you are publishing data is to use another MQTT client, for example the mosquitto client and subscribe to the same topic to receive data. After installing mosquitto, you can start the mosquitto subscriber client in the following way :

mosquitto_sub -h test.mosquitto.org -t /pi2mqtt/temp -q 0 -d

Now … you should see data received from you Raspberry Pi 2 !

05_pi2mqtt

Conclusion

Of course, it isn’t an article to deep into Windows IoT Core, MQTT protocol, M2Mqtt library and so on. I’d like only to show how you have an “out of box” way (as the M2Mqtt library) to integrate your embedded system into a huge MQTT solution and you can develop it using Windows IoT Core as OS or in general your Windows 10 system !

MQTT & Eclipse Paho : nuove versioni per il client M2Mqtt ed il broker GnatMQ !!

Finalmente la nuova versione M2Mqtt 4.1 è stata rilasciata !

Negli ultimi mesi, la mia libreria è stata messa sotto pressione grazie ai miei amici Olivier Vigliengo (Adeneo) e Nicolas Besson (Microsoft MVP su Windows Embedded, Adeneo). Essi hanno usato il client MQTT per i loro progetti hobbistici, stressandolo non poco !

Dopo un fitto scambio di email e letture di log traces, ho risolto una serie di bug ed aggiunto degli miglioramenti :

  • L’evento di “published” fornisce la flag IsPublished (nell’event args) grazie alla quale poter capire se il messaggio è stato realmente pubblicato oppure no a causa del timeout (l’evento viene sollevato lo stesso);
  • Alcuni cambi “interni” per la gestione degli eventi;
  • Fixato un bug su timeout negativo;
  • Migliorata la stabilità della libreria in condizioni di rete poco affidabile;
  • Fixato un bug sulla gestione delle sessioni;
  • Aggiunte le informazioni di trace sul “queueing” dei messaggi;

Ovviamente, questi cambiamenti hanno avuto il loro impatto anche sul broker GnatMQ che è ancora in Beta ma che mi piacerebbe rilasciare al più presto : spero che le persone che lo usino mi diano dei feedback (sembra che il team di XSocket lo stia usando per fornire il supporto MQTT alla loro piattaforma) 🙂

Come sempre potete trovare M2Mqtt nel progetto Eclipse Paho, su CodePlex e su Nuget. Il broker GnatMQ è disponibile solo su CodePlex. Infine, ho aggiornato anche il progetto M2Mqtt4CE (per Windows Embedded Compact 2013) su CodePlex.

MQTT OASIS 3.1.1 : supporto ufficiale per M2Mqtt e GnatMQ

oasis_m2mqtt_gnat

Dopo circa un mesetto di lavoro durante il mio tempo libero, è giunto finalmente il momento di rilasciare la nuova versione della libreria M2Mqtt (4.0.0.0) e del corrispondente broker GnatMQ (0.9.3.0 Beta) con il supporto alla specifica MQTT OASIS 3.1.1 (oltre ad una serie di bug fix).

Questa è stata la causa della mia assenza sul blog !

I cambiamenti apportati complessivamente al progetto sono i seguenti :

  • Supporto per entrambe le specifiche, la vecchia 3.1.0 e la nuova OASIS 3.1.1;
  • L’evento MqttMsgDisconnect è esposto solo al broker GnatMQ per poter gestire il messaggio DISCONNECT ricevuto dal client e quindi non è più accessibile dalla classe MqttClient. E’ stato aggiunto il nuovo evento ConnectionClosed che viene sollevato quanto la connessione è chiusa (da uno dei peer). Ovviamente, si tratta di un “breaking change” e ne chiedo scusa …
  • Aggiunta la gestione delle sessioni (con le subscription ed i messaggi in coda con QoS 1 e 2);
  • Il keep alive può essere disabilitato se viene impostato il timeout a zero;
  • Aggiunta la gestione della flag di “session present” nel messaggio di risposta CONNACK con il quale il broker (GnatMQ) avvisa il client che è presente una sua vecchia sessione e che è stata ripristinata (subscription e messaggi in coda);
  • E’ ammesso un client identifier a lunghezza zero. In questo caso il broker (GnatMQ) assegna un client identifier casuale ma è ovvio che non è in grado di gestirne la sessione (il client non è a conoscenza dell’identificativo assegnatogli e non può trasmetterlo al broker in caso di riconnessione);
  • Rimosso il limite di 23 caratteri per il client identifier;
  • E’ possibile impostare la dimensione massima della “inflight queue” in modo che non possano essere inseriti altri messaggi se essa è piena (causa problemi di connessione che non permettono la trasmissione dei pacchetti);
  • Apportati una serie di miglioramenti sul thread di ricezione;
  • Una richiesta di subscription ad un topic può anche essere rifiutata dal broker (GnatMQ) specificando tale errore nel messaggio di SUBACK;

Come sempre ho provveduto ad aggiornare tutti i repositori nei quali potete trovare il mio progetto :

Questo può essere considerato il mio regalo di Natale !

🙂

M2Mqtt è ufficialmente parte del progetto Paho !

Chi conosce il protocollo MQTT, utilizzato nell’ambito dell’Internet Of Things, sa benissimo che il punto di riferimento per quest’ultimo è il progetto Paho della Eclipse Foundation.

01

Tale progetto fornisce una serie di implementazioni di client MQTT nei principali linguaggi quali C/C++ (sia in ambiente Windows o Linux che su sistemi embedded), Java, (per J2SE ed Android), Javascript, Python e Go.

In questo folto elenco, fino a qualche giorno fa mancava un’implementazione in C# che fosse utilizzabile sulle piattaforme .Net e WinRT. Ebbene, questa lacuna è stata colmata con l’ingresso del mio progetto M2Mqtt nell’ambito di Paho per il quale sono diventato ufficialmente un committer !

02

Non nascondo che questo è un grande onore per me e non potevo immaginare che poco dopo più di un anno dalla nascita del mio client, i membri principali della Eclipse Foundation e tutti coloro che lavorano sul protocollo MQTT mi potessero chiedere di fare parte di questo progetto. Tra di essi ci sono Benjamin Cabè (IoT Community Evangelist della Eclipse Foundation), Ian Craggs (Software Engineer alla IBM e project lead del progetto Paho), Andy Piper (Developer Advocat a Twitter e project lead del progetto Paho), Ian Skerrett (Vice Presidente Marketing alla Eclipse Foundation). Ad essi vanno poi aggiunti anche Nicholas O’Leary (Emerging Technology Specialist alla IBM, committer su Paho e padre di Node-RED), Roger Light (creatore del broker Mosquitto e committer su Paho) e tutti gli altri committer su Paho (Al Stockdill-Mander, Andy Gelme, Dave Locke) che hanno votato all’unanimità l’ingresso di M2Mqtt.

03

Queste sono le persone che hanno creduto che M2Mqtt fosse degna di far parte del progetto Paho insieme ovviamente a tutti coloro che usano la mia libreria in progetti professionali e non.

Come Microsoft MVP (su Windows Embedded ed IoT), sono molto felice del fatto che il linguaggio C# e le piattaforme .Net e WinRT fanno parte, grazie ad M2Mqtt, di un grande progetto open source come Paho. Questo significa che M2Mqtt può essere considerata l’implementazione MQTT di riferimento per le piattaforme Microsoft.

Ovviamente, tutti coloro che sino ad oggi hanno utilizzato CodePlex come repository di riferimento potranno continuare a farlo e lo stesso vale per Nuget. Esso sarà sempre allineato con gli aggiornamenti ed i miglioramenti apportati alla libreria nel progetto Paho.

M2Mqtt Spy : Windows Store e Windows Phone companion apps per “spiare” il traffico MQTT

Logo.scale-240

windows-logo-new-370x229

Dopo aver aggiunto il supporto per WinRT al progetto M2Mqtt nell’ultima release, ho deciso di realizzare una semplice “companion” app : M2Mqtt Spy.

01 02

Questa applicazione è disponibile come Windows Store e Windows Phone app e fornisce le seguenti funzionalità base :

  • connessione ad un broker MQTT locale o remoto (con eventuali username e password);
  • sottoscrizione/desottoscrizione ad uno o più topics (con QoS 0);
  • dashboard con i messaggi ricevuti in corrispondenza dei topics sottoscritti;
  • pubblicazione di un messaggio con (QoS 0) su un topic;
01 02 03_thumb.png

Ovviamente, questa è solo la versione iniziale, grazie alla quale poter aver un primo applicativo di testing MQTT sui proprio device Windows.

M2Mqtt e GnatMQ … adesso anche su Windows 8.1 e Windows Phone 8.1 !!

Windows_logo_-_2012 m2mqttpng
gnat

Sembra che io sia stato in vacanza (vista la mancanza di post sul mio blog) ed è la verità !

Nonostante la vacanza, ho proseguito lo sviluppo del progetto M2Mqtt ed oggi ho il piacere di rilasciare la nuova versione 3.6.0.0 con il supporto per WinRT !!

Ho dovuto intervenire soprattutto nei punti di gestione del networking (in WinRT non ci sono le Socket ma gli StreamSocket), dei thread (passaggio da Thread a Task) e della sicurezza ma finalmente il primo client MQTT per tutte le piattaforme .Net, da oggi funziona anche sui tablet dotati di Windows 8.1 e sugli smartphone con Windows Phone 8.1.

Il nuovo progetto, aggiunto in una solution per Visual Studio 2013, è una “portable class library” che ho utilizzato e testato in una Universal App.

La stessa sorte è toccata al “fratello” GnatMQ, giunto alla versione (Beta) 0.9.2.0, grazie al quale abbiamo a disposizione un broker MQTT da eseguire su tablet e smartphone della famiglia Windows 8.1.

Ovviamente ho provveduto ad aggiornare anche il package Nuget aggiungendo gli assemblies per WinRT.

Spero di aver fatto cosa gradita per moltissimi utilizzatori di M2Mqtt che in passato mi hanno richiesto fortemente questo porting !

Sorriso