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

PC Xbee and Netduino Xbee communication


  • Please log in to reply
5 replies to this topic

#1 Shehab

Shehab

    New Member

  • Members
  • Pip
  • 7 posts

Posted 22 June 2012 - 12:12 AM

Hi All,

I hope I could get help with this project I have been reading a lot trying to get it to work. I am trying to communicate Netduino Plus that has an Xbee on top of it with a shield and a potentiometer connected to the A0 Pin for input, I created this Netduino application and i can debug and see different readings when I change the potentiometer:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO.Ports;
using System.IO;

namespace XBeeTest
{
    public class Program
    {

        public static void Main()
            {

                SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
                serialPort.ReadTimeout = 0;
                  serialPort.Open();
                  // 1. connect the middle pin of the potentiometer to A0
                  // 2. connect the other pin of the potentiometer to GND
                  // 3. connect the other pin of the potentiometer to 3.3v
                  AnalogInput inputPort = new AnalogInput(Pins.GPIO_PIN_A0);
                  try
                  {
                        while (true)
                        {
                              // read the value
                              int potentiometerValue = inputPort.Read();
                              // some required convertion
                              float analogConversion = (float)potentiometerValue / 1023 * 3.3f;
                              // then we will prepare sending the message
                    byte[] buffer = new byte[analogConversion.ToString().Length];
                              // convert the string to bytes
                    buffer = System.Text.Encoding.UTF8.GetBytes(analogConversion.ToString());
                              // write it to Xbee through serial
                    serialPort.Write(buffer, 0, buffer.Length);
                              // wait 500 seconds
                              Thread.Sleep(500);
                        }
                  }
                  finally
                  {
                      
                      serialPort.Close();
                  }                
            }
    }
}

I am assuming after I run this one on the Netduino plus with xbee, I can unplug it from the PC and connect it to the battery and it would still do the same right?

OK, at the other Xbee that I connect to the PC, I created a console app to read the serial port and print out the potentiometer readings and here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace WirelessAlarm
{
    // Console program for receiving the data
    class Program
    {
        static void Main(string[] args)
        {
            
            SerialPort sensor = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            int bytesToRead = 0;
            bool isPlaying = false;
            string message;
            sensor.Open();
            try
            {
                while (true)
                {
                    // check if there are bytes incoming
                    bytesToRead = sensor.BytesToRead;
                    string strbytesToRead = sensor.ReadExisting();
                    if (bytesToRead > 0)
                    {
                        byte[] input = new byte[bytesToRead];
                        // read the Xbee's input
                        sensor.Read(input, 0, bytesToRead);
                        // convert the bytes into string
                        message = System.Text.Encoding.UTF8.GetString(input);
                        // message contains the string we received from the Serial port
                        // TODO: put the code here to do something with the message
                    }
                }

            }
            finally
            {
                
                sensor.Close();
            }
        }
    }
}

for some reason, I get nothing on the console app in the "bytesToRead", what am I doing wrong? I hope this makes sense.

Thanks,
Shehab

#2 awaiK

awaiK

    Advanced Member

  • Members
  • PipPipPip
  • 90 posts

Posted 22 June 2012 - 05:07 AM

Are you sure your XBee configuration is correct? You need at least one XBee Cooridnator and one XBee Router or one XBee End Device. If you are using the AT-Command firmware, you need to pair your two XBees.

#3 Shehab

Shehab

    New Member

  • Members
  • Pip
  • 7 posts

Posted 22 June 2012 - 11:14 AM

Hi awaiK, I think you are right, I figured I need to configure the 2 Xbees together, do you have any documentation or tutorial? Here is the Xbee kit I bought: http://www.sparkfun.com/products/9897. one is connected to the Netduino and the other is on the Xbee explorer that connects to the PC. Thank you very much for your help.

#4 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 27 June 2012 - 05:36 AM

Hi Shehab, I just got my Xbee working using a WPF application to communicate from the PC using COM3. At first I too could not get the Xbee's to communicate and discovered the Win7 had my Xbee placed on a random COM port. Once I fixed this I tested the communication via Putty and X-CTU. As soon as I get some sleep and can think and talk coherently I will post some examples and bit of a Xbee primer. Cheers, Steve

#5 Shehab

Shehab

    New Member

  • Members
  • Pip
  • 7 posts

Posted 27 June 2012 - 11:13 AM

Thank you Steve, I got it to work too, the only issue is that sometimes it just stops working. Please post your example if you can. Thanks, Shehab

#6 sebswed

sebswed

    Advanced Member

  • Members
  • PipPipPip
  • 45 posts
  • LocationSweden

Posted 10 April 2013 - 05:39 AM

Hi there.

 

Any chance one one of you guys could post the working code? I'm a complete beginner.

I do have 2 Xbee modules successfully talking in AT mode. I have a little Arduino Fio as Router in AT mode sending data to a coordinator connected to my PC but would like to receive this data on my Netduino 2+ instead.

 

ps. I downloaded Grommet but have no idea on how to import this into Visual C# 2010 Express.

 

Thanks in advance!


Netduino Plus 2 (v4.2.2.2)





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.