B4R Question esp32 truncate gsm send message

mariottino82

Member
Licensed User
Longtime User
Hi, i have increment StackBuffersize to 600 the asyncstream of gsm serialnative to 200 and the asyncstream of webserver to 200 size bytes but the get request of the message to send with the GSM module is ok and all bytes are parsed, instead the send command for the gsm truncate the message and output this error, i think it's a buffer size error : "+CMS ERROR: invalid input value". The astream size buffer is 200 byte and i have set also delay time to 100ms? anyone can help me for this problem?


webserver with gsm module esp32:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region

Sub Process_Globals
    'Private serial As SoftwareSerial
    Private retryEmptyMessages As Byte
    Private serialnative1 As Stream
    Private SMSSlot(4) As Byte
    Private astream As AsyncStreams
    Private ServerRequest As AsyncStreams
    Private EOL() As Byte = Array As Byte(13)
    Private wifi As ESP8266WiFi
    Public Socket1 As WiFiServerSocket
    Public Serial1 As Serial
    Private led As Pin
    Private PowerGSM As Pin
    Private timer1 As Timer
    Private timerLed As Timer
    Dim bc As ByteConverter
End Sub

Private Sub AppStart
    RunNative("SetIP", Null)
    
    
    
    
    Serial1.Initialize(115200)
    Log("AppStart")
    Init
    PowerGSM.Initialize(23,PowerGSM.MODE_OUTPUT)
    led.Initialize(13, led.MODE_OUTPUT)
    'led.DigitalWrite(True)
    PowerGSM.DigitalWrite(True)
    Delay(1000)
    timer1.Initialize("timer1_Tick", 10000)
    timerLed.Initialize("timerLed_Tick",1500)
    timer1.Enabled = True
    
    If wifi.Connect2("SYS-TECH","82s25e335h") Then
    
        Socket1.Initialize(8088,"Socket1_NewConnection")
        
        Socket1.Listen
        timerLed.Enabled=True
        Log(wifi.LocalIp)
        
        
    End If
    
    
End Sub


Sub Socket1_NewConnection(NewSocket As WiFiSocket)
    
    Log("Client Connesso")
    ServerRequest.Initialize(NewSocket.Stream,"ServerRequest_NewData","ServerRequest_Error")
    ServerRequest.WaitForMoreDataDelay=100
    ServerRequest.MaxBufferSize=256
    
End Sub


Sub timerLed_Tick
    led.DigitalWrite(XOR(led.DigitalRead,True))
    'Log(led.DigitalRead)
End Sub


Public Sub XOR(a As Boolean, b As Boolean) As Boolean
    Return a <> b
End Sub


Sub Timer1_Tick

    astream.Write("AT+CREG?").Write(EOL)
    
    
End Sub


Public Sub MessageArrived(msg() As Byte)
    If msg = "On" Then
    
    Else
        Log("error: ", msg)
    End If
End Sub




Public Sub Init()
    RunNative("SerialNative1", Null)
    
    
    astream.Initialize(serialnative1, "astream_NewData", Null)
    astream.WaitForMoreDataDelay = 100 'make sure that we receive full messages
    astream.MaxBufferSize=256
    astream.Write("AT+CMGF=1").Write(EOL)
    Delay(1000)
    
    astream.Write("AT+CNMI=2,1").Write(EOL) 'aBILITA EVENTI SMS
    
End Sub




'

'PhoneNumber - should start with + and include country code.
Private Sub SendSMS(phoneNumber() As Byte, message() As Byte) As Boolean
    Log(phoneNumber)
    Log(message)

    astream.Write("AT+CMGF=1").Write(EOL)
    Delay(1000)
    astream.Write("AT+CMGS=""").Write(phoneNumber).Write("""").Write(EOL)
    Delay(1000)
    astream.Write(ReplaceString(message,"%20"," ")).Write(Array As Byte(0x1a))
    'astream.Write(message).Write(Array As Byte(0x1a))
    
    
    Return True
    
End Sub



Sub AStream_NewData (Buffer() As Byte)
    
    
            Log(Buffer)
    If bc.IndexOf(Buffer, "+CMTI:") > -1 Then
        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(200)
        astream.Write("AT+CMGR=").Write(slot).Write(EOL)
        
        
    End If
    
    If bc.IndexOf(Buffer, "+CREG: 0,1") > -1 Or bc.IndexOf(Buffer, "+CREG: 0,5") > -1 Then
        Log("Registrato sulla rete GSM")
        timer1.Enabled=False
        astream.Write("AT+CMGF=1").Write(EOL)
        Delay(1000)
    Else
        Log("Not registered")
    End If
    
    
    
    If bc.IndexOf(Buffer, "+CMGR:") > -1 Then
        
        
            
            
                Dim counter As Int = 0
                For Each s() As Byte In bc.Split(Buffer, """")
                    If counter = 3 Then
                        Log("Inviato da: ", s)
                    Else If counter = 7 Then
                        Log("Data: ", s)
                    
                      Else If counter = 8 Then
            Log("Testo: ", s)
                    End If
                    counter = counter + 1
                Next
                Delay(100)
        astream.Write("AT+CMGD=").Write(SMSSlot).Write(EOL)
            End If
            
            
            
            
        
        
        
        
    
    
    
End Sub


Sub ServerRequest_NewData(Buffer() As Byte)
    Log("**************************")
    Log(Buffer)
    Log("**************************")
    If Socket1.Socket.Connected Then
        If bc.IndexOf(Buffer, "SEND") <> -1 Then
    
    Dim numero() As Byte
    Dim messaggio() As Byte
        ServerRequest.Write("HTTP/1.1 200").Write(CRLF)
        ServerRequest.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
        ServerRequest.Write("<html><body>ok</body></html>")
    
            
    
    
    
        Dim i1 As Int = 0
        Dim i2 As Int = 0
        For Each b1() As Byte In bc.Split(Buffer, " ")
            If i1 = 1 Then
                For Each b2() As Byte In bc.Split(b1,"$")
                    Select i2
                        Case 2
                            numero = bc.StringFromBytes(b2)
                        Case 3
                            messaggio = bc.StringFromBytes(b2)
                            Log("il messaggio inviato è: ")
                            Log(messaggio)
                    End Select
                    i2 = i2 + 1
                Next
                    
            End If
            i1 = i1 + 1
        Next
        
            Socket1.Socket.Stream.Flush
            Socket1.Socket.Close
        
        SendSMS(numero,messaggio)
        
        
        
        End If
        
End If
    
    
    Log("Socket Chiuso")
    Log(Buffer)
  
End Sub



Sub ServerRequest_Error
    Socket1.Listen
    
    Log("ERRORE LISTEN SOCKET")
    
    
End Sub




Public Sub ReplaceString(Original() As Byte, SearchFor() As Byte, ReplaceWith() As Byte) As Byte()
    'count number of occurrences
    Dim bc2 As ByteConverter
    Dim c As Int = 0
    Dim i As Int
    If SearchFor.Length <> ReplaceWith.Length Then
        i = bc2.IndexOf(Original, SearchFor)
        Do While i > -1
            c = c + 1
            i = bc2.IndexOf2(Original, SearchFor, i + SearchFor.Length)
        Loop
    End If
    Dim result(Original.Length + c * (ReplaceWith.Length - SearchFor.Length)) As Byte
    Dim prevIndex As Int = 0
    Dim targetIndex As Int = 0
    i = bc2.IndexOf(Original, SearchFor)
    Do While i > -1
        bc2.ArrayCopy2(Original, prevIndex, result, targetIndex, i - prevIndex)
        targetIndex = targetIndex + i - prevIndex
        bc2.ArrayCopy2(ReplaceWith, 0, result, targetIndex, ReplaceWith.Length)
        targetIndex = targetIndex + ReplaceWith.Length
        prevIndex = i + SearchFor.Length
        i = bc2.IndexOf2(Original, SearchFor, prevIndex)
    Loop
    If prevIndex < Original.Length Then
        bc2.ArrayCopy2(Original, prevIndex, result, targetIndex, Original.Length - prevIndex)
    End If
    Return result
End Sub



#if C
void SerialNative1(B4R::Object* unused){

::Serial1.begin(9600, SERIAL_8N1, 26, 27);
b4r_main::_serialnative1->wrappedStream = &::Serial1;
}



  void SetIP(B4R::Object* o) {

  IPAddress ip(192, 168, 1, 252); 
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  IPAddress dns(192, 168, 1, 1);
  WiFi.config(ip, gateway, subnet, dns);

  }







#End If
 

mariottino82

Member
Licensed User
Longtime User
The problem is the sms character is truncate to 69 byte, when i change the asynstream of the http request is working correctly but the asyncstream of the gsm module is not working properly and if the sms message it's more length of 69 chars there is this error in output +CMS ERROR: invalid input value, i think it may be an buffer error of the astream.write but i have set the buffersize of astream to 200.

this is the log
B4X:
Client Connesso
**************************
GET /$SEND$3472967365$openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd HTTP/1.1
Host: 192.168.1.252:8088
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: M
**************************
il messaggio inviato è:
openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd
3472967365
openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd
Socket Chiuso
GET /$SEND$3472967365$openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd HTTP/1.1
Host: 192.168.1.252:8088
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: M
OK
>
+CMS ERROR: invalid input value
Not registered
ERRORE LISTEN SOCKET
Client Connesso
**************************
GET /$SEND$3472967365$openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd HTTP/1.1
Host: 192.168.1.252:8088
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: M
**************************
Socket Chiuso
GET /$SEND$3472967365$openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd HTTP/1.1
Host: 192.168.1.252:8088
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: M
**************************
ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0
**************************
Socket Chiuso
ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0
**************************
.9
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
**************************
Socket Chiuso
.9
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7
ERRORE LISTEN SOCKET
Client Connesso
**************************
GET /favicon.ico HTTP/1.1
Host: 192.168.1.252:8088
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Accept: image/avif,image/webp,image/apng,image/svg+
**************************
Socket Chiuso
GET /favicon.ico HTTP/1.1
Host: 192.168.1.252:8088
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Accept: image/avif,image/webp,image/apng,image/svg+
**************************
xml,image/*,*/*;q=0.8
Referer: http://192.168.1.252:8088/$SEND$3472967365$openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.9,en-US;q=0.8
**************************
il messaggio inviato è:
openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd
Accept-Encoding:
3472967365
openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd
Accept-Encoding:
Socket Chiuso
xml,image/*,*/*;q=0.8
Referer: http://192.168.1.252:8088/$SEND$3472967365$openfiber%20clicca%20sul%20link%20sottostante%20e%20vedi%20systechweb.eu/pos.aspx?thdghfhgfhfgdfhfdghffd
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.9,en-US;q=0.8
OK
>
>
+CMS ERROR: invalid input value
Not registered
ERRORE LISTEN SOCKET
 
Upvote 0

mariottino82

Member
Licensed User
Longtime User
I think it may be a problem of the serial1.rxsetbuffersize i have set it to 256 but there is the same problem, the board is an esp32 with sim800L gsm modulo.
 
Upvote 0

mariottino82

Member
Licensed User
Longtime User
Hi, i have implementated a code only for sms but the problem is the same....

LOG:
B4X:
Sto caricando la configurazione...
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
AppStart
configuraizone ok
+CREG: 0,0
OK
RDY
+CFUN: 1
+CPIN: READY
+CREG: 0,2
OK
+CREG: 0,2
OK
+CREG: 0,2
OK
Call Ready
+CREG: 0,2
OK
SMS Ready
+CREG: 0,2
OK
+CREG: 0,2
OK
+CREG: 0,2
OK
+CREG: 0,1
OK
Registrato sulla rete GSM
OK
>
+CMS ERROR: invalid input value
Timer Enabled
+CREG: 0,1
OK
+CTZV: +4,0
*PSUTTZ: 2022,1,19,12,21,57,"+4",0
DST: 0
+CIEV: 10,"22210","vodafone IT","vodafone IT", 0, 0
Registrato sulla rete GSM
OK
>
+CMS ERROR: invalid input value
Timer Enabled

and this is the code, i think or a problem of asyncstream or a problem of rxbuffer of the serial hardware ..




B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src

Sub Process_Globals
    
    
    Private PowerGSM As Pin
    Private astream As AsyncStreams
    Public Serial1 As Serial
    Private serialnative1 As Stream
    Private EOL() As Byte = Array As Byte(13)
    Private bc As ByteConverter
    Private timer1 As Timer
    Private busy As Boolean
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    PowerGSM.Initialize(23,PowerGSM.MODE_OUTPUT)
    PowerGSM.DigitalWrite(True)
    timer1.Initialize("timer1_Tick",1000)
    
    RunNative("SerialNative1", Null)
    Delay(1000)
    astream.Initialize(serialnative1, "astream_NewData", Null)
    astream.WaitForMoreDataDelay = 100 'make sure that we receive full messages
    astream.MaxBufferSize=256
    Delay(1000)
    astream.Write("AT").Write(EOL)
    Delay(500)
    astream.Write("AT+CSQ").Write(EOL)
    Delay(500)
    astream.Write("AT+CCID").Write(EOL)
    Delay(500)
    astream.Write("AT+CREG?").Write(EOL)
    Delay(500)
    
    Log("configuraizone ok")
    timer1.Enabled=True
End Sub

Sub timer1_tick
    astream.Write("AT+CREG?").Write(EOL)
    Delay(500)
    
End Sub
Sub invia_sms
    If busy=False Then
    busy=True
    timer1.Enabled=False
    astream.Write("AT+CMGF=1").Write(EOL)
    Delay(1000)
    
    'astream.Write("AT+CSMP=17,167,0,8").Write(EOL)
    
    astream.Write("AT+CMGS=""").Write("3472967365").Write("""").Write(EOL)
    Delay(1000)
    'bc.ArrayCopy(ReplaceString(message,"%20"," "), messageToSend)
        astream.Write("SALVE DA OPEN FIBER PER CONSENTIRE LA SU POSIZIENE CLICCHI SU LINK RIPORTATO http://www.systechweb.eu/posizione/rete/posizone.aspx?tel=3472967365").Write(Array As Byte(0x1a))
    'astream.Write2(ReplaceString(message,"%20"," "),0,159).Write(Array As Byte(0x1a))
    
    
    
    End If
    busy=False
End Sub

Sub aStream_NewData (Buffer() As Byte)
    If busy =False Then
    busy=True
    Log(Buffer)
    If bc.IndexOf(Buffer, "+CREG: 0,1") > -1 Or bc.IndexOf(Buffer, "+CREG: 0,5") > -1 Then
        Log("Registrato sulla rete GSM")
        busy=False
        invia_sms
    End If
        If bc.IndexOf(Buffer, "+CMS ERROR") > -1 Then
        
        Log("Timer Enabled")
        timer1.Enabled=True
        End If
    busy=False
    
    End If
    
End Sub

#if C
void SerialNative1(B4R::Object* unused){

::Serial1.begin(4800, SERIAL_8N1, 26, 27);
::Serial1.setRxBufferSize(512);
b4r_main::_serialnative1->wrappedStream = &::Serial1;
}

#End If
 
Upvote 0

mariottino82

Member
Licensed User
Longtime User
Hi @Erel, also in C i have the" +CMS ERROR: invalid input value ", i think it may be an error of unicode of SIM800L and more then 70 characters i don't can send to the gsm module. All the examples on the Internet related to Arduino or ESp32 are all with short text send to the module. Have you any idea?

Thank you very much

C:
#define SIM800L_RX     27
#define SIM800L_TX     26
#define SIM800L_PWRKEY 4
#define SIM800L_RST    5
#define SIM800L_POWER  23

char replybuffer[255];




uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

#define LED_BLUE  13
#define RELAY 14

String smsString = "";

void setup()
{
  pinMode(LED_BLUE, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(SIM800L_POWER, OUTPUT);

  digitalWrite(LED_BLUE, HIGH);
  digitalWrite(SIM800L_POWER, HIGH);

  Serial.begin(9600);


  // Make it slow so its easy to read!
  Serial1.begin(9600, SERIAL_8N1, SIM800L_TX, SIM800L_RX);

delay(20000);


 
  Serial1.println("AT"); //Once the handshake test is successful, it will back to OK
  updateSerial();

 Serial1.println("AT+CSCS=\"GSM\"");
 updateSerial();
 Serial1.println("AT+CSCS?");
 updateSerial();
  Serial1.println("AT+CMGF=1"); // Configuring TEXT mode
  updateSerial();
  Serial1.println("AT+CMGS=\"3472967365\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  updateSerial();
  Serial1.print("Last Minute Engineers | lastminuteengineers.com www.google.it e campii mario fjdfshkkfdkshfkjdshkjhkhksjdhh"); //text content
  updateSerial();
 // Serial1.write(26);
 //  updateSerial();
}

void loop()
{
 
}

void updateSerial()
{
  delay(1000);
  while (Serial.available())
  {
    Serial1.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(Serial1.available())
  {
    Serial.write(Serial1.read());//Forward what Software Serial received to Serial Port
  }
}
 
Upvote 0
Top