· 6 years ago · Nov 29, 2019, 02:16 PM
1
2#include <ESP32Servo.h>
3
4Servo myservo; // create servo object to control a servo
5// 16 servo objects can be created on the ESP32
6
7int pos = 0; // variable to store the servo position
8// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
9int servoPin = 2;
10
11#include <WiFi.h>
12#include <WiFiMulti.h>
13#include <HTTPClient.h>
14#include <SPI.h>
15#include <MFRC522.h>
16#include <Wire.h>
17#include <LiquidCrystal_I2C.h>
18#include "esp32-hal-ledc.h"
19
20#define ID "7A 4C 90 1A" // cambiar de vuelta por 7A 4C 90 1A
21#define LedVerde 13
22#define LedVermelho 12
23#define buzzer 15
24#define SS_PIN 14
25#define RST_PIN 27
26//#define COUNT_LOW 0
27//#define COUNT_HIGH 8888
28
29#define TIMER_WIDTH 16
30
31String inString = ""; // string to hold input
32int i;
33int carli;
34WiFiMulti WiFiMulti;
35HTTPClient ask;
36const char* ssid = "JuanCortes"; //Wifi SSID
37const char* password = "juanelo1"; //Wifi Password
38const char* apiKeyIn = "OLAkTHqZnme3kazq7RXpAo53O0ms6D1h"; // API KEY IN
39const unsigned int writeInterval = 2000; // write interval (in ms)
40
41// ASKSENSORS API host config
42const char* host = "api.asksensors.com"; // API host name
43const int httpPort = 80; // port
44
45MFRC522 mfrc522(SS_PIN, RST_PIN); // define os pinos de controle do modulo de leitura de cartoes RFID
46LiquidCrystal_I2C lcd(0x3F, 16, 2); // define informacoes do lcd como o endereço I2C (0x27) e tamanho do mesmo
47
48void setup(){
49 myservo.setPeriodHertz(50); // standard 50 hz servo
50 myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object
51 // open serial
52 Serial.begin(115200);
53 Serial.println("*****************************************************");
54 Serial.println("********** Program Start : Connect ESP32 to AskSensors.");
55 Serial.println("Wait for WiFi... ");
56
57
58 // connecting to the WiFi network
59 WiFiMulti.addAP(ssid, password);
60 while (WiFiMulti.run() != WL_CONNECTED) {
61 Serial.print(".");
62 delay(500);
63 }
64 // connected
65 Serial.println("WiFi connected");
66 Serial.println("IP address: ");
67 Serial.println(WiFi.localIP());
68
69 pinMode(LedVerde, OUTPUT);
70 pinMode(LedVermelho, OUTPUT);
71 pinMode(buzzer, OUTPUT);
72// ledcSetup(1, 50, TIMER_WIDTH); // channel 1, 50 Hz, 16-bit width
73// ledcAttachPin(2, 1); // GPIO 22 assigned to channel 1
74
75 SPI.begin();
76 lcd.begin();
77 mfrc522.PCD_Init();
78
79 Serial.println("RFID + ESP32");
80 Serial.println("Pasar tarjeta RFID para revisar.");
81
82}
83
84
85void loop(){
86
87 // Use WiFiClient class to create TCP connections
88 WiFiClient client;
89 String address;
90 // RFID
91
92 lcd.home(); // bota o cursor do lcd na posicao inicial
93 lcd.print("Acerque su"); // imprime na primeira linha a string "Aguardando"
94 lcd.setCursor(0,1); // seta o cursor para a segunda linha
95 lcd.print("tarjeta RFID"); // mostra na tela a string "Leitura RFID"
96
97 if ( ! mfrc522.PICC_IsNewCardPresent()) {
98 return; // se nao tiver um cartao para ser lido recomeça o void loop
99 }
100 if ( ! mfrc522.PICC_ReadCardSerial()) {
101 return; //se nao conseguir ler o cartao recomeça o void loop tambem
102 }
103
104 String conteudo = ""; // cria uma string
105
106 Serial.print("id da tag :"); //imprime na serial o id do cartao
107
108 for (byte i = 0; i < mfrc522.uid.size; i++){ // faz uma verificacao dos bits da memoria do cartao
109 //ambos comandos abaixo vão concatenar as informacoes do cartao...
110 //porem os 2 primeiros irao mostrar na serial e os 2 ultimos guardarao os valores na string de conteudo para fazer as verificacoes
111 Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
112 Serial.print(mfrc522.uid.uidByte[i], HEX);
113 conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
114 conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
115 }
116Serial.println();
117 conteudo.toUpperCase(); // deixa as letras da string todas maiusculas
118
119
120 if (conteudo.substring(1) == ID){ // verifica se o ID do cartao lido tem o mesmo ID do cartao que queremos liberar o acesso
121
122 digitalWrite(LedVerde, LOW); // ligamos o led verde
123 lcd.clear(); // limpamos oque havia sido escrito no lcd
124 lcd.print("AGREGUE BASURA"); // informamos pelo lcd que a tranca foi aberta
125 for (pos = 180; pos <= 60; pos += 2) { // goes from 0 degrees to 180 degrees
126 // in steps of 1 degree
127 myservo.write(pos); // tell servo to go to position in variable 'pos'
128 delay(200); // waits 15ms for the servo to reach the position
129 }
130 for (pos = 180; pos >= 0; pos -= 2) { // goes from 180 degrees to 0 degrees
131 myservo.write(pos); // tell servo to go to position in variable 'pos'
132 delay(200); // waits 15ms for the servo to reach the position
133 }
134
135 for(byte s = 5; s > 0; s--){ //vai informando ao usuario quantos segundos faltao para a tranca ser fechada
136 lcd.setCursor(8,1);
137 lcd.print(s);
138 delay(200);
139 }
140
141 digitalWrite(LedVerde, LOW); // e desliga o led
142 lcd.clear(); // limpa os caracteres q estao escritos no lcd
143
144 }else{ // caso o cartao lido nao foi registrado
145
146 digitalWrite(LedVerde, HIGH); // vamos ligar o led vermelho
147 for (pos = 0; pos <= 0; pos += 2) { // goes from 0 degrees to 180 degrees
148 // in steps of 1 degree
149 myservo.write(pos); // tell servo to go to position in variable 'pos'
150 delay(200); // waits 15ms for the servo to reach the position
151 }
152
153 for(byte s = 5; s > 0; s--){ // uma contagem / espera para poder fazer uma nova leitura
154
155 lcd.clear(); // limpa as informacoes que estao na tela
156 lcd.home(); // nota na posicao inicial
157 lcd.print("REGISTRE SU ID"); // infoma ao usuario que ele nao tem acesso
158 lcd.setCursor(8,1); // coloca o cursor na coluna 8 da linha 2
159 lcd.print(s);
160 delay(400);// informa quantos segundos faltam para pode fazer uma nova leitura
161
162
163
164 }
165 lcd.clear(); // limpa as informacoes do lcd
166 }
167address = conteudo;
168int carli = (int) strtol( &address[1], NULL, 16);
169Serial.print("************** Address: ");
170Serial.print(address);
171Serial.println(" ID ");
172
173 if (!client.connect(host, httpPort)) {
174 Serial.println("connection failed");
175 return;
176 }else {
177
178 // Create a URL for updating module1 and module 2
179 String url = "http://api.asksensors.com/write/";
180 url += apiKeyIn;
181 url += "?module1=";
182 url += carli;
183
184 Serial.print("********** requesting URL: ");
185 Serial.println(url);
186 // send data
187 ask.begin(url); //Specify the URL
188
189 //Check for the returning code
190 int httpCode = ask.GET();
191
192 if (httpCode > 0) {
193
194 String payload = ask.getString();
195 Serial.println(httpCode);
196 Serial.println(payload);
197 } else {
198 Serial.println("Error on HTTP request");
199 }
200
201 ask.end(); //End
202 Serial.println("********** End ");
203 Serial.println("*****************************************************");
204
205 }
206
207 client.stop(); // stop client
208 delay(writeInterval); // delay
209}