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

OnInterrupt problem calling WiFly


  • Please log in to reply
12 replies to this topic

#1 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 20 November 2012 - 04:42 PM

Hi guys,

This project includes a Netduino Plus 2, a photo sensor and a RN-XV WiFly module (with wireless shield).

What I am aiming for is that when the application starts a connection is made to the wifi network; then when the photo sensor is triggered a counter variable is sent to a webpage, as a querystring variable, to add it to a database.

Before I create the HTTP request I thought I would make sure I can access the WiFly "instance" from within the OnInterrupt "function/call" by simply getting the Local IP address again and writing it to the Debug window (apologies again if I'm getting the terminology wrong).

There is no problem populating the string "strLocalIP_1" but as soon as I try populate "strLocalIP_2" the application hangs.

What am I missing?

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 Toolbox.NETMF;
using Toolbox.NETMF.NET;
using Toolbox.NETMF.Hardware;

namespace sandbox
{
    public class Program
    {
        // declare counter variable, photoSensor and wifly
        static uint counter = 0;
        static InterruptPort mySensor;
        static WiFlyGSX WifiModule;

        public static void Main()
        {
            // Create sensor instance
            mySensor = new InterruptPort(Pins.GPIO_PIN_D7, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            mySensor.OnInterrupt += new NativeEventHandler(mySensor_OnInterrupt);

            // Create WiFly instance 
            WifiModule = new WiFlyGSX();

            // Allow some time for WiFly to power up and find network
            Thread.Sleep(5000);

            // Join Network
            WifiModule.EnableDHCP();
            WifiModule.JoinNetwork("SSID", 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, "Password");

            // Wait for authentication and joining
            Thread.Sleep(5000);

            Debug.Print("WiFi connected");

            // Get the Local IP to confirm connection to the wifi network
            string strLocalIP_1;
            strLocalIP_1 = WifiModule.LocalIP;

            Debug.Print(strLocalIP_1);
            
            // Indefinitely wait for any sensor interrupts...
            Thread.Sleep(Timeout.Infinite);
        }

        public static void mySensor_OnInterrupt(uint port, uint state, DateTime time)
        {
            // get the "interrupt" state to stop counter counting twice
            if (state == 0)
            {
                // Write the count to the debug window
                Debug.Print("counter: " + counter);

                // Get the Local IP to confirm connection to the wifi network
                string strLocalIP_2;
                strLocalIP_2 = WifiModule.LocalIP;

                Debug.Print(strLocalIP_2);

                // increase counter by 1
                counter++;
            }
        }
    }
}


Posted Image

I'm sure it's a real noob oversight on my part but would appreciate any help.

Thanks,
Donovan

#2 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 07:26 AM

Hi there,

Ok... so I have now added the on-board LED to the project

public class Program
    {
        static WiFlyGSX WifiModule;
        static OutputPort led;

and here

public static void Main()
        {
             WifiModule = new WiFlyGSX();
             led = new OutputPort(Pins.ONBOARD_LED, false);

Now when the event handler (the interruption of the photo sensor) is triggered I can interact with the led and turn it on and off:

This added to the method:

public static void mySensor_OnInterrupt(uint port, uint state, DateTime time)
        {
           led.Write(state == 0);

So why can I interact with the led but not the WifiModule in the event method?

What am I missing? :blink:

Thanks,
Donovan

#3 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 07:54 AM

ok... so in the mySensor_OnInterrupt method I CAN actually interact with the WifiModule itself like this:

public static void mySensor_OnInterrupt(uint port, uint state, DateTime time)
        {
            Debug.Print(WifiModule.MacAddress);

The problem is that I can't access anything related to the WiFi connection like the Local IP Address or creating a socket to send an HTTP request for posting the counter value.

#4 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 10:21 AM

Would a way around this be to set the wifi details using coolTerm directly onto the RN-XV module so that as soon as it has power it will automatically connect to the wifi network? Then surely I'd be able to create the socket and send the HTTP request each time the sensor is interrupted as the wifi connection will be in place?

#5 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 21 November 2012 - 02:20 PM

Hi there! I think the biggest problem is that the wifly module can only handle one task at the time; so when it has an open socket it can't do anything else. When using interrupts, there's a big chance the previous request is still going on, so making a new one will make it hang. Could that be the case here?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#6 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 02:24 PM

Hi Stefan, Thanks for getting back to me... I have just started to think the same thing because when I do the socket.open and the HTTP send in the main thread it's fine but there is only a problem when I try and do it in the interrupt method. I'm using a try/catch now to try and see if that's the issue. Could be a major setback for my project if it's the case and I may have to go back to the Arduino :(

#7 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 02:25 PM

I have amended the "Port.InterruptMode.InterruptEdgeBoth" to be "Port.InterruptMode.InterruptEdgeHigh" so it should only catch one part of the state change for the interrupt... ...will see if that helps at all and let you know.

#8 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 21 November 2012 - 02:32 PM

Could be a major setback for my project if it's the case and I may have to go back to the Arduino :(

Actually the Arduino has the same issue;
1. the Wifi module has the limitation of 1 action at the same time, whether you use an arduino or not
2. the Arduino has not that many interrupt pins

:)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#9 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 21 November 2012 - 02:32 PM

I have amended the "Port.InterruptMode.InterruptEdgeBoth" to be "Port.InterruptMode.InterruptEdgeHigh" so it should only catch one part of the state change for the interrupt...

...will see if that helps at all and let you know.

You could also make a main loop, not working with interrupts, like you probably did with an Arduino before.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#10 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 02:37 PM

You could also make a main loop, not working with interrupts, like you probably did with an Arduino before.


I think that was how I did it before... with a timer to ensure the wifly was only handling one request at a time.

I am thinking that for this to work I may need to have the interrupt write the "count" to the SD card and then every minute (or whatever) have the wiFly upload that file (maybe XML) to the server and then send an HTTP request for the server to get the data from the file.

Are there any examples for FTP with the wifly?

#11 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 21 November 2012 - 02:39 PM

Are there any examples for FTP with the wifly?

Not yet written by me. it's on my wishlist, but I currently lack quite some time, regretfully.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#12 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 02:58 PM

I hope I'm able to find someone who's written some FTP code then or my project could be scuppered! Thanks for the help Stefan :D

#13 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 21 November 2012 - 03:09 PM

If anyone is interested in FTP I have found this: http://forums.netdui...-client-socket/ and will let you know how I get on with it. Hmmm... seems to be for an Ethernet connection and not wifi...

Edited by don664, 21 November 2012 - 03:16 PM.





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.