Created
March 16, 2020 08:08
-
-
Save xhidee/85d3b035225b62d7ad8c9e4afd28ecc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
// WIFI & MQTT | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
// #define SID "Wif1456" | |
// #define PS "zxasqw12" | |
#define SID "PALETTE-AP" | |
#define PS "zxasqw12" | |
const char* ssid = SID; | |
const char* pswd = PS; | |
const char* mqtt_server = "192.168.0.100"; // Laptop | |
const char* topic = "rfid"; // rhis is the [root topic] | |
long timeBetweenMessages = 1000 * 20 * 1; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
int value = 0; | |
int status = WL_IDLE_STATUS; // the starting Wifi radio's status | |
// RFID | |
#include <SPI.h> | |
#include <MFRC522.h> | |
#define SS_PIN D2 // sda pin d2 | |
#define RST_PIN D3 // reset pin d3 | |
MFRC522 mfrc522(SS_PIN, RST_PIN); | |
String lastContent = ""; | |
// ANTARES | |
// #include <AntaresESP8266HTTP.h> | |
// #define AK "1100e6c0fa6293e3:ca9a30127821b866" | |
// #define PRJ "RFID01" | |
// #define DEV "pallet1" | |
// AntaresESP8266HTTP antares(AK); | |
void setupRfid() { | |
SPI.begin(); | |
mfrc522.PCD_Init(); | |
Serial.println(); | |
Serial.println("Dekatkan Tag RFID (Dapat berupa kartu atau gantungan kunci) ke RFID reader"); | |
Serial.println(); | |
} | |
void setupAntares() { | |
// // conect wifi | |
// // Serial.begin(115200); | |
// antares.setDebug(false); | |
// antares.wifiConnection(SID, PS); | |
} | |
void setupWifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, pswd); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
// Switch on the LED if an 1 was received as first character | |
if ((char)payload[0] == '1') { | |
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level | |
// but actually the LED is on; this is because | |
// it is acive low on the ESP-01) | |
} else { | |
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH | |
} | |
} | |
String macToStr(const uint8_t* mac) | |
{ | |
String result; | |
for (int i = 0; i < 6; ++i) { | |
result += String(mac[i], 16); | |
if (i < 5) | |
result += ':'; | |
} | |
return result; | |
} | |
String composeClientID() { | |
// uint8_t mac[6]; | |
// WiFi.macAddress(mac); | |
// String clientId; | |
// clientId += "esp-"; | |
// clientId += macToStr(mac); | |
// return clientId; | |
String ipAddress = WiFi.localIP().toString(); | |
ipAddress.replace(".", ":"); | |
return ipAddress; | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
String clientId = composeClientID() ; | |
clientId += "-"; | |
clientId += String(micros() & 0xff, 16); // to randomise. sort of | |
// Attempt to connect | |
if (client.connect(clientId.c_str())) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
client.publish(topic, ("connected " + composeClientID()).c_str() , true ); | |
// ... and resubscribe | |
// topic + clientID + in | |
String subscription; | |
subscription += topic; | |
subscription += "/"; | |
subscription += composeClientID() ; | |
subscription += "/in"; | |
client.subscribe(subscription.c_str() ); | |
Serial.print("subscribed to : "); | |
Serial.println(subscription); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.print(" wifi="); | |
Serial.print(WiFi.status()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void publishData(String content, int time) | |
{ | |
// Notifikasi LED / Buzzer | |
if (lastContent != content) { | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(time); | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
// // Kirim Data | |
// antares.add("UID", content); | |
// antares.send(PRJ, DEV); | |
long now = millis(); | |
// if (now - lastMsg > timeBetweenMessages ) { | |
// lastMsg = now; | |
// ++value; | |
String payload = "{\"micros\":"; | |
payload += micros(); | |
payload += ",\"val\":\""; | |
payload += content; | |
payload += "\",\"client\":\""; | |
payload += composeClientID(); | |
payload += "\"}"; | |
String pubTopic; | |
pubTopic += topic ; | |
pubTopic += "/"; | |
pubTopic += composeClientID(); | |
pubTopic += "/out"; | |
client.publish( (char*) pubTopic.c_str() , (char*) payload.c_str(), true ); | |
Serial.print("Publish to: "); | |
Serial.println(pubTopic); | |
lastContent = content; | |
// } | |
} | |
void setup() | |
{ | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.begin(9600); | |
// digitalWrite(LED_BUILTIN, HIGH); | |
setupRfid(); | |
setupWifi(); | |
// Connect MQTT | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
publishData("", 10); | |
} | |
int countBlank = 0; | |
void loop() | |
{ | |
// confirm still connected to mqtt server | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
digitalWrite(LED_BUILTIN, HIGH); | |
if (!mfrc522.PICC_IsNewCardPresent()) { | |
// Tricky karena walaupun card masih nempel, tapi ada kebaca kosong 1x, https://github.com/miguelbalboa/rfid/issues/188 | |
countBlank++; | |
// Serial.println("E0" + String(countBlank)); | |
if (lastContent != "" && countBlank > 2) { | |
// countBlank = 0; | |
publishData("", 10); | |
} | |
return; | |
} else if (!mfrc522.PICC_ReadCardSerial()) { | |
// Never called | |
Serial.println("S_1"); | |
delay(1000); | |
return; | |
} | |
countBlank = 0; | |
String content = ""; | |
for (int i = 0; i < mfrc522.uid.size; i++) | |
{ | |
// Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); | |
// Serial.print(mfrc522.uid.uidByte[i], DEC); | |
//content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); | |
content.concat(String(mfrc522.uid.uidByte[i], DEC)); | |
} | |
publishData(content, 1000); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment