ESP8266 Httpclient get方法有效,但不适用于mfrc522

时间:2018-06-25 06:28:26

标签: arduino request client esp8266 rfid

我的ESP8266有一个我确实可以理解的问题。我目前正在开发一个安全系统,该系统必须使用一些RFID卡来管理门的访问。

因此,我创建了一个C#Web API + SQL数据库来管理人员和每个RFID卡。

Web api是基本的,您只需发送“ http:/// api / values /”即可,如果您不在数据库中,则将得到“ GO”或“ Deny”。

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

void setup () {

  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.print("Connecting..");

  }

}

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    HTTPClient http;  //Declare an object of class HTTPClient

    http.begin("http://jsonplaceholder.typicode.com/users/1");  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request

    if (httpCode > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload

    }

    http.end();   //Close connection

  }

  delay(30000);    //Send a request every 30 seconds

}

...但是一旦我添加了RFID代码...该请求将不再起作用...

代码看起来像这样

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 5
#define RST_PIN 4

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

const char* ssid = "mywifi";
const char* password = "Password";

void setup () {

  Serial.begin(9600);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.print("Connecting..");

  }
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522

}

void loop() {
// Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }

  if (WiFi.status() == WL_CONNECTED) {
    //Check WiFi connection status

    HTTPClient http;  //Declare an object of class HTTPClient
    //http://<myip>/api/values/<cardCode>
    http.begin("http://<192.168.1.5>/api/values/1231");  //THIS IS A TEST, IT SHOULD WORK WITHOUT ANY PROBLEM!
    int httpCode = http.GET();                                                                  //Send the request

    if (httpCode > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload

    }

    http.end();   //Close connection

  }

  delay(30000);    //Send a request every 30 seconds

}

这里的问题是我的httpCode返回-1 ...现在响应...

我不明白的是为什么... ESP可以运行...但是没有MFRC522库吗?

提前谢谢!

0 个答案:

没有答案