B4R Question Astream_Newdata event not getting fired...

rbghongade

Active 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 :
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();
}

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


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);
 }
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); 
    
 }
 }
 

rbghongade

Active Member
Licensed User
Longtime User
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

thetahsk

Active Member
Licensed User
Longtime User
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

rbghongade

Active Member
Licensed User
Longtime User
Have tried this:
RX CODE:
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
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
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

Erel

B4X founder
Staff member
Licensed User
Longtime User
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:
B4X:
Log(serial.Stream.BytesAvailable)
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
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

rbghongade

Active Member
Licensed User
Longtime User
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

rbghongade

Active Member
Licensed User
Longtime User
Now I have used b4rserializator also, works flawlessly but only by reading the serial.stream directly!
 
Upvote 0

embedded

Active Member
Licensed User
Longtime User
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
Top