Categories

Archives

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

RC522

Code

#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <Wire.h>
#include “SSD1306Ascii.h”
#include “SSD1306AsciiWire.h”

// 0X3C+SA0 – 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

SSD1306AsciiWire oled;
//——————————————————————————

const String PROGRAM_VERSION = “60101500”;
String pp = PROGRAM_VERSION + “>> “;

String webload;
ESP8266WiFiMulti WiFiMulti;
const char* ssid = “ddcard”;
const char* password = “@ddcard123”;
String new_ssid;
String urlserver = “http://182.93.155.51”;
String urlinit = “/dd-card/init.php”;
String urlget = “/dd-card/api.php”;
byte mac[6];
IPAddress localAddr;
String macAddr;
String ipAddr;

#define RST_PIN 16 // Configurable, see typical pin layout above
#define SS_PIN 15 // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

MFRC522::MIFARE_Key key;

/**
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
Wire.begin();
oled.begin(&Adafruit128x64, I2C_ADDRESS);
oled.set400kHz();

oled.clear();
oled.setFont(fixed_bold10x15);
oled.println(” ddCard”);

for(uint8_t t = 4; t > 0; t–) {
Serial.print(pp);
Serial.printf(“[SETUP] WAIT %d…\n”, t);
Serial.flush();
delay(1000);
}

WiFiMulti.addAP(ssid, password);
WiFi.macAddress(mac);
macAddr=mac2String(mac);
Serial.print(pp);
Serial.println(“MAC = ” + macAddr);

while((WiFiMulti.run() != WL_CONNECTED)) {
Serial.print(“.”);
delay(1000);
}

if((WiFiMulti.run() == WL_CONNECTED)) {

Serial.println(“done2.”);
localAddr = WiFi.localIP();
byte oct1 = localAddr[0];
byte oct2 = localAddr[1];
byte oct3 = localAddr[2];
byte oct4 = localAddr[3];
char sip[16];
sprintf(sip, “%d.%d.%d.%d”, oct1, oct2, oct3, oct4);
ipAddr=String(sip);
Serial.print(pp);
Serial.println(“IP = “+ipAddr);

HTTPClient http;
String serverurlget = urlserver + urlinit + “?” + “mac=”+macAddr + “&ip=”+ipAddr + “&v=”+PROGRAM_VERSION + “&s=init”;
Serial.println(serverurlget);
http.begin(serverurlget); //HTTP

int httpCode = http.GET();
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.print(pp);
Serial.printf(“[HTTP] GET… code: %d\n”, httpCode);

// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
webload=payload;
Serial.println(payload);
delay(2500);
}
} else {
Serial.print(pp);
Serial.printf(“[HTTP] GET… failed, error: %s\n”, http.errorToString(httpCode).c_str());
}
http.end();
}

oled.setFont(Callibri11);
oled.println(“MAC=”+macAddr);
oled.println(“IP=”+ipAddr);
oled.setFont(Callibri10);
oled.print(PROGRAM_VERSION);
oled.print(” – “);
oled.print(ssid);
oled.println();

while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card

// Prepare the key (used both as key A and as key B)
// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}

Serial.println(F(“Scan a MIFARE Classic PICC to demonstrate read and write.”));
Serial.print(F(“Using key (for A and B):”));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();

Serial.println(F(“BEWARE: Data will be written to the PICC, in sector #1”));

}

/**
* Main loop.
*/
void loop() {
int uptime;
uptime=millis()/1000;

String uid;

// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;

// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;

// Show some details of the PICC (that is: the tag/card)
Serial.print(F(“Card UID: “));
uid=dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.print(uid);
Serial.println();
Serial.print(F(“PICC type: “));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));

oled.clear();
oled.setFont(Callibri11);
oled.println(“UID = “+uid);
oled.println();

// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F(“This sample only works with MIFARE Classic cards.”));
return;
}

if((WiFiMulti.run() == WL_CONNECTED)) {

HTTPClient http;

Serial.print(pp);
Serial.print(“[HTTP] begin…\n”);

String serverurlget = urlserver + urlget + “?” + “mac=”+macAddr + “&v=” + PROGRAM_VERSION +
“&uptime=” + String(uptime) + “&uid=”+uid;
http.begin(serverurlget); //HTTP

Serial.print(pp);
Serial.print(“[HTTP] GET…\n”);
Serial.println(serverurlget);

// start connection and send HTTP header
int httpCode = http.GET();

//oled.setFont(fixed_bold10x15);
//oled.println(” ddCard”);
oled.setFont(TimesNewRoman16);

// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.print(pp);
Serial.printf(“[HTTP] GET… code: %d\n”, httpCode);

// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
webload=payload;
Serial.println(payload);
if (webload.indexOf(“Not found”) != -1) {
oled.println(payload);
} else {
oled.setFont(Callibri10);
oled.set2X();
oled.println(payload);
oled.set1X();
}
}
} else {
Serial.print(pp);
Serial.printf(“[HTTP] GET… failed, error: %s\n”, http.errorToString(httpCode).c_str());
oled.println(“Server fail…”);
}

http.end();
} else {
webload=””;
oled.println(“Connection loss…”);
}

// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();

delay(2000);
oled.clear();
oled.setFont(fixed_bold10x15);
oled.println(” ddCard”);
oled.setFont(Callibri11);
oled.println();
oled.println(” Please TAP ID Card”);
}

/**
* Helper routine to dump a byte array as hex values to Serial.
*/
String dump_byte_array(byte *buffer, byte bufferSize) {
String s=””;
for (byte i = 0; i < bufferSize; i++) {
char buf[3];
sprintf(buf, “%02X”, buffer[i]);
s = buf + s;
}
return s;
}

void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print(“SSID: “);
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print(“IP Address: “);
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(” dBm”);
}

String mac2String(byte ar[])
{
String s;
for (byte i = 0; i < 6; ++i)
{
char buf[3];
sprintf(buf, “%02X”, ar[i]);
s += buf;
if (i < 5) s += ‘:’;
}
return s;
}

Leave a Reply