1 | /*
|
2 | ESP8266 RFID Reader - MQTT Sender
|
3 | Copyright 2016 Christian Moll <christian@chrmoll.de>
|
4 |
|
5 | This program is free software: you can redistribute it and/or modify
|
6 | it under the terms of the GNU General Public License as published by
|
7 | the Free Software Foundation, either version 3 of the License, or
|
8 | (at your option) any later version.
|
9 |
|
10 | This program is distributed in the hope that it will be useful,
|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 | GNU General Public License for more details.
|
14 |
|
15 | You should have received a copy of the GNU General Public License
|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
|
18 | Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
|
19 | der GNU General Public License, wie von der Free Software Foundation,
|
20 | Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
|
21 | veröffentlichten Version, weiterverbreiten und/oder modifizieren.
|
22 |
|
23 | Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
|
24 | OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
|
25 | Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
|
26 | Siehe die GNU General Public License für weitere Details.
|
27 |
|
28 | Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
|
29 | Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
|
30 | */
|
31 |
|
32 | #include <ESP8266WiFi.h>
|
33 | #include <WiFiClient.h>
|
34 | #include <ESP8266WebServer.h>
|
35 | #include <ESP8266mDNS.h>
|
36 | #include <ESP8266HTTPUpdateServer.h>
|
37 |
|
38 | #include <MQTTClient.h>
|
39 | #include <SPI.h>
|
40 | #include <MFRC522.h>
|
41 |
|
42 | #define SS_PIN D8 //Pin on WeMos D1 Mini
|
43 | #define RST_PIN D3 //Pin on WeMos D1 Mini
|
44 |
|
45 | const char* host = "rfid_reader"; // will also be used on shiftr.io
|
46 | const char* ssid = "ssid";
|
47 | const char* password = "passphrase";
|
48 |
|
49 | const char* brocker = "broker.shiftr.io";
|
50 | const char* mqttUser = "mqttuser";
|
51 | const char* mqttPass = "mqttpassword";
|
52 |
|
53 | ESP8266WebServer httpServer(80);
|
54 | ESP8266HTTPUpdateServer httpUpdater;
|
55 |
|
56 | WiFiClient wifi;
|
57 | MQTTClient mqtt;
|
58 |
|
59 | MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
|
60 | MFRC522::MIFARE_Key key;
|
61 |
|
62 | unsigned int batt;
|
63 | double battV;
|
64 | unsigned long oldMillis;
|
65 |
|
66 | void connect();
|
67 |
|
68 | void setup(void){
|
69 | SPI.begin();
|
70 | rfid.PCD_Init();
|
71 |
|
72 | Serial.begin(115200);
|
73 | Serial.println();
|
74 | Serial.println("Booting Sketch...");
|
75 | WiFi.mode(WIFI_AP_STA);
|
76 | WiFi.begin(ssid, password);
|
77 |
|
78 | mqtt.begin(brocker, wifi);
|
79 | mqtt.publish("/rfid_reader/resetReason", ESP.getResetReason());
|
80 |
|
81 | connect();
|
82 |
|
83 | MDNS.begin(host);
|
84 |
|
85 | //Attach handles for different pages.
|
86 | httpUpdater.setup(&httpServer);
|
87 |
|
88 | httpServer.on("/", handleRoot);
|
89 |
|
90 | httpServer.begin();
|
91 |
|
92 | MDNS.addService("http", "tcp", 80);
|
93 | Serial.println("Up and running!");
|
94 | }
|
95 |
|
96 | void loop(void){
|
97 | if(!mqtt.connected()) {
|
98 | connect();
|
99 | }
|
100 |
|
101 | httpServer.handleClient();
|
102 | mqtt.loop();
|
103 | delay(10);
|
104 |
|
105 | handleRFID();
|
106 |
|
107 | //things that should only be transmitted all 60 sec
|
108 | if (millis()-oldMillis > 60000) {
|
109 | batt = analogRead(A0);
|
110 | battV = mapDouble(batt, 0, 1023, 0.0, 6.6);
|
111 | mqtt.publish("/rfid_reader/batt", String(battV));
|
112 | mqtt.publish("/rfid_reader/battRaw", String(batt));
|
113 |
|
114 | oldMillis = millis();
|
115 | }
|
116 | }
|
117 |
|
118 | void connect() {
|
119 | while(WiFi.waitForConnectResult() != WL_CONNECTED){
|
120 | WiFi.begin(ssid, password);
|
121 | Serial.println("WiFi failed, retrying.");
|
122 | }
|
123 |
|
124 | Serial.print("IP address: ");
|
125 | Serial.println(WiFi.localIP());
|
126 |
|
127 | while (!mqtt.connect(host, mqttUser, mqttPass)) {
|
128 | Serial.print(".");
|
129 | }
|
130 | Serial.println("\nconnected!");
|
131 | }
|
132 |
|
133 | void handleRoot() {
|
134 | httpServer.send(200, "text/plain", "It works!!!");
|
135 | }
|
136 |
|
137 | void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
|
138 | Serial.print("incoming: ");
|
139 | Serial.print(topic);
|
140 | Serial.print(" - ");
|
141 | Serial.print(payload);
|
142 | Serial.println();
|
143 | }
|
144 |
|
145 | void handleRFID() {
|
146 | if (!rfid.PICC_IsNewCardPresent()) return;
|
147 | if (!rfid.PICC_ReadCardSerial()) return;
|
148 | mqtt.publish("/rfid_reader/uid", printHex(rfid.uid.uidByte, rfid.uid.size));
|
149 | rfid.PICC_HaltA();
|
150 | rfid.PCD_StopCrypto1();
|
151 | }
|
152 |
|
153 | double mapDouble(double x, double in_min, double in_max, double out_min, double out_max)
|
154 | {
|
155 | double temp = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
156 | temp = (int) (4*temp + .5);
|
157 | return (double) temp/4;
|
158 | }
|
159 |
|
160 | String printHex(byte *buffer, byte bufferSize) {
|
161 | String id = "";
|
162 | for (byte i = 0; i < bufferSize; i++) {
|
163 | id += buffer[i] < 0x10 ? "0" : "";
|
164 | id += String(buffer[i], HEX);
|
165 | }
|
166 | return id;
|
167 | }
|