2

I would like my esp8266 to retrieve the mac address of the AP it`s connected to as a client (station), following this discussion on How to get Access Point MAC adress.

Here is my code:

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

For void wifi_handle_event_cb(System_Event_t *evt) I get the following error:

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

How can I fix that?

scopchanov
  • 7,966
  • 10
  • 40
  • 68

2 Answers2

0

Your setup()-function doesn't have it's closing bracket. You should also place all the global variables at the top, or make them static inside the function that uses them. (ref: int value = 0 ,although I don't know what you're using the variable for)

Furthermore, avoid the user of very long delays. delay(30000)causes the ESP8266 IP-stack to behave strangely. You should better use the millis-structure:

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

This answer should help other beginners:
Copy and pasting code without understanding the basics is never a good idea.

Basic structure of an Arduino program (=sketch)

  • Definitions, global vars
    int value = 0;
    should be placed there
  • SETUP
    void setup(){
    All one time functions, initalization routines, etc

    }

  • LOOP
    void loop(){
    recuring tasks call functions from here in the above example
    Call the example function like this
    wifi_handle_event_cb(SomeParamToHandOver);


NEVER use delay - it stops processing for the given time
Not a good idea in server client scenarios ->
look for blink without delay in the Arduino examples for more
Never use while in here it may lead to dead locks

}

  • functions/modules you call from the setup or loop
    void wifi_handle_event_cb(System_Event_t *evt) {
    Exmple from the question
    }
    plus all remarks in the answer above

All beginners should start with https://www.arduino.cc/en/Tutorial/BuiltInExamples

Codebreaker007
  • 2,911
  • 1
  • 10
  • 22
  • Providing those guidlines is appreciated, however this doesn't attempt to answer the question. Please, read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer): _Read the question carefully. What, specifically, is the question asking for? Make sure your answer provides that – or a viable alternative._ – scopchanov Aug 22 '18 at 13:23
  • The question to the error he gets **How can I fix that?** is answered, the problem is not the function ` void wifi_handle_event_cb(System_Event_t *evt) ` which does the job the user askes for, but the missing basics I pointed out. If he follows those steps the code compiles fine. Please try my solution out yourself before giving feedback that it does not answer the question. BTW I am not willing to do the work of the user and post the corrected code, this would not educate but enforce copy & paste culture. – Codebreaker007 Aug 24 '18 at 07:57
  • Just a thought: Writing a good answer requires a bit more effort than that. I am (I think) not a beginner neither in programming, nor in Arduino. Though I have difficulties to follow how this helps in this case, not in general. Maybe if you make more aparent what solution you propose (also with proper structure/formatting), it would look more like _on-the-topic_ and less like general guidlines. This is my point of view. If you think it is worth something, take it into consideration. If not - ignore it. After all, I am not the only one with an opinion around here and the community decides. – scopchanov Aug 24 '18 at 08:20