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: 554
Last edited:

Starchild

Active Member
Licensed User
Longtime User
I have updated my "rESP8266extras" library to v1.01
It can be downloaded from the original top post.

v1.01 changes:

ConfigAP() .. now returns true/false so that AP configuration changes can be confirmed.

new functions added:

GetInternetIP() .. returns the current internet IP address in use by this connection.
Host2IP() .. resolves a HostName to it's IP address.
IsBadIP() .. tests if a IP address is valid or not (0.0.0.0 is not valid)

-------------
 

Cableguy

Expert
Licensed User
Longtime User
Sorry, I didn't had the time today to test this out, hopefully tomorrow, or maybe someone else can also try it
 

Starchild

Active Member
Licensed User
Longtime User
I have again updated "rESP8266extras" library now to v1.02
It can be downloaded from the original top post.

v1.02 new function:

ConnectWPS() .. connect to a secure network using WPS mode
SSID() .. returns a string containing the current SSID
GetLocalIP() .. current IP address (as byte array)
GetSubnetMask .. current subnet mask (as byte array)
GetGatewayIP .. current gateway IP (as byte array)

-------------
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
I think I've burned my ESP8266, I have ordered a new one that should arrive in a few weeks... sorry for the delay in posting, and THANK YOU for testing and upgrading the Lib
 

Starchild

Active Member
Licensed User
Longtime User
I have again updated "rESP8266extras" library now to v1.03
It can be downloaded from the original top post.
v1.03 new function:
ConnectWPS2(EEPROM_Position)

Connects to a secure network via WPS and stores the login info in the EEPROM.
The next ConnectWPS2 attempt will use the stored login info to do a fast Reconnect without the need for another WPS.
If the fast Reconnect attempt fails, then a new WPS connection process will be started.
 

Cableguy

Expert
Licensed User
Longtime User
Very cool addition....

Have you taken a look at the ad-hoc possibilities of esp8266?
 

Starchild

Active Member
Licensed User
Longtime User
I have again updated "rESP8266extras" library now to v1.04
It can be downloaded from the original top post.

I have added hardware module specific PIN classes.

. Wemos_LegacyD1_Pins ... (generally these boards are no longer available)
. Wemos_D1_R2_Pins ... (similar to the D1_Pins class in rESP8266 library)
. NodeMCU_ESP12_pins

There is not a lot of differents between them. They each relate to the exact I/O pin descriptions labelled on the PCBs themselves.

The included XML contains pop-up information provided through the IDE to clearly detail the few conflicts that exist physically in the I/O hardware of each module (eg. LED1 and D4 are the same I/O pin). This helps reduce confusion when debugging a design.

Also added one more extra function
. SetPower ... Set the Wifi output power level
 
Last edited:

Starchild

Active Member
Licensed User
Longtime User
How use function for read Vcc voltage ? Where add ADC_MODE(ADC_VCC) ?

I have never tried to implement the get VCC function previously.

It does NOT seem possible to add the line
ADC_MODE(ADC_VCC);
from inside the B4R environment or using a B4R library.

It can only be added using the Arduino IDE suite itself, and must be the first line of the main code tab.

Sorry.
 

Starchild

Active Member
Licensed User
Longtime User
I have again updated "rESP8266extras" library now to v1.05
It can be downloaded from the original top post.

New to version 1-05

I was contacted by user "janderkan" and asked to add the following functions;
AnalogWriteRange(uint32_t Range) .. Allows you to change the PWM range of AnalogWrite.
AnalogWriteFreq(uint32_t Freq) .. Allows you to change the PWM frequency of AnalogWrite.
SerialSwap() .. Swap the Serial I/O port on the GPIO pins ie. GPIO15 (TX) & GPIO13 (RX) >> GPIO1 and GPIO3
SetWifiMode(mode) .. Set the current Wifi mode. use new constants WIFI_MODE_OFF, WIFI_MODE_AP, WIFI_etc.

Also added new pins to classes Wemos_D1_R2_Pins, and NodeMCU_ESP12_pins for defining SPI comms.

To help with checking current Wifi mode I added the function;
PrintWifiDetails() .. It actually calls the function "ESP8266WiFiClass: printDiag" in the ESP environment.

NB: current default Wifi Mode in the ESP environment is WIFI_MODE_AP_STA which supports both ACCESS-POINT and STATION connections.

Hope these additions are useful to others.

PS. Special thanks to "janderkan" for making the requests and doing the testing.
 
Top