【问题标题】:How to get the MAC adress of an access point when esp8266 is in client (station) mode?当esp8266处于客户端(站)模式时,如何获取接入点的MAC地址?
【发布时间】:2017-04-26 13:01:27
【问题描述】:

我希望我的 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

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    您的 setup() 函数没有右括号。 您还应该将所有全局变量放在顶部,或者在使用它们的函数中将它们设为静态。 (参考:int value = 0,虽然我不知道你用这个变量做什么)

    此外,避免给用户带来很长的延迟。 delay(30000)导致 ESP8266 IP 堆栈行为异常。你最好使用millis结构:

    static unsigned long lastMillis = 0;
    if (millis() - lastMillis < 30 * 1000UL) {
      lastMillis = millis();
      //do your stuff
    }
    

    【讨论】:

      【解决方案2】:

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

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

      • 定义,全局变量
        整数值 = 0;
        应该放在那里
      • 设置
        无效设置(){
        所有一次性函数、初始化例程等

        }

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


      永远不要使用延迟 - 它会在给定的时间内停止处理
      在服务器客户端场景中不是一个好主意 ->
      立即在 Arduino 示例中查找闪烁以获取更多信息
      千万不要在这里使用它可能会导致死锁

      }

      • 函数/模块您从设置或循环中调用
        void wifi_handle_event_cb(System_Event_t *evt) {
        问题示例
        }
        加上上面答案中的所有评论

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

      【讨论】:

      • 感谢提供这些指导方针,但这并不试图回答这个问题。请阅读How do I write a good answer?仔细阅读问题。具体来说,这个问题要问的是什么?确保您的答案提供了这一点——或者一个可行的替代方案。
      • 他得到的错误的问题 我该如何解决这个问题? 已回答,问题不是函数 ` void wifi_handle_event_cb(System_Event_t *evt) ` 完成这项工作用户要求,但我指出缺少基本知识。如果他遵循这些步骤,代码编译得很好。在给出没有回答问题的反馈之前,请自己尝试我的解决方案。顺便说一句,我不愿意做用户的工作并发布更正的代码,这不会教育而是强制复制和粘贴文化。
      • 只是一个想法:写一个好的答案需要更多的努力。我(我认为)不是编程初学者,也不是 Arduino 初学者。虽然我很难理解这在这种情况下是如何帮助的,但不是一般情况下。也许如果您更清楚地提出您提出的解决方案(也具有适当的结构/格式),它看起来更像 on-the-topic 而不像一般指导方针。这是我的观点。如果您认为它有价值,请考虑一下。如果不是 - 忽略它。毕竟,我不是唯一一个有意见的人,社区决定。
      猜你喜欢
      • 2017-07-24
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多