Been playing around with UDP broadcasting as a way of developing a low overhead sensor network. The basic idea is that the sensors are not transferring any "secure" data, just simple stuff like temperature etc. The sensors would broadcast the data over WiFi and so the data should be reasonably secure as the sensor need to be logged on to the WiFi network in the first place.
Speed is proving to be an issue though. I'm getting random responses from UDP broadcasts, sometime data is received in 2 seconds, sometime 15 and other any mixture of responses in between.
C# code is:
using Microsoft.SPOT; using System.Net; using System.Net.Sockets; using System.Threading; namespace NETMFUDPServer { public class Program { public static void Main() { // // Wait until we have an IP address. // while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any) { Thread.Sleep(10); } Debug.Print("IP Address: " + IPAddress.GetDefaultLocalAddress()); EndPoint endPoint = new IPEndPoint(IPAddress.Any, 11000); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Bind(endPoint); while (true) { if (socket.Poll(1000, SelectMode.SelectRead)) { byte[] buffer = new byte[socket.Available]; int bytesRead = socket.ReceiveFrom(buffer, ref endPoint); int length = buffer.Length; char[] text = new char[length]; for (int i = 0; i < length; i++) { text[i] = (char) buffer[i]; } Debug.Print("Message received from " + endPoint.ToString() + ": " + new string(text)); } } } } }
I know the ESP code is triggering every second as I have a console App on the PC which confirms that data is being transmitted every second.
Anyone with more experience with UDP who could help with optimisation or advise? I know NETMF is a little slower than full .NET but the period between packets being picked up can be the best part of 15 seconds.
Regards,
Mark