User Tools

Site Tools


en:pc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:pc [2018/01/29 10:12] (current)
Line 1: Line 1:
 +===== 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. \\
 +[[http://​playground.arduino.cc/​Interfacing/​Java|Arduino+Java]]
 +
 +Much more different solutions can be found on [[http://​playground.arduino.cc//​Main/​InterfacingWithSoftware|Arduino Playground]] ​
 +
 +----
 +
 +==== From Arduino side ====
 +
 +Firs step is to begin with serial communication.
 +<file java>
 +Serial.begin(speed,​ config);
 +</​file> ​
 +
 +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 [[http://​arduino.cc/​en/​Serial/​Begin|here]].
 +
 +**Writing**
 +
 +For writing on serial link it serves two main functions: ​
 +  * [[http://​arduino.cc/​en/​Serial/​Print|Serial.write()]] -this function sends it's data input in binary form  ​
 +  * [[http://​arduino.cc/​en/​Serial/​Print|Serial.print()]] -usualy used when printing ASCI characters.  ​
 +
 +**Reading**
 +
 +If we send data to arduino it is at first stored into buffer.
 +To get the actual number of bytes in the buffer:
 +<file java>
 +Serial.available(); ​
 +</​file>​
 +For reading from buffer:
 +<file java>
 +Serial.read();​
 +</​file>​
 +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:
 +<file java>
 +#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");
 +}
 +}
 + 
 +}
 +}
 +
 +</​file>​
 +
 +Goal of this program is to monitor the temperature and CO<​sub>​2</​sub>​ level and also signalize if fan or gas bomb valve with CO<​sub>​2</​sub>​ 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)