B4R Question Astream_Newdata event not getting fired...

rbghongade

Well-Known Member
Licensed User
Longtime User
Dear friends,
I am experimenting with EBYTE E32-433T30D modules for LORA. The modules are correctly configured. I am able to transmit and receive data using two NODEMCU devices using c/c++ code in Arduino IDE. But when I tried the same(similar!) approach with B4R , the transmit code works fine but the receive code fails. The RX pin activity is seen as I have used D4( GPIO2 with built-in LED). The Astream_Newdata event does not fire at all.
B4R version:3.00
ESP8266 ARDUINO CORE version:2.6.3
Here is my transmit and receive code :
[CODE lang="b4x" title="TX CODE"]Sub Process_Globals
Public Serial1 As Serial
Public softserial As SoftwareSerial
Private d1 As NodeMCU_ESP12_pins
Private timer1 As Timer
Private ser As B4RSerializator
Dim TEMP As Float
Dim HUMIDITY As Float
Private astream As AsyncStreams
Dim i As Long
End Sub

Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
softserial.Initialize(9600,d1.D4,d1.D3)
astream.Initialize(softserial.Stream,"astream_Newdata","astream_Error")
RunNative("setup",Null)
Delay (100)
timer1.Initialize("timer1_Tick",5000)
timer1.Enabled=True

End Sub

Sub Timer1_Tick

RunNative("read",Null)
Log(TEMP)
Log(HUMIDITY)
Dim msg As String=JoinStrings(Array As String(i,",",TEMP,",",HUMIDITY,CRLF))
astream.Write(msg.GetBytes)
Log(msg)
i=i+1
End Sub

Sub astream_Newdata (Buffer() As Byte)
Log("NewData from ESP:",Buffer)
End Sub

Sub Astream_Error
Log("Error.")

End Sub



#if C


#include <Wire.h>
#include "Adafruit_SHT31.h"
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup(B4R::Object* o){
sht31.begin(0x44);
}
void read (B4R::Object* o) {

b4r_main::_temp=sht31.readTemperature();
b4r_main::_humidity=sht31.readHumidity();
}
[/CODE]

[CODE lang="b4x" title="RX CODE"]Sub Process_Globals
Public Serial1 As Serial
Private astream As AsyncStreams
Private softserial As SoftwareSerial
Dim d1pins As NodeMCU_ESP12_pins
Dim bc As ByteConverter

End Sub

Private Sub AppStart
Serial1.Initialize(115200)
Log("Appstart")
softserial.Initialize(9600, d1pins.D4, d1pins.D3)
astream.Initialize(softserial.Stream, "Astream_Newdata", "Astream_Error")

End Sub

Sub Astream_Newdata (Buffer() As Byte)
Log(Buffer)
End Sub

Sub Astream_Error
Log("Error.")
End Sub[/CODE]


[CODE lang="clike" title="TX CODE C/C++ WORKING"]#include <SoftwareSerial.h>
#include <Arduino.h>
#include <Wire.h>
#define SOFT_TX D3
#define SOFT_RX D4
#include "Adafruit_SHT31.h"

Adafruit_SHT31 sht31 = Adafruit_SHT31();

SoftwareSerial softSerial(SOFT_RX, SOFT_TX); // RX, TX
long i;
void setup() {
pinMode(SOFT_RX, INPUT);
pinMode(SOFT_TX, OUTPUT);

sht31.begin(0x44);
Serial.begin(115200);
softSerial.begin(9600);

}

void loop() {
String msg="";
i++;
float t = sht31.readTemperature();
float h = sht31.readHumidity();
msg=msg+i+","+t+","+h+"\r";
softSerial.println (msg );
delay(2000);
Serial.println (msg);
delay(2000);
}[/CODE]
[CODE lang="clike" title="RX CODE C/C++ WORKING"]#include <SoftwareSerial.h>
#define SOFT_TX D3
#define SOFT_RX D4
SoftwareSerial softSerial(SOFT_RX, SOFT_TX); // RX, TX
void setup() {
pinMode(SOFT_RX, INPUT);
pinMode(SOFT_TX, OUTPUT);
Serial.begin(115200);
softSerial.begin(9600);
}


void loop() {
if(softSerial.available()){
char i;
i = softSerial.read();
Serial.print(i);
delay (10);

}
}[/CODE]
 
Had tried that already with no effect! In fact I simply cross-connected the two NODEMCU devices directly and still am having the same problem.
 
Upvote 0
D3 is your output pin and D4 is the input pin. Where is the Initialize routines for these two pin's ?
B4X:
Pin4.Initialize(4,Pin4.MODE_INPUT)
Pin3.Initialize(3,Pin3.MODE_OUTPUT)
 
Last edited:
Upvote 0
Have tried this:
[CODE lang="b4x" title="RX CODE" highlight="4,5"]Private Sub AppStart
Serial1.Initialize(115200)
Log("Appstart")
rx.Initialize(d1pins.D4,rx.MODE_INPUT)
tx.Initialize(d1pins.D3,tx.MODE_OUTPUT)
softserial.Initialize(9600, d1pins.D4, d1pins.D3)
astream.Initialize(softserial.Stream, "Astream_Newdata", "Astream_Error")
End Sub[/CODE]
 
Upvote 0
Have tried with different pins, but no luck yet! (The Arduino code works properly so no issue with pins mostly!)
However when I used the hardware serial port I received the data.
Even tried with Wemos D1 R2 mini, same issue! The rSoftwareserial library version is 1.0.0 . Is there any update?
How do I read the data directly from softserial.Stream ?
 
Upvote 0
Even tried with Wemos D1 R2 mini, same issue! The rSoftwareserial library version is 1.0.0 . Is there any update?
rSoftwareserial is just a thin wrapper. It will use the installed SoftwareSerial SDK.

How do I read the data directly from softserial.Stream ?
Start with checking the bytes available with a timer:
Code:
Log(serial.Stream.BytesAvailable)
 
Upvote 0
Checked with timer:
getting 18 bytes, which increments to 19 then 37 ,38,56,57 and saturates to 64 with receipt of each message.
 
Upvote 0
The first line #1 above throws error since "serial.Stream.BytesAvailable" returns an integer rather than boolean!
This worked!
B4X:
If softserial.Stream.BytesAvailable> 1 Then
    Dim b(softserial.Stream.BytesAvailable) As Byte
    Dim len As Int = softserial.Stream.ReadBytes(b, 0, b.Length)
    Log (b)
End If
Thanks Erel,
But how can we acquire the data in Astream_Newdata event?
 
Upvote 0
Now I have used b4rserializator also, works flawlessly but only by reading the serial.stream directly!
 
Upvote 0
Now I have used b4rserializator also, works flawlessly but only by reading the serial.stream directly!
New Event is not fired because for softserial there is no hardware buffer which raised an interrupt when it is full. By polling method you can read serial data while using softserial.
 
Upvote 0
Back
Top