Created
December 24, 2017 14:39
-
-
Save kheminw/efaa48da03babbcf130a7a15603444b6 to your computer and use it in GitHub Desktop.
NodeMCU to MicroGear
This file contains 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
//NETPIE CONNECTION | |
#include <MicroGear.h> | |
#include <ESP8266WiFi.h> | |
//RFID PART | |
#include <SPI.h> | |
#include <MFRC522.h> | |
//CONNECT TO WEB PART | |
#include <MicroGear.h> | |
#include <ESP8266WiFi.h> | |
//CONNECT TO STM32 | |
#include<SoftwareSerial.h> | |
SoftwareSerial mySerial(5, 4); // RX, TX | |
// constants won't change. They're used here to | |
// set pin numbers: | |
//#define D0 16 // USER LED Wake | |
#define ledPin D0 // the number of the LED pin | |
//Wifi Connection | |
const char* ssid = "***"; | |
const char* password = "***"; | |
//Netpie | |
#define APPID "SmartKey" | |
#define KEY "***" | |
#define SECRET "***" | |
#define ALIAS "esp8266" | |
#define TargetWeb "server" | |
WiFiClient client; | |
MicroGear microgear(client); | |
//RFID PART | |
#define SS_PIN 2 //FOR RFID SS PIN BECASUSE WE ARE USING BOTH ETHERNET SHIELD AND RS-522 | |
#define RST_PIN 15 | |
#define No_Of_Card 4 | |
//RFID | |
MFRC522 rfid(SS_PIN,RST_PIN); | |
MFRC522::MIFARE_Key key; | |
//Door and Key | |
byte id_door_a[No_Of_Card][4]={ | |
{21,254,42,26} //RFID NO-1 | |
}; | |
byte id_door_b[No_Of_Card][4]={ | |
{127,192,91,169} //RFID NO-2 | |
}; | |
byte id_door_c[No_Of_Card][4]={ | |
{21,254,42,26}, //RFID NO-1 | |
{127,192,91,169} //RFID NO-2 | |
}; | |
byte id_temp[4][4]; | |
byte i; | |
char bufferr[128]; | |
char state[4]; | |
char c; | |
int count; | |
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) | |
{ | |
Serial.print("Incoming message --> "); | |
Serial.print(topic); | |
Serial.print(" : "); | |
char strState[msglen]; | |
for (int i = 0; i < msglen; i++) | |
{ | |
strState[i] = (char)msg[i]; | |
Serial.print((char)msg[i]); | |
} | |
// mySerial.write('P'); | |
// mySerial.write('S'); | |
// delay(100); | |
// mySerial.readBytes(state, 3); | |
mySerial.write('P'); | |
mySerial.write('S'); | |
Serial.println("PS"); | |
delay(200); | |
c = '0'; | |
count = 0; | |
while (mySerial.available() > 0) { c = mySerial.read(); state[count%3] = c; count++; } | |
delay(500); | |
// Serial.println(state); | |
Serial.println(); | |
Serial.println(state); | |
microgear.chat("server", state); | |
String stateStr = String(strState).substring(0, msglen); | |
//ถ้ามีคำสั่งจาก web มา ให้ส่งไปที่ STM32 และเปิดไฟที่ node | |
// if(mySerial.available()){ | |
if(true){ | |
for(int i = 0; i < stateStr.length(); i++){ | |
mySerial.write(stateStr[i]); | |
} | |
digitalWrite(ledPin, HIGH); | |
delay(1000); | |
} | |
digitalWrite(ledPin, LOW); | |
} | |
void onFoundgear(char *attribute, uint8_t* msg, unsigned int msglen) { | |
Serial.print("Found new member --> "); | |
for (int i=0; i<msglen; i++) | |
Serial.print((char)msg[i]); | |
Serial.println(); | |
mySerial.begin(38400); | |
mySerial.write('P'); | |
mySerial.write('S'); | |
Serial.println("PS"); | |
delay(200); | |
c = '0'; | |
count = 0; | |
while (mySerial.available() > 0) { c = mySerial.read(); state[count%3] = c; count++; } | |
delay(500); | |
// Serial.println(state); | |
Serial.println(); | |
Serial.println(state); | |
microgear.chat("server", state); | |
} | |
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) | |
{ | |
Serial.println("Connected to NETPIE..."); | |
microgear.setAlias(ALIAS); | |
} | |
void setup() | |
{ | |
/* Event listener */ | |
microgear.on(MESSAGE,onMsghandler); | |
microgear.on(PRESENT, onFoundgear); | |
microgear.on(CONNECTED,onConnected); | |
state[0] = '0'; | |
state[1] = '1'; | |
state[2] = '0'; | |
state[3] = '\0'; | |
count = 0; | |
c = '0'; | |
//ต่อกับSerial ของคอมที่ต่อ node | |
Serial.begin(38400); | |
Serial.println("Starting..."); | |
//เปิด UART เชื่อมต่อกับ STM32 | |
pinMode(5, INPUT); | |
pinMode(4, OUTPUT); | |
Serial.println("[NODE] Started"); | |
//ต่อ wifi | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(250); | |
Serial.print("."); | |
} | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
//เปิด Microgear เชื่อมต่อกับ NETPIE และ WEB | |
microgear.init(KEY,SECRET,ALIAS); | |
microgear.connect(APPID); | |
//เปิด SPI เชื่อมต่อ RFID | |
SPI.begin(); | |
rfid.PCD_Init(); | |
for(byte i=0;i<6;i++){ | |
key.keyByte[i]=0xFF; | |
} | |
// pinMode(ledPin,OUTPUT); | |
// digitalWrite(ledPin,HIGH); // Turn off LED | |
} | |
void loop() | |
{ | |
if(microgear.connected()) | |
{ | |
microgear.loop(); | |
// Serial.println("."); | |
} | |
else | |
{ | |
Serial.println("connection lost, reconnect..."); | |
microgear.connect(APPID); | |
} | |
//RFID PART | |
if(!rfid.PICC_IsNewCardPresent()) | |
return; | |
if(!rfid.PICC_ReadCardSerial()) | |
return; | |
//read and print RFID tag | |
for(i=0;i<4;i++) | |
{ | |
id_temp[0][i]=rfid.uid.uidByte[i]; | |
delay(50); | |
} | |
for(i=0;i<No_Of_Card;i++) | |
{ | |
Serial.print((int)id_temp[0][i]); | |
Serial.print(" "); | |
} | |
//ทุกๆครั้งที่ STM32 ส่งค่า state กลับมา ให้ส่งไปที่ web | |
String command1 = "PS"; | |
mySerial.write(command1[0]); | |
mySerial.write(command1[1]); | |
Serial.println("PS"); | |
delay(200); | |
c = '0'; | |
count = 0; | |
while (mySerial.available() > 0) { c = mySerial.read(); state[count%3] = c; count++; } | |
delay(500); | |
Serial.println(state); | |
// microgear.chat("web", bufferr); | |
// microgear.chat("server", Serial.readString()); | |
//ขอประตูปัจจุบันเพื่อเช็คว่า keycard นี้เข้าได้ไหม | |
// mySerial.write("DD"); //Ask STM32 for current door | |
for(int i = 0; i < 2; i++){ | |
mySerial.write('D'); | |
} | |
Serial.println("DD"); | |
delay(200); | |
// mySerial.readBytes(bufferr,1); //Get Ans form STM32 | |
c = '0'; | |
count = 0; | |
while (mySerial.available() > 0) { c = mySerial.read(); bufferr[count%1] = c; count++; } | |
delay(500); | |
Serial.println(bufferr[0]); | |
bool can_open = false; | |
//เช็คว่าบัตรใบนี้ เข้าประตูได้ไหม | |
if(bufferr[0] == '1'){ | |
for(i=0;i<No_Of_Card;i++){ | |
can_open = true; | |
for(int j=0;j<4;j++){ | |
if(id_temp[0][j] != id_door_a[i][j]){ | |
can_open = false; | |
break; | |
} | |
} | |
} | |
} | |
else if(bufferr[0] == '2'){ | |
for(i=0;i<No_Of_Card;i++){ | |
can_open = true; | |
for(int j=0;j<4;j++){ | |
if(id_temp[0][j] != id_door_b[i][j]){ | |
can_open = false; | |
break; | |
} | |
} | |
} | |
} | |
else if(bufferr[0] == '3'){ | |
for(i=0;i<No_Of_Card;i++){ | |
can_open = true; | |
for(int j=0;j<4;j++){ | |
if(id_temp[0][j] != id_door_c[i][j]){ | |
can_open = false; | |
break; | |
} | |
} | |
} | |
} | |
else can_open = false; | |
//ถ้าบัตรใบนี้ เข้าประตูได้ ให้ส่งคำสั่ง "ประตู"+"1" ไปให้ STM32 | |
if(true){ | |
mySerial.write(bufferr[0]); | |
mySerial.write('0'); | |
Serial.print(bufferr[0]); | |
Serial.print('0'); | |
Serial.println(); | |
} | |
rfid.PICC_HaltA(); | |
rfid.PCD_StopCrypto1(); | |
// delay(250); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment