lundi 29 avril 2019

Sending data to web by connected to Mac Address of Esp8266

I know the Mac address of my esp8266 but i don't need to send it through esp8266 ip address. i need to connect to the server through esp8266 mac address. so every time i don't want to manually change the ip address and burn the code.

const char server[] = "192.168.43.189"; 

I need that my server string should use mac address rather than ip address and can communicate with web.

this line connect esp8266 to the server: 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);
  }
}




Aucun commentaire:

Enregistrer un commentaire