Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Robert Hugo's Content

There have been 15 items by Robert Hugo (Search limited from 26-May 19)


By content type

See this member's

Sort by                Order  

#63552 Introducing Netduino 3 Wi-Fi

Posted by Robert Hugo on 16 July 2015 - 03:19 AM in Netduino 3

Is there any form of sharable timeframe for the UWP API availability on Netduino3?




#62859 Micro.Udp.Cast

Posted by Robert Hugo on 25 May 2015 - 02:50 AM in Netduino 3

Sample snippet for a Windows (8.1) Universal app (inject into a XAML .cs file):

		Universal.Udp.Cast cast;

		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			cast = new Universal.Udp.Cast(new Universal.Udp.EndPoint(new Windows.Networking.HostName("224.5.6.7"), "4567"));

			cast.DataReceived += Cast_DataReceived;

			cast.SendDataAsync(System.Text.Encoding.UTF8.GetBytes("Hello World!"));
		}

		protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
		{
			cast.DataReceived -= Cast_DataReceived;

			cast.Dispose();
		}

		async void Cast_DataReceived(object sender, Universal.Udp.Cast.DataReceivedEventArgs e)
		{
			string text = new string(System.Text.Encoding.UTF8.GetChars(e.Bytes));

			await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
			{
				string s = "<" + e.Remote.ToString() + "> " + '"' + text + '"';

				textBlockOutput.Text += s + "\r\n";
			});
		}

		// E.g. for event Click of send Button.
		private void EventSend(object sender, RoutedEventArgs e)
		{
			byte[] bytes = System.Text.Encoding.UTF8.GetBytes("App: " + textBoxInput.Text);

			textBoxInput.Text = string.Empty;

			cast.SendDataAsync(bytes);

			textBoxInput.Focus(Windows.UI.Xaml.FocusState.Programmatic);
		}



#62858 Micro.Udp.Cast

Posted by Robert Hugo on 25 May 2015 - 02:43 AM in Netduino 3

Here is a N3 sample snippet:

namespace NetduinoApplication1
{
	public class Program
	{
		static Micro.Udp.Cast _udpCast = null;

		public static void Main()
		{
			while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any) ;

			_udpCast = new Micro.Udp.Cast(new System.Net.IPEndPoint(System.Net.IPAddress.Parse("224.5.6.7"), 4567), UdpCast_DataReceived);	
		}

		static void UdpCast_DataReceived(object sender, Micro.Udp.Cast.DataReceivedEventArgs e)
		{
			byte[] bytes = e.Bytes;

			string text = new String(System.Text.Encoding.UTF8.GetChars(bytes));

			Debug.Print(text);
		}
	}
}



#62857 Micro.Udp.Cast

Posted by Robert Hugo on 25 May 2015 - 02:37 AM in Netduino 3

This is the Windows (8.1) Store apps equivalent class file (Universal.Udp.cs): 

using System;

namespace Universal.Udp
{
	public class EndPoint
	{
		public Windows.Networking.HostName HostName { get; private set; }
		public string ServiceName { get; private set; }

		public EndPoint(Windows.Networking.HostName hostName, string serviceName)
		{
			HostName = hostName;
			ServiceName = serviceName;
		}

		public override string ToString()
		{
			return HostName.ToString() + ':' + ServiceName;
		}
	}

	public class Cast
		: Flux.ManagedDisposable
	{
		#region "Event Handling"
		public class DataReceivedEventArgs : System.EventArgs
		{
			public byte[] Bytes { get; private set; }
			public EndPoint Remote { get; private set; }

			public DataReceivedEventArgs(byte[] bytes, EndPoint remote)
			{
				Bytes = bytes;
				Remote = remote;
			}
		}

		public event System.EventHandler<DataReceivedEventArgs> DataReceived;

		protected virtual void RaiseDataReceivedEvent(DataReceivedEventArgs e)
		{
			System.EventHandler<DataReceivedEventArgs> handler = DataReceived;

			if (handler != null)
				handler(this, e);
		}
		#endregion

		public EndPoint Remote { get; private set; }
		public Windows.Networking.Sockets.DatagramSocket Socket { get; private set; }

		public Cast(EndPoint remote)
		{
			Remote = remote;

			Socket = new Windows.Networking.Sockets.DatagramSocket();
			Socket.Control.DontFragment = true;
			Socket.Control.InboundBufferSizeInBytes = 1024;
			Socket.MessageReceived += Socket_MessageReceived;
			Socket.BindServiceNameAsync(Remote.ServiceName.ToString()).GetResults();
			if (System.Text.RegularExpressions.Regex.IsMatch(Remote.HostName.ToString(), @"2(?:2[4-9]|3\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?|0)){3}"))
				Socket.JoinMulticastGroup(Remote.HostName);
		}
		~Cast()
		{
			Dispose();
		}

		private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
		{
			using (Windows.Storage.Streams.DataReader dataReader = args.GetDataReader())
			{
				byte[] bytes = new byte[dataReader.UnconsumedBufferLength];

				dataReader.ReadBytes(bytes);

				RaiseDataReceivedEvent(new DataReceivedEventArgs(bytes, new EndPoint(args.RemoteAddress, args.RemotePort)));
			}
		}

		async public void SendDataAsync(byte[] bytes)
		{
			using (Windows.Storage.Streams.IOutputStream outputStream = await Socket.GetOutputStreamAsync(Remote.HostName, Remote.ServiceName.ToString()))
			{
				using (Windows.Storage.Streams.DataWriter dataWriter = new Windows.Storage.Streams.DataWriter(outputStream))
				{
					dataWriter.WriteBytes(bytes);

					await dataWriter.StoreAsync();
				}
			}
		}

		protected override void DisposeManagedResources()
		{
			if (Socket != null)
			{
				Socket.Dispose();
				Socket = null;
			}
		}
	}
}

The Flux.Disposable.cs file below is also needed:

namespace Flux
{
	/// <summary>Base Implementation of System.IDisposable. Only needed if lots of managed resources needs cleaning up right away (as opposed to wait for the GC).</summary>
	public class ManagedDisposable
		: System.IDisposable
	{
		protected bool IsDisposed { get; set; } // Disposed has already occured?

		public virtual void Dispose()
		{
			if (!IsDisposed)
			{
				DisposeManagedResources();

				IsDisposed = true;
			}
		}

		/// <summary>Override for disposal of managed resources.</summary>
		protected virtual void DisposeManagedResources() { }
	}

	/// <summary>Extended implementation of (System.IDisposable) ManagedDisposable. Only needed if directly using unmanaged resources (add the call "Dispose(false)" inside the finalizer) , and possibly if lots of managed resources needs cleaning up right away.</summary>
	public class UnmanagedDisposable :
		ManagedDisposable
	{
		public override void Dispose()
		{
			Dispose(true);

			System.GC.SuppressFinalize(this); // Inform GC that our resources has already been cleaned up, no need to call finalize later.
		}

		/// <summary>Overload implementation of dispose; if isDisposing = false the method has been called by the runtime from inside the finalizer and only unmanaged resources should be disposed, else if isDisposing = true the method has been called directly or indirectly by a user's code and both managed and unmanaged resources should be disposed.</summary>
		public void Dispose(bool isDisposing)
		{
			if (!IsDisposed)
			{
				if (isDisposing)
					base.Dispose();

				DisposeUnmanagedResources();

				if (!isDisposing)
					IsDisposed = true;
			}
		}

		/// <summary>Override for disposal of unmanaged resources.</summary>
		protected virtual void DisposeUnmanagedResources() { }
	}
}




#62856 Micro.Udp.Cast

Posted by Robert Hugo on 25 May 2015 - 02:29 AM in Netduino 3

Here is a Micro.NET class file (Micro.Udp.Cast.cs) that uses Multicast functionality. I seem to be having problems with the receiving buffer. (From tablet TO N3:) If I send 5 characters initially, I get 0 received, if I then send 15, I get 5, and if I then send 25, I get 15, and so on. It's like the buffer size is (re)set from the previous received size.

 

Anyways, this works on the N3 and N2+.

namespace Micro.Udp
{
	public class Cast
	{
		#region "Event Handling"
		public class DataReceivedEventArgs
		{
			public byte[] Bytes { get; private set; }
			public System.Net.EndPoint Remote { get; private set; }

			public DataReceivedEventArgs(byte[] bytes, System.Net.EndPoint remote)
			{
				Bytes = bytes;
				Remote = remote;
			}
		}

		private DataReceivedEventHandler DataReceivedExternal;

		private System.Threading.Thread DataReceivedThread;

		public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);

		protected virtual void RaiseDataReceivedEvent(DataReceivedEventArgs e)
		{
			DataReceivedEventHandler eventHandler = DataReceivedExternal;

			if (eventHandler != null)
				eventHandler(this, e);
		}
		#endregion

		public System.Net.IPEndPoint Remote { get; private set; }

		public System.Net.Sockets.Socket Socket { get; private set; }

		public Cast(System.Net.IPEndPoint remote, DataReceivedEventHandler dataReceivedEventHandler)
		{
			Remote = remote;

			Socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
			Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.DontFragment, true);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.IpTimeToLive, 127);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReuseAddress, true);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveBuffer, 1024);
			//Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendBuffer, 1024);
			Socket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, Remote.Port));
			if (new System.Text.RegularExpressions.Regex(@"2(?:2[4-9]|3\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d?|0)){3}").IsMatch(Remote.Address.ToString()))
			{
				//byte[] multicastOpt = new byte[] { 224, 5, 6, 7, 0, 0, 0, 0 };
				byte[] multicastOpt = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
				System.Array.Copy(Remote.Address.GetAddressBytes(), multicastOpt, 0);
				System.Array.Copy(Remote.Address.GetAddressBytes(), multicastOpt, 4);
				Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.MulticastTimeToLive, 127);
				Socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.AddMembership, multicastOpt);
			}

			DataReceivedExternal = dataReceivedEventHandler;

			DataReceivedThread = new System.Threading.Thread(DataReceivedInternal);
			DataReceivedThread.Start();
		}

		private void DataReceivedInternal()
		{
			System.Net.EndPoint remoteEP = (System.Net.EndPoint)new System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), Remote.Port);

			while (true)
			{
				if (Socket.Available > 0)
				{
					byte[] buffer = new byte[Socket.Available];

					Socket.ReceiveFrom(buffer, ref remoteEP);

					RaiseDataReceivedEvent(new DataReceivedEventArgs(buffer, remoteEP));
				}
			}
		}

		public void SendBytes(byte[] bytes)
		{
			Socket.SendTo(bytes, Remote);
		}
		public void SendString(string text)
		{
			SendBytes(System.Text.Encoding.UTF8.GetBytes("Netduino: " + text));
		}
	}
}



#62752 Best practices: how to wait for a Wi-Fi network connection

Posted by Robert Hugo on 20 May 2015 - 04:05 AM in Netduino 3

For option 4 above, what is the reason to wait for both address changed and availability? Meaning, is it enough to wait only for availability, which is what *I* really care about, per se (the system may disagree, which is why I am asking)?

 

Thanks,

Rob




#62735 Recommended HTTP API for the future?

Posted by Robert Hugo on 19 May 2015 - 04:31 AM in Netduino 3

What is the official API to use for HTTP communication "from now on"? I now in the past certain libraries have been the focus, but with Web API in ASP.NET and things being pushed to various likings, I'd like to know what is recommended for the future of IoT and Netduino (3 and 2+, in my case).

 

I hope my question make sense.

 

Thank you,

Rob

 




#62716 Netduino 3 Wi-Fi Firmware v4.3.2 (update 2)

Posted by Robert Hugo on 16 May 2015 - 03:01 AM in Netduino 3

I am liking the update frequency.

 

Unfortunately the N3 is still "on the way". :)

 

Are you guys working hard enough!? :D (That IS a joke!)




#62635 Gobus with Netduino 3

Posted by Robert Hugo on 13 May 2015 - 12:10 AM in Netduino 3

With such a generic title, I felt bad about creating a new topic about the Gobus. I am pretty sure that I've asked before, but I am hoping for an update. :) Will it be possible to attach more than one Shield Base, e.g. one on each of the three ports, on the N3 (and the NGo, for that matter). I haven't followed any updates for a while (sorry to say) so if it's been covered, I am sorry to iterate the question.




#62626 HTTPS Error

Posted by Robert Hugo on 12 May 2015 - 12:16 PM in Netduino 3

Just FYI, Nevyn posted code (thank you for sharing) that you can find it under the title "Temperature Logging" in the "Netduino 3" forum.




#62601 Best practices: how to wait for a Wi-Fi network connection

Posted by Robert Hugo on 11 May 2015 - 03:22 AM in Netduino 3

I totally concur, Frode. Much appreciated, Chris.




#62597 Introducing Netduino 3 Wi-Fi

Posted by Robert Hugo on 11 May 2015 - 02:03 AM in Netduino 3

Absolutely fabulous. I will try as soon as the N3WiFi arrives. I have to say that adding WiFi to a Netduino was a very nice move. It's not hard to use cables, on the N2+, but it limits the location/mobility quite a bit. I do a lot of programming while watching a show on TV with the family, but it's frustrating because I do not have a port nearby the TV, so I normally program a bit and then go try it, cumbersome. Now, location won't be an issue, and that is better than great.

 

Thanks for providing cool stuff!!!




#62536 Introducing Netduino 3 Wi-Fi

Posted by Robert Hugo on 09 May 2015 - 05:14 AM in Netduino 3

+1 to UDP Multicast support! (And brilliant, on the Universal side, thanks for the info!!)




#62534 Introducing Netduino 3 Wi-Fi

Posted by Robert Hugo on 09 May 2015 - 05:04 AM in Netduino 3

How "Universal" (in respect to Windows 10/Windows IoT) are these applications?




#59424 Database file?

Posted by Robert Hugo on 29 July 2014 - 12:11 AM in Netduino Plus 2 (and Netduino Plus 1)

You could use XML, which in a sense can be treated or thought of as a "database", as it can also contain hierarchical structure. There are complexities with XML, but it is an option, albeit perhaps a bit contrived.

 

If it is just a matter of single transactions, however, a text file (or several) seems more direct.

 

The file system itself could probably (being unfamiliar with pitfalls using SD cards on the Netduino) be used, treating files in a folder as the transactional records themselves. You could keep all necessary information inside the file itself, even history of attempts, etc. This way files could be removed as they are successfully submitted to the actual database. 

 





home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.