├── rfid ├── image.png └── rfid.ino ├── presenca ├── image.png └── sensor-presenca.ino ├── teclado ├── image.png └── teclado.ino ├── obstaculo └── obstaculo.ino ├── esp-lora ├── lora-receiver.ino └── lora-sender.ino └── temperatura-led └── temperatura-led.ino /rfid/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramonfontes/iot/main/rfid/image.png -------------------------------------------------------------------------------- /presenca/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramonfontes/iot/main/presenca/image.png -------------------------------------------------------------------------------- /teclado/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramonfontes/iot/main/teclado/image.png -------------------------------------------------------------------------------- /obstaculo/obstaculo.ino: -------------------------------------------------------------------------------- 1 | int count; 2 | void setup() { 3 | 4 | Serial.begin (9600); 5 | pinMode (15, INPUT); //Sensor output 6 | } 7 | void loop() { 8 | Serial.print ("Sensor: "); 9 | Serial.println (digitalRead(15)); //print the sensor output 10 | delay (500); //wait half a second 11 | } 12 | -------------------------------------------------------------------------------- /presenca/sensor-presenca.ino: -------------------------------------------------------------------------------- 1 | #define LED 5 2 | #define PIN_SENSOR 4 3 | 4 | void setup() { 5 | 6 | Serial.begin(9600); // initialize serial 7 | //abaixo vamos confirar cada um dos pinos como entrada ou saída de dados 8 | pinMode(PIN_SENSOR, INPUT); 9 | pinMode(LED, OUTPUT); 10 | } 11 | 12 | void loop() { 13 | //faz a leitura do sensor de presença (retorna HIGH ou LOW) 14 | int sinal = digitalRead(PIN_SENSOR); 15 | 16 | //HIGH : movimento detectado 17 | if(sinal == HIGH){ 18 | //aciona o led 19 | Serial.print("high\n"); 20 | digitalWrite(LED, HIGH); 21 | } 22 | //LOW : nenhum movimento detectado 23 | else{ 24 | //desativa o led 25 | Serial.print("low\n"); 26 | digitalWrite(LED, LOW); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /teclado/teclado.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ROW_NUM 4 // four rows 4 | #define COLUMN_NUM 4 // four columns 5 | 6 | char keys[ROW_NUM][COLUMN_NUM] = { 7 | {'1', '2', '3', 'A'}, 8 | {'4', '5', '6', 'B'}, 9 | {'7', '8', '9', 'C'}, 10 | {'*', '0', '#', 'D'} 11 | }; 12 | 13 | byte pin_rows[ROW_NUM] = {21, 19, 18, 5}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins 14 | byte pin_column[COLUMN_NUM] = {12, 13, 14, 15}; // GIOP16, GIOP4, GIOP0, GIOP2 connect to the column pins 15 | 16 | Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); 17 | 18 | void setup() { 19 | Serial.begin(9600); 20 | } 21 | 22 | void loop() { 23 | char key = keypad.getKey(); 24 | if (key) { 25 | Serial.println(key); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /esp-lora/lora-receiver.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "SSD1306.h" 4 | 5 | SSD1306 display(0x3c, 4, 15); 6 | 7 | #define SS 18 8 | #define RST 14 9 | #define DI0 26 10 | #define BAND 915E6 11 | #define L12 12 12 | #define L13 13 13 | #define L17 17 14 | #define L21 21 15 | #define L22 22 16 | #define L23 23 17 | 18 | void setup() { 19 | 20 | pinMode(16,OUTPUT); 21 | pinMode(L12, OUTPUT); 22 | pinMode(L13, OUTPUT); 23 | pinMode(L17, OUTPUT); 24 | pinMode(L21, OUTPUT); 25 | pinMode(L22, OUTPUT); 26 | pinMode(L23, OUTPUT); 27 | digitalWrite(16, LOW); // set GPIO16 low to reset OLED 28 | delay(50); 29 | digitalWrite(16, HIGH); 30 | 31 | display.init(); 32 | display.flipScreenVertically(); 33 | display.setFont(ArialMT_Plain_10); 34 | display.setTextAlignment(TEXT_ALIGN_LEFT); 35 | 36 | Serial.begin(115200); 37 | while (!Serial); //if just the the basic function, must connect to a computer 38 | delay(1000); 39 | Serial.println("LoRa Receiver"); 40 | display.drawString(5,5,"LoRa Receiver"); 41 | display.display(); 42 | SPI.begin(5,19,27,18); 43 | LoRa.setPins(SS,RST,DI0); 44 | 45 | if (!LoRa.begin(BAND)) { 46 | display.drawString(5,25,"Starting LoRa failed!"); 47 | while (1); 48 | } 49 | Serial.println("LoRa Initial OK!"); 50 | display.drawString(5,25,"LoRa Initializing OK!"); 51 | display.display(); 52 | } 53 | 54 | void loop() { 55 | // try to parse packet 56 | int packetSize = LoRa.parsePacket(); 57 | if (packetSize) { 58 | // received a packets 59 | Serial.print("Received packet. "); 60 | display.clear(); 61 | display.setFont(ArialMT_Plain_16); 62 | display.drawString(3, 0, "Received packet "); 63 | display.display(); 64 | 65 | // read packet 66 | while (LoRa.available()) { 67 | String data = LoRa.readString(); 68 | Serial.print(data); 69 | display.drawString(20,22, data); 70 | display.display(); 71 | 72 | if (data == "22-on") 73 | digitalWrite(L22, HIGH); 74 | else if (data == "22-off") 75 | digitalWrite(L22, LOW); 76 | else if (data == "12-on") 77 | digitalWrite(L12, HIGH); 78 | else if (data == "12-off") 79 | digitalWrite(L12, LOW); 80 | else if (data == "13-on") 81 | digitalWrite(L13, HIGH); 82 | else if (data == "13-off") 83 | digitalWrite(L13, LOW); 84 | else if (data == "17-on") 85 | digitalWrite(L17, HIGH); 86 | else if (data == "17-off") 87 | digitalWrite(L17, LOW); 88 | else if (data == "21-on") 89 | digitalWrite(L21, HIGH); 90 | else if (data == "21-off") 91 | digitalWrite(L21, LOW); 92 | else if (data == "23-on") 93 | digitalWrite(L23, HIGH); 94 | else if (data == "23-off") 95 | digitalWrite(L23, LOW); 96 | } 97 | // print RSSI of packet 98 | Serial.print(" with RSSI "); 99 | Serial.println(LoRa.packetRssi()); 100 | display.drawString(20, 45, "RSSI: "); 101 | display.drawString(70, 45, (String)LoRa.packetRssi()); 102 | display.display(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /esp-lora/lora-sender.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "SSD1306.h" 4 | #include 5 | #include 6 | #include 7 | 8 | //informações da rede WIFI 9 | const char* ssid = "SSID"; //SSID da rede WIFI 10 | const char* password = "PASSWORD"; //senha da rede wifi 11 | 12 | //informações do broker MQTT - Verifique as informações geradas pelo CloudMQTT 13 | const char* mqttServer = "SERVER_DOMAIN"; //server 14 | const char* mqttUser = "USER"; //user 15 | const char* mqttPassword = "PASSWORD"; //password 16 | const int mqttPort = 1883; //port 17 | const char* mqttTopicSub1 ="filhas"; 18 | 19 | SSD1306 display(0x3c, 4, 15); 20 | 21 | #define SS 18 22 | #define RST 14 23 | #define DI0 26 24 | #define BAND 915E6 //433E6 25 | 26 | WiFiClient espClient; 27 | PubSubClient client(espClient); 28 | 29 | 30 | void setup() { 31 | pinMode(25,OUTPUT); //Send success, LED will bright 1 second 32 | pinMode(16,OUTPUT); 33 | digitalWrite(16, LOW); // set GPIO16 low to reset OLED 34 | delay(50); 35 | digitalWrite(16, HIGH); 36 | 37 | Serial.begin(115200); 38 | while (!Serial); //If just the the basic function, must connect to a computer 39 | // Initialising the UI will init the display too. 40 | display.init(); 41 | display.flipScreenVertically(); 42 | display.setFont(ArialMT_Plain_10); 43 | display.setTextAlignment(TEXT_ALIGN_LEFT); 44 | display.drawString(5,5,"LoRa Filhas"); 45 | display.display(); 46 | 47 | SPI.begin(5,19,27,18); 48 | LoRa.setPins(SS,RST,DI0); 49 | Serial.println("LoRa Filhas"); 50 | if (!LoRa.begin(BAND)) { 51 | Serial.println("Starting LoRa failed!"); 52 | while (1); 53 | } 54 | 55 | WiFi.begin(ssid, password); 56 | 57 | while (WiFi.status() != WL_CONNECTED) { 58 | delay(500); 59 | #ifdef DEBUG 60 | Serial.println("Conectando ao WiFi.."); 61 | #endif 62 | } 63 | 64 | #ifdef DEBUG 65 | Serial.println("Conectado na rede WiFi"); 66 | #endif 67 | 68 | client.setServer(mqttServer, mqttPort); 69 | client.setCallback(callback); 70 | 71 | while (!client.connected()) { 72 | #ifdef DEBUG 73 | Serial.println("Conectando ao Broker MQTT..."); 74 | #endif 75 | 76 | if (client.connect("ESP8266Client1", mqttUser, mqttPassword )) { 77 | #ifdef DEBUG 78 | Serial.println("Conectado"); 79 | #endif 80 | } else { 81 | #ifdef DEBUG 82 | Serial.print("falha estado "); 83 | Serial.print(client.state()); 84 | #endif 85 | delay(2000); 86 | } 87 | } 88 | 89 | //subscreve no tópico 90 | client.subscribe(mqttTopicSub1); 91 | Serial.println(WiFi.status()); 92 | 93 | Serial.println("LoRa Initial OK!"); 94 | display.drawString(5,20,"LoRa Initializing OK!"); 95 | display.display(); 96 | delay(2000); 97 | } 98 | 99 | void callback(char* topic, byte* payload, unsigned int length) { 100 | //armazena msg recebida em uma sring 101 | payload[length] = '\0'; 102 | String strMSG = String((char*)payload); 103 | 104 | if (strcmp(topic, mqttTopicSub1)==0){ 105 | // send packet 106 | display.clear(); 107 | display.setFont(ArialMT_Plain_16); 108 | display.drawString(3, 5, "Sending packet "); 109 | display.drawString(50, 30, strMSG); 110 | display.display(); 111 | LoRa.beginPacket(); 112 | LoRa.print(strMSG); 113 | LoRa.endPacket(); 114 | digitalWrite(25, HIGH); // turn the LED on (HIGH is the voltage level) 115 | delay(1000); // wait for a second 116 | digitalWrite(25, LOW); // turn the LED off by making the voltage LOW 117 | delay(1000); // wait for a second 118 | } 119 | } 120 | 121 | void loop() { 122 | client.loop(); 123 | } 124 | -------------------------------------------------------------------------------- /temperatura-led/temperatura-led.ino: -------------------------------------------------------------------------------- 1 | //Acionamento de lampada com MQTT Dash 2 | 3 | #include 4 | #include 5 | #include "DHT.h" 6 | 7 | #define INTERVALO_ENVIO 10000 8 | #define DHTPIN 19 // o sensor dht11 foi conectado ao pino 2( D4 do node MCU) 9 | #define DHTTYPE DHT22 10 | 11 | DHT dht(DHTPIN, DHTTYPE); 12 | 13 | #define DEBUG 14 | #define L1 4 //pino de saida para acionamento da Lampada L1 15 | 16 | //informações da rede WIFI 17 | const char* ssid = "ssid"; //SSID da rede WIFI 18 | const char* password = "senha"; //senha da rede wifi 19 | 20 | //informações do broker MQTT - Verifique as informações geradas pelo CloudMQTT 21 | const char* mqttServer = "192.168.151.106"; //server 22 | const char* mqttUser = "user"; //user 23 | const char* mqttPassword = "user"; //password 24 | const int mqttPort = 1883; //port 25 | const char* mqttTopicSub ="led/L1"; //tópico que sera assinado 26 | 27 | int ultimoEnvioMQTT = 0; 28 | #define CONNECTION_TIMEOUT 5 29 | 30 | WiFiClient espClient; 31 | PubSubClient client(espClient); 32 | 33 | void setup() { 34 | 35 | Serial.begin(115200); 36 | pinMode(L1, OUTPUT); 37 | 38 | WiFi.begin(ssid, password); 39 | int timeout_counter = 0; 40 | 41 | while (WiFi.status() != WL_CONNECTED) { 42 | delay(200); 43 | #ifdef DEBUG 44 | Serial.println("Conectando ao WiFi.."); 45 | timeout_counter++; 46 | if(timeout_counter >= CONNECTION_TIMEOUT*5){ 47 | ESP.restart(); 48 | } 49 | #endif 50 | } 51 | #ifdef DEBUG 52 | Serial.println("Conectado na rede WiFi"); 53 | #endif 54 | 55 | client.setServer(mqttServer, mqttPort); 56 | client.setCallback(callback); 57 | 58 | while (!client.connected()) { 59 | #ifdef DEBUG 60 | Serial.println("Conectando ao Broker MQTT..."); 61 | #endif 62 | 63 | if (client.connect("ESP8266Client", mqttUser, mqttPassword )) { 64 | #ifdef DEBUG 65 | Serial.println("Conectado"); 66 | #endif 67 | 68 | } else { 69 | #ifdef DEBUG 70 | Serial.print("falha estado "); 71 | Serial.print(client.state()); 72 | #endif 73 | delay(2000); 74 | } 75 | } 76 | 77 | //subscreve no tópico 78 | client.subscribe(mqttTopicSub); 79 | dht.begin(); 80 | } 81 | 82 | void callback(char* topic, byte* payload, unsigned int length) { 83 | 84 | //armazena msg recebida em uma sring 85 | payload[length] = '\0'; 86 | String strMSG = String((char*)payload); 87 | 88 | #ifdef DEBUG 89 | Serial.print("Mensagem chegou do tópico: "); 90 | Serial.println(topic); 91 | Serial.print("Mensagem:"); 92 | Serial.print(strMSG); 93 | Serial.println(); 94 | Serial.println("-----------------------"); 95 | #endif 96 | 97 | if (strcmp(topic, mqttTopicSub)==0){ 98 | Serial.print("LED..."); 99 | if (strMSG == "1"){ //se msg "1" 100 | digitalWrite(L1, LOW); //coloca saída em LOW para ligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo 101 | }else if (strMSG == "0"){ //se msg "0" 102 | digitalWrite(L1, HIGH); //coloca saída em HIGH para desligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo 103 | } 104 | } 105 | } 106 | 107 | //função pra reconectar ao servido MQTT 108 | void reconect() { 109 | //Enquanto estiver desconectado 110 | while (!client.connected()) { 111 | #ifdef DEBUG 112 | Serial.print("Tentando conectar ao servidor MQTT"); 113 | #endif 114 | 115 | bool conectado = strlen(mqttUser) > 0 ? 116 | client.connect("ESP8266Client", mqttUser, mqttPassword) : 117 | client.connect("ESP8266Client"); 118 | 119 | if(conectado) { 120 | #ifdef DEBUG 121 | Serial.println("Conectado!"); 122 | #endif 123 | //subscreve no tópico 124 | client.subscribe(mqttTopicSub, 1); //nivel de qualidade: QoS 1 125 | } else { 126 | #ifdef DEBUG 127 | Serial.println("Falha durante a conexão.Code: "); 128 | Serial.println( String(client.state()).c_str()); 129 | Serial.println("Tentando novamente em 10 s"); 130 | #endif 131 | //Aguarda 10 segundos 132 | delay(10000); 133 | } 134 | } 135 | } 136 | 137 | void loop() { 138 | if (!client.connected()) { 139 | reconect(); 140 | } 141 | 142 | //envia a cada X segundos 143 | if ((millis() - ultimoEnvioMQTT) > INTERVALO_ENVIO) 144 | { 145 | enviaDHT(); 146 | ultimoEnvioMQTT = millis(); 147 | } 148 | 149 | client.loop(); 150 | } 151 | 152 | //função para leitura do DHT11 153 | void enviaDHT(){ 154 | 155 | char MsgUmidadeMQTT[10]; 156 | char MsgTemperaturaMQTT[10]; 157 | 158 | float umidade = dht.readHumidity(); 159 | float temperatura = dht.readTemperature(); 160 | 161 | if (isnan(temperatura) || isnan(umidade)) 162 | { 163 | #ifdef DEBUG 164 | Serial.println("Falha na leitura do dht11..."); 165 | #endif 166 | } 167 | else 168 | { 169 | #ifdef DEBUG 170 | Serial.print("Umidade: "); 171 | Serial.print(umidade); 172 | Serial.print(" \n"); 173 | Serial.print("Temperatura: "); 174 | Serial.print(temperatura); 175 | Serial.println(" °C"); 176 | #endif 177 | 178 | sprintf(MsgUmidadeMQTT,"%f",umidade); 179 | client.publish("casa/umidade", MsgUmidadeMQTT); 180 | sprintf(MsgTemperaturaMQTT,"%f",temperatura); 181 | client.publish("casa/temperatura", MsgTemperaturaMQTT); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /rfid/rfid.ino: -------------------------------------------------------------------------------- 1 | #include //biblioteca responsável pela comunicação com o módulo RFID-RC522 2 | #include //biblioteca para comunicação do barramento SPI 3 | 4 | #define SS_PIN 21 5 | #define RST_PIN 22 6 | 7 | #define SIZE_BUFFER 18 8 | #define MAX_SIZE_BLOCK 16 9 | 10 | #define pinVerde 12 11 | #define pinVermelho 32 12 | 13 | //esse objeto 'chave' é utilizado para autenticação 14 | MFRC522::MIFARE_Key key; 15 | //código de status de retorno da autenticação 16 | MFRC522::StatusCode status; 17 | 18 | // Definicoes pino modulo RC522 19 | MFRC522 mfrc522(SS_PIN, RST_PIN); 20 | 21 | void setup() { 22 | // Inicia a serial 23 | Serial.begin(9600); 24 | SPI.begin(); // Init SPI bus 25 | 26 | pinMode(pinVerde, OUTPUT); 27 | pinMode(pinVermelho, OUTPUT); 28 | 29 | // Inicia MFRC522 30 | mfrc522.PCD_Init(); 31 | // Mensagens iniciais no serial monitor 32 | Serial.println("Aproxime o seu cartao do leitor..."); 33 | Serial.println(); 34 | 35 | } 36 | 37 | void loop() 38 | { 39 | // Aguarda a aproximacao do cartao 40 | if ( ! mfrc522.PICC_IsNewCardPresent()) 41 | { 42 | return; 43 | } 44 | // Seleciona um dos cartoes 45 | if ( ! mfrc522.PICC_ReadCardSerial()) 46 | { 47 | return; 48 | } 49 | 50 | //chama o menu e recupera a opção desejada 51 | int opcao = menu(); 52 | 53 | if(opcao == 0) 54 | leituraDados(); 55 | else if(opcao == 1) 56 | gravarDados(); 57 | else { 58 | Serial.println(F("Opção Incorreta!")); 59 | return; 60 | } 61 | // instrui o PICC quando no estado ACTIVE a ir para um estado de "parada" 62 | mfrc522.PICC_HaltA(); 63 | // "stop" a encriptação do PCD, deve ser chamado após a comunicação com autenticação, caso contrário novas comunicações não poderão ser iniciadas 64 | mfrc522.PCD_StopCrypto1(); 65 | } 66 | 67 | //faz a leitura dos dados do cartão/tag 68 | void leituraDados() 69 | { 70 | //imprime os detalhes tecnicos do cartão/tag 71 | mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); 72 | 73 | //Prepara a chave - todas as chaves estão configuradas para FFFFFFFFFFFFh (Padrão de fábrica). 74 | for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; 75 | 76 | //buffer para colocar os dados ligos 77 | byte buffer[SIZE_BUFFER] = {0}; 78 | 79 | //bloco que faremos a operação 80 | byte bloco = 1; 81 | byte tamanho = SIZE_BUFFER; 82 | 83 | 84 | //faz a autenticação do bloco que vamos operar 85 | status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloco, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file 86 | if (status != MFRC522::STATUS_OK) { 87 | Serial.print(F("Authentication failed: ")); 88 | Serial.println(mfrc522.GetStatusCodeName(status)); 89 | digitalWrite(pinVermelho, HIGH); 90 | delay(1000); 91 | digitalWrite(pinVermelho, LOW); 92 | return; 93 | } 94 | 95 | //faz a leitura dos dados do bloco 96 | status = mfrc522.MIFARE_Read(bloco, buffer, &tamanho); 97 | if (status != MFRC522::STATUS_OK) { 98 | Serial.print(F("Reading failed: ")); 99 | Serial.println(mfrc522.GetStatusCodeName(status)); 100 | digitalWrite(pinVermelho, HIGH); 101 | delay(1000); 102 | digitalWrite(pinVermelho, LOW); 103 | return; 104 | } 105 | else{ 106 | digitalWrite(pinVerde, HIGH); 107 | delay(1000); 108 | digitalWrite(pinVerde, LOW); 109 | } 110 | 111 | Serial.print(F("\nDados bloco [")); 112 | Serial.print(bloco);Serial.print(F("]: ")); 113 | 114 | //imprime os dados lidos 115 | for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++) 116 | { 117 | Serial.write(buffer[i]); 118 | } 119 | Serial.println(" "); 120 | } 121 | 122 | //faz a gravação dos dados no cartão/tag 123 | void gravarDados() 124 | { 125 | //imprime os detalhes tecnicos do cartão/tag 126 | mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); 127 | // aguarda 30 segundos para entrada de dados via Serial 128 | Serial.setTimeout(30000L) ; 129 | Serial.println(F("Insira os dados a serem gravados com o caractere '#' ao final\n[máximo de 16 caracteres]:")); 130 | 131 | //Prepara a chave - todas as chaves estão configuradas para FFFFFFFFFFFFh (Padrão de fábrica). 132 | for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; 133 | 134 | //buffer para armazenamento dos dados que iremos gravar 135 | byte buffer[MAX_SIZE_BLOCK] = ""; 136 | byte bloco; //bloco que desejamos realizar a operação 137 | byte tamanhoDados; //tamanho dos dados que vamos operar (em bytes) 138 | 139 | //recupera no buffer os dados que o usuário inserir pela serial 140 | //serão todos os dados anteriores ao caractere '#' 141 | tamanhoDados = Serial.readBytesUntil('#', (char*)buffer, MAX_SIZE_BLOCK); 142 | //espaços que sobrarem do buffer são preenchidos com espaço em branco 143 | for(byte i=tamanhoDados; i < MAX_SIZE_BLOCK; i++) 144 | { 145 | buffer[i] = ' '; 146 | } 147 | 148 | bloco = 1; //bloco definido para operação 149 | String str = (char*)buffer; //transforma os dados em string para imprimir 150 | Serial.println(str); 151 | 152 | //Authenticate é um comando para autenticação para habilitar uma comuinicação segura 153 | status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 154 | bloco, &key, &(mfrc522.uid)); 155 | 156 | if (status != MFRC522::STATUS_OK) { 157 | Serial.print(F("PCD_Authenticate() failed: ")); 158 | Serial.println(mfrc522.GetStatusCodeName(status)); 159 | digitalWrite(pinVermelho, HIGH); 160 | delay(1000); 161 | digitalWrite(pinVermelho, LOW); 162 | return; 163 | } 164 | //else Serial.println(F("PCD_Authenticate() success: ")); 165 | 166 | //Grava no bloco 167 | status = mfrc522.MIFARE_Write(bloco, buffer, MAX_SIZE_BLOCK); 168 | if (status != MFRC522::STATUS_OK) { 169 | Serial.print(F("MIFARE_Write() failed: ")); 170 | Serial.println(mfrc522.GetStatusCodeName(status)); 171 | digitalWrite(pinVermelho, HIGH); 172 | delay(1000); 173 | digitalWrite(pinVermelho, LOW); 174 | return; 175 | } 176 | else{ 177 | Serial.println(F("MIFARE_Write() success: ")); 178 | digitalWrite(pinVerde, HIGH); 179 | delay(1000); 180 | digitalWrite(pinVerde, LOW); 181 | } 182 | 183 | } 184 | 185 | //menu para escolha da operação 186 | int menu() 187 | { 188 | Serial.println(F("\nEscolha uma opção:")); 189 | Serial.println(F("0 - Leitura de Dados")); 190 | Serial.println(F("1 - Gravação de Dados\n")); 191 | 192 | //fica aguardando enquanto o usuário nao enviar algum dado 193 | while(!Serial.available()){}; 194 | 195 | //recupera a opção escolhida 196 | int op = (int)Serial.read(); 197 | //remove os proximos dados (como o 'enter ou \n' por exemplo) que vão por acidente 198 | while(Serial.available()) { 199 | if(Serial.read() == '\n') break; 200 | Serial.read(); 201 | } 202 | return (op-48);//do valor lido, subtraimos o 48 que é o ZERO da tabela ascii 203 | } 204 | --------------------------------------------------------------------------------