Adding CoAP to our devices

We are now ready to implement CoAP in our projects. To do this, we create two new projects, which we will call SensorCoap and ActuatorCoap, copying the corresponding code from the Sensor and Actuator projects. If we wish to retain HTTP functionality, we can copy the code from the SensorHttp and ActuatorHttp projects instead. Our devices can support multiple protocols. We use the same hardware as we used in previous chapters. We add the Waher.Networking.CoAP.UWP NuGet package to both projects.

For .NET standard, .NET Core, or traditional .NET Framework projects, you can use the Waher.Networking.CoAP NuGet instead. Universal Windows Platform apps use different libraries and runtime binaries when it comes to accessing network adaptors. For this reason, it requires a somewhat modified version of the original library.

We must also make sure to initialize the runtime inventory of classes with a reference to the CoAP library assembly, in the beginning of the application, to make sure content encoding and decoding includes the content formats defined in it:

Types.Initialize( 
   typeof(FilesProvider).GetTypeInfo().Assembly, 
   typeof(RuntimeSettings).GetTypeInfo().Assembly, 
   typeof(IContentEncoder).GetTypeInfo().Assembly, 
   typeof(ICoapContentFormat).GetTypeInfo().Assembly, 
   typeof(IDtlsCredentials).GetTypeInfo().Assembly, 
   typeof(App).GetTypeInfo().Assembly); 
We must not forget to add the internetClientServer capability to our set of capabilities in the Package.appxmanifest files as well:
<Capability Name="internetClientServer" /> 
We must also make sure to create a Device ID, as we did in the HTTP chapter.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset