B4R Question WEMOS D1 mini + SOFTWARESERIAL

Maksymon

Member
Licensed User
Hello, my problem is this. I configure a softwareserial in my wemos (TX -> D7 and RX -> D6). It sends but when it receives the data, it does not detect it. I was able to verify that the data goes out and arrives, but the wemos does not detect it. Some help?
 

barx

Well-Known Member
Licensed User
Maybe see if the pin can operate an LED on and off just to make sure it is working right and correctly selected.
 
Upvote 0

Maksymon

Member
Licensed User
@Erel Indeed, I am not using the prefix mode. I attach part of the code that I use, to see if you can give me a hand.

This is the Main
B4X:
Sub Process_Globals
    
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private SSID As String = "maksymon"
    Private PASSWORD As String = "26615386"
    Private mensaje As String = "hola"
    Private direccion As String = "http://192.168.100.100/Alertas%20SMS_service/sms_service.asp?origen=-1&remitente=1168624327&mensaje=prueba%20servicio%20SMS"
    Dim bc As ByteConverter
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    Log("AppStart")
    GSM.Init(12,13)
    If wifi.Connect2(SSID, PASSWORD) Then
        Log("Connected to router.")
    Else
        Log("Failed to connect to router.")
        Return
    End If
    HttpJob.Initialize("SMS")
    HttpJob.Download(direccion)
End Sub

Sub JobDone (Job As JobResult)
    Log("*******************************")
    Log("JobName: ", Job.JobName)
    If Job.Success Then
        
        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length))) 'truncate to 200 characters
        Dim counter As Int = 0
        For Each s() As Byte In bc.Split(Job.Response, """")
            If counter = 11 Then
                bc.ArrayCopy(s,mensaje)
                Log("send: ", mensaje)
            End If
            counter = counter + 1
        Next
    Else
        
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        Log(Job.Response)
    End If
End Sub

Sub MessageArrived (msg() As Byte)
    Log("OK")
End Sub

This is the Module GSM
B4X:
Sub Process_Globals
    Private serialgsm As SoftwareSerial
    Private bc As ByteConverter
    Private astream As AsyncStreams
    Private EOL() As Byte = Array As Byte(13, 10)
    Private busy As Boolean
    Private messageToSend(160) As Byte
    Private SMSSlot(4) As Byte
    Private retryEmptyMessages As Byte
End Sub

Public Sub Init(rx As Byte, tx As Byte)
    serialgsm.Initialize(9600,rx,tx)
    astream.Initialize(serialgsm.Stream, "astream_NewData", Null)
    astream.WaitForMoreDataDelay = 50 'make sure that we receive full messages
    SendCommand("AT+CMGDA=DEL ALL", True)
    busy = False
    SendCommand("AT+CMGF=1", True)
    busy = False
    SendCommand("AT+IPR=9600", True)
    busy = False
    EnableSMSEvents
End Sub

Public Sub EnableSMSEvents As Boolean
    Return SendCommand("AT+CNMI=2,1,0,0,0", True)
End Sub

Public Sub SendSMS(PhoneNumber() As Byte, message() As Byte) As Boolean
    If busy Then Return False
    busy = True
    astream.Write("AT+CMGS=""").Write(PhoneNumber).Write("""").Write(EOL)
    bc.ArrayCopy(message, messageToSend)
    Return True
End Sub

Private Sub SendSMSPart2
    busy = True
    astream.Write(messageToSend).Write(Array As Byte(0x1a))
End Sub

Private Sub SendSMSPart3
    Log("Message was sent successfully")
End Sub

Public Sub Hangup As Boolean
    Return SendCommand("ATA", False)
End Sub

Private Sub SendCommand (cmd() As Byte, ResponseExpected As Boolean) As Boolean
    If busy Then Return False
    busy = ResponseExpected
    astream.Write(cmd).Write(EOL)
    Delay(200)
    Return True
End Sub

Public Sub CheckRegistered
    SendCommand("AT+CREG?", True)
End Sub

Private Sub CheckRegisteredResult (buffer() As Byte)
    If bc.IndexOf(buffer, "+CREG: 0,1") > -1 Or bc.IndexOf(buffer, "+CREG: 0,5") > -1 Then
        Log("Registered")
    Else
        Log("Not registered")
    End If
End Sub

Private Sub SMSArrived1(buffer() As Byte)
    Dim i As Int = bc.IndexOf(buffer, ",")
    Dim slot() As Byte = bc.SubString(buffer, i + 1)
    bc.ArrayCopy(slot, SMSSlot)
    Log("SMS received, slot = ", SMSSlot)
    retryEmptyMessages = 0
    Delay(500)
    astream.Write("AT+CMGR=").Write(SMSSlot).Write(EOL)
    busy = True
End Sub

Private Sub SMSArrived2(Buffer() As Byte)
    busy = True
    Dim afterCMGR As Boolean = False
    Dim emptyMessage As Boolean = True
    For Each line() As Byte In bc.Split(Buffer, Array As Byte(13, 10))
        If afterCMGR Then
            If line.Length = 0 Or bc.StartsWith(line, "OK") Then Continue
            Log("Msg: ", line)
            If emptyMessage Then
                Main.MessageArrived(line)
                emptyMessage = False
            End If
        Else If bc.StartsWith(line, "+CMGR") Then
            Dim counter As Int = 0
            For Each s() As Byte In bc.Split(line, """")
                If counter = 3 Then
                    Log("From: ", s)
                Else If counter = 7 Then
                    Log("Date: ", s)
                End If
                counter = counter + 1
            Next
            afterCMGR = True
        End If
    Next
    If emptyMessage And retryEmptyMessages < 3 Then
        retryEmptyMessages = retryEmptyMessages + 1
        Log("Trying to read message again.")
        Delay(200)
        astream.Write("AT+CMGR=").Write(SMSSlot).Write(EOL)
    Else
        astream.Write("AT+CMGD=").Write(SMSSlot).Write(EOL)
    End If
End Sub

Sub astream_NewData (Buffer() As Byte)
    Select True
        Case bc.IndexOf(Buffer, "+CREG:") > -1
            CheckRegisteredResult(Buffer)
        Case bc.IndexOf(Buffer, "AT+CMGS") > -1
            SendSMSPart2
        Case bc.IndexOf(Buffer, "+CMGS:") > -1
            SendSMSPart3
        Case bc.IndexOf(Buffer, "+CMTI:") > -1
            SMSArrived1(Buffer)
        Case bc.IndexOf(Buffer, "+CMGR:") > -1
            SMSArrived2(Buffer)
        Case Else
            Log("****************")
            Log(Buffer)
    End Select
    busy = False
End Sub

The httpJob module would be missing, but it is irrelevant since it works.
 
Upvote 0

Maksymon

Member
Licensed User
Maybe see if the pin can operate an LED on and off just to make sure it is working right and correctly selected.


I could already verify with a serial converter that the Wemos sends the information and receives it, but the latter does not detect it.
 
Upvote 0

barx

Well-Known Member
Licensed User
I could already verify with a serial converter that the Wemos sends the information and receives it, but the latter does not detect it.

So, you KNOW that data is being sent to the pin BUT the wemos does not detect? Then as I say, maybe a bad pin or bad pin setup. Hence I say test with an LED to see if pin can sink and source ok. Either that or try another pin. I have software serial working reliably on Rx - D5, Tx - D6.
 
Upvote 0

barx

Well-Known Member
Licensed User
Another thought, Maybe the AsyncStream Newdata sub isn't set out correctly. Maybe post some code of setup and sub?
 
Upvote 0

Maksymon

Member
Licensed User
So, you KNOW that data is being sent to the pin BUT the wemos does not detect? Then as I say, maybe a bad pin or bad pin setup. Hence I say test with an LED to see if pin can sink and source ok. Either that or try another pin. I have software serial working reliably on Rx - D5, Tx - D6.

Try what you say and yes, it works. But communication still doesn't work. Above, post the code I am using.
 
Upvote 0

rbghongade

Active Member
Licensed User
Hi, probably the same issue, I faced , solution is provided by Erel himself in the same thread.
 
Upvote 0

barx

Well-Known Member
Licensed User
have you tried setting an error sub in the Astream Initialize and creating the relevant sub to consume the event? Not sure if that would make a difference.

It has been a while since I played with this code.
Also have you tried it without
B4X:
astream.WaitForMoreDataDelay = 50
To see if any different.

Lastly, try commenting out the code in
astream_NewData
And try a simple Log("bleh") just to see if you get anything.

Just trying to strip it back to bare bones and work up from there.
 
Upvote 0

Maksymon

Member
Licensed User
Hi, probably the same issue, I faced , solution is provided by Erel himself in the same thread.

Thanks for the contribution! It works effectively! Now it remains to be seen that AsyncSrteams does not work ... But the code already works!
 
Upvote 0

Maksymon

Member
Licensed User
Doing tests, I could see that I only receive up to 64 bytes. This, with the AsyncStreams library, was fixed by modifying the WaitForMoreDataDelay parameter. Now that I no longer use AsyncStreams what do you suggest I do
 
Upvote 0
Top