User Tools

Site Tools


en:pc

PC connection

From PC side

The simpliest way is to connect Arduino with PC thru USB cabel and install necessary drivers. After that arduino pretends like serial port device and it is possible to listen to them on appropriate port. For example in Arduino IDE we can open the serial communication by clicking on Serial Monitor icon in right-top corner.

In general after installing the drivers we have open door to arduino and we communicate with it using different intermediators. Eg. in Java rxtx library can serves for this purpose.
Arduino+Java

Much more different solutions can be found on Arduino Playground


From Arduino side

Firs step is to begin with serial communication.

Serial.begin(speed, config);

The Metod has two parametrs. First argument is the connection speed (bits per second) and it is necessary. Second parametr is optional, there can be set number of transmitted bits (in one packet), parity and number of stop bits. Default value is 8N1, which means 8 bits without parity and with one stopbite. A more detailed description is here.

Writing

For writing on serial link it serves two main functions:

Reading

If we send data to arduino it is at first stored into buffer. To get the actual number of bytes in the buffer:

Serial.available(); 

For reading from buffer:

Serial.read();

It returns the first from buffer (first in-first out) and delete it's position in buffer so the whole buffer queue moves forward.

Next important function is method SerialEvent(). This method is invoked when new data arrives on serial port. So it serves for handling events on serial port.

Example of communication on serial link:

#include <DHT.h>
#include <Time.h>
#define DHTInput 6
#define TRIAK 3
#define COTRIAK 4
#define CO2_input A3
#define DHTTYPE DHT11
 
DHT dht(DHTInput, DHTTYPE);
 
void setup() {
dht.begin();
pinMode(TRIAK, OUTPUT);
pinMode(COTRIAK, OUTPUT);
pinMode(CO2_input, INPUT);
pinMode(DHTInput, INPUT);
Serial.begin(9600);
}
 
float temp;
float CO2;
boolean triakOn;
boolean triakCOOn;
 
void loop() {
  temp=dht.readTemperature();
  CO2=CO2_read(CO2_input);
 
  Serial.print(temp);
  Serial.print("    CO2:");
  Serial.print(CO2);
  Serial.print("    Ventilace:");
  Serial.print(triakOn);
  Serial.print("    CO2_ON:");
  Serial.print(triakCOOn);
  Serial.print("    cas:");
  Serial.println(now());
  delay(2000);
}
 
float CO2_read(int pin){
float value=((double)analogRead(pin)*5000)/1024;
return value; 
}
 
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////SERIAL COMUNICATION/////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void serialEvent(){
time_t pctime;
while (Serial.available() > 0){
char inByte=Serial.read();
 
if((char)inByte=='R'){
if(!triakOn){
digitalWrite(TRIAK, HIGH);
triakOn=1;
Serial.println("ON VENTILACE");
}else{
digitalWrite(TRIAK, LOW);
triakOn=0;
Serial.println("OFF VENTILACE");
}
}
 
if((char)inByte=='S'){
if(!triakCOOn){
digitalWrite(COTRIAK, HIGH);
triakCOOn=1;
Serial.println("ON CO2");
}else{
digitalWrite(COTRIAK, LOW);
triakCOOn=0;
Serial.println("OFF CO2");
}
}
 
}
}

Goal of this program is to monitor the temperature and CO2 level and also signalize if fan or gas bomb valve with CO2 is open. If chracter 'S' is send to arduino valve change it's state (open/close) and sending character 'R' one can switch on or off the fan.

Example of output:

26.00    CO2:4487.30    Ventilace:0    CO2_ON:0    cas:0
26.00    CO2:4487.30    Ventilace:0    CO2_ON:0    cas:2
26.00    CO2:4482.42    Ventilace:0    CO2_ON:0    cas:4
26.00    CO2:4487.30    Ventilace:0    CO2_ON:0    cas:7
26.00    CO2:4482.42    Ventilace:0    CO2_ON:0    cas:9
26.00    CO2:4482.42    Ventilace:0    CO2_ON:0    cas:11
26.00    CO2:4487.30    Ventilace:0    CO2_ON:0    cas:13
26.00    CO2:4487.30    Ventilace:0    CO2_ON:0    cas:16
26.00    CO2:4487.30    Ventilace:0    CO2_ON:0    cas:18
en/pc.txt · Last modified: 2018/01/29 10:12 (external edit)