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



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



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):



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:

barx

Well-Known Member
Licensed User
It's funny that you have released this today as today I have ordered myself this exact thing before seeing your post. how crazy is that........?
 

rwblinn

Well-Known Member
Licensed User
Thanks a Lot = Great!
First Experience using the example code above.
  • Connected a NodeMCU 0.9
  • Selected initially as board: NodeMCU 0.9 (ESP-12 Module) (UploadSpeed=115200) - COM6
  • When compiling, got error: c:/users/robert/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: cannot open linker script file {build.flash_ld}: No such file or directory. collect2.exe: error: ld returned 1 exit status; exit status 1; Error compiling for board NodeMCU 0.9 (ESP-12 Module).
  • Found hint that this has to do with the flash size. Changed the board to NodeMCU 0.9 (ESP-12 Module) (Flashsize=4M1M)
  • Compiled but received timeout error. Increased the timeout from 60 to 90 seconds. Compile went OK.
  • Test socket example running fine :)
 

rwblinn

Well-Known Member
Licensed User
Another simple test successfully running: Switch, via B4J server, a LED ON/OFF (connected to ESP8266 pin D4).

B4R Code
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private client As WiFiSocket
    Private astream As AsyncStreams
    Private LEDPin As Pin
    Private serverIp() As Byte = Array As Byte(192, 168, 0, 4)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
     If wifi.Connect2("SSID", "**********") Then
        Log("Connected to wireless network.")
    Else
        Log("Failed to connect.")
         Return
       End If
    LEDPin.Initialize(4, LEDPin.MODE_OUTPUT)
       Connect(0)
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)
    LEDPin.DigitalWrite(Buffer(0) = 1)
    Dim s As String = LEDPin.DigitalRead
    astream.Write("ESP8266 reporting LED Status = ").Write(s)
End Sub

Sub AStream_Error
   Log("Error")
   CallSubPlus("Connect", 1000, 0)
End Sub
B4J Code
B4X:
Sub Process_Globals
   Private server As ServerSocket
   Private astream As AsyncStreams
   Private Timer1 As Timer
   Private LEDOn As Boolean = False
End Sub

Sub AppStart(Args() As String)
   server.Initialize(51042, "server")
   server.Listen
   Timer1.Initialize("Timer1", 2000)
   Timer1.Enabled = True
   StartMessageLoop
End Sub

Sub Timer1_Tick
    Dim b(1) As Byte
    LEDOn = Not(LEDOn)
    If LEDOn Then
        b(0) = 1
    Else
        b(0) = 0
    End If
    Log($"${DateTime.Time(DateTime.Now)}: Setting ESP8266 LED to ${b(0)}"$)
    astream.Write(b)
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
Output
B4X:
20:19:43: Setting ESP8266 LED to 1
ESP8266 reporting LED Status = true
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Hi,

tested MQTT sucessfully with B4R as client and a B4J server.
The test experiments (B4R & B4J code) attached. As hardware a NodeMCU 0.9 (ESP-12 Module) (Flashsize=4M1M) is used.
  • Switch LED ON/OFF connected to ESP8266 pin D2 via B4J server using MQTT (ledcontrolmqtt.zip)
  • DHT11 sensor temperature & humidity publish to server using MQTT (dht11mqtt.zip)

 

Attachments

rwblinn

Well-Known Member
Licensed User
Hi,

an observation:
Compiling and Deploying does not give an error in the compiler information window, if no ESP8266 is connected to a COM port.
An error occurs in the log after the compiler information window closes.

To consider After compiling is completed and no ESP8266 connected, to provide a message like Compile successfull, deploy failed as no connected device found.

Example
compiler information window

B4X:
Building project    (0.14s)
Compiling & deploying Ino project (NodeMCU 0.9 (ESP-12 Module) - COM6)    (15.89s)
    Sketch uses 238,589 bytes (22%) of program storage space. Maximum is 1,044,464 bytes.
    Global variables use 32,972 bytes (40%) of dynamic memory, leaving 48,948 bytes for local variables. Maximum is 81,920 bytes.
  
Completed successfully.
log
B4X:
Error opening port
jssc.SerialPortException: Port name - COM6; Method name - openPort(); Exception type - Port not found.
 

Toley

Active Member
Licensed User
Hi Erel, I always get this error when I try to compile. Sorry it's in french but easy to understand, it says it cannot find the rwifi101 library but it is there.
B4R version: 1.20 BETA #2
Parsing code. (0.00s)
Compiling code. (0.01s)
Building project Error
Impossible de trouver une partie du chemin d'accès 'C:\Program Files (x86)\Anywhere Software\B4R\libraries\rwifi101'.
 

barx

Well-Known Member
Licensed User
My board arrived today, got a simple example running just listing networks to see if it works. And of course it does. For the price, this thing is brill. just adding wifi is worth the money alone. The only downside I see it that it is 3.3v logic. There is alway lgic level converters though if you need to talk 5v.
 

positrom2

Active Member
Licensed User
Another simple test successfully running: Switch, via B4J server, a LED ON/OFF (connected to ESP8266 pin D4).
Does not compile (B4R).
1.: Syntax error: Missing Gänsefüßchen (") (Line 14) (being too picky?:()
2.: "Ein Teil des Pfades "C:\Program Files (x86)\Anywhere Software\B4R\libraries\rwifi101" konnte nicht gefunden werden".
My libraries tree after having shuffeled the ESP entries to the Additional Libraries folder (path setting is ok) look as this: But I am not sure about the proper organization.
What does rwifi101 mean?
upload_2016-6-25_21-30-58.png
 

rwblinn

Well-Known Member
Licensed User
Does not compile (B4R).
1.: Syntax error: Missing Gänsefüßchen (") (Line 14) (being too picky?:()
2.: "Ein Teil des Pfades "C:\Program Files (x86)\Anywhere Software\B4R\libraries\rwifi101" konnte nicht gefunden werden".
My libraries tree after having shuffeled the ESP entries to the Additional Libraries folder (path setting is ok) look as this: But I am not sure about the proper organization.
What does rwifi101 mean?
View attachment 45437
Regarding 1:
The password must be in "" (I forgot that in during copy paste = post updated)
B4X:
If wifi.Connect2("SSID", "**********") Then
Regarding 2:
The B4R Internal Lib folder I use after installing B4R = this includes the rWiFi101.
upload_2016-6-25_21-59-2.png


Suggest to consider installing B4R new, to ensure the lib folder is setup correct.
 

positrom2

Active Member
Licensed User
rwblinn:
Thanks to your answer.
Prior to reinstalling B4R I adjusted the libraries folder to your image above.
Afterwards I restarted B4R and received the same rwifi101 error as posted above.
The error message translated is:
"Part of the path .... could not be found. " So apparently some part of the lib is found.
Afterwards I reloaded B4R Beta from this link:
https://www.b4x.com/android/forum/threads/b4r-beta-is-available-for-download.65651/page-2
That turned out to be an old version not compatible with your code.
Then I reloaded the hopefully most recent version from this link:
http://www.b4x.com/b4r/files/beta.exe
and installed as administrator (Win7).
This yields the same rwifi101 error as posted above.
So, there are two problems:
Links to different versions of B4R (a B4X site problem)
The "part of rwifi101" problem.

https://www.b4x.com/android/forum/members/rwblinn.40947/
 

positrom2

Active Member
Licensed User
Thanks, Erel.
rwblinn: I think it is not very useful for promoting B4R to post code that has not been tested in the posted version.
Now "proceeding", B4J throws this error:
Program started.
An error occurred:
(Line: 0) null
java.lang.Exception: Sub appstart signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4j.example.main_subs_0._appstart(anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
 

rwblinn

Well-Known Member
Licensed User
Hi,

thanks for feedback:
I did test the code with the latest B4R 1.20 (using the link in post #1) beta and no issues (else really would not post). To go for sure, tested again and worked fine.
The B4J project is a non-ui app, so ensure when creating a new B4J project it is of type non-ui.

Please find the project attached used for testing - hope that helps.
 

Attachments

barx

Well-Known Member
Licensed User
Is it possible to add the wifi.status?

All the examples I have seen seem to use the following scheme

B4X:
 WiFi.begin(WiFiSSID, WiFiPSK);

  while (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(LED_PIN, ledStatus); // Write LED high/low
    ledStatus = (ledStatus == HIGH) ? LOW : HIGH;

    delay(100);
  }
I'm assuming that WiFi.begin() is our wifi.connect() or wifi.connect2().

but as I see it we don't have a way of checking if we actually have connected? unless I've missed something....
 
Status
Not open for further replies.
Top