Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <EthernetUdp.h>
  5.  
  6. //MAC Address da placa, configurável
  7. //DE-AD-BE-EF-FE-ED
  8. byte mac[] = {0xAF, 0xBE, 0xCD, 0xDC, 0xEB, 0xFA};
  9.  
  10. //Setando IP da shield
  11. IPAddress ip_placa(192, 168, 0, 185);
  12.  
  13. //IP do servidor 1
  14. IPAddress ip_server1(192, 168, 0 ,10);
  15.  
  16. //IP do servidor 2
  17. IPAddress ip_server2(192, 168, 0, 11);
  18.  
  19. //Protocolo UDP para enviar
  20. EthernetUDP Udp;
  21.  
  22. //LEITURA DO RFID
  23. SoftwareSerial mySerial(68, 69);
  24.  
  25. //NOVO
  26. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  27.  
  28. #define _p1 22
  29. #define _p2 23
  30. #define _solenoide 31
  31.  
  32. unsigned long _ultimaLeitura = 0;
  33.  
  34. //String _cartao;
  35.  
  36. void setup()
  37. {
  38. Ethernet.begin(mac,ip_placa);
  39. Udp.begin(7776);
  40.  
  41. mySerial.begin(9600);
  42.  
  43. //monitor
  44. Serial.begin(9600);
  45.  
  46. //Ativar o pino 4, isso ajuda a gerenciar a fila de pacotes UDP
  47. pinMode(4,OUTPUT);
  48. digitalWrite(4,HIGH);
  49.  
  50. pinMode(_p1,INPUT);
  51. pinMode(_p2,INPUT);
  52. pinMode(_solenoide,OUTPUT);
  53. }
  54.  
  55. void loop()
  56. {
  57. if(digitalRead(_p1) == HIGH)
  58. {
  59. EnviaCartao(ip_server1,LerCartao());
  60. }
  61.  
  62. if(digitalRead(_p2) == HIGH)
  63. {
  64. EnviaCartao(ip_server2,LerCartao());
  65. }
  66.  
  67. // if(mySerial.available())
  68. // {
  69. // int _cartao = mySerial.read();
  70. // Serial.println(_cartao);
  71. // }
  72. }
  73.  
  74. void AcionaSolenoide()
  75. {
  76. digitalWrite(_solenoide,HIGH);
  77. digitalWrite(_solenoide,LOW);
  78. Serial.println("Acionado!");
  79. }
  80.  
  81. String LerCartao()
  82. {
  83. String _cartao;
  84. String _cartaoLido;
  85.  
  86. while(mySerial.available())
  87. {
  88. char _cartaoChar = mySerial.read();
  89. _cartao.concat(_cartaoChar);
  90. }
  91.  
  92. //if(millis() - _ultimaLeitura > 3000)
  93. //{
  94. // _cartao = _cartaoLido;
  95. // _ultimaLeitura = millis();
  96. //}
  97.  
  98. return _cartao;
  99. }
  100.  
  101. void EnviaCartao(IPAddress _ipServidor, String _cartao)
  102. {
  103. if(_cartao != "")
  104. {
  105. Serial.println(_cartao + " - " + _cartao.length());
  106. Udp.beginPacket(_ipServidor,12345);
  107. Udp.print(_cartao);
  108. Udp.endPacket();
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement