Hi all,
I'm tasked with the project of building a sensor network containing lots of arduino powered sensors and connecting them to a computer. I've never done anything like this before and am hoping to get some ideas and suggestions for my approach.
The data is read in using analogRead functions to get integers and needs to be passed to a computer running matlab. Matlab then analyzes the data. I've done some searching and came up with this master slave solution. It's not a finished product but I believe it has most of the needed basics covered.
The slave devices obtain data from the environment and wait for the master to call on them. Then they send it to the master. The master sends the information out of the standard serial port to the computer. I'll build a matlab program to catch the data coming from the serial port.
Due to the distances involved in the real project I have to use RS485. I've chosen to use these adapters http://arduino-info.wikispaces.com/RS485-Modules
Please let me know if there is a better way to do this or if I'm missing anything.
I've heard modbus being tossed around and I wonder if it could help me?
here is the master code
#include <SoftwareSerial.h>
//initializing software serial pins
#define RX 10
#define TX 11
#define TXcontrol 3
#define Transmit HIGH
#define Recieve LOW
SoftwareSerial mySerial(RX,TX);
int dataOne=1;
int dataTwo=1;
int one=501; // first slave address
int two=502; //second slave address
void setup() {
// put your setup code here, to run once:
//(error,OUTPUT);
//digitalWrite(error,LOW);
pinMode(TXcontrol,OUTPUT);
mySerial.begin(4800); //start serial communication between arduinos
Serial.begin(9600); //start communication with computer
delay(2500);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TXcontrol,Transmit); //prepare to transmit to slaves
mySerial.write(one); //call first slave
digitalWrite(TXcontrol,Recieve); //prepare for first slave data
dataOne=mySerial.read(); //recieve first slave data
Serial.println(dataOne); //send data to console
digitalWrite(TXcontrol,Transmit); //prepare to transmit to slaves
mySerial.write(one); //call second slave
digitalWrite(TXcontrol,Recieve); //prepare for second slave data
dataTwo=mySerial.read(); //recieve second slave data
Serial.println(dataOne); //send data to console
}
here is the slave code
#include <SoftwareSerial.h>
//initializing software serial pins
#define RX 10
#define TX 11
#define TXcontrol 3
#define Transmit HIGH
#define Recieve LOW
SoftwareSerial mySerial(RX,TX);
int potpin=1; //analog input pin
int ledpin=12; //blinky pin
int error=4; //error pin, turn on if there is an error
int data=1; //initial data value for potentiometer
int com=500; //communications parameter
int address=501; //slave address for communication purposes
void setup() {
// put your setup code here, to run once:
//initialize all needed pins to output
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin,LOW);
pinMode(error,OUTPUT);
digitalWrite(error,LOW);
pinMode(TXcontrol,OUTPUT);
digitalWrite(TXcontrol,Recieve); //prepare to recieve command from master
mySerial.begin(4800); //start serial communication
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
data=analogRead(potpin); // read in data
if (data > 400){ // reduce data value if too high
data=400;
}
digitalWrite(ledpin,HIGH);
delay(data);
digitalWrite(TXcontrol,Recieve); // prepare to recieve command from master
if (mySerial.available()==0);{ // check to see data coming in
digitalWrite(error,HIGH);
delay(500);
}
com=mySerial.read(); // read in value from serial stream
if (com==address){ // check to see if master is calling this slave
digitalWrite(TXcontrol,Transmit); // allow transmission
mySerial.write(data); // send data to master
}
digitalWrite(TXcontrol,Recieve); // set serial port to recieve
digitalWrite(error,LOW); // reset error pin to off
com=500;
digitalWrite(ledpin,LOW);
delay(data);
}
Just found out this network doesn't work, my code seems to be wrong but that's not surprising because I'm still learning.