Serial Communication without computer?

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)

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.

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

RF Link Transmitter - 434MHz - WRL-08946 - SparkFun Electronics (transmitter)
http://www.sparkfun.com/products/8949 (reciever)

which are analog

Thanks

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:

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

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.

okay for the Duemilanove i am using this -

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) -

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

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?

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

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

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.

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).

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

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

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.

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.

readValue = (Serial.read() << 8);
  	readValue += (Serial.read());

Rob

I think what is really needed is to use VirtualWire between the 2 arduinos.

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.

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

Read the VirtualWire document - it does not use the serial ports, it uses D11 & D12 as defualt, they can be changed.
Put the data you want in an array, it sends it out. Here is heart of the code that would go in void loop().
There is more that goes in setup, read the document, the demo worked as-is for me sending characters. Very straightforward.
I reduced it a little as I was only sending 1 byte out.

#include <VirtualWire.h> // Wireless transmitter/receiver library

    msg[0]=key;                               // load the array with the key character
    // msg[1]=NULL;                           // Rx side seems to work without this for me (I only send 1 byte from a keypad)

    digitalWrite(ledPin, true);               // Flash a light to show transmitting

    vw_send((uint8_t *)msg, strlen(msg));     // send the character out

//    Serial.println(key);                // for debugging only

    vw_wait_tx();                             // Wait until the whole message is gone

On the receive side:

// look for wireless input or for unsolicited message from arduino2

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen)) // Non-blocking
{
  int i;
  digitalWrite(ledPin, LOW); // Turn on LED to show working a message

  // Message with a good checksum received, dump it.
  //Serial.println("Got: ");  // Show on PC for debugging

  //    for (i = 0; i < buflen; i++)
  //    {
  //Serial.print(byte(buf[i]));
  //    }
  //Serial.println(""); // spaces it out for the monitor