สอบถามครับ ผมใช้ RFID-RC522 เชื่อกับดาต้าเบส ผ่านesp8266 แต่ส่งค่าไม่ได้ ขึ้น -1 และนานๆที่ จะ ขึ้น -11 ผมต้องแก้ตรงไหน ครับ
Code ที่ผมใช้อยู่
**********************************************************************
#include <ESP8266WiFi.h> //Include Esp library
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h> //include RFID library
#define SS_PIN D8 //RX slave select
#define RST_PIN D3
#define RedLed D1
#define BlueLed D2
#define GreenLed D0
#define BUZZER_PIN D4
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
/* Set these to your desired credentials. */
//const char *ssid = "AndroidAP"; //ENTER YOUR WIFI SETTINGS
//const char *password = "1122334455";
////Web/Server address to read/write from
//const char *host = "192.168.43.214"; //IP address of server
const char *ssid = "TPST_2.4G"; //ENTER YOUR WIFI SETTINGS
const char *password = "A123456789";
//Web/Server address to read/write from
const char *host = "192.168.88.80"; //IP address of server
String getData , Link;
String CardID = "";
IPAddress ip;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
delay(1000);
Serial.begin(115200);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting to ");
Serial.print(ssid);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.println("Connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
pinMode(RedLed, OUTPUT);
pinMode(BlueLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
}
void loop() {
digitalWrite(BUZZER_PIN, LOW);
if (WiFi.status() != WL_CONNECTED) {
WiFi.disconnect();
WiFi.mode(WIFI_STA);
Serial.print("Reconnecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
//look for new card
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;//got to start of loop if there is no card present
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;//if read card serial(0) returns 1, the uid struct contians the ID of the read card.
}
for (byte i = 0; i < mfrc522.uid.size; i++) {
CardID += mfrc522.uid.uidByte[i];
}
HTTPClient http; //Declare object of class HTTPClient
//GET Data
getData = "?CardID=" + CardID; //Note "?" added at front
ip = WiFi.localIP();
//Link = "http://" + ip.toString() + "/tps_hr/test.php" + getData;
Link = "http://192.168.88.80/tps_hr/test.php" + getData;
Serial.println(Link);
http.begin(Link);
int httpCode = http.GET(); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
Serial.println(CardID); //Print Card ID
if (httpCode == 200) {
if (payload == "login") {
digitalWrite(RedLed, HIGH);
Serial.println("red on");
delay(500); //Post Data at every 5 seconds
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
}
if (payload == "logout") {
digitalWrite(BlueLed, HIGH);
Serial.println("Blue on");
delay(500); //Post Data at every 5 seconds
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, LOW);
}
if (payload == "succesful" || payload == "Cardavailable") {
digitalWrite(GreenLed, HIGH);
delay(500);
digitalWrite(BUZZER_PIN, HIGH);
delay(300);
digitalWrite(BUZZER_PIN, LOW);
delay(300);
digitalWrite(BUZZER_PIN, HIGH);
delay(300);
digitalWrite(BUZZER_PIN, LOW);
}
}
else {
Serial.println("No");
}
delay(5000);
CardID = "";
getData = "";
Link = "";
http.end(); //Close connection
digitalWrite(RedLed, LOW);
digitalWrite(BlueLed, LOW);
digitalWrite(GreenLed, LOW);
}
//=======================================================================
จากรูปเช็คได้ดังนี้
เช็คว่าโค้ด PHP MySQL ทำงานถูกต้อง โดยพิมพ์ url เข้าไปทางหน้าเว็บโดยตรง อาจจะมีปัญหากับตรงนี้ครับ
ผม ปิด windows firewall แล้วสามารถส่งข้อมูลได้ ดูจากวงสีดำ หลังจากนั้นพอแสกนบัตร ขึ้น -1 ตลอดเลย ครับ
เกิดจากบอร์ดรับสัญญาน wifi ไม่เสถียร หรือ จาก Code รึเปล่าครับ
ESP8266 ผลิตจากบริษัทระดับโลก
จากการใช้งานจริง เปิดมา 1 ปี 24 ชั่วโมง โดยไม่ปิด ยังไม่มีปัญหา
อุปกรณ์ IoT ที่จำหน่ายใช้กันทั่วโลก เช่น Sonoff ที่ใช้สำหรับเปิด/ปิดไฟผ่านโทรศัพท์มือถือ ก็ใช้ ESP8266
ตามรูปสามารถเชื่อมต่อได้ปกติ ส่งข้อมูลได้ถูกต้อง ปัญหาน่าจะมาจาก Code ในบางจุด
สำหรับตัวบอร์ด ESP8266 สามารถเช็คความสเถียรได้ โดยรันโค้ดง่าย ๆ เช่นโค้ดไฟกระพริบ หรือโค้ดสั่งเปิด/ปิดไฟด้วย wifi แนะนำใช้ App Blynk จะเขียนโค้ดง่ายมาก เปิดทิ้งไว้ตลอล 24 ชั่วโมง 7 วัน เพื่อทดลองได้ครับ