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.
Photo

N+(2) Aquarium Controller

N+2 aquarium webserver scheduler plugin json html POST

  • Please log in to reply
75 replies to this topic

#21 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 01 November 2012 - 02:37 PM

I managed to get it working on my Netduino!

Few things...

Referencing SecretLabs.NETMF.Hardware.AnalogInput will not work. Although it will let you compile - it can't find the assembly on the device.

Updating the analog input reference to .net mf 4.2's method will fix the issue (AnalogInput ain = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);). However, AnalogInput() now returns a double instead of a float.

I went and changed all references from floats to doubles to fix this issue (it seems most of the time the float was being cast to a double anyway).


The example config will not work. You'll need to prefix the JSON with var config= and put ; at the end.
You'll then need to update JSON.cs - configString = configString.Substring(12, configString.Length - 13); Completely forgot about this!!!!



The thingspeak/aquariumstatus plugins threw errors for me - but I just disabled them for the moment.



H07R0D - your code is exactly what I'm looking for and I plan on using it to write an output plugin to post data to my website.
Thanks!

Thanks for that JS catch!
If you're using a straight Netduino, and not an N+, the Thingspeak and status plugins will definitely not work as they are using IP stuff to function. Thingspeak writes straight out to their API through IP address, and status does timestamp updates through NST services. If you don't have an ethernet port, these definitely won't work.

#22 zemuss

zemuss

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts

Posted 01 November 2012 - 07:43 PM

I am only thinking our loud here. But the netDuino can produce a txt file correct? So if it can dump the file to a location: ftp, SD card, Drive space. I can create a windows service that moves the file and uploads the data at a set interval. I don't want my netduino to host a server, I know it can't perform that task and SQL isn't that small. LOL.. What I want to do is Netduino records data to file then file is uploaded then file is processed to SQL server. Thoughts?

#23 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 01 November 2012 - 10:22 PM

I am only thinking our loud here. But the netDuino can produce a txt file correct? So if it can dump the file to a location: ftp, SD card, Drive space. I can create a windows service that moves the file and uploads the data at a set interval. I don't want my netduino to host a server, I know it can't perform that task and SQL isn't that small. LOL..

What I want to do is Netduino records data to file then file is uploaded then file is processed to SQL server. Thoughts?

That makes much more sense :)

#24 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 03 November 2012 - 07:51 AM

I seem to be getting random uncaught exceptions. It happens during the polltasks method but not every time, and happens randomly after executing the temperature plugin An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.dll Microsoft.SPOT.Hardware.dll!Microsoft.SPOT.Hardware.Port.ReservePin(Microsoft.Spot.Hardware.Cpu.Pin pin, bool fReserve) [Managed to Native Transition] Microsoft.Spot.Hardware.dll!Microsoft.Spot.Hardware.AnalogInput.Dispose(bool fDisposing) Microsoft.Spot.Hardware.dll!Microsoft.Spot.Hardware.AnalogInput.Finalize()

#25 zemuss

zemuss

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts

Posted 06 November 2012 - 03:56 AM

I just received my netduino go, so let me get over the learning curve in the upcoming weeks and will be able to lend a hand. Currently in school and work full time as a .net programmer so im pretty booked.

#26 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 07 November 2012 - 12:54 PM

So I've hacked up H07R0D's code a fair bit.. - Downgraded to 4.1.1 (OneWire) - Stripped it right down (removed unfinished web ui code and uneeded plugins) - Wrote a plugin for DS18B20 temp sensor - Wrote a plugin to post data to my own MVC Web API (will make this public) Working perfect now! Next is an 8 channel relay control plugin

#27 zemuss

zemuss

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts

Posted 07 November 2012 - 05:54 PM

Cool.. I would like to see your code if you can post it. Maybe start another thread and link to it?

#28 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 10 November 2012 - 10:30 AM

TemperatureDS18B20.cs
namespace Plugins
{
    using System.Threading;

    using CW.NETMF.Hardware;

    using Controller;
    using System.Collections;
    using System;

    using SecretLabs.NETMF.Hardware.NetduinoPlus;

    public class TempData : IPluginData
    {
        private double value;

        public double GetValue() { return this.value; }

        public void SetValue(double _value) { this.value = _value; }

        public string DataUnits() { return "C"; }

        public DataFields DataType() { return DataFields.Temperature; }
    }

    public class TemperatureDS : InputPlugin
    {
        private readonly TimeSpan timerInterval;

        private static OneWire oneWire;

        public TemperatureDS(object _config)
        {
            var config = (Hashtable)_config;

            var interval = config["interval"] as ArrayList;

            oneWire = new OneWire(Pins.GPIO_PIN_D0);

            var times = new int[3];

            for (var i = 0; i < 3; i++)
            {
                times[i] = (int)(double)interval[i];
            }

            this.timerInterval = new TimeSpan(times[0], times[1], times[2]);
        }

        ~TemperatureDS() { Dispose(); }

        public override void Dispose() { }

        public override TimeSpan TimerInterval { get { return this.timerInterval; } }

        public IPluginData GetData() { return null; }

        public override void EventHandler(object sender, IPluginData data)
        {
            throw new NotImplementedException();
        }

        public override void TimerCallback(object state)
        {
            var tempData = new TempData();

            tempData.SetValue(CalculateTemperature());

            var ida = (InputDataAvailable)state;

            ida(tempData);
        }

        private static double CalculateTemperature()
        {
            if (oneWire.Reset())
            {
                oneWire.WriteByte(OneWire.SkipRom);
                oneWire.WriteByte(DS18B20.ConvertT);

                Thread.Sleep(750);

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

                var tempLo = oneWire.ReadByte();
                var tempHi = oneWire.ReadByte();

                var temp = DS18B20.GetTemperature(tempLo, tempHi);

                return temp;
            }

            return 0;
        }
    }
}


#29 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 10 November 2012 - 10:37 AM

And my very simple web service logger

Output here: http://my.aquariumwise.net/1

AquariumWise.cs
namespace Plugins
{
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;

    using Controller;

    using Microsoft.SPOT.Net.NetworkInformation;

    public class AquariumWise : OutputPlugin
    {
        private readonly string httpPost;
        private const string MyAquariumWiseUrl = "my.aquariumwise.net";

        ~AquariumWise() { this.Dispose(); }

        public override void Dispose() { }

        public AquariumWise(object _config)
        {			
            this.httpPost = "POST /api/temperature/log HTTP/1.1\nHost: my.aquariumwise.net\nConnection: close\n";
            this.httpPost += "Content-Type: application/json\nContent-Length: ";	
        }

        public override void EventHandler(object sender, IPluginData data)
        {
            var allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            var macAddress = "";

            const string HexChars = "0123456789ABCDEF";

            for (var b = 0; b < 6; b++)
            {
                macAddress += HexChars[allNetworkInterfaces[0].PhysicalAddress[b] >> 4];
                macAddress += HexChars[allNetworkInterfaces[0].PhysicalAddress[b] & 0x0F];

                if (b < 5)
                    macAddress += "-";
            }
            
            var fieldData = "{\"Value\":" + data.GetValue() + ",\"DeviceMac\":\"" + macAddress + "\"}";

            var postString = this.httpPost + fieldData.Length + "\n\n" + fieldData;

            try
            {
                var hostEntry = Dns.GetHostEntry(MyAquariumWiseUrl);

                using (var myAquariumWiseSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    myAquariumWiseSocket.Connect(new IPEndPoint(hostEntry.AddressList[0], 80));

                    var sendBytes = Encoding.UTF8.GetBytes(postString);

                    myAquariumWiseSocket.Send(sendBytes, sendBytes.Length, 0);

                    var buffer = new Byte[256];
                    var page = String.Empty;

                    while (myAquariumWiseSocket.Poll(20 * 1000000, SelectMode.SelectRead))
                    {
                        if (myAquariumWiseSocket.Available == 0)
                            break;

                        Array.Clear(buffer, 0, buffer.Length);

                        var bytesRead = myAquariumWiseSocket.Receive(buffer);

                        page = page + new String(Encoding.UTF8.GetChars(buffer));
                    }

                    myAquariumWiseSocket.Close();
                }
            }
            catch
            {

            }
        }
    }
}



#30 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 13 November 2012 - 10:22 AM

I've now added support for multiple temp sensors on the one wire bus.
No need for storing rom id's as it uploads them to the webservice and you can give them friendly labels there.

I've also update the WebUI a fair bit - check it out at http://my.aquariumwise.net/1

            var tempData = new TempData();

            var ida = (InputDataAvailable)state;

            var rom = new byte[8];

            if (oneWire.Reset())
            {
                oneWire.WriteByte(OneWire.ReadRom);
                oneWire.Read(rom);

                var deviation = 0;

                do
                {
                    if ((deviation = oneWire.Search(rom, deviation)) == -1)
                        break;
                    if (OneWire.ComputeCRC(rom, count: 7) == rom[7])
                    {
                        if (rom[0] == DS18B20.FamilyCode)
                        {
                            oneWire.Reset();
                            oneWire.WriteByte(OneWire.SkipRom);
                            oneWire.WriteByte(DS18B20.ConvertT);
                            Thread.Sleep(750);

                            var matchRom = new byte[9];
                            Array.Copy(rom, 0, matchRom, 1, 8);
                            matchRom[0] = OneWire.MatchRom;

                            oneWire.Reset();
                            oneWire.Write(matchRom);
                            oneWire.WriteByte(DS18B20.ReadScratchpad);

                            var tempLo = oneWire.ReadByte();
                            var tempHi = oneWire.ReadByte();

                            tempData.SetValue(DS18B20.GetTemperature(tempLo, tempHi));

                            tempData.SetExtValue(OneWireExtensions.BytesToHexString(rom));

                            ida(tempData);
                        }
                    }
                }
                while (deviation > 0);
            }


#31 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 13 November 2012 - 01:37 PM

I like the look of all this, I've never dealt with any OneWire stuff before, looks like it may be worth looking into :)

#32 zemuss

zemuss

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts

Posted 16 November 2012 - 09:58 PM

Thanks for posting the code. This is very nice! Is it ok if I use the code as a reference to start my project?

#33 bvogel

bvogel

    New Member

  • Members
  • Pip
  • 2 posts

Posted 28 November 2012 - 11:20 AM

Hi, just saw your amazing Aquarium project! respect. How do you measure pH? I'm asking as I'm searching a pH sensor which is reliable. thanks, Bernie

#34 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 28 November 2012 - 12:56 PM

Hey bvogel

I only just purchased a pH kit from Atlas Scientific.

In the process of wiring it up right now.

So far I'm having some issues getting it correctly calibrated so I have contacted their support.

But it's super easy to wire up and communicate with.

Posted Image

#35 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 28 November 2012 - 02:39 PM

That's the exact same kit I went with, really nice having just a straight serial comm for all the details. For my calibration, I just used a serial port from my PC and Putty. Made the calibration a lot easier than writing code for the Netduino.

#36 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 29 November 2012 - 12:50 AM

I've written an app to do the calibration, which all works well (haven't got a PC that has a serial port - or a USB -> serial adapter). But after the calibration has completed, it reads incorrect values. 7 shows 6.8 (unless I rest my hand on my mousepad, it then reads 7 - wtf!) 4 shows 4 10 shows 9.8 Their support was pretty quick to reply - have sent them a bunch of photo's and they said they'll look in to it.

#37 strent

strent

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 02 December 2012 - 12:30 PM

Got my pH probe all sorted and it's logging to the website now. How are you going with the WebServer H07R0D? I tried adding NeonMika's code (http://neonmikawebserver.codeplex.com/), but I constantly get out of memory exceptions (even after removing a large chunk of the un-needed stuff)

#38 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 03 December 2012 - 01:16 PM

Got my pH probe all sorted and it's logging to the website now.

How are you going with the WebServer H07R0D?

I tried adding NeonMika's code (http://neonmikawebserver.codeplex.com/), but I constantly get out of memory exceptions (even after removing a large chunk of the un-needed stuff)


Glad to hear the pH is going!

I've pulled the NeonMika repo as well, it does a lot of stuff very nicely. I'm stripping things down now, and I always find dropping anything XML will save a lot of space.
I'm working with that and pulling some concepts/code from https://github.com/o...p/TinyWebServer too. It's an arduino web server, so it's super small, and has a decent POST handler, which is my biggest concern.

But the holidays approach, so family/travel will be most of my time, probably won't have much time until after Christmas for more of thisPosted Image

#39 Pny

Pny

    New Member

  • Members
  • Pip
  • 1 posts

Posted 04 December 2012 - 02:48 PM

Very interesting! subscribed. :-)

#40 zemuss

zemuss

    Advanced Member

  • Members
  • PipPipPip
  • 69 posts

Posted 07 December 2012 - 06:09 PM

Strent Where did you get the relay and does it work with a 120 volt source? I want to start building my power strip to manage pumps and lights. Thanks,





Also tagged with one or more of these keywords: N+2, aquarium, webserver, scheduler, plugin, json, html, POST

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.