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