Sunday, July 17, 2011

Netduino - Getting somewhere...

Since my last post I haven't had much time to do anything with my project, but yesterday I had a fantastically productive day. I decided to bypass the 6-7 volt requirement of the 5v regulator on the serial backpack for my LCD and to power the 5v pin directly from my netduino. This meant I could instantly start playing around with my LCD. I found I really useful class on the netduino forums: http://forums.netduino.com/index.php?/topic/687-driver-class-for-sparkfun-lcd-graphic-backpack/ and quickly managed to get temperatures on the screen.

Currently my project is reading temperatures, determining if it is with a range, then if it is in range a green led is on and when it goes out of range a red led comes on. Also the temperature is then printed to the display, see my code so far below:


using System;
using System.Threading;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using CW.NETMF.Hardware;
using SecretLabs.NETMF.IO;


namespace SerialGraphicLcd
{

    public class Program
    {

        static SGLcd disp = new SGLcd(SerialPorts.COM2);

        public static void Main()
        {
            int maxT = 28;
            int minT = 27;
            float oldTemp = 0;
            OutputPort tled = new OutputPort(Pins.ONBOARD_LED, false);
            OutputPort rled = new OutputPort(Pins.GPIO_PIN_D7, false);
            OutputPort gled = new OutputPort(Pins.GPIO_PIN_D6, false);
            var oneWire = new OneWire(Pins.GPIO_PIN_D0); // Adjust the pin if necessary
            disp.BackLightDutyCycle(1);
            if (oneWire.Reset())
            {
                while (true)
                {
                    // DS18B20 Thermometer
                    oneWire.WriteByte(OneWire.SkipRom); // Address all devices
                    oneWire.WriteByte(DS18B20.ConvertT);
                    Thread.Sleep(750);                  // Wait Tconv (for default 12-bit resolution)

                    oneWire.Reset();
                    oneWire.WriteByte(OneWire.SkipRom);
                    oneWire.WriteByte(DS18B20.ReadScratchpad);

                    // Read just the temperature (2 bytes)
                    var tempLo = oneWire.ReadByte();
                    var tempHi = oneWire.ReadByte();
                    var temp = DS18B20.GetTemperature(tempLo, tempHi); // ((short)((tempHi << 8) | tempLo))/16F
                   
                    Debug.Print("New temp: " + temp.ToString() + " \u00b0" + "C    -    Old temp: " + oldTemp.ToString() + " \u00b0" + "C");

                    if (temp > (int)maxT | temp < (int)minT)
                    {
                        rled.Write(true);
                        gled.Write(false);
                    }
                    else
                    {
                        rled.Write(false);
                        gled.Write(true);
                    }
                    if (oldTemp != (float)temp)
                    {
                        disp.ClearDisplay();
                        disp.GotoXY(2, 63);
                        disp.Write(temp.ToString() + " oC");  //\u00b0
                    }
                    oneWire.Reset();
                    Thread.Sleep(5000);
                    oldTemp = (float)temp;
                }
            }
        }
    }

}


My next aim is to get the data logged on to the Micro SD, in a format that can be used by the netduino and could also be used to plug in to a computer. The end idea for the SD is to give me more space for my code and also to store data, for example if the internet went down or the server that I will log my data on in the future, the netduino would have a backup on the SD. As each entry will be time stamped it can find the last entry and then reconcile the missing times. This data will then be graphically display in graphs etc to allow me to monitor fluctuations in parameters of the tank over time.

Update to my shopping list....

I have just order a real time clock board which has a DS1307 rtc IC on it, and 6 relays from Hong Kong. The relays are solid state and operate on a 3-32 volt switching current and can take up to 15amps @ 480v. I am going to build a board, perhaps using some diodes to isolate the relays completely and some transistors to switch them, with LEDs to let me know which relays are on. The great thing with the SSRs is I can do PWM on lights, essentially allowing me to dim any light, although I am eventually going to use LED lighting in all my tanks. The primary reason for the SSRs was just because they require a low voltage so I can power them directly from the netduino and they isolate the high current inside them. I will need to look at using a shift register to operate the relays as I don't want to use to many pins from the netduino. I have already had a little play with shift registers to control LEDs but only managed to daisy chain 2, the third seemed to operate inversely to other 2.

No comments:

Post a Comment