当esp8266处于客户端(站)模式时,如何获取接入点的MAC地址?

时间:2017-04-26 13:01:28

标签: callback esp8266 mac-address arduino-ide arduino-esp8266

我希望我的 esp8266 How to get Access Point MAC adress上讨论之后,检索它作为客户端(电台)连接的AP的mac地址。

这是我的代码:

#include <ESP8266WiFi.h>

const char* ssid     = "somrmthing";
const char* password = "somrmthing"; //

const char* host = "aubs.gear.host"; //create webserver & correct address

uint8_t MAC_array[6];
char MAC_char[18];

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  delay(100);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

int value = 0;

void loop() {
  // put your main code here, to run repeatedly:

/*
 * http://stackoverflow.com/questions/34078497/esp8266-wificlient-simple-http-get
 */
  delay(30000);
    ++value;

    /*
     * https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino
     */

  Serial.print("connecting to ");
  Serial.println(host);

// Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }


// getting the mac address  http://bbs.espressif.com/viewtopic.php?f=15&t=3102&p=10569&hilit=Access+Point+MAC+adress&sid=a68dcff311ea05ece032126d6f93902f#p10569
void wifi_handle_event_cb(System_Event_t *evt) 
{
      os_printf("event %x\n", evt->event);
              switch (evt->event){
                      case EVENT_STAMODE_CONNECTED:
                            os_printf("connect to ssid %s, channel %d\n", evt->event_info.connected.ssid, evt->event_info.connected.channel);
                            os_printf("AP MAC address is  %s\n", evt->event_info.connected.bssid);
                      break;

                      case ....
                      ....
              }
}


//old wrong MAC ADDRESS
  // getting the mac address //Serial.println(MAC_char); - See more at: http://www.esp8266.com/viewtopic.php?f=29&t=3587#sthash.bwWPqcc6.dpuf
      WiFi.macAddress(MAC_array);
    for (int i = 0; i < sizeof(MAC_array); ++i){
      sprintf(MAC_char,"%s%02x:",MAC_char,MAC_array[i]);
    }

  // We now create a URI for the request
  String url = "/store.php";                // String url = "/input/";
  url += "?dev_id=";
  url += "BikeShare9";
  url += "&hoster=";
  url += MAC_char;
  url += "&ip_add=";
  url += WiFi.localIP();

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

   // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

对于void wifi_handle_event_cb(System_Event_t *evt),我收到以下错误:

C:\Users\Tinotenda\Desktop\ver1.0\ver1.0.ino: In function 'void setup()':

ver1.0:48: error: a function-definition is not allowed here before '{' token

 void loop() {

             ^

ver1.0:129: error: expected '}' at end of input

 }

 ^

exit status 1
a function-definition is not allowed here before '{' token

我该如何解决?

2 个答案:

答案 0 :(得分:0)

您的设置() - 功能没有关闭括号。 您还应该将所有全局变量放在顶部,或者在使用它们的函数内使它们成为静态变量。 (参考:import QtQuick 2.7 import QtQuick.Controls 2.0 ApplicationWindow { id: win visible: true width: 640 height: 480 title: qsTr("Hello World") TextArea{ font.pixelSize: 20 anchors.fill: parent wrapMode: TextArea.Wrap leftPadding: 100 * win.width/640 rightPadding: 100 * win.width/640 } } ,虽然我不知道你使用该变量的是什么)

此外,避免用户长时间延迟。 int value = 0导致ESP8266 IP堆栈表现异常。你最好使用millis-structure:

delay(30000)

答案 1 :(得分:0)

此答案应该对其他初学者有所帮助:
在不了解基础知识的情况下复制和粘贴代码绝不是一个好主意。

Arduino程序的基本结构(= sketch)

  • 定义,全局变量
        int值= 0;
    应该放在这里
  • 设置
    void setup(){
    所有一次性功能,初始化例程等

    }

  • LOOP
    无效循环(){
    在上面的示例中,从此处调用任务调用函数
    像这样调用示例函数
    wifi_handle_event_cb(SomeParamToHandOver);


永远不要使用延迟-它会在给定的时间内停止处理
在服务器客户端方案中不是一个好主意->
在Arduino示例中寻找眨眼而没有延迟
切勿在此处使用,否则可能导致死锁

}

    您从设置或循环中调用的
  • 功能/模块
    void wifi_handle_event_cb(System_Event_t * evt){
    问题中的例子
    }
    加上以上答案中的所有备注

所有初学者均应以https://www.arduino.cc/en/Tutorial/BuiltInExamples开头