user3076458
user3076458

Reputation: 11

Using C# and Netduino to log data

I am developing a telemetry platform for a nano-printing project. My team is using a Netduino 2 plus (not my first choice, but what are you going to do?) I am not at all familiar with C# and am a novice programmer, to be certain.

We have some code written that successfully polls an I2C temperature sensor and uses debug.print to write to the console. I would like this data written to a file, instead.

There are examples out there to transfer files from the SD card to a PC, but this seems unnecessary to me (though it may be completely necessary in order to not overrun the buffer?). Is there a call that will simply write the data to a file instead of writing to the console?

It is my understanding that we may need an application to listen to the serial port on the PC. It seems we would also need a corresponding application to write from the hardware. I've used microcontrollers in the past that simply open a serial and send over USB to a file location.

Here is the code we are using to print the data to the console:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;

namespace NetduinoApplication1
{
    public class Program
    {
        public static void Main()
        {
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            OutputPort p = new OutputPort(Pins.GPIO_PIN_SDA, true); 
            p.Write(false); 
            p.Dispose();
            // write your code here
            byte[] Addr = new byte[1];
            Addr[0] = 0x07;
            byte[] TxBuff = new byte[9];
            byte[] RxBuff = new byte[9];
            I2CDevice.Configuration I2C_Configuration = new I2CDevice.Configuration(0x5A, 100);
            I2CDevice I2C1 = new I2CDevice(I2C_Configuration);
            I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateWriteTransaction(TxBuff) };
            I2CDevice.I2CTransaction[] ReadTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateReadTransaction(RxBuff) };
            while (true)
            { 
                int iWriteCount = I2C1.Execute(WriteTran, 1000);
                //Debug.Print("Write Count: " + iWriteCount.ToString());
                led.Write(true);
                Thread.Sleep(200);
                int iReadCount = I2C1.Execute(ReadTran, 1000);
                if (iReadCount >= 2)
                {
                    int iVal = RxBuff[1] * 256 + RxBuff[0];
                    double Temperature = ((iVal * 0.02) - 273.15) * 9.0 / 5.0 + 32.0;
                    Debug.Print("Temperature: " + Temperature.ToString() + " deg F");
                }
                led.Write(false);
                Thread.Sleep(200);
            }

        }

    }
}

If I need to create an application, I'd like to do it, but I could really use some direction. A little experience here would go a long way.

Upvotes: 1

Views: 459

Answers (1)

Christian Loris
Christian Loris

Reputation: 4294

So, a couple things come to mind.

  1. If your Netduino has network connectivity, you could write data to a file on the SD card and then transfer the file out via FTP. There are plenty of libraries to do this with.
  2. Same as 1, except push the data to a web service. There is an HTTPRequest library you could use to do up a quick JSON post to a web service. You would need to write the web service. But there are also plenty of free services out there that will take data feeds and store them like ubidots.
    1. If you really want to go the serial port route, the bad news is that the Netduino's USB Serial port is reserved for debugging. But you can get a cheap little USB UART adapter to do this. There is a great blog post here that explains it.

Good Luck!

Upvotes: 1

Related Questions