Other B4R v1.20 BETA - Support for ESP8266 Boards

Status
Not open for further replies.

Erel

B4X founder
Staff member
Licensed User
Longtime 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:

positrom2

Active Member
Licensed User
Longtime User
rwblinn:
Thank you for posting the zipped files. They are compiling.
Now I have the problem connecting to the server.
B4R: connected to wireless netwok
Failed to connect to server
B4J: No connection with an ESP
I tried disabling the local firewall and allowing Port 51042 for incoming and outgoing connections in the private network, without success.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
By sudden it works (don't know why, having done several settings to the firewall, restarted both B4R, B4J, reseted the ESP).
Could someone find the time to describe what is really necessary (Win7 prof.)?
 
Upvote 0

Toley

Active Member
Licensed User
Longtime User
Remove the reference to rWifi101. The file shouldn't have been there. There is no such library.
Thank you Erel, this was also my problem. Your example works very well on a NodeMCU 1.0 (by far my favorite ESP8266 board).
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
How comes 'client' and 'server' are hidden in the rESP8266WiFi lib, are they still work in progress?
 
Upvote 0
D

Deleted member 103

Guest
Hi Erel,

I have maybe a stupid question, you can either connect to android phone or iOS phone without a server?
Phone = Server
Board = client
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

I have followed all your setup instructions above and while trying to compile your example I get the following error message.

B4R version: 1.20 BETA #2
Parsing code. (0.00s)
Compiling code. (0.07s)
Building project (0.05s)
Compiling & deploying Ino project (WeMos D1 R2 & mini - COM6) Error

sketch\IRremote.cpp:24:27: fatal error: avr/interrupt.h: No such file or directory
#include <avr/interrupt.h>
^
compilation terminated.
exit status 1
Error compiling for board WeMos D1 R2 & mini.

probably something simple but I can't find it.

Any ideas?

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Not on purpose.....

I use the IRremote in another B4R app only by way of an "#if C / #end If" sub.

I created a new folder "rWeMos" for this app
When I look in the objects/src folder for this app the "IRremote.cpp" plus others has been included.
Does the run/compile do this?
How do I get around it?

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

I removed the lRremote from the library folder in B4R, removed it from the objects/scr folder and it still gives me the same compile error as well as adds the iRremote back into the objects/scr folder.

Does it need to be removed from the arduino libraries folder as well?

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

I have removed the iRremote folder from the Arduino libraries as well and still have the same problem.
Not sure what else to try.

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi All,

Does anyone have an example of using the WeMos board as a server instead of a client?

Thanks in advance
 
Upvote 0
Status
Not open for further replies.
Top