Go Down

Topic: Serial Communication without computer? (Read 2808 times) previous topic - next topic

x11kjm

Hi all, Im trying to use serial to wirelessly communicate between two arduinos, a mega and a duemilanove.. I dont know if this is the right place to post, but ive tried to unplug them from my computer and then they dont use serial anymore... How do i fix this?


Thanks for the help

BTW, FIRST POST (in this section)!! UBER PWND  8)

jezuz

What hardware are you using to communicate wirelessly? To clarify - the arduino's you listed aren't capable of wireless serial communication without external hardware like xbees, bluetooth, or rf modules.

x11kjm

TELEPATHY  :) (i wish)  I am using these rf modules:

http://www.sparkfun.com/products/8946 (transmitter)
http://www.sparkfun.com/products/8949 (reciever)

which are analog

Thanks

zonedabone

This should be in the coding section. For wireless, you'll want to check out the XBee radios for arduino:

http://www.arduino.cc/playground/Shields/Xbee01

If you don't need wireless, and are fine with just a few wires between the systems, you can use a simple connection.

Connect board 1 pin 0 to board 2 pin 1
Connect board 1 pin 1 to board 2 pin 0
Connect the two grounds together.

You'll then want to look at the Serial library:

http://arduino.cc/en/Reference/Serial

x11kjm

#4
Jan 26, 2011, 05:03 am Last Edit: Jan 26, 2011, 05:12 am by x11kjm Reason: 1
Sorry, All i need to know is how to make it so that the arduinos will communicate without being hooked up to the computer.. I have it set up already so that they will transmit data, however they only do it when connected to computer (as if their talking to it) i am transmitting from one to the other, not bi directional, would that effect it?

Thanks

James C4S

What does your code look like?  There's no reason the hardware should need to be connected to your PC unless you are doing something wrong in the code.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

x11kjm

okay for the Duemilanove i am using this -

Code: [Select]
int gTempCmd  = 0b00000011;
int gHumidCmd = 0b00000101;

int shiftIn(int dataPin, int clockPin, int numBits)
{
  int ret = 0;
  int i;

  for (i=0; i<numBits; ++i)
  {
    digitalWrite(clockPin, HIGH);
    delay(10);  // I don't know why I need this, but without it I don't get my 8 lsb of temp
    ret = ret*2 + digitalRead(dataPin);
    digitalWrite(clockPin, LOW);
  }

  return(ret);
}

void sendCommandSHT(int command, int dataPin, int clockPin)
{
  int ack;

  // Transmission Start
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, LOW);

  // The command (3 msb are address and must be 000, and last 5 bits are command)
  shiftOut(dataPin, clockPin, MSBFIRST, command);

  // Verify we get the coorect ack
  digitalWrite(clockPin, HIGH);
  pinMode(dataPin, INPUT);
  ack = digitalRead(dataPin);
  if (ack != LOW)
    Serial.println("Ack Error 0");
  digitalWrite(clockPin, LOW);
  ack = digitalRead(dataPin);
  if (ack != HIGH)
    Serial.println("Ack Error 1");
}

void waitForResultSHT(int dataPin)
{
  int i;
  int ack;

  pinMode(dataPin, INPUT);

  for(i= 0; i < 100; ++i)
  {
    delay(10);
    ack = digitalRead(dataPin);

    if (ack == LOW)
      break;
  }

  if (ack == HIGH)
    Serial.println("Ack Error 2");
}

int getData16SHT(int dataPin, int clockPin)
{
  int val;

  // Get the most significant bits
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  val = shiftIn(dataPin, clockPin, 8);
  val *= 256;

  // Send the required ack
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);

  // Get the lest significant bits
  pinMode(dataPin, INPUT);
  val |= shiftIn(dataPin, clockPin, 8);

  return val;
}

void skipCrcSHT(int dataPin, int clockPin)
{
  // Skip acknowledge to end trans (no CRC)
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);

  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);
}

void setup()
{
  Serial.begin(2400); // open serial
}

void loop()
{
  int theDataPin  = 10;
  int theClockPin = 11;
  char cmd = 0;
  int ack;

  int val;
  int temp;

  sendCommandSHT(gTempCmd, theDataPin, theClockPin);
  waitForResultSHT(theDataPin);
  val = getData16SHT(theDataPin, theClockPin);
  skipCrcSHT(theDataPin, theClockPin);
  temp = -40.0 + 0.018 * (float)val;
  Serial.print("t");
  Serial.println(temp, HEX);         
}



And for the Mega I am using this (just to simply receive it) -
Code: [Select]
int incomingByte = 0; // for incomi
boolean temp = false;
boolean readFirst = false;
boolean readSecond = false;
int readValue = -1;

void setup() {
  Serial.begin(2400); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    incomingByte = Serial.read(); // read the incoming byte
    if (!readFirst) { // if we haven't read the first byte of the triad, then we are now
      char incomingChar = (char) incomingByte; // make it a character
      Serial.println(incomingChar); // print it to serial
      if (incomingChar == 't') { // if it's 't'
        temp = true; // then we are reading a temperature
      } else { // otherwise, it's an 'h'
        temp = false; // and we don't have temperature
      }
      readFirst = true; // we have read in the first of a triad
    } else if (!readSecond) { // if we haven't read the second byte, then we are now
      readValue = (incomingByte - 48) * 10; // so make the readValue the ASCII digit it represents * 10
      readSecond = true; // and mark that we read the second byte
    } else { // we must be reading the third byte
      readValue += (incomingByte - 48); // so add in the next ASCII digit
      processData(); // process the data
      readFirst = false; // and clear both flags
      readSecond = false; // so we read another value
    }
  }
}

void processData() {
  // do something with the data
}


Thanks

Graynomad

#7
Jan 27, 2011, 05:47 am Last Edit: Jan 27, 2011, 05:53 am by Graynomad Reason: 1
Don't know if I'm missing something but you seem to be transmitting using synch and receiving using async.

Is that the case or did I not pay enough attention to the code?

Quote
wirelessly communicate

Where does the wireless come into it?

Also you are doing Serial.prints, if the PC is not connected these will hang waiting to clear the TX buffer (I'm pretty sure that's the case).
______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

x11kjm

I dont understand what you mean by "transmitting using synch and receiving using async."  However the wireless comes in with the second piece of code.  I am using basic rf modules, so there is no programming needed for those.

Thanks for reply

Graynomad

The first code uses "shiftin" which is sync (ie uses a clock signal), but I don't see any "shiftOuts" in the other code. So how are they talking to each other.

Quote
Also you are doing Serial.prints, if the PC is not connected these will hang waiting to clear the TX buffer (I'm pretty sure that's the case).


Rob Gray aka the GRAYnomad www.robgray.com

x11kjm

I am pretty new to arduino, and no i do not use "shiftOuts."  However, they are not talking to each other, one is sending the other the data, and it takes it from there, but does not send anything back. (Transmitter --> Reciever)  I am using Serial.prints, so how would i fix that?

Thanks for the help

Graynomad

OK, I see now, you are sending with Serial, the shiftIn stuff is for the sensor or whatever.

I'll have a closer look at th ecode.
______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

I think your main loop is really prone to errors with all the state variables, partly because the

Serial.println(temp, HEX);

line will also send CR LF at the end of the two HEX chars, and as the data is a constant stream you could easily get out of sync.

I think something simpler may work, and if not should be easier to debug.

Code: [Select]
void loop() {
   
  if (Serial.available() > 0) {
    if (Serial.read() == 't') {
    while (Serial.available() < 2) {}; // wait for two bytes
  readValue = (Serial.read() - 48) * 10;
  readValue += (Serial.read() - 48);
    processData(); // process the data
    }
  }
}


Also consider sending the data ad a binary # so you don't have to do the -48 stuff.

   
Code: [Select]
readValue = (Serial.read() << 8);
  readValue += (Serial.read());
   

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

CrossRoads

I think what is really needed is to use VirtualWire between the 2 arduinos.
http://www.open.com.au/mikem/arduino/VirtualWire.pdf
Works great between those 2 devices, I use it as a remote control to send button pushes to a receiver.
It puts in some bytes for the receiver to recognize something is coming, has some encoding also to ensure data integrity.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

x11kjm

#14
Jan 28, 2011, 07:15 pm Last Edit: Jan 28, 2011, 08:42 pm by x11kjm Reason: 1
I tried to use that code and it didn't seem to do anything (rx/tx lights not on, or serial output).  However I did try a code (i forget where from) and it worked without being connected to my computer.  What code would i be able to use with just the arduinos+transmitters/recievers (no buttons or anything) just to see if it works?

Thanks

Go Up