2017-04-26 54 views
2

我想我esp8266檢索AP的MAC地址,它連接到一個客戶端(站)如何獲得接入點MAC地址時esp8266是在客戶機(站)方式

http://bbs.espressif.com/viewtopic.php?f=15&t=3102&p=10569&hilit=Access+Point+MAC+adress&sid=a68dcff311ea05ece032126d6f93902f#p10569

我收到以下錯誤。對於空隙wifi_handle_event_cb(System_Event_t * EVT) //

C:\用戶\ Tinotenda \桌面\ VER1.0 \ ver1.0.ino:在函數 'void設置()':

VER1.0 :48:錯誤:一個函數的定義在這裏不允許使用前 '{' 標記

空隙環(){

 ^

VER1.0:129:錯誤:預期 '}' 在輸入的端

}

^

退出狀態1 一個函數的定義在這裏不允許使用之前, '{' 令牌

//

我的代碼

#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"); 
} 

回答

0

你的設置()功能沒有它的右括號。 您還應該將所有全局變量放在頂部,或者在使用它們的函數內部使其變爲靜態。 (參考:int value = 0,雖然我不知道你在用什麼變量)

此外,避免用戶很長的延遲。 delay(30000)導致ESP8266 IP堆棧行爲異常。你應該更好地使用millis結構:

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