Skip to content

Instantly share code, notes, and snippets.

@adrenalinehit
Last active May 2, 2019 01:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adrenalinehit/a4e2684a0b3b0a49b48e to your computer and use it in GitHub Desktop.
Save adrenalinehit/a4e2684a0b3b0a49b48e to your computer and use it in GitHub Desktop.
MQTT Publisher sample console application to demonstrate connecting to AWS IoT
using System;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using uPLibrary.Networking.M2Mqtt;
namespace MQTT.Sample
{
public class Program
{
/// <summary>
/// AWS IoT endpoint - replace with your own
/// </summary>
private const string IotEndpoint = "*******.iot.eu-west-1.amazonaws.com";
/// <summary>
/// TLS1.2 port used by AWS IoT
/// </summary>
private const int BrokerPort = 8883;
/// <summary>
/// this must match - partially - what the subscribed is subscribed too
/// nicksthings = the THING i created in AWS IoT
/// t1/t555 is just an arbitary topic that i'm publishing to. (It needs 2 parts for the rule I'm using to work)
/// </summary>
private const string Topic = "YOURTHING/t1/t555";
public static void Main(string[] args)
{
var publisher = new Program();
publisher.Publish();
}
/// <summary>
/// Configure client and publish a message
/// </summary>
public void Publish()
{
//convert to pfx using openssl - see confluence
//you'll need to add these two files to the project and copy them to the output (not included in source control deliberately!)
var clientCert = new X509Certificate2("YOURPFXFILE.pfx", "YOURPFXFILEPASSWORD");
var caCert = X509Certificate.CreateFromSignedFile("root.pem");
// create the client
var client = new MqttClient(IotEndpoint, BrokerPort, true, caCert, clientCert, MqttSslProtocols.TLSv1_2);
//message to publish - could be anything
var message = "Insert your message here";
//client naming has to be unique if there was more than one publisher
client.Connect("clientid1");
//publish to the topic
client.Publish(Topic, Encoding.UTF8.GetBytes(message));
//this was in for debug purposes but it's useful to see something in the console
if (client.IsConnected)
{
Console.WriteLine("SUCCESS!");
}
//wait so that we can see the outcome
Console.ReadLine();
}
}
}
@keithejc
Copy link

keithejc commented Dec 1, 2016

shouldn't

var caCert = X509Certificate.CreateFromSignedFile("root.pem");

be

var caCert = X509Certificate.CreateFromCertFile("root.pem");

@Salma175
Copy link

Will this Work with unity 2017.2

@sivshan
Copy link

sivshan commented Jan 13, 2018

Will this work with asp.net core?

@pkbui
Copy link

pkbui commented Jan 17, 2018

I keep receiving MqttCommunicationException when trying to connect, which got thrown from SendReceive(Byte[] msgBytes, Int32 timeout). Anyone have any idea what could be the problem? Thank you in advance/

@jens-andersson-2-wcar
Copy link

jens-andersson-2-wcar commented Mar 8, 2018

I can't get it to work either. First of all, CreateFromSignedFile says that Amazon's root CA cert is not signed, so like keithejc I need to use CreateFromCertFile in my code (basically the same as the snippet above; I can't find anything significant that differs). However, like many others online I get "The authentication or decryption has failed" when I try to run it in Unity 2017.3.1f1 with the .NET 4.6 Experimental runtime (which I have to use for M2Mqtt.Net.dll to work at all).

Googling for "uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException: Exception connecting to the broker ---> System.IO.IOException: The authentication or decryption has failed." gives a bunch of hits where others seem to be stuck on exactly the same use case: using M2Mqtt in Unity to connect to AWS IoT.

I've converted the client cert to pfx, and even before converting it I have sanity checked it with mosquitto command line, which works fine for the same certificate and AWS endpoint, so there is nothing wrong with the certificate as such (or the AWS policies for it).

Any ideas?

Edit: tried it as a standalone program, outside Unity. Then the above works (with the CreateFromCertFile modification) also for me with my certs and endpoint. So I guess it is some sort of Unity 2017.3.1f1 problem. Still, if anybody has any suggestions then I would very much appreciate it!

Edit again: also tried it with Unity 2018.1 beta -- still failing. After finding https://issuetracker.unity3d.com/issues/unity-does-not-allow-to-use-tls-version-higher-than-1-dot-0 , I am however now pretty certain it is a Unity issue and that it won't work until they support TLS1.2.

@mvisoindev
Copy link

I've got error "A call to SSPI failed" , have any idea?

I've testing on Xamarin OSX

@TejaswiniiB
Copy link

What I observed is : we need to publish with QOS level 1 to receive puback.
client.Publish(topic, Encoding.UTF8.GetBytes(msg), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE,false);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment