通过连接到Esp8266的Mac地址将数据发送到Web

时间:2019-04-29 16:38:21

标签: web esp8266 arduino-ide arduino-esp8266

我知道esp8266的Mac地址,但是我不需要通过esp8266 ip地址发送它。我需要通过esp8266 mac地址连接到服务器。所以每次我不想手动更改IP地址并刻录代码时。

const char server[] = "192.168.43.189"; 

我需要我的服务器字符串使用mac地址而不是IP地址,并且可以与Web进行通信。

此行将esp8266连接到服务器:if (client.connect(server, 80))

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

    constexpr uint8_t RST_PIN = 0;          // Configurable, see typical pin 
    layout above
    constexpr uint8_t SS_PIN = 15;         // Configurable, see typical pin 
    layout above

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

    const char server[] = "192.168.43.189"; 

    const char* MY_SSID = "project";
    const char* MY_PWD =  "12345678";

    WiFiClient client;

    unsigned long getID(){
    if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial 
    and continue
    return -1;
    }
    unsigned long hex_num;
    hex_num =  mfrc522.uid.uidByte[0] << 24;
    hex_num += mfrc522.uid.uidByte[1] << 16;
    hex_num += mfrc522.uid.uidByte[2] <<  8;
    hex_num += mfrc522.uid.uidByte[3];
    mfrc522.PICC_HaltA(); // Stop reading
    return hex_num;
    }


    void setup() {
     Serial.begin(115200);   // Initialize serial communications with the PC
     while (!Serial);    // Do nothing if no serial port is opened (added 
     for Arduinos based on ATMEGA32U4)
     SPI.begin();      // Init SPI bus
     mfrc522.PCD_Init();   // Init MFRC522
     mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 
     Card Reader details
     Serial.println(F("Scan PICC to see UID, SAK, type, and data 
     blocks..."));
     Serial.print("Connecting to "+*MY_SSID);
     WiFi.begin(MY_SSID, MY_PWD);
     Serial.println("going into wl connect");

     while (WiFi.status() != WL_CONNECTED) //not connected,  ...waiting to 
     connect
      {
        delay(1000);
        Serial.print(".");
      }
      Serial.println("wl connected");
      Serial.println("");
      Serial.println("Credentials accepted! Connected to wifi\n ");
      Serial.println("");


      if(mfrc522.PICC_IsNewCardPresent()) {
       unsigned long uid = getID();
       if(uid != -1){
        Serial.print("Card detected, UID: "); 
        Serial.println(uid);
        // if you get a connection, report back via serial:
         if (client.connect(server, 80)) {
           Serial.println("connected");
           // Make a HTTP request:
          client.println("GET 
                    /Embedded_System/EmbeddedSystem/EmbeddedSystem/match.php HTTP/1.1");
          client.println("Host: 192.168.43.189");
          client.println("Connection: close");
          client.println();
    }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
    }
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    if(c == 1){
      Serial.print("1");
    } else {
      Serial.print("2");
    }
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

0 个答案:

没有答案