B4R Question esp8266 - how to initialize

derez

Expert
Licensed User
Longtime User
I have a ESP8266 which is the version -01, not the WeMos card.

upload_2016-7-27_0-5-55.png

I managed to connect it to Arduino and make it connect as client to my local wifi network (using arduino sketch that implements just SoftwareSerial), following this link http://www.martyncurrey.com/arduino-esp8266/
I can send AT commands and get response accordingly.
I tried to do the same with B4R, using softserial but I only get the same string that I send. I tried to add "\n" and "\r" but it does not change the situation.
I tried to use ESP8266WiFi library but get error:
Compiling & deploying Ino project (Arduino/Genuino Uno - COM3) Error

In file included from sketch\B4RDefines.h:27:0,
from D:\B4R\WifiPir\Objects\src\src.ino:1:
sketch\rESP8266WiFi.h:3:25: fatal error: ESP8266WiFi.h: No such file or directory
#include <ESP8266WiFi.h>
^
compilation terminated.
I need to tell the device both the two pins that connect by the SoftSerial and the serverIP and port that ESP8266Wifi use.
Please help.

This is the code that does not work with softserial only but I guess I need the esp8266wifi anyway to continue with connection to the server.
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private astream As AsyncStreams
   Private tmr As Timer

   Private txt As String = "AT"  'Array As Byte ("A","T")
   Private softserial As SoftwareSerial
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")

   softserial.Initialize(115200, 10, 11)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
 
    tmr.Initialize("tmr_Tick",2000)
    tmr.Enabled = True
End Sub

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

Sub AStream_Error
   Log("error")
End Sub

Sub tmr_Tick
    astream.Write(txt.GetBytes)
End Sub

The logs:
AppStart
AppStart
Received: AT

The sketch that I tried to imitate is this (with the monitor set to NL&CR) :
B4X:
#include <SoftwareSerial.h>
SoftwareSerial softSerial(10,11); // RX, TX

void setup()
{
  uint32_t baud = 115200;
  Serial.begin(baud);
  softSerial.begin(baud);
  Serial.print("SETUP!! @");
  Serial.println(baud);
}

void loop()
{
    while(softSerial.available() > 0)
    {
      char a = softSerial.read();
      if(a == '\0')
        continue;
      if(a != '\r' && a != '\n' && (a < 32))
        continue;
      Serial.print(a);
    }

    while(Serial.available() > 0)
    {
      char a = Serial.read();
      Serial.write(a);
      softSerial.write(a);
    }
}
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
When the esp is initialised as server the IP is a default one (192.268.4.1 if I'm not mistaken) and at this moment that cannot be changed...
 
Upvote 0

derez

Expert
Licensed User
Longtime User
When the esp is initialised as server the IP is a default one (192.268.4.1 if I'm not mistaken) and at this moment that cannot be changed...
This is a different case, the esp is set as a client and gets the IP that I allocated for it in the router (by its mac address).
The IP and port that I need to code are the server's.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
You haven't selected the correct board type in the boards manager.
As you can see in the picture, the board that is connected to the com port is the arduino uno. The ESP cannot connect directly, it can be connected either by FTDI or Arduino. I succeeded in both. The connection to the esp-01 is by softwareserial.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Programmed the ESP with this program:
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, 119)
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   'ScanNetworks
   If wifi.Connect2("rderez","xxx...") 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
 
    Dim s As Byte = 1
     
    If client.Connected Then
        astream.Write(Array As Byte(s))
    End If
    Log("Detection ", s)

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


Got the following error:

B4R version: 1.20 BETA #2
Parsing code. (0.00s)
Compiling code. (0.01s)
Building project (0.04s)
Compiling & deploying Ino project (Generic ESP8266 Module - 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 Generic ESP8266 Module.

I tried several other esp boards definition but the output error is the same.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I used the non-beta with its ESP8266 libs, the program compiled but nothing happens, no logs and no response from the server.
How do I use the IR library ?
I looked at links how to program the board (arduino IDE -I was not successful, loading always fail) and saw that GPIO 2 has to be grounded for the loading. Is it required in B4R as well ? It does not change anything.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Grounding GPIO 0 while running the program it was loading - the blue led on the esp and the led on the ftdi were blinking for about 30 seconds.
There were no logs and the program is not running even when the GPIO is not grounded anymore.
 
Upvote 0
Top