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

Stops writing to database

Netduino 2 plus

  • Please log in to reply
9 replies to this topic

#1 alharlow

alharlow

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationNebrask USA

Posted 09 September 2015 - 12:52 AM

I have ND2+ reading a temperature sensor then calling a PHP web page on Apache using HTTP.  The page writes the temp to a mySQL database.  Works fine for maybe a day or two then just stops updating the database.  I don't know how to tell if the ND2+ is still doing its thing or if it's hung.  If I'm running it through the Visual Studio debugger I have a few debug.print statements that stop appearing like the ND2+ is hung.

 

Anyone have any ideas?



#2 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 09 September 2015 - 02:26 AM

Hi,

I'm doing something similar yet different:

  • I have a Netduino Plus 2 connected to a DS18B20 temperature sensor (interfaced using OneWire)
  • I created a program (based on Cuno Pfister's "Getting Started with the Internet of Things" book to upload the temperature to Xively every 15 minutes

My program has been running for 3 weeks continuously and has not been affected by my having taken my LAN down several times for maintenance over that same time period.  It just keeps running & it seems very robust.

 

If yours runs and then stops after a while, I suspect you may have some kind of a memory leak (i.e. your program consumes resources & does not free them up).  It's a bit of a stretch for me to say that without actually seeing your code, but the behaviour is consistent.

 

Try adding the following to your code (I would put in just 1 place - i.e. after the spot where you send your data up to the server):

            Debug.Print("time: " + DateTime.Now);
            Debug.Print("memory available: " + Debug.GC(true));

If you DON'T have a memory leak, the number should stay fairly constant (it may vary slightly).  If you DO have a leak, the available memory will decrease over time.

 

I would love to see your code, if you'd care to post it...  What sensor are you using?

 

Ian


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi

#3 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 09 September 2015 - 05:38 AM

You could also flash the on board LED to tell you if the main program is running.  At least you would know if the Netduino is still alive.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 09 September 2015 - 11:54 PM

Hey alharlow,

Nevyn's tip on blinking the blue LED (on a timer) is what I'd do, to start...

Also: are you using Netduino.IP or the traditional lwIP networking stack?

Chris

#5 alharlow

alharlow

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationNebrask USA

Posted 10 September 2015 - 11:52 PM

I did the onboard LED thing.  The LED stops blinking when it hangs (the LED stays on when it hangs).

Also used the debug.GC(true) call.  The memory available stays around 94,448 so apparently its not a memory leak.

I added some additional debug.print statements and found that it's hanging on the dataStream = request.GetRequestStream(); in the DoThePost method.  My understanding is that that statement is creating an object.

 

I have attache the code.

 

Help!

Attached Files



#6 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 11 September 2015 - 02:16 AM

Hi Al,

 

I've taken a look at your code.  Your webrequest class looks a bit shaky to me.  I didn't notice major issues with your other classes.

 

I would like to recommend that you take a look at the way Cuno Pfister does things.  This is the website that accompanies his book (mentioned in my previous post):  http://www.gsiot.info/download/

 

In the section entitled GSIoT Sample Code for NETMF 4.3.1 you will find a ZIP file entitled "Device samples for Netduino Plus 2".

Inside that file you'll find a folder called Gsiot.XivelyClient.Netmf, and inside that folder you'll find a C# code file containing a class called XivelyClient.

 

I think it should be a fairly simple matter for you to clone that class and then adapt it for your requirements (i.e. use it in place of your webrequest class).  I think the implementation is much cleaner than what you're doing, in that you don't have to fool around with stray instances (e.g. setting things to null before/after using them).  You'll see 3 places in his code where he's utilizing the 

using ( this ) { do that }

 construct.  It handles dispositions for you...

 

Ian


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi

#7 alharlow

alharlow

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationNebrask USA

Posted 16 September 2015 - 03:48 AM

I went to the site you provided and downloaded the code, swapped it in for my 'webrequest' class.  Modified it slightly for my situation and ran it.  It runs fine the first time through but on the second time around it hangs on the line after the debug.print("X4") line.  Code shown below.  This is a static class.  Do the objects created within the class get recreated the second time through the code?

 

 

//Developed for the book
//  "Getting Started with the Internet of Things", by Cuno Pfister.
//  Copyright 2011 Cuno Pfister, Inc., 978-1-4493-9357-1.
//
// Version 4.3, for the .NET Micro Framework release 4.3.
 
using Microsoft.SPOT;
using System;
using System.IO;
using System.Net;
using System.Text;
 
namespace App4ReadTemp
{
    public static class XivelyClient
    {
        const string baseUri = "http://www..............php";
 
        public static void Send(float ftemp)
        {
            Debug.Print("time: " + DateTime.Now);
            Debug.Print("memory available: " + Debug.GC(true));
            try
            {
                using (var request = CreateRequest(ftemp))
                {
                    request.Timeout = 5000;     // 5 seconds
                    // send request and receive response
                    Debug.Print("X6");
                    using (var response =
                        (HttpWebResponse)request.GetResponse())
                    {
                        Debug.Print("X7");
                        HandleResponse(response);
                        Debug.Print("X8");
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
        }
 
        static HttpWebRequest CreateRequest(float ftemp)
        {
            ftemp = (float)(System.Math.Round((double)ftemp));
            string Temp = ftemp.ToString();
            string postData = "temp1=" + Temp;
 
            byte[] buffer = Encoding.UTF8.GetBytes(postData);
 
            Debug.Print("X1");
            var request = (HttpWebRequest)WebRequest.Create(baseUri);
            Debug.Print("X2");
 
            // request line
            request.Method = "PUT";
 
            // request headers
            request.ContentLength = buffer.Length;
            request.ContentType = "application/x-www-form-urlencoded";
 
            // request body
            Debug.Print("X3");
            using (Stream stream = request.GetRequestStream())
            {
                Debug.Print("X4");
                stream.Write(buffer, 0, buffer.Length);
                Debug.Print("X5");
            }
 
            return request;
        }
 
        public static void HandleResponse(HttpWebResponse response)
        {
            Debug.Print("Status code: " + response.StatusCode);
        }
    }
}


#8 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 16 September 2015 - 07:19 AM

Hi Al,

 

You're talking about objects based on these classes (I've included links to the NETMF documentation):

in the following code segments:

using (Stream stream = request.GetRequestStream())
{ do stuff... }

using (var request = CreateRequest(ftemp))
{ do stuff... }

using (var response = (HttpWebResponse)request.GetResponse())
{ do stuff... }

You'll see in the documentation that Stream implements the IDisposable Interface.

 

You'll see also that HttpWebRequest is an implementation of the WebRequest abstract class and that HttpWebResponse is an implementation of the WebResponse abstract class.  Both of these abstract classes implement the IDisposable Interface also.

 

Because you're creating objects based on these classes inside using ( object ) { do something ... } code blocks, they DO get properly disposed of when you exit the code segment.  Please see:  using Statement (C# Reference).  It's very elegant...

 

I am disappointed to hear that making this change did not resolve the problem.  I still think that Pfister's code is the way to go for this portion of what you're trying to implement (I hope you can see how elegant it is...)  Anyway, if you give me a day or so I'll work through the rest of your code and see if I can duplicate the problem / figure out what's really wrong...

 

Regards,

Ian


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi

#9 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 16 September 2015 - 10:18 AM

Hi Al

 

I have a sample application on my blog which reliably uploads data from nRF24L01 equipped Arduino, devDuino & Netduino devices to xively

 

The relevant code (simplified) is included it below for comparison

 

ftemp = (float)(System.Math.Round((double)ftemp));
string Temp = ftemp.ToString();
string postData = "temp1=" + Temp;
 
simplified to
 
string postData = "temp1=" + ftemp.ToString("F0");
 
@KiwBryn

blog.devmobile.co.nz

 

 

private void xivelyFeedUpdate( string channel, string value)
{
   try
   {
      using (HttpWebRequest request = (HttpWebRequest)WebRequest.Create(xivelyApiBaseUrl + xivelyApiFeedID + xivelyApiEndpoint))
      {
         byte[] buffer = Encoding.UTF8.GetBytes(channel + "," + value);
 
         request.Method = "PUT";
         request.ContentLength = buffer.Length;
         request.ContentType = xivelyApiContentType;
         request.Headers.Add("X-ApiKey", xivelyApiKey);
         request.KeepAlive = false;
 
         using (Stream stream = request.GetRequestStream())
         {
            stream.Write(buffer, 0, buffer.Length);
         }
 
         using (var response = (HttpWebResponse)request.GetResponse())
         {
            Debug.Print(" Status: " + response.StatusCode + " : " + response.StatusDescription);
         }
      }
   }
   catch (Exception ex)
   {
      Debug.Print(ex.Message);
   }
}

 

 



#10 martinhoyle

martinhoyle

    New Member

  • Members
  • Pip
  • 2 posts

Posted 25 October 2015 - 06:46 PM

Hi

 

Have you got an external hardware watchdog - if not I would recommend adding one. I have been running an Netduino plus 2 for months posting data to a website.

I use a watchdog see https://highfieldtal...r-the-netduino/

 







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.