Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#include <FS.h>
2
#include <LittleFS.h>
3
 
4
#include <ArduinoJson.h>       // https://github.com/bblanchon/ArduinoJson
5
#include <MySQL.h>             // https://github.com/cotestatnt/Arduino-MySQL
6
#include <AsyncFsWebServer.h>  // https://github.com/cotestatnt/async-esp-fs-webserver
7
#include <MFRC522v2.h>         // https://github.com/OSSLibraries/Arduino_MFRC522v2
8
#include <MFRC522DriverSPI.h>
9
#include <MFRC522DriverPinSimple.h>
10
#include <MFRC522Debug.h>
11
 
12
#define PIN_MISO    15
13
#define PIN_MOSI    16
14
#define PIN_SCLK    17
15
#define PIN_CS      18
16
 
17
// This set of variables can be updated using webpage http://<esp-ip-address>/setup
18
String user = "xxxxxxxxxxx";                   // MySQL user login username
19
String password = "xxxxxxxxxxxx";              // MySQL user login password
20
String dbHost = "192.168.1.1";                 // MySQL hostname/URL
21
String database = "xxxxxxxxxxx";               // Database name
22
uint16_t dbPort = 3306;                        // MySQL host port
23
 
24
// Var labels (in /setup webpage)
25
#define MY_SQL_HOST "MySQL Hostname"
26
#define MY_SQL_PORT "MySQL Port"
27
#define MY_SQL_DB   "MySQL Database name"
28
#define MY_SQL_USER "MySQL Username"
29
#define MY_SQL_PASS "MySQL Password"
30
#define RFID_READER_ID "RFID Reader ID"
31
 
32
MFRC522DriverPinSimple ss_pin(PIN_CS); // Configurable, see typical pin layout above.
33
MFRC522DriverSPI driver{ss_pin};       // Create SPI driver.
34
MFRC522 mfrc522{driver};               // Create MFRC522 instance.
35
 
36
uint32_t chipId = 0;
37
bool addLogRecord = true;
38
 
39
#include "mysql_impl.h"
40
#include "webserver_impl.h"
41
 
42
void setup() {
43
  SPI.begin(PIN_SCLK, PIN_MISO, PIN_MOSI, PIN_CS);
44
  Serial.begin(115200);
45
  Serial.println("\n\n\n\nStarting ESP32 RFID gateway...");
46
 
47
  mfrc522.PCD_Init();  // Init MFRC522 board.
48
  MFRC522Debug::PCD_DumpVersionToSerial(mfrc522, Serial);	// Show details of PCD - MFRC522 Card Reader details.
49
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
50
 
51
  /* Init and start configuration webserver */
52
  if (startWebServer()) {
53
 
54
    /* Init and start MySQL task */
55
    if (connectToDatabase()) {
56
      /*
57
      * Check if working table exists and create if not exists.
58
      * If not already present, also a default admin user will be created (admin, admin);
59
      */ 
60
      if (!checkAndCreateTables()) {
61
        Serial.println("Error! Tables not created properly");
62
      }
63
    }
64
    else {
65
      Serial.println("\nDatabase connection failed.\nCheck your connection");
66
    }
67
  }
68
 
69
  #if defined(ESP32)
70
  for(byte i=0; i<17; i=i+8) {
71
    chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
72
  }
73
  #elif defined(ESP8266)
74
  chipId = ESP.getChipId();
75
  #endif
76
  Serial.print("ESP Chip ID: ");
77
  Serial.println(chipId);
78
}
79
 
80
void loop() {
81
  delay(10);
82
 
83
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
84
    static uint32_t readTime = millis();
85
    static uint64_t oldCode = 0;
86
 
87
    // The UID of an RFID tag can be up to 64bit long
88
    uint64_t tagCode = 0;   
89
 
90
    // tagCode is swapped, but it doesn't matter We need only it's a unique number
91
    for(byte i = 0; i < mfrc522.uid.size; i++) {
92
      tagCode |= mfrc522.uid.uidByte[i] << (8*i);
93
    }
94
 
95
    if ((tagCode != oldCode) || (millis() - readTime > 5000)) {
96
      oldCode = tagCode;
97
      readTime = millis();
98
      Serial.printf("\nTag code: %llu\n", tagCode);
99
 
100
      if (addLogRecord) {
101
        DataQuery_t data;
102
        if (queryExecute(data, "SELECT username, level FROM users WHERE tag_code = %llu;", tagCode)) {
103
          sql.printResult(data, Serial);
104
          String SQL = "INSERT INTO logs (epoch, username, tag_code, reader) VALUES (UNIX_TIMESTAMP(), '";
105
          SQL += data.getRowValue(0, "username");  SQL += "', ";
106
          SQL += tagCode; SQL += ", ";
107
          SQL += chipId; SQL += ");";
108
 
109
          if (queryExecute(data, SQL.c_str())) {
110
            Serial.printf("\"%s\" registered on reader %lu\n", data.getRowValue(0, "username"), chipId);
111
          }
112
        }
113
      }
114
    }
115
  }
116
}
117