6.4: MQTT Publisher
Created by ppatierno on 2/12/2014 2:55:10 AM

MQTT Publisher

Before starting to develop the software on publisher side, you need to configure network settings for your Netduino Plus board. This operation can be executed programmatically or using MFDeploy tool installed with .Net Micro Framework and located at C:\Program Files\Microsoft .NET Micro Framework\v4.3\Tools. After connecting Netduino Plus to your PC via USB cable, you can select “USB” and the board name will appear to the right.

Fig6.6

Figure 6.6 : Select Netduino Plus board connected to the PC

Click on “Ping” to check that the board communicates with the PC and then on “Device Capabilities” in the menu “Target” to receive all information about Netduino firmware onboard.

Fig6.7

Figure 6.7 : Information about TinyCLR and firmware on the Nedtuino Plus board

After this check you can click on “Network” under “Target” menu and then “Configuration” to see the current network settings.

Fig6.8

Figure 6.8 : Network configuration on Netduino Plus board

You can set a static IP address with all other information (Subnet Mask, Default Gateway, …) or enable DHCP to receive them from your DHCP server. Click on “Update” and the configuration will be saved on the device.

The MFDeploy tool has a lot of other features to show device info, memory map, erase and deploy new firmware and so on.

Now you can start to develop publisher firmware !

Create a blank solution (called MqttTemperatureAlarm for example) and add a new C# project for .Net Micro Framework targeting Netduino Plus board. In the same solution you have to add the M2Mqtt project from CodePlex (http://m2mqtt.codeplex.com); regarding the TMP102 temperature sensor you need a managed driver and you can find it in the uPLibrary project (https://uplibrary.codeplex.com/). If you want you can reference the entire uPLibrary but in this way you will deploy on Netduino Plus a lot of features that the library provides but you won’t use (ex. ultrasonic driver, humidity sensor driver, HttpClient and so on) with a waste of space on Flash.

Your board is located in the room1 of the building1 you are monitoring, so the topic to publish temperature values will be building1/room1/temp. The publisher gets the temperature value from the sensor every 10 seconds (using Temperature() method of TMP102 class) and then publishes it to the broker (using Publish() method of MqttClient class). Regarding MQTT client, all the default values are used like broker port 1883, QoS Level 1, no username and password, no “will” message and so on.

public class Program

{

// MQTT broker IP address

private const string MQTT_BROKER_ADDRESS = "192.168.1.6";

// MQTT topic to publish temperature value

private const string MQTT_TEMP_TOPIC = "building1/room1/temp";

 

// istance of TMP102 managed driver

private static TMP102 tmp102;

// istance of MQTT client

private static MqttClient mqttClient;

 

public static void Main()

{

     tmp102 = new TMP102(A0AddressSelect.GND, 100);

 

     // create MQTT client and connect to the broker

     mqttClient = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));

     mqttClient.Connect(Guid.NewGuid().ToString());

 

     float temp;

     while (true)

     {

        // get temperature value...

        temp = tmp102.Temperature();

        // ...publish it to the broker

        mqttClient.Publish(MQTT_TEMP_TOPIC,

                           Encoding.UTF8.GetBytes(temp.ToString()));

 

        Thread.Sleep(10000);

     }

}

}

 

print
  Comments


Turkish porno izle video site in rokettubeporno izle