Skip to content

Commit

Permalink
Missing set up WiFI network code
Browse files Browse the repository at this point in the history
include directly into program.cs
  • Loading branch information
AdrianSoundy committed Feb 25, 2019
1 parent 856db22 commit 57efdb2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
91 changes: 76 additions & 15 deletions TestMqtt/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using System;
using System.Threading;
using System.Text;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;

using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using NetHelper;

namespace TestMqtt
{
public class Program
{
private const string c_SSID = "myssid";
private const string c_AP_PASSWORD = "mypassword";

public static void Main()
{

Expand All @@ -18,7 +22,7 @@ public static void Main()
bool running = true;

// Wait for Wifi/network to connect (temp)
Net.SetupAndConnectNetwork();
SetupAndConnectNetwork();

// Loop forever
while (true)
Expand All @@ -28,23 +32,19 @@ public static void Main()
string BrokerAddress = "192.168.2.129";
client = new MqttClient(BrokerAddress);

//X509Certificate caCert = new X509Certificate(MyCaCert);
//X509Certificate clCert = new X509Certificate(MyClCert);
//client = new MqttClient(BrokerAddress, 8883, true, caCert, clCert, MqttSslProtocols.TLSv1_2);


// 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 = new Guid(1, 23, 44, 32, 45, 33, 22, 11, 1, 2, 3).ToString();

Log.WriteLine("Connecting MQTT");
Console.WriteLine("Connecting MQTT");

client.Connect(clientId);

Log.WriteLine("Connected MQTT");
Console.WriteLine("Connected MQTT");
// Subscribe topics
// client.Subscribe(new string[] { "Test1", "Test2" }, new byte[] { 2, 2 });

Expand All @@ -56,10 +56,10 @@ public static void Main()
"/Automation/Lights/#"
};

Log.WriteLine("Subscribe /Automation/Lights/#");
Console.WriteLine("Subscribe /Automation/Lights/#");
client.Subscribe(SubTopics, new byte[] { 2 });

Log.WriteLine("Enter wait loop");
Console.WriteLine("Enter wait loop");
while (running)
{
Thread.Sleep(10000);
Expand All @@ -71,7 +71,7 @@ public static void Main()
catch (Exception ex)
{
// Do whatever please you with the exception caught
Log.WriteLine("Main exception " + ex.Message);
Console.WriteLine("Main exception " + ex.Message);
}

// Wait before retry
Expand All @@ -81,19 +81,80 @@ public static void Main()

private static void Client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
{
Log.WriteLine("Client_MqttMsgSubscribed ");
Console.WriteLine("Client_MqttMsgSubscribed ");
}

private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
string topic = e.Topic;
byte[] bmes = e.Message;

string message = Encoding.UTF8.GetString(e.Message,0,e.Message.Length);

Console.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
Console.WriteLine("Network connection is: Wi-Fi");

Log.WriteLine("Publish Received Topic:" + topic + " Message:" + message);
Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD)
{
// have to update Wi-Fi configuration
wc.Ssid = c_SSID;
wc.Password = c_AP_PASSWORD;
wc.SaveConfiguration();
}
else
{ // Wi-Fi configuration matches
}
}
else
{
// network interface is Ethernet
Console.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()
{
Console.WriteLine("Waiting for IP...");

while (true)
{
NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0];
if (ni.IPv4Address != null && ni.IPv4Address.Length > 0)
{
if (ni.IPv4Address[0] != '0')
{
Console.WriteLine($"We have an IP: {ni.IPv4Address}");
break;
}
}

Thread.Sleep(500);
}
}


}
}
1 change: 0 additions & 1 deletion TestMqtt/TestMqtt.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="..\..\Net-tests\NetHelper\NetHelper.projitems" Label="Shared" />
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
Expand Down

0 comments on commit 57efdb2

Please sign in to comment.