B4R Library ESP8266extras Library

I wanted to be able to assign a new IP address when running the ESP8266 in AP mode so I started to look into writing a library for B4R. Once I started to look inside the ESP and WIFI classes in Arduino I realised that there are many interesting and useful functions available.

I started with the ESP8266 library provided in B4R v1.20 and wrapped a lot of these extra functions together with a little extra code to make some of them easier to use.

ConfigAP
GetMacAddress
DeepSleep
ResetReason
UniqueID
and many other.

Have a look at the test code below to see the extra functions available.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private esp As ESP8266extras
    Private Wifi As ESP8266WiFi
    Private EE As EEPROM
    Private ip(4) As Byte
    Private mac(6) As Byte
    Private bc As ByteConverter
    Private count As Long
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("                               ")
    Log("AppStart: ",count)
    count = count + 1

    Log("Reset: ",esp.ResetReason," - ",esp.ResetReasonMessage)
    esp.GetAPIP(ip)
    Log("Original AP IP: ",esp.IP2str(ip))
    esp.ConfigAP("192.168.110.12","0.0.0.0","255.255.255.0")
    Wifi.StartAccessPoint("ABC")
    esp.GetAPIP(ip)
    Log("New AP IP: ",esp.IP2str(ip))  'same as Wifi.AccessPointIp
    Log("Local IP: ",Wifi.LocalIp)
    Log("UniqueID: ",esp.UniqueID)
    Log("EEPROM Size: ",EE.Size)
    Log("FlasfID: ",esp.FlashChipId)
    Log("FlahsSize: ",esp.FlashChipSize)
    Log("FlashSpeed: ",esp.FlashChipSpeed)
    Log("CPU Speed MHz: ",esp.CpuFreqMHz)
    Log("Program Size: ",esp.SketchSize)
    Log("Free Space: ",esp.FreeSketchSpace)
    Log("CycleCount: ",esp.CpuCycleCount)
    Log("HeapSpace: ",esp.FreeHeapSpace)
    esp.GetMacAddress(mac)
    Log("MAC: ",bc.HexFromBytes(mac))
    Log("")
    esp.DeepSleep(5000000,esp.WAKE_RFCAL)    'every 5sec
    Log("NEVER CONTINUES")
End Sub

I hope it's of use to others.

PS. The DeepSleep function causes a RESET when it wakes up. B4R currently starts again clearing all variables and structures making a Sleep mode little more function than just power savings. You would currently need to hold values in EEPROM to remember information after waking up. It needs looking into further.

Some useful reference links I have found are:
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Esp.cpp
http://arduino-kit.ru/userfiles/image/Kolbans-Book-on-the-ESP8266-October-2015_b.pdf
http://www.esp8266.com/wiki/doku.php?id=esp8266_power_usage

Library now updated to v1.07
see posts below for details.
 

Attachments

  • rESP8266extras 1-07.zip
    9.4 KB · Views: 673
Last edited:

bdunkleysmith

Active Member
Licensed User
Longtime User
As per the inline code in post #7 here Use of ESP8266 WiFi.BSSID in inline code
I updated the ESP8266extras library to include the function:

GetAPMacAddress

which returns the MAC address of the access point to which the ESP8266 is connected.

The library updated to version 1.06 is attached.

Usage:

B4X:
Sub Process_Globals

    Private MacArray(6) As Byte

End Sub

Sub ConnectToNetwork

        esp.GetAPMacAddress(MacArray)
        Log("MAC address of AP connected to: ", bc.HexFromBytes(MacArray))

End Sub
 

Attachments

  • rESP8266extras 1-06.zip
    9.4 KB · Views: 501
Last edited:

BertI

Member
Licensed User
Longtime User
I have tried using the following inline C to get the current WiFi mode:
B4X:
  void WiFiGetMode(B4R::Object* o) {
  b4r_main::_wifimode = WiFi.getMode();
  }
However the results from this seem not to be the same as the values used in the rESP8266extras library. Specifically, in the library, AP mode has a value of 1 and STA mode a value of 2 whereas the values returned by the code above are 2 and 1 respectively. Although I can see the switch statement in the library's cpp file I don't know where the constants WIFI_AP and WIFI_STA are defined. I noted that at the AT command level the ESP8266 documentation defines Station mode as a value of 1 and AP mode a value of 2 (so like the return from the inline C). Is there a reason for this discrepancy?
 

Starchild

Active Member
Licensed User
Longtime User
I have tried using the following inline C to get the current WiFi mode:
B4X:
  void WiFiGetMode(B4R::Object* o) {
  b4r_main::_wifimode = WiFi.getMode();
  }
However the results from this seem not to be the same as the values used in the rESP8266extras library. Specifically, in the library, AP mode has a value of 1 and STA mode a value of 2 whereas the values returned by the code above are 2 and 1 respectively. Although I can see the switch statement in the library's cpp file I don't know where the constants WIFI_AP and WIFI_STA are defined. I noted that at the AT command level the ESP8266 documentation defines Station mode as a value of 1 and AP mode a value of 2 (so like the return from the inline C). Is there a reason for this discrepancy?

Yes. The WIFI_MODE_AP and WIFI_MODE_STA constants declared in the rESP8266extras library were swapped by mistake. This wouldn't have caused any problems calling the declared functions in ESP826extras, but they did not match those returned by Wifi.getMode();

I have updated the library in the top post to v1.06 and fixed this mistake.
 

BertI

Member
Licensed User
Longtime User
Yes. The WIFI_MODE_AP and WIFI_MODE_STA constants declared in the rESP8266extras library were swapped by mistake. This wouldn't have caused any problems calling the declared functions in ESP826extras, but they did not match those returned by Wifi.getMode();

I have updated the library in the top post to v1.06 and fixed this mistake.
Ah yes, thanks, I understand now. BTW there is already a version 1.06 (post 22) which contains additional functions introduced by another user so maybe have to incorporate these into your update and make into v1.07?
 

Starchild

Active Member
Licensed User
Longtime User
Ah yes, thanks, I understand now. BTW there is already a version 1.06 (post 22) which contains additional functions introduced by another user so maybe have to incorporate these into your update and make into v1.07?
Yep. I grabbed the v1.06 from post 22 and made the same changes to that one.
Now updated this new version 1.07 in my top post of this tread.
 
Top