-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
153 lines (129 loc) · 5.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using nanoFramework.M2Mqtt;
using nanoFramework.M2Mqtt.Messages;
using System;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
namespace IoTSharp.nanoDevice
{
public class Program
{
private const string _token = "be2b6208db0742d99be85c2d74715cba";
private const string BrokerAddress = "139.9.232.10";
public static void Main()
{
MqttClient client = null;
string clientId;
// Wait for Wifi/network to connect (temp)
SetupAndConnectNetwork();
long dida = 0;
// Loop forever
while (true)
{
try
{
client = new MqttClient(BrokerAddress);
// register a callback-function (we have to implement, see below) which is called by the library when a message was received
client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
client.MqttMsgSubscribed += Client_MqttMsgSubscribed;
// use a unique id as client id, each time we start the application
//clientId = Guid.NewGuid().ToString();
clientId = Guid.NewGuid ().ToString();
Debug.WriteLine("Connecting MQTT");
client.Connect(clientId, _token, "") ;
Debug.WriteLine("Connected MQTT");
// Subscribe topics
// client.Subscribe(new string[] { "Test1", "Test2" }, new byte[] { 2, 2 });
var buffer = nanoFramework.Hardware.Stm32.Utilities.UniqueDeviceId;
byte[] message = Encoding.UTF8.GetBytes("{\"DeviceId\":\""+ Encoding.UTF8.GetString(buffer,0, buffer.Length) +"\"}");
client.Publish("devices/"+ clientId + "/attributes", message, MqttQoSLevel.ExactlyOnce, false);
string[] SubTopics = new string[]
{
"/devices/"+clientId+"/rpc/request/+/+",
"/devices/"+clientId+"/attributes/update/"
};
Debug.WriteLine("Subscribe attributes and request");
client.Subscribe(SubTopics, new MqttQoSLevel[] { MqttQoSLevel.ExactlyOnce, MqttQoSLevel.ExactlyOnce });
Debug.WriteLine("Enter wait loop");
while (client.IsConnected)
{
try
{
Thread.Sleep(10000);
dida++;
message = Encoding.UTF8.GetBytes("{\"NowDateTime\":\"" + DateTime.UtcNow.ToString() + "\",\"Dida\":" + dida.ToString() + "}");
var result= client.Publish("devices/" + clientId + "/telemetry", message, MqttQoSLevel.ExactlyOnce, false);
Debug.WriteLine(result.ToString());
}
catch (Exception ex)
{
Debug.WriteLine("Publish exception " + ex.Message);
}
}
client.Disconnect();
}
catch (Exception ex)
{
// Do whatever please you with the exception caught
Debug.WriteLine("Main exception " + ex.Message);
}
// Wait before retry
Thread.Sleep(10000);
}
}
private static void Client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
{
Debug.WriteLine("Client_MqttMsgSubscribed ");
}
private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
string topic = e.Topic;
string message = Encoding.UTF8.GetString(e.Message, 0, e.Message.Length);
Debug.WriteLine("Publish Received Topic:" + topic + " Message:" + message);
}
public static void SetupAndConnectNetwork()
{
NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
if (nis.Length > 0)
{
// get the first interface
NetworkInterface ni = nis[0];
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
// network interface is Wi-Fi
Debug.WriteLine("Network connection is: Wi-Fi");
}
else
{
// network interface is Ethernet
Debug.WriteLine("Network connection is: Ethernet");
ni.EnableDhcp();
}
// wait for DHCP to complete
WaitIP();
}
else
{
throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
}
}
static void WaitIP()
{
Debug.WriteLine("Waiting for IP...");
while (true)
{
NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0];
if (ni.IPv4Address != null && ni.IPv4Address.Length > 0)
{
if (ni.IPv4Address[0] != '0')
{
Debug.WriteLine($"We have an IP: {ni.IPv4Address}");
break;
}
}
Thread.Sleep(500);
}
}
}
}