Vous êtes sur la page 1sur 28

Universidade Federal de Uberlndia

FEMEC42082
Rede Industriais
Relatrio Modulo 2

Ana Lcia 11111EEL073


Luc Duga 11321EMT023
Prof. Dr. J. Jean-Paul Z.de S. Tavares

Uberlndia, 7 de janeiro de 2015

Sumrio
1 Introduo

2 Exerccio 1

3 Exerccio 2

4 Exerccio 3

5 Exerccio 4

6 Exerccio Extra

7 Concluso

10

8 Referncias Bibliogrficas

10

9 Anexos
9.1 Cdigo fonte Exerccio 1
9.1.1 Mestre . . . . . .
9.1.2 Escravo Serial . .
9.1.3 Escravo I2C . . .
9.2 Cdigo fonte Exerccio 2
9.3 Cdigo fonte Exerccio 3
9.3.1 Mestre 1 . . . . .
9.3.2 Mestre 2 . . . . .
9.3.3 Escravo 1 . . . . .
9.3.4 Escravo 2 . . . . .
9.4 Cdigo fonte Exerccio 4

11
11
11
13
14
15
17
17
20
23
26
27

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

Lista de Figuras
1
2
3
4
5

Exerccio Mestre-escravo. . . . .
Exerccio Token ring. . . . . . .
Exerccio Multi Mestre-escravo.
Exerccio Arduino Yun. . . . . .
Exerccio Token Passing. . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

5
6
7
8
9

Introduo

Apresentar-se-o formas de comunicao entre dispositivos em um barramento,


bem como sua implementao em exercicios. Entre estas formas de comunicao tem-se os protocolos: mestre-escravo, token ring, token passing, multimestre-escravo e ainda a comunicao como cliente-servidor.
Protocolo Mestre-escravo:
Apenas a estao mestre detm o direito de transmisso, centralizando o
comando. O direito de acesso ao meio distribudo por tempo limitado
pela estao mestre s demais, que so denominadas estaes escravas.
Todas as trocas de dados ocorrem apenas entre mestres e escravos. A estao mestre realiza varredura cclica em cada estao escrava, verificando
se alguma delas quer transmitir dados. As estaes escravas respondem ao
mestre com um formato de quadro especfico para informar se querem ou
no enviar dados. A configurao de protocolo mestre escravo bastante
utilizada em sistemas de controle lgico programado onde o controlador
o mestre e os escravos so sensores e atuadores microprocessados.
Protocolo Token-passing:
Sistemas com comando distribudo persistem a definio de mais de uma
estao com direito de acesso ao meio fsico. O protocolo token-passing
(passagem de ficha) transmite o direito de acesso ao meio passando uma
ficha entre as vrias estaes, que trocam dados entre si, sem intermedirias. A figura a seguir ilustra uma configurao de rede token bus que o
barramento com passagem de linha.
Cada estao pode reter o token por tempo limitado, aps o qual envia
o token para estao de maior prioridade. Quando a rede iniciada, o
protocolo Token passing bus cria um anel lgico nas estaes ligadas ao
barramento e define uma prioridade para cada uma delas. Quanto maior
o nmero definido para cada estao, mais elevada ser sua profundidade.
Uma variao dessa tcnica o protocolo Token ring, onde utilizada uma
rede com topologia, um anel no lugar do barramento.
Cada tipo de comunicao ser mostrado em um exerccio utilizando arduinos, com exceo do token passing, que se apresenta no exerccio extra.
O ltimo exerccio apresenta o Arduino Yn como um Web Server.

Exerccio 1

O exerccio consiste em utilizar um Arduino como mestre comunicando com


dois Arduinos escravos. Deve utilizar as portas de comunicao serial RX/TX
4

em um Arduino escravo e a comunicao I2C SDA/SCL para o outro escravo.


Um escravo medir a temperatura atravs do sensor LM35, enquanto que o outro escravo ser responsvel por ligar/desligar os LEDs.
A sequncia das trocas de mensagens :
1. Mestre solicita o valor da temperatura e aguarda resposta.
2. Escravo 1 recebe o comando e envia a temperatura;
3. Mestre envia o comando para o escravo 2;
4. Escravo 2 executa o comando recebido e retorna o status;
5. Mestre imprime o status no Serial Monitor.

Figura 1: Exerccio Mestre-escravo.

Exerccio 2

O exerccio consiste em utilizar 3 Arduinos dispostos em forma de anel para


comunicao serial utilizando as portas RX/TX. Nesta topologia o token re5

presentado pelo canal vazio, e cada dispositivo s pode enviar uma mensagem
se seu canal de recebimento estiver vazio. Para este exerccio so utilizados 3
botes e 3 LEDs. Cada boto e LED est conectado a um Arduino. Ao pressionar um boto uma mensagem deve ser enviada para acionar o LED do Arduino
que se encontra na posio anterior a sua no anel.

Figura 2: Exerccio Token ring.

Exerccio 3

O exerccio consiste em utilizar dois Arduinos como mestre, comunicando-se


entre si via rede Ethernet UDP/IP. Estes Arduinos apenas se comunicam com
o(s) escravo(s) se possurem o token. Cada mestre se comunica com os dois
escravos, um via porta serial e o outro via I2C.
Um escravo responsvel por medir a temperatura e o outro por acionar os
LEDs. Esta temperatura deve ser enviada sempre que solicitada pelo mestre.
O mestre tambm aciona as sadas digitais do escravo responsvel pelos LEDs.
O token passado para o outro mestre que solicita o status do escravo com
6

os LEDs e escreve na tela do Serial Monitor. Se um boto for acionado neste


escravo, o mestre deve enviar um comando para o outro escravo acender o LED
do Arduino.

Figura 3: Exerccio Multi Mestre-escravo.

Exerccio 4

O exerccio consiste em utilizar um Arduino Yn com um Web server para colher a temperatura de um sensor LM35 e apresenta-la na pgina Web, alm do
status de algumas sadas digitais.

Figura 4: Exerccio Arduino Yun.

Exerccio Extra

O exerccio extra semelhante ao exerccio 2, porm a comunicao feita por


rede Ethernet UDP/IP. Os Arduinos esto conectados ao barramento, mas um
anel virtual utilizado para a passagem do token. O token nesta topologia
representado por uma mensagem que enviada de um Arduino no final da
execuo do programa.

Figura 5: Exerccio Token Passing.


No fizemos o cdigo deste exerccicio.

Concluso

A implementao dos protocolos nos exerccios apresentados facilita a compreenso destas formas de comunicao, pois podemos verificar seu funcionamento.

Referncias Bibliogrficas

TAVARES, J. J. Z. S.; FERREIRA, M. V. M; Mdulo II - Protocolo Mestre-escravo,


Token Ring, Token Passing, Multi Mestre-escravo e Cliente-servidor
www.arduino.cc
http://arduino.cc/en/Guide/ArduinoYun
http://arduino.cc/en/Tutorial/TemperatureWebPanel

10

Anexos

9.1
9.1.1

Cdigo fonte Exerccio 1


Mestre

// MESTRE
# include <Wire . h>
const i n t A d r e s s S l a v e = 3 ;
i n t L1 ;
i n t L2 ;
i n t L3 ;
i n t temperatura ;
int n ;
int s t a t ;
int estado [ 3 ] ;
void setup ( ) {
S e r i a l . begin ( 9 6 0 0 ) ;
S e r i a l 1 . begin ( 9 6 0 0 ) ;
Wire . begin ( ) ;
}

// I n i c i a com . S e r i a l > PC
// I n i c i a com . S e r i a l > Escravo 1
// I n i c i a com . I 2 c > Escravo 2

void loop ( ) {
// S o l i c i t a r temperatura
S e r i a l 1 . p r i n t ( T ) ;
delay ( 5 0 0 ) ;
// Recuperar temperatura
while ( S e r i a l 1 . a v a i l a b l e ( ) ) {
temperatura = S e r i a l 1 . read ( ) ;
Serial1 . flush ( ) ;
}
S e r i a l . p r i n t ( " temperatura : " ) ;
S e r i a l . p r i n t l n ( temperatura ) ;
// D e c i d i r Led a l i g a r
i f ( temperatura <28)
{
L1 =1;
L2 =0;
L3 =0;
}
e l s e i f ( temperatura >=28 && temperatura <30)
{
L1 =1;
L2 =1;
L3 =0;

11

}
e l s e i f ( temperatura >=32)
{
L1 =1;
L2 =1;
L3 =1;
}
// Mandar l i g a r os l e d s
Ligar ( ) ;
// Recuperar os s t a t u s
Recuperar ( ) ;

// Imprimir S t a t u s
estado [0]= s t a t /100;
e s t a d o [ 1 ] = ( s t a t 100* e s t a d o [ 0 ] ) / 1 0 ;
e s t a d o [ 2 ] = ( s t a t 100* e s t a d o [0] 10* e s t a d o [ 1 ] ) ;
// S e r i a l . p r i n t ( e s t a d o [ 0 ] ) ;
// S e r i a l . p r i n t ( e s t a d o [ 1 ] ) ;
// S e r i a l . p r i n t l n ( e s t a d o [ 2 ] ) ;
S e r i a l . p r i n t ( " Led 1 e s t a " ) ;
i f ( estado [0]==0) {
Serial . println ( " desligado " ) ;
}
else {
Serial . println ( " ligado " ) ;
}
S e r i a l . p r i n t ( " Led 2 e s t a " ) ;
i f ( estado [1]==0) {
Serial . println ( " desligado " ) ;
}
else {
Serial . println ( " ligado " ) ;
}
S e r i a l . p r i n t ( " Led 3 e s t a " ) ;
i f ( estado [2]==0) {
Serial . println ( " desligado " ) ;
}
else {
Serial . println ( " ligado " ) ;
}
delay ( 1 0 0 0 ) ;
}

12

void L i g a r ( ) {
Wire . beginTransmission ( A d r e s s S l a v e ) ;
Wire . w r i t e ( L1 ) ;
Wire . w r i t e ( L2 ) ;
Wire . w r i t e ( L3 ) ;
Wire . endTransmission ( ) ;
}
i n t Recuperar ( ) {
Wire . requestFrom ( AdressSlave , 1 ) ;
while ( Wire . a v a i l a b l e ( ) ) {
// o e s c r a v o pode e n v i a r menos do que o
solicitado
s t a t = Wire . read ( ) ; // r e c e i v e a byte as c h a r a c t e
}
// S e r i a l . p r i n t l n ( s t a t ) ;
return s t a t ;
}

Algoritmo Arduino
9.1.2

Escravo Serial

const i n t s e n s o r = A4 ;
i n t temp = 0 ;
i n t cmd ;

// LM35 na p o r t a A4

void setup ( ) {
S e r i a l . begin ( 9 6 0 0 ) ;
}
void loop ( ) {
while ( S e r i a l . a v a i l a b l e ( ) >0) {
cmd = S e r i a l . read ( ) ;
}
i f (cmd== T | | cmd == t ) {
temp =round ( ( ( 4 . 8 * analogRead ( s e n s o r ) ) /1023) * 100) ;
S e r i a l . w r i t e ( temp ) ;
}
}

Algoritmo Arduino

13

9.1.3

Escravo I2C

// ESCRAVO I2C
# include <Wire . h>
const i n t myAdress = 3 ;
const i n t Led1 = 4 ;
const i n t Led2 = 3 ;
const i n t Led3 = 2 ;
boolean e s t a d o ;
i n t cmd [ 3 ] ;
void setup ( ) {
Wire . begin ( myAdress ) ;
pinMode ( Led1 ,OUTPUT) ;
pinMode ( Led2 ,OUTPUT) ;
pinMode ( Led3 ,OUTPUT) ;
Wire . onReceive ( Reception ) ;
Wire . onRequest ( Request ) ;
}
void loop ( ) {
delay ( 1 0 0 ) ;
}

// Define o endereco do e s c r a v o como 3

//
//
//
//
//

I n i c i a a comunicacao I2C
Configura a p o r t a 4 como s a i d a
Configura a p o r t a 3 como s a i d a
Configura a p o r t a 2 como s a i d a
Chamda quando r e c e p c a o

// Espera enquanto nao r e c e b e

void Reception ( i n t Quanto ) {


i n t n=0;
while ( Wire . a v a i l a b l e ( ) >0) {
cmd [ n ] = Wire . read ( ) ;
// Recebe o comando 1 , 2 ou 3
n++;
}
i f (cmd[ 0 ] = = 0 ) {
d i g i t a l W r i t e ( Led1 ,LOW) ;
}
else {
d i g i t a l W r i t e ( Led1 , HIGH) ;
}
i f (cmd[ 1 ] = = 0 ) {
d i g i t a l W r i t e ( Led2 ,LOW) ;
}
else {
d i g i t a l W r i t e ( Led2 , HIGH) ;
}
i f (cmd[ 2 ] = = 0 ) {
d i g i t a l W r i t e ( Led3 ,LOW) ;
}
else {
d i g i t a l W r i t e ( Led3 , HIGH) ;

14

}
}
void Request ( )
{
i n t L1 , L2 , L3 ;
i f ( d i g i t a l R e a d ( Led1 ) ==HIGH) {
L1 =1;
}
else
{
L1 =0;
}
i f ( d i g i t a l R e a d ( Led2 ) ==HIGH) {
L2 =1;
}
else
{
L2 =0;
}
i f ( d i g i t a l R e a d ( Led3 ) ==HIGH) {
L3 =1;
}
else
{
L3 =0;
}
i n t s t a t =100*L1+10*L2+L3 ;
Wire . w r i t e ( s t a t ) ;
}

Algoritmo Arduino

9.2

Cdigo fonte Exerccio 2

# include < S o f t w a r e S e r i a l . h>


// Arduino n o 1
const byte MeuID=B00010000 ;
const byte I D _ s l a v e=B01000000 ;
//
/*
// Arduino n o 2
c o n s t byte MeuID=B00100000 ;
c o n s t byte I D _ s l a v e=B00010000 ;
//

15

// Arduino n o 3
c o n s t byte MeuID=B01000000 ;
c o n s t byte I D _ s l a v e=B00100000 ;
//
*/
// C r i a c a o de um s e r i a l v i r t u a l para p e r m i t i r o monitoramento v i a UBSB
S o f t w a r e S e r i a l Ring ( 1 0 , 1 1 ) ; // RX, TX

const i n t Led =2;


const i n t botao = 3 ;
byte
byte
byte
byte

msg ;
cmd ;
endereco ;
estado_led ;

void setup ( ) {
pinMode ( Led , OUTPUT) ;
pinMode ( botao , INPUT ) ;
S e r i a l . begin ( 9 6 0 0 ) ;
Ring . begin ( 9 6 0 0 ) ;
}
void loop ( )
{
cmd=0;
i f ( d i g i t a l R e a d ( botao ) ==HIGH)
{
cmd = I D _ s l a v e ;
cmd++;
delay ( 2 5 0 ) ;
}
// Para s a b e r s e o c a n a l e s t v a z i o e e l e pode t r a n s m i t i r
i f ( Ring . a v a i l a b l e ( ) ==0 && cmd>0) // no r e c e b e nemhum dados
{ // PODE TRANSMITIR
Ring . w r i t e (cmd ) ;
S e r i a l . p r i n t ( "cmd=" ) ;
S e r i a l . p r i n t l n (cmd ) ;
}
e l s e i f ( Ring . a v a i l a b l e ( ) >0) // s e r e c e b e r alguma mensagem v i a
serial
{
msg = Ring . read ( ) ; // armazena o byte em msg
// d el ay ( 1 0 0 ) ;
S e r i a l . p r i n t ( " msg=" ) ;

16

S e r i a l . p r i n t l n ( msg ) ;
endereco = msg & B11110000 ;
e s t a d o _ l e d = msg & B00001111 ;
i f ( endereco==MeuID )
{
i f ( e s t a d o _ l e d >0)
{
d i g i t a l W r i t e ( Led , ! d i g i t a l R e a d ( Led ) ) ;
}
}
else
{
Ring . w r i t e ( msg ) ;
// d el ay ( 2 5 0 ) ;
}
}
}

Algoritmo Arduino

9.3
9.3.1

Cdigo fonte Exerccio 3


Mestre 1

/*
MODULO 2
EXERCICIO 3
PROGRAMA MESTRE 1 ( L e i t u r a temperatura e l i g a r l e d )
*/
# include <SPI . h>
# include <E t h e r n e t . h>
# include <EthernetUdp . h>
# include <Wire . h>
# include < S o f t w a r e S e r i a l . h>
# define A d r e s s S l a v e 4
/* I n i c i a l i z a o S e r i a l v i r t u a l */
S o f t w a r e S e r i a l Escravo ( 1 0 , 11) ; // RX, TX
/* */
/* I n i c i a l i z a o para e t h e r n e t */
EthernetUDP Udp ;
byte MAC[ ] = { 0 x90 , 0xA2 , 0xDA, 0x00 , 0x64 , 0x50 } ;

17

byte MyIP [ ] = { 1 9 2 , 1 6 8 , 1 , 1 2 5 } ;
byte OtherIP [ ] = { 1 9 2 , 1 6 8 , 1 , 2 5 0 } ;
unsigned i n t p o r t = 8888;
char p a c k e t B u f f e r [ UDP_TX_PACKET_MAX_SIZE ] ;
/* */
/* Declara o das v a r i a v e i s */
i n t TOKEN=0;
i n t temperatura ;
i n t L1 ;
i n t L2 ;
i n t L3 ;
/* */

void setup ( )
{
E t h e r n e t . begin (MAC, MyIP ) ;
Udp . begin ( p o r t ) ;
S e r i a l . begin ( 9 6 0 0 ) ;
Escravo . begin ( 9 6 0 0 ) ;
Wire . begin ( ) ;
pinMode ( 1 3 ,OUTPUT) ;
d i g i t a l W r i t e ( 1 3 ,LOW) ;
}
void loop ( )
{
S e r i a l . p r i n t ( "TOKEN=" ) ;
S e r i a l . p r i n t l n (TOKEN) ;
while (TOKEN==0)
{
/* V e r i f i c a r Token */
char p a c k e t S i z e = Udp . p a r s e P a c k e t ( ) ;
i f ( packetSize )
{
Udp . read ( p a c k e t B u f f e r , UDP_TX_PACKET_MAX_SIZE ) ;
i f ( p a c k e t B u f f e r [0]== T )
{
TOKEN=1;
d i g i t a l W r i t e ( 1 3 ,HIGH) ;
S e r i a l . p r i n t l n ( "TOKEN r e c e b i d o " ) ;
}
else

18

{
d i g i t a l W r i t e ( 1 3 ,LOW) ;
}
}
/* */
}
i f (TOKEN==1)
{
Escravo . w r i t e ( t ) ;
while ( Escravo . a v a i l a b l e ( ) >0)
{
i n t temperatura = Escravo . read ( ) ;
}
S e r i a l . p r i n t ( " Temperatura = " ) ;
S e r i a l . p r i n t l n ( temperatura ) ;

i f ( temperatura <=26)
{
S e r i a l . p r i n t l n ( " Temperatura < 26 " ) ;
S e r i a l . p r i n t ( "Cama OFF/ C l o s e t OFF/ Banheiro OFF" ) ;
L1 =0;
L2 =0;
L3 =0;
}
e l s e i f ( temperatura >26&&temperatura <=28)
{
S e r i a l . p r i n t l n ( " Temperatura e n t r e 26 e 28 " ) ;
S e r i a l . p r i n t ( "Cama ON/ C l o s e t OFF/ Banheiro OFF" ) ;
L1 =1;
L2 =0;
L3 =0;
}
e l s e i f ( temperatura >28&&temperatura <=30)
{
S e r i a l . p r i n t l n ( " Temperatura e n t r e 28 e 30 " ) ;
S e r i a l . p r i n t ( "Cama ON/ C l o s e t ON/ Banheiro OFF" ) ;
L1 =1;
L2 =1;
L3 =0;
}
else
{
S e r i a l . p r i n t l n ( " Temperatura acima de 30 " ) ;
S e r i a l . p r i n t ( "Cama ON/ C l o s e t ON/ Banheiro ON" ) ;
L1 =1;
L2 =1;
L3 =1;

19

}
Ligar ( ) ;
/* P a s s a r Token */
Udp . b e gi n Pa c ke t ( OtherIP , p o r t ) ;
Udp . w r i t e ( T ) ;
Udp . endPacket ( ) ;
TOKEN=0;
d i g i t a l W r i t e ( 1 3 ,LOW) ;
S e r i a l . p r i n t l n ( "TOKEN mandado " ) ;
/* */
}
}
void L i g a r ( ) {
Wire . beginTransmission ( A d r e s s S l a v e ) ;
Wire . w r i t e ( L1 ) ;
Wire . w r i t e ( L2 ) ;
Wire . w r i t e ( L3 ) ;
Wire . endTransmission ( ) ;
}

Algoritmo Arduino
9.3.2

Mestre 2

/*
MODULO 2
EXERCICIO 3
PROGRAMA MESTRE 2 ( L e i t u r a s t a t u s l e d e l i g a r com botao )
*/
# include <SPI . h>
# include <E t h e r n e t . h>
# include <EthernetUdp . h>
# include <Wire . h>
# include < S o f t w a r e S e r i a l . h>
# define A d r e s s S l a v e 3
/* I n i c i a l i z a o S e r i a l v i r t u a l */
S o f t w a r e S e r i a l Escravo ( 1 0 , 11) ; // RX, TX
/* */
/* I n i c i a l i z a o para e t h e r n e t */
EthernetUDP Udp ;

20

byte MAC[ ] = { 0 x90 , 0xA2 , 0xDA, 0x00 , 0x64 , 0x44 } ;


byte MyIP [ ] = { 1 9 2 , 1 6 8 , 1 , 2 5 0 } ;
byte OtherIP [ ] = { 1 9 2 , 1 6 8 , 1 , 1 2 5 } ;
unsigned i n t p o r t = 8888;
char p a c k e t B u f f e r [ UDP_TX_PACKET_MAX_SIZE ] ;
/* */
/* Declara o das v a r i a v e i s */
i n t TOKEN=1;
int s t a t ;
int estado [ 2 ] ;
/* */

void setup ( )
{
E t h e r n e t . begin (MAC, MyIP ) ;
Udp . begin ( p o r t ) ;
S e r i a l . begin ( 9 6 0 0 ) ;
Escravo . begin ( 9 6 0 0 ) ;
Wire . begin ( ) ;
pinMode ( 1 3 ,OUTPUT) ;
d i g i t a l W r i t e ( 1 3 ,HIGH) ;
}
void loop ( )
{
S e r i a l . p r i n t ( "TOKEN=" ) ;
S e r i a l . p r i n t l n (TOKEN) ;
while (TOKEN==0)
{
/* V e r i f i c a r Token */
i n t p a c k e t S i z e = Udp . p a r s e P a c k e t ( ) ;
i f ( packetSize )
{
Udp . read ( p a c k e t B u f f e r , UDP_TX_PACKET_MAX_SIZE ) ;
i f ( p a c k e t B u f f e r [0]== T )
{
TOKEN=1;
d i g i t a l W r i t e ( 1 3 ,HIGH) ;
S e r i a l . p r i n t l n ( "TOKEN r e c e b i d o " ) ;
}
else

21

{
d i g i t a l W r i t e ( 1 3 ,LOW) ;
}
}
/* */
}
i f (TOKEN==1)
{
Escravo . w r i t e ( s ) ;
while ( Escravo . a v a i l a b l e ( ) >0)
{
i n t s t a t = Escravo . read ( ) ;
}
i f ( s t a t == b )
{
Mandar ( ) ;
S e r i a l . p r i n t l n ( " Bot o acionado " ) ;
}
else
{
estado [0]= s t a t /100;
e s t a d o [ 1 ] = ( s t a t 100* e s t a d o [ 0 ] ) / 1 0 ;
e s t a d o [ 2 ] = ( s t a t 100* e s t a d o [0] 10* e s t a d o [ 1 ] ) ;
S e r i a l . p r i n t ( " Led 1 e s t a " ) ;
i f ( estado [0]==0)
{
Serial . println ( " desligado " ) ;
}
else
{
Serial . println ( " ligado " ) ;
}
S e r i a l . p r i n t ( " Led 2 e s t a " ) ;
i f ( estado [1]==0)
{
Serial . println ( " desligado " ) ;
}
else
{
Serial . println ( " ligado " ) ;
}
S e r i a l . p r i n t ( " Led 3 e s t a " ) ;
i f ( estado [2]==0)
{
Serial . println ( " desligado " ) ;

22

}
else
{
Serial . println ( " ligado " ) ;
}
}
S e r i a l . p r i n t l n ( " ok " ) ;
/* P a s s a r Token */
Udp . b e gi n Pa c ke t ( OtherIP , p o r t ) ;
Serial . println ( " 1 " ) ;
Udp . w r i t e ( T ) ;
Serial . println ( " 2 " ) ;
Udp . endPacket ( ) ;
Serial . println ( " 3 " ) ;
TOKEN=0;
d i g i t a l W r i t e ( 1 3 ,LOW) ;
S e r i a l . p r i n t l n ( "TOKEN mandado " ) ;
/* */
}
}
void Mandar ( ) {
Wire . beginTransmission ( A d r e s s S l a v e ) ;
Wire . w r i t e ( b ) ;
Wire . endTransmission ( ) ;
}

Algoritmo Arduino
9.3.3

Escravo 1

/*
MODULO 2
EXERCICIO 3
PROGRAMA ESCRAVO 1 ( l i g a r os l e d s ( I2C ) e bot o ( S e r i a l ) )
*/
# include <Wire . h>
# define A d r e s s S l a v e 4
/* Declara o das v a r i a v e i s */
i n t L1 ;
i n t L2 ;
i n t L3 ;
i n t Led1 =4;
i n t Led2 =3;
i n t Led3 =2;

23

i n t cmd [ 3 ] ;
i n t botao =10;
/* */

void setup ( )
{
S e r i a l . begin ( 9 6 0 0 ) ;
Wire . begin ( A d r e s s S l a v e ) ;
Wire . onReceive ( Reception ) ;
pinMode ( Led1 ,OUTPUT) ;
pinMode ( Led2 ,OUTPUT) ;
pinMode ( Led3 ,OUTPUT) ;
pinMode ( botao , INPUT ) ;
}

// Chamda quando r e c e p c a o

void loop ( )
{
while ( S e r i a l . a v a i l a b l e ( ) >0)
{
i n t c = S e r i a l . read ( ) ;
i f ( c== s )
{
Serial . write ( estado ( ) ) ;
}
}
i f ( d i g i t a l R e a d ( botao ) )
{
Serial . write ( b ) ;
delay ( 2 5 0 ) ;
}
}
void Reception ( i n t Quanto )
{
i n t n=0;
while ( Wire . a v a i l a b l e ( ) >0)
{
cmd [ n ] = Wire . read ( ) ;
n++;
}
i f (cmd[ 0 ] = = 0 )
{
d i g i t a l W r i t e ( Led1 ,LOW) ;
}
else
{
d i g i t a l W r i t e ( Led1 , HIGH) ;
}

// Recebe o comando 1 , 2 ou 3

24

i f (cmd[ 1 ] = = 0 )
{
d i g i t a l W r i t e ( Led2 ,LOW) ;
}
else
{
d i g i t a l W r i t e ( Led2 , HIGH) ;
}
i f (cmd[ 2 ] = = 0 )
{
d i g i t a l W r i t e ( Led3 ,LOW) ;
}
else
{
d i g i t a l W r i t e ( Led3 , HIGH) ;
}
}
int estado ( )
{
i n t L1 , L2 , L3 ;
i f ( d i g i t a l R e a d ( Led1 ) ==HIGH) {
L1 =1;
}
else
{
L1 =0;
}
i f ( d i g i t a l R e a d ( Led2 ) ==HIGH) {
L2 =1;
}
else
{
L2 =0;
}
i f ( d i g i t a l R e a d ( Led3 ) ==HIGH) {
L3 =1;
}
else
{
L3 =0;
}
i n t s t a t =100*L1+10*L2+L3 ;
return ( s t a t ) ;
}

Algoritmo Arduino
25

9.3.4

Escravo 2

/*
MODULO 2
EXERCICIO 3
PROGRAMA ESCRAVO 2 ( l i g a r o l e d ( I2C ) e temperatura ( S e r i a l ) )
*/
# include <Wire . h>
# define A d r e s s S l a v e 3
/* Declara o das v a r i a v e i s */
i n t s e n s o r = A0 ;
i n t Led =13;
i n t cmd=0;
int c ;
i n t temperatura ;
/* */

void setup ( )
{
S e r i a l . begin ( 9 6 0 0 ) ;
Wire . begin ( A d r e s s S l a v e ) ;
Wire . onReceive ( Reception ) ;
pinMode ( Led ,OUTPUT) ;
}

// Chamda quando r e c e p c a o

void loop ( )
{
while ( S e r i a l . a v a i l a b l e ( ) >0)
{
c = S e r i a l . read ( ) ;
i f ( c== t )
{
temperatura = round ( ( ( 4 . 8 * analogRead ( s e n s o r ) ) /1023) * 100) ;
S e r i a l . w r i t e ( temperatura ) ;
}
Serial . flush ( ) ;
}
}
void Reception ( i n t Quanto )
{
cmd = 0 ;
while ( Wire . a v a i l a b l e ( ) >0)
{

26

cmd = Wire . read ( ) ;


// Recebe o comando 1 , 2 ou 3
}
i f (cmd== b )
{
d i g i t a l W r i t e ( Led , ! d i g i t a l R e a d ( Led ) ) ;
}
}

Algoritmo Arduino

9.4
//

Cdigo fonte Exerccio 4


I n t e r f a c e web de Temperatura

# include <Bridge . h>


# include <YunServer . h>
# include <YunClient . h>
// L i s t e n on d e f a u l t p o r t 5555 , the webserver on the Yun
// w i l l forward t h e r e a l l the HTTP r e q u e s t s f o r us .
YunServer s e r v e r ;
String startString ;
long h i t s = 0 ;
i n t s e n s o r = A0 ;
void setup ( ) {
S e r i a l . begin ( 9 6 0 0 ) ;
// Bridge s t a r t u p
pinMode ( 1 3 ,OUTPUT) ;
d i g i t a l W r i t e ( 1 3 , LOW) ;
Bridge . begin ( ) ;
d i g i t a l W r i t e ( 1 3 , HIGH) ;

// L i s t e n f o r incoming c o n n e c t i o n only from l o c a l h o s t


// ( no one from the e x t e r n a l network could connect )
server . listenOnLocalhost ( ) ;
s e r v e r . begin ( ) ;
// g e t the time t h a t t h i s s k e t c h s t a r t e d :
Process startTime ;
s t a r t T i m e . runShellCommand ( " date " ) ;
while ( s t a r t T i m e . a v a i l a b l e ( ) ) {
char c = s t a r t T i m e . read ( ) ;
s t a r t S t r i n g += c ;
}

27

}
void loop ( ) {
// Get c l i e n t s coming from s e r v e r
YunClient c l i e n t = s e r v e r . a c c e p t ( ) ;
// There i s a new c l i e n t ?
if ( client ) {
// read the command
S t r i n g command = c l i e n t . r e a d S t r i n g ( ) ;
command . trim ( ) ;
// k i l l whitespace
S e r i a l . p r i n t l n ( command ) ;
// i s " temperature " command?
i f ( command == " temperature " ) {
// g e t the time from the s e r v e r :
P r o c e s s time ;
time . runShellCommand ( " date " ) ;
String timeString = " " ;
while ( time . a v a i l a b l e ( ) ) {
char c = time . read ( ) ;
t i m e S t r i n g += c ;
}
Serial . println ( timeString ) ;
i n t sensorValue = analogRead ( s e n s o r ) ;
// c o n v e r t the r e a d i n g t o m i l l i v o l t s :
f l o a t v o l t a g e = sensorValue * (5000/ 1024) ;
// c o n v e r t the m i l l i v o l t s t o temperature c e l s i u s :
f l o a t temperature = ( v o l t a g e 500) / 1 0 ;
// p r i n t the temperature :
c l i e n t . p r i n t ( " Temperatura : " ) ;
c l i e n t . p r i n t l n ( temperature ) ;

}
// Close c o n n e c t i o n and f r e e r e s o u r c e s .
c l i e n t . stop ( ) ;
}
delay ( 5 0 ) ; // P o l l every 50ms
}

Algoritmo Arduino

28

Vous aimerez peut-être aussi