Advertisement
Guest User

automacao

a guest
Jun 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include<Wire.h>
  2. #include<LiquidCrystal_I2C.h>
  3. #include<ESP8266WiFi.h> //biblioteca para conexao wifi
  4. #include<WiFiClient.h> //biblioteca cliente para comunicacao com servidor
  5.  
  6. int pino_y = A0; //Pino ligado ao Y do joystick
  7. int angulo; //Armazena o valor do eixo Y
  8.  
  9. //variaveis produto e maquina da producao
  10. const String idProduto = "2";
  11. const String idMaquina = "1";
  12.  
  13. WiFiClient client;
  14.  
  15. LiquidCrystal_I2C lcd(0x3F, 16, 2);
  16.  
  17. //ip e porta servidor
  18. IPAddress server(192,168,1,4);
  19. int portaServer = 27015;
  20.  
  21. //rede e senha wifi
  22. const char ssid[] = "Oi 6784";
  23. const char psw[] = "kiki95##$";
  24.  
  25. //variaveis para registrar passagem/contagem caixa
  26. int contaCx = 0;
  27. int ang_min = 100;
  28. int ang_max = 140;
  29. boolean passou = false;
  30.  
  31. //variaveis botao
  32. int BT1 = 15;
  33. boolean bt1 = LOW;
  34.  
  35. void setup() {
  36. lcd.init();
  37. lcd.backlight();
  38. conectaWifi();
  39. delay(1000);
  40. conectaServidor();
  41. delay(1000);
  42. }
  43.  
  44. void loop() {
  45. if(WiFi.status() == WL_CONNECTED){
  46. if (client.connected()) {
  47. telaFixa();
  48. telaVariavel();
  49. calculaAngulo();
  50. verificaPassagemCaixa();
  51. botao();
  52. } else {
  53. lcd.clear();
  54. lcd.setCursor(2, 0);
  55. lcd.print("SERVIDOR OFF");
  56. client.stop();
  57. conectaServidor();
  58. lcd.clear();
  59. }
  60. } else {
  61. conectaWifi();
  62. lcd.clear();
  63. }
  64. yield();
  65. }
  66.  
  67. void telaVariavel() {
  68. lcd.setCursor(11, 1);
  69. lcd.print(contaCx);
  70. }
  71.  
  72. void telaFixa() {
  73. lcd.setCursor(0, 0);
  74. lcd.print("EMPRESA PROD.");
  75. lcd.setCursor(0, 1);
  76. lcd.print("PRODUZIDO: ");
  77. }
  78.  
  79. void verificaPassagemCaixa() {
  80. if(angulo > ang_max){
  81. if(passou == false){
  82. passou = true;
  83. }
  84. } else {
  85. if(angulo < ang_min){
  86. if(passou == true){
  87. contaCx++;
  88. client.println(idProduto + ";" + idMaquina);
  89. passou = false;
  90. }
  91. }
  92. }
  93. }
  94.  
  95. int calculaAngulo() {
  96. angulo = analogRead(pino_y);
  97. angulo = map(angulo, 0, 1023, 1, 180);
  98. return angulo;
  99. }
  100.  
  101. void conectaWifi()
  102. {
  103. lcd.setCursor(4, 0);
  104. lcd.print("WIFI OFF");
  105. WiFi.begin(ssid, psw);
  106. while (WiFi.status() != WL_CONNECTED) //substituir por if se possivel
  107. {
  108. delay(500);
  109. lcd.setCursor(0, 1);
  110. lcd.print("CONECTANDO...");
  111. }
  112. }
  113.  
  114. void conectaServidor() {
  115. client.connect(server, portaServer);
  116. }
  117.  
  118. void botao() {
  119. bt1 = !digitalRead(BT1);
  120. if (!bt1) {
  121. if (contaCx > 0) {
  122. client.println("-" + idProduto + ";" + idMaquina);
  123. delay(1500);
  124. contaCx--;
  125. lcd.clear();
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement