Hey, guys, I'm faced with a problem I can NOT seem to take care of - or even find what's causing it.
Basically there's one Arduino sending serially the string !A18 and other Arduino (this one) receiving it.
Here's the second Arduino's code:
/*
* Temperature and Humidity Server
* version 1.0a
* by Pedro Tome'
*
* How it works:
* Basically, one Master Arduino receives data
* from various Slave Arduinos who have Temperature
* and Humidity sensors, which is then uploaded to
* a Pachube.com account.
*
* This code is for:
* SLAVE :: TEMPERATURE AND HUMIDITY
*/
/* Temperature Sensor's pre-commands */
#include <OneWire.h>
OneWire ds(7); // Temperature Sensor's pin 2 (output) connected to Arduino's pin D7
/* Humidity Sensor's pre-commands */
int humSensor_pin = 0; // Humidity Sensor's pin 2 (output) connected to Arduino's pin A0
float max_voltage = 3.27; // Humidity Sensor's output when: 100% humidity @ 0ºC
float ZeroPercentVoltage = 0.8; // Humidity Sensor's output when: 0% humidity
/* Other pre-comands */
int statusLED = 2; // Status LED connected to pin D2 to show whether the sketch is running or not
int RX_pin = 0; // Serial Input pin D0
int TX_pin = 1; // Serial Output pin D1
char selfAddressLetter = 'A'; // Letter 'A'. Every Arduino has a different Letter Address (Duh!).
void setup() {
Serial.begin(115200);
pinMode(statusLED, OUTPUT);
pinMode(RX_pin, INPUT);
pinMode(TX_pin, OUTPUT);
}
void loop() {
Serial.flush();
boolean stringValid = false; // initialize a variable that holds the validity status of the received strings
byte receivedAddressLetter[1] = {'/0'}; // initalize the variable that contains the received Address Letter
char receivedAddressCRC[2]; // initalize the array that contains the received Address CRC, which is always two characters long
memset(receivedAddressCRC, '/0', 2); // set the addressCRC array as all NULL characters
byte startByte = '/0'; // initialize startByte, the first relevant character of the received messages
unsigned long startTime = millis(); // sets the startTime = to now
int timeout = 1000; // sets the timout to 1 second
while (millis() - startTime < timeout && startByte != '!') {
startByte = Serial.read();
}
if (startByte == '!') { // if the Arduino has received a serial message that started with the character '!'
unsigned long startTime = millis(); // set the startTime = to now
int timeout = 1000; // set the timout to 1 second
while (millis() - startTime < timeout && Serial.available() < 3) {
; // wait for the buffer to be filled with 3 characters while doing nothing
}
/* Read the received Address and its CRC */
receivedAddressLetter[0] = Serial.read();
for (int i=0; i < 2; i++) {
receivedAddressCRC[i] = Serial.read();
}
/* Check the validity of the received content by generating and comparing CRCs */
unsigned int INTreceivedAddressCRC;
INTreceivedAddressCRC = htoi(receivedAddressCRC[0])*16+htoi(receivedAddressCRC[1]); // convert the CRC array into an int
if (INTreceivedAddressCRC == GenerateCRC8(receivedAddressLetter, 1)) {
stringValid = true;
Serial.println("The received string is valid.");
}
else {
stringValid = false;
Serial.println("The received string is NOT valid.");
}
/* Check if the receivedAddressLetter matches THIS ARDUINO's selfAddressLetter
and, if so, collect data from the sensors and send it to the MASTER */
if (stringValid == true && receivedAddressLetter[0] == selfAddressLetter) {
/*
* Temperature Sensor code :: START
*/
int HighByte, LowByte, TReading, SignBit, Tc_100;
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//Serial.println("No more addresses.");
ds.reset_search();
delay(250);
return;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
if ( addr[0] != 0x28) {
Serial.println("Device is not a DS18B20 family device.");
return;
}
// The DallasTemperature library can do all this work for you!
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(900); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// Converting HEX to readable data...
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // If negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
float floated_Tc_100 = Tc_100; // Floats can't divide ints, so we convert the int to a float
float cTemperature = floated_Tc_100 / 100;
if (SignBit) // If it's negative
{cTemperature = -1 * cTemperature;}
//Serial.print("Tc_100: "); Serial.println(Tc_100); // Only to make sure cTemperature is correct
Serial.print("Temperature (Celsius): "); Serial.println(cTemperature);
/*
* Temperature Sensor code :: END
*/
//--------------------------------------------------------------------------------------
/*
* Humidity Sensor code :: START
*/
float humSensor_rawRead = analogRead(humSensor_pin);
//Serial.print("Raw Humidity value: "); Serial.println(humSensor_rawRead);
max_voltage = (3.27-(0.006706*cTemperature)); // The max voltage value drops 0.006705882 Volts for each ºC over 0ºC
float RH = ((((humSensor_rawRead/1023)*5)-ZeroPercentVoltage)/max_voltage)*100;
Serial.print("Relative Humidity (%): "); Serial.println(RH);
/*
* Humidity Sensor code :: END
*/
Serial.println("LOOP END -------------------");
Serial.println();
} //END if (stringValid == true && receivedAddressLetter[0] == selfAddressLetter)
} //END if (startByte == '!')
} //END void loop()
Here's the expected output:
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 84.38
LOOP END -------------------
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 83.76
LOOP END -------------------
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 83.92
LOOP END -------------------
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 83.61
LOOP END -------------------
And here's the ACTUAL output:
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 84.38
LOOP END -------------------
The received string is valid.
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 83.76
LOOP END -------------------
The received string is valid.
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 83.92
LOOP END -------------------
The received string is valid.
The received string is valid.
Temperature (Celsius): 16.62
Relative Humidity (%): 83.61
LOOP END -------------------
This is what I think is happening:
- The Arduino receives the string and follows the code to the end.
- The Arduino receives the string and DOES NOT follow the code to the end.
- The Arduino receives the string and follows the code to the end.
- The Arduino receives the string and DOES NOT follow the code to the end.
- The Arduino receives the string and follows the code to the end.
- etc.
So, can you see the problem?
Oh, and this also happens with a 9600 baud rate.
I read in another thread this:
Serial.begin(9600);
This is a limiting factor. Processing and Arduino can talk much faster then this. Try a higher speed, like 115200
So I thought "hey, why the hell not?" and then I turned my baud rate up.
Does a higher baud rate offer more problems than a lower baud rate? The Master Arduino and the several Slave Arduinos are supposed to talk serially at a distance of approximately 300m (through RS485)...
Thank you!