Skip to content

Instantly share code, notes, and snippets.

@jamesbulpin
Created March 15, 2019 17:36
Show Gist options
  • Save jamesbulpin/9d27cab82dbfacb293b12b4518d509be to your computer and use it in GitHub Desktop.
Save jamesbulpin/9d27cab82dbfacb293b12b4518d509be to your computer and use it in GitHub Desktop.
Quick test of a MQTT client to control PowerPoint slides
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Client;
using PPt = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;
namespace MqttToPPt
{
class Program
{
static void Main(string[] args)
{
MainAsync(args);
Console.ReadLine();
}
static async void MainAsync(string[] args)
{
PPt.Application pptApplication = Marshal.GetActiveObject("PowerPoint.Application") as PPt.Application;
PPt.Presentation presentation = pptApplication.ActivePresentation;
// Create a new MQTT client.
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();
// Create TCP based options using the builder.
var options = new MqttClientOptionsBuilder()
.WithClientId(/* Random GUID */)
.WithTcpServer("io.adafruit.com")
.WithCredentials(/* Your Adafruit IO username */, /* Your Adafruit IO key */)
.WithTls()
.WithCleanSession()
.Build();
mqttClient.ApplicationMessageReceived += (s, e) =>
{
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
Console.WriteLine();
switch (Encoding.UTF8.GetString(e.ApplicationMessage.Payload))
{
case "next":
Console.WriteLine("next slide");
pptApplication.SlideShowWindows[1].View.Next();
break;
case "prev":
Console.WriteLine("prev slide");
pptApplication.SlideShowWindows[1].View.Previous();
break;
}
};
mqttClient.Connected += async (s, e) =>
{
Console.WriteLine("### CONNECTED WITH SERVER ###");
// Subscribe to a topic
await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("jamesbulpin/feeds/testfeed").Build());
Console.WriteLine("### SUBSCRIBED ###");
};
Console.WriteLine("Connecting to server...");
await mqttClient.ConnectAsync(options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment