This is a serial data transmission class for my weather station.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1265087241
It has 3 functions:
SSERIAL.begin(int baud)
This sets the baud rate. The int baud sets the pulse width in microsecs.
If this function is not used a default pulse width of 2500 microsecs will
be set to give a baud rate 0f 400.
unsigned int SSERIAL.request()
This requests a remote audino to send in serial a unsigned integer.
If the remote does not respond in 1 second the function returns 65535.
void senddata(unsigned int send_data)
This waits for a request then sends in serial the unsigned integer .
There is no timeout.
I have a remote site to monitor weather data. This data is collected using an arduino pro.
My “master” station is indoors and uses a arduino 2009.
This arduino will request data from the remote arduino.
On receiving a request the remote will send an unsigned integer in serial.
All of my weather sensors produce unsigned integers.
The class uses software serial. It operates in the same manner as
SoftwareSerial library in the arduino-0017.
I have included a timeout which is used to produce an error message
at the master.
The remote send the serial data from its D3.
It listens on D2 for the request (LOW).
The master sends the request (LOW) from D3.
It listens for serial data on D2.
The following details apply to windows:
To try out this class you will need to store the SSERIAL folder in:
C:\arduino-0017\hardware\libraries
Or where you have your arduino-0017.
Start arduino-0017 and look under file-examples.
Select master for your main arduino.
Upload it to the port it is allocated - mine was COM8.
/* Serial data transfer of an unsigned integer from a remote arduino.
This is the master arduino
SSERIAL.begin(baud); sets up the baud speed for serial transmission
SSERIAL.request() send request to remote arduino.
Then it waits for an unsigned integer to be sent back in serial.
Returns 65535 if times out after 1 second.
The request is sent (LOW) on D3.
The data is received on D2.
*/
#include <SSERIAL.h>
int baud = 100; //the pulse width of the serial data
unsigned int rec_data = 0; //the integer received
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // initialize serial communication with computer
SSERIAL.begin(baud); // set transmission baud rate default = 400
}
void loop() // run over and over again
{
rec_data = SSERIAL.request();
Serial.println(rec_data); //print the data from remote
delay(100);
}//end of loop
Start another instance of arduino-0017 and look under file-examples.
Select slave for your slave arduino.
Upload it to the port it is allocated - mine was COM9.
/* Serial data transfer of an unsigned integer from a remote arduino.
This is the slave arduino
SSERIAL.begin(baud); sets up the baud speed for serial transmission
SSERIAL.senddata(send_data); Waits until request for data no timeout.
when it gets request it sends send_data in serial to master.
D2 is polled for request (LOW).
Data is sent on D3.
*/
#include <SSERIAL.h>
unsigned int send_data = 30555; //the integer to send
const int baud = 100; //the pulse width of the serial data
void setup() // run once, when the sketch starts
{
SSERIAL.begin(baud); // set transmission baud rate default = 400
}
void loop() // run over and over again
{
SSERIAL.senddata(send_data); //sends data when requested
}//end of loop
There is also another example you can use for the master.
master_error
This will check groups of 100 data transfers and print out the number
of errors.
/* Serial data transfer of an unsigned integer from a remote arduino.
This is the master arduino
SSERIAL.begin(baud); sets up the baud speed for serial transmission
SSERIAL.request() send request to remote arduino.
Then it waits for an unsigned integer to be sent back in serial.
Returns 65535 if times out after 1 second.
The request is sent (LOW) on D3.
The data is received on D2.
*/
#include <SSERIAL.h>
int baud = 100; //the pulse width of the serial data
//anything faster than this gives large error rate
unsigned int rec_data = 0; //the integer received
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // initialize serial communication with computer
SSERIAL.begin(baud); // set transmission baud rate default = 400
}
void loop() // run over and over again
{
unsigned int temp = 0;
unsigned int errors = 0; //number of errors
byte j = 0; // for start counter
for (j = 0; j < 100 ; j +=1) //get 100 data transfers
{
temp = SSERIAL.request(); //get an integer transfer
if (temp != 30555)
{
//Serial.println(temp);
errors +=1; //we have an error
}//end of != 30555
delay(1);
}//end of get 100 data transfers
Serial.println(errors);
delay(1);
}//end of loop
With a pulse width of 100 microsecs there should never be any errors.
If you decrease the pule width(increase baud rate) you will find that
errors start to occur.
Below 80 it drops its bundle. The same happens for SoftwareSerial
there is too much slop in the timing when using software.
The new programs and diagrams are available from:
Download SERIAL.zip