Other B4R v1.20 BETA - Support for ESP8266 Boards

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
This version adds support for ESP8266 boards. The ESP8266 is a very interesting chip. For a few dollars you get a board that is more powerful than Arduino and with built-in support for wifi. This makes it a great component for IoT solutions.

Working with ESP8266 can be a bit more challenging as Arduino is the de facto standard.

I recommend developers new to B4R, to start with Arduino.

I did all my tests with WeMos D1 R2: http://www.wemos.cc/Products/d1_r2.html

r2_1.jpg


It includes everything required for development and I highly recommend it.

Installation instructions:

1. Open Arduino IDE - File - Preferences and add the following URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json

SS-2016-06-22_17.33.54.png


2. Tools - Board - Boards Manager. Search for esp and install esp8266 by ESP8266 community.

Open the boards selector in B4R and select the serial port and the board type (select the highest upload speed):

SS-2016-06-23_12.16.05.png


B4R includes two ESP8266 specific libraries:
rESP8266WiFi - Similar to rEthernet library. It includes the following types:
ESP8266WiFi - Responsible for connecting or creating the wireless network.
WiFiSocket - Equivalent to EthernetSocket.
WiFiServerSocket - Equivalent to EthernetServerSocket.
WiFiUDP - Equivalent to EthernetUDP

WiFiSocket is compatible with AsyncStreams, MQTT and WebSocketClient libraries.

rESP8266 - Currently only includes D1Pins. This type maps the pins names to the actual pin numbers. Relevant for WeMos boards only.



Working with ESP8266WiFi is simple and similar to working with the Ethernet shield.

Example of a socket connection (depends on rESP8266WiFi and rRandomAccessFile):
B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private wifi As ESP8266WiFi
  Private client As WiFiSocket
  Private astream As AsyncStreams
  Private timer1 As Timer
  Private serverIp() As Byte = Array As Byte(192, 168, 0, 6)
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   'ScanNetworks
   If wifi.Connect("dlink") Then 'change to your network SSID (use Connect2 if a password is required).
     Log("Connected to wireless network.")
   Else
     Log("Failed to connect.")
     Return
   End If
   timer1.Initialize("timer1_Tick", 1000)
   timer1.Enabled = True
   Connect(0)
End Sub

Sub Timer1_Tick
   If client.Connected Then
     astream.Write("Time here is: ").Write(NumberFormat(Millis, 0, 0))
   End If
End Sub

Private Sub Connect(u As Byte)
   If client.ConnectIP(serverIp, 51042) Then
     Log("Connected to server.")
     astream.Initialize(client.Stream, "astream_NewData", "astream_Error")
   Else
     Log("Failed to connect to server")
     CallSubPlus("Connect", 1000, 0)
   End If
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Log("Received: ", Buffer)
End Sub

Sub AStream_Error
   Log("Error")
   CallSubPlus("Connect", 1000, 0)
End Sub

Private Sub ScanNetworks 'ignore
   Dim numberOfNetworks As Byte = wifi.Scan
   Log("Found: ", numberOfNetworks, " networks.")
   For i = 0 To numberOfNetworks - 1
     Log(wifi.ScannedSSID(i))
   Next
End Sub

B4J code:
B4X:
Sub Process_Globals
   Private server As ServerSocket
   Private astream As AsyncStreams
End Sub

Sub AppStart (Args() As String)
   server.Initialize(51042, "server")
   server.Listen
   StartMessageLoop
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
   If Successful Then
     If astream.IsInitialized Then astream.Close
     astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
   End If
   server.Listen
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Log(BytesToString(Buffer, 0, Buffer.Length, "utf8"))
End Sub

Sub AStream_Error
   Log("Error")
End Sub

Sub AStream_Terminated
   Log("Terminated")
End Sub

Notes

- Under the hood there are many differences between ESP8266 and the Arduinos. One of the differences which can be relevant for developers is that the network stream is buffered. If you are writing directly to WiFiClient.Stream then you will need to call WiFiClient.Stream.Flush or the data will not be sent. This is not required when writing with AsyncStreams (which is the recommended way).
- Check the board voltage. The WeMos board is 3.3v.
- Not all libraries are supported. Specifically EEPROM is not supported (there may be others as well).
- WebSocketClient library has moved to the internal libraries folder.
- Serial.Initialize2 has been removed as it is not supported by many boards. It can be accessed with inline C.
- WiFi remote configuration example: https://www.b4x.com/android/forum/threads/esp8266-wifi-remote-configuration.68596/
 
Last edited:

janderkan

Active Member
Licensed User
Thank you for the WiFi.disConnect method.
Like to have the WiFi Mac address.


This works, using global variables.
I cannot figure out how to use global array in C part.

B4X:
sub Process_Globals   
    Dim MacArray(6) As Byte
    Dim M1,M2,M3,M4,M5,M6 As Byte            'ignore
End Sub

Sub MacAddress() As Byte()
    RunNative("getMac", Null)
    bc.ArrayCopy(Array As Byte(M1,M2,M3,M4,M5,M6),MacArray)
    Return MacArray
End Sub
#if C
    #include <ESP8266WiFi.h>
    uint8_t MAC_array[6];
    void getMac(B4R::Object* u) {
         WiFi.macAddress(MAC_array);
         b4r_main::_m1 = MAC_array[0];
         b4r_main::_m2 = MAC_array[1];
         b4r_main::_m3 = MAC_array[2];
         b4r_main::_m4 = MAC_array[3];
         b4r_main::_m5 = MAC_array[4];
         b4r_main::_m6 = MAC_array[5];
    }
#end if
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top