Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<Wire.h>
- #include<LiquidCrystal_I2C.h>
- #include<ESP8266WiFi.h> //biblioteca para conexao wifi
- #include<WiFiClient.h> //biblioteca cliente para comunicacao com servidor
- int pino_y = A0; //Pino ligado ao Y do joystick
- int angulo; //Armazena o valor do eixo Y
- //variaveis produto e maquina da producao
- const String idProduto = "2";
- const String idMaquina = "1";
- WiFiClient client;
- LiquidCrystal_I2C lcd(0x3F, 16, 2);
- //ip e porta servidor
- IPAddress server(192,168,1,4);
- int portaServer = 27015;
- //rede e senha wifi
- const char ssid[] = "Oi 6784";
- const char psw[] = "kiki95##$";
- //variaveis para registrar passagem/contagem caixa
- int contaCx = 0;
- int ang_min = 100;
- int ang_max = 140;
- boolean passou = false;
- //variaveis botao
- int BT1 = 15;
- boolean bt1 = LOW;
- void setup() {
- lcd.init();
- lcd.backlight();
- conectaWifi();
- delay(1000);
- conectaServidor();
- delay(1000);
- }
- void loop() {
- if(WiFi.status() == WL_CONNECTED){
- if (client.connected()) {
- telaFixa();
- telaVariavel();
- calculaAngulo();
- verificaPassagemCaixa();
- botao();
- } else {
- lcd.clear();
- lcd.setCursor(2, 0);
- lcd.print("SERVIDOR OFF");
- client.stop();
- conectaServidor();
- lcd.clear();
- }
- } else {
- conectaWifi();
- lcd.clear();
- }
- yield();
- }
- void telaVariavel() {
- lcd.setCursor(11, 1);
- lcd.print(contaCx);
- }
- void telaFixa() {
- lcd.setCursor(0, 0);
- lcd.print("EMPRESA PROD.");
- lcd.setCursor(0, 1);
- lcd.print("PRODUZIDO: ");
- }
- void verificaPassagemCaixa() {
- if(angulo > ang_max){
- if(passou == false){
- passou = true;
- }
- } else {
- if(angulo < ang_min){
- if(passou == true){
- contaCx++;
- client.println(idProduto + ";" + idMaquina);
- passou = false;
- }
- }
- }
- }
- int calculaAngulo() {
- angulo = analogRead(pino_y);
- angulo = map(angulo, 0, 1023, 1, 180);
- return angulo;
- }
- void conectaWifi()
- {
- lcd.setCursor(4, 0);
- lcd.print("WIFI OFF");
- WiFi.begin(ssid, psw);
- while (WiFi.status() != WL_CONNECTED) //substituir por if se possivel
- {
- delay(500);
- lcd.setCursor(0, 1);
- lcd.print("CONECTANDO...");
- }
- }
- void conectaServidor() {
- client.connect(server, portaServer);
- }
- void botao() {
- bt1 = !digitalRead(BT1);
- if (!bt1) {
- if (contaCx > 0) {
- client.println("-" + idProduto + ";" + idMaquina);
- delay(1500);
- contaCx--;
- lcd.clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement