Blog

All Blog Posts  |  Next Post  |  Previous Post

Raspberry Pi to Delphi messaging with MQTT

Bookmarks: 

Tuesday, February 28, 2017

As promised in the blog about building a chat application with MQTT, here is a follow-up article on using the TMS MQTT client, this time to setup messaging between a Raspberry Pi and a Windows PC. The TMS MQTT client fully supports Lazarus and thus also targetting Linux and its variant Raspbian, so we can use the TMS MQTT Client from Raspberry Pi. For this short demo, we setup Lazarus on Raspberry Pi and installed the TMS MQTT client component in the IDE as well as our free Raspberry Pi hardware lib.

We'll use the component TMSLCLAdaADC12b to be able to use the 12bit ADC connected via i2c to the Raspberry Pi. The goal of the setup is to read-out noise meter values via the ADC and send the measurements via messaging to the channel 'tms/raspi'. A Windows Delphi application will then listen through this same MQTT client to the messages on channel 'tms/raspi' and will display the values in our TMS Instrumentation Workshop component TvrScope. Best of all, with the help of these components, this is not much more than a 15 minute project where most of the time will be spent to properly connect the ADC breakout board via a breadboard to the Raspberry Pi i2c port.



For the code, we use the same setup in the Windows Delphi application as on the Raspberry Pi Lazarus application to connect our client to the Mosquitto test broker:

begin
  TMSMQTTClient1.BrokerHostName := 'test.mosquitto.org';
  TMSMQTTClient1.Connect;
end;

On the Raspberry Pi Lazarus app, we add a timer that will get the ADC value every 200msec and send the value as a message to 'tms/raspi'. For reason of simplicity, we'll send the value as a text message via MQTT. The code to do this is added to the timer OnTimer() event handler:

var
  i: integer;
begin
  // get value from the 4 channel ADC channel 0 to which the analog noise meter output is connected
  i := TMSLCLAdaADC12B1.ReadChannel(0);
  // send the value as text over MQTT
  TMSMQTTClient1.Publish('tms/raspi', inttostr(i)); 
end;

To get the i2c communication working on the Raspberry Pi, we open the i2c connection to the ADC in the form's OnCreate event:

procedure TForm1.FormCreate(Sender: TObject);
begin
  // make sure to start the Raspberry Pi app with sufficient permissions to be able to open access to i2c
  if not TMSLCLAdaADC12B1.Open then
  begin  
    ShowMessage('error opening i2c');
    Exit;
  end;
end;
and we close the port again from the form's OnClose event:

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  TMSLCLAdaADC12B1.Close;
end;

For the Delphi Windows client, here we'll add an event handler for TMSMQTTClient.OnPublishReceived() that is triggered when a message is received. From this event, we store the received value in a variable containing the last received value LastVal. Here the (positive) value of the ADC is mapped onto the 0 to 100 range:

procedure TForm1.TMSMQTTClient1PublishReceived(ASender: TObject;
  APacketID: Word; ATopic: string; APayload: TArray<System.Byte>);
var
  s:string;
begin
  s := TEncoding.UTF8.GetString(APayload);
  LastVal := Round(100 * strtoint(s) / 1024);
end;

To visualize the data, the TMS Instrumentation Workshop TVrScope component is used where we added one channel and automatic display (VrScope.Active = true). This means that at a configured frequency (VrScope.Frequency), this scope component requests the channel value and displays it in the scope. This is done via the TvrScope.OnNeedData() event that is triggered every time the scope advances and needs a new value. Here, the last received value LastVal is returned:
procedure TForm1.VrScope1NeedData(Sender: TObject; Channel: Integer;
  var Value: Integer);
begin
  Value := LastVal;
end;

As a result, here you can see some quickly captured data in our test setup:


In summary, this small demo shows how really quick & easy you can get started using m2m messaging using MQTT with the TMS MQTT Client and this on a wide range of devices, from desktop PC to mobile device, to Linux machine and to Raspberry Pi SBC. We wish you much fun with your projects and we love to hear from all the exciting stuff you create with these building blocks!

Bruno Fierens


Bookmarks: 

This blog post has received 2 comments.


1. Tuesday, August 6, 2019 at 4:36:27 PM

Olá. gostaria de saber se o acessoa ao Firebase ou ao ao CloudMqtt com o TMS MQTT funciona no Delphi Community

Fiz uns testes e não conecta, não sei de onde tirar essas informações
FAppkey = ????;
FAppSecret = ???;
FAppDatabase = ???;

teria como referenciar cada um desses tópicos para o nome do console do firebase? Pois esses 3 parametros que vocês usam, não tem os nome iguais, é muito confuso.

Gilson


2. Tuesday, August 6, 2019 at 4:57:13 PM

Information where to get the application key & secret is here: https://www.tmssoftware.com/site/cloudkey.asp#GoogleFirebase

The database is the name of the database you did setup in Firebase

Bruno Fierens




Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post