B4R Question Sending bytes using WebSocket client

yaqoob

Active Member
Licensed User
Longtime User
Hello everyone,

Thanks to miker2069, I am using the https://www.b4x.com/android/forum/t...and-video-streaming-updated-with-code.110131/. to transfer an image from the ESP32 camera to the host server through MQTT. The ESP32 sends checks of bytes, the server receives them, and stores them in a file on the server. Using the following code

Mqtt sending image:
For i = 0 To (num_chunks - 1)
        ESP32CAM.byte_offset = i * chunk_size
        ESP32CAM.MoveFrameBufferPtr
        
        TotalLength=TotalLength+ESP32CAM.buffer.Length
        Next
        'Log("this total lenght of image",TotalLength)
    'Log("Before publish pic")
    mqtt.BeginPublish(JoinStrings(Array As String("Server/",DeviceID,"/",bc.StringFromBytes(GlobalStore.Slot0),"/Camera/Image")),TotalLength, False)

    For i = 0 To (num_chunks - 1)
        ESP32CAM.byte_offset = i * chunk_size
        ESP32CAM.MoveFrameBufferPtr
'       
        'TotalLength=TotalLength+ESP32CAM.buffer.Length
        'Log("This for mqtt ",ESP32CAM.buffer.Length)
'   
'   
        mqtt.WriteChunk(ESP32CAM.buffer)
    Next
'   
    mqtt.EndPublish

The problem is that the MQTT gets disconnected from time to time. Is it possible to send chunks of bytes using a WebSocket client ( https://www.b4x.com/android/forum/threads/websocketclient.66212/#content )? I tried using the WebSocket to send chunks of images as bytes, but it did not work. The WebSocket expects strings, so I tried converting the byte to a string, but it did not work. Does anyone have any suggestions or examples to share? Thank you in advance
 

Fernando Solá

Member
Licensed User
Longtime User
There could be a few reasons for this disconnection issue. If you are just sending an image every now and then, maybe a pure Socket connection could work. If you are aggresively sending data to the Broker, maybe your device is getting kicked out or being detected as a DoS attacker by some security feature in the network or in the Broker. My guess is that your issue is related to this.
 
Upvote 0

yaqoob

Active Member
Licensed User
Longtime User
There could be a few reasons for this disconnection issue. If you are just sending an image every now and then, maybe a pure Socket connection could work. If you are aggresively sending data to the Broker, maybe your device is getting kicked out or being detected as a DoS attacker by some security feature in the network or in the Broker. My guess is that your issue is related to this.

I am sending images from time to time, not aggressively. The issue does not occur while sending images, but after connecting for a while.
 
Upvote 0

Fernando Solá

Member
Licensed User
Longtime User
If I was doing this... and I have to let you know first that I don't usually develop using B4R... I would try do it with plain Socket and ServerSocket. I would connect to the server, send the whole image bytes with AsyncStreams (Prefix mode) and then disconnect from the server. This way the ESP32 would only be connected to the server when sending images. Of course I'm also asuming that the server is being developed in B4J. I don't really know what your project requirements are, and it might be that the way I would program it won't match your project specs. In any case I would also check the broker side and connectivity reliability between the ESP32 and the network just in case.
 
Upvote 0

yaqoob

Active Member
Licensed User
Longtime User
Thank you, Fernando and Erel, for your response. The problem is solved. The application's sequence of events caused the error. Once the application starts, some code happens faster and others slower. When the ESP32 reboots, it gives the "Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled." I asked Chat GPT about the error, and it gave different suggestions, but for me, this was the problem." Trying to use a global object (like MQTT, WiFi, etc.) before calling Initialise." Once I changed the sequence of the startup code, the problem disappeared

Thank you again
 
Upvote 0
Top