RE: MSW Wifi connection for XDK
Answer
8/23/18 8:08 AM as a reply to Franjo Stjepandic.

Hi Franjo,

You mean I need to specify a format data on Python code right ? I tried to convert byte array from XDK to int but it didn't work.

0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
8/23/18 2:38 PM as a reply to Nhan Tran Dinh.
Hello Nhan,

I actually mean that you need to make a specification about in which format the sensor data is sent on the XDK side.

With this information, you can then implement the decoding logic on the Python side. Furthermore, please note that the format is only available if the data is sent in a string.

If the data is sent, as for example with UDP as raw integers, you can simply decode them by using the correct reading method in Python.

Maybe you could get more into detail about the data you are receiving and if you are currently using USB or UDP?

Please let me know if this was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/4/18 4:27 AM as a reply to Franjo Stjepandic.

Hi Franjo,

After one week and I couldn't display sensor values on my servers.I used UDP example and tried so many ways to decode but still not working.

0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/4/18 3:27 PM as a reply to Nhan Tran Dinh.
Hello Nhan,

I will try to help you as good as possible, to solve the issue you are facing.

Could you please provide more details about the exact issue itself? What does not work as expected? Does the workbench console show any error codes? Are you able to receive the sensor data from the XDK? As asked in my last post, could you please specify, if you use UDP or USB? I assume, if you are using a server, it might be UDP, but it would be very helpful to be clear.

In that regard, if it is UDP, you could struggle to decode the data? On that behalf, could you please show me an outline of your sending function, in your XDK application and the code, which constructs the payload to be sent over UDP?

Kind regards,
Franjo
0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/5/18 1:20 AM as a reply to Franjo Stjepandic.

Hi Franjo,

1.The workbench console had no error code.It print Environment sensor values and "UDP sending successful" message.

2.Yes, I had problem with data decoding as I asked in my last post

This is my SendDataOverWifi function.I modified from your example and replace with Environment sensor instead of Accel

static void SendDataOverWifi(void *param1, uint32_t port)
{
    Retcode_T advancedApiRetValue = (Retcode_T) RETCODE_FAILURE;
    BCDS_UNUSED(param1);
    SlSockAddrIn_t Addr;
    uint16_t AddrSize = (uint16_t) ZERO;
    int16_t SockID = (int16_t) ZERO;
    int16_t Status = (int16_t) ZERO;
    /**
     * This buffer holds the data to be sent to server via UDP
     * */
    static int32_t bsdBuffer_mau[3] = { INT32_C(0), INT32_C(0), INT32_C(0) };

    Environmental_Data_T bme280 = { INT32_C(0), UINT32_C(0), UINT32_C(0) };

    /*Reading environment Data (p,t,h) */

    advancedApiRetValue = Environmental_readData(xdkEnvironmental_BME280_Handle, &bme280);
    if (RETCODE_OK == advancedApiRetValue)
    {
        bsdBuffer_mau[0] = bme280.pressure;
        bsdBuffer_mau[1] = bme280.temperature;
        bsdBuffer_mau[2] = bme280.humidity;
        printf("BME280 Environmental Data : p =%ld Pa T =%ld mDeg h =%ld %%rh\n\r",
                (long int) bme280.pressure, (long int) bme280.temperature, (long int) bme280.humidity);
    }
    else
    {
        printf("Reading of data failed \n");
    }
    /*Data(p,t,h) Sending to WIFI Api*/
    Addr.sin_family = SL_AF_INET;
    Addr.sin_port = sl_Htons((uint16_t) port);
    Addr.sin_addr.s_addr = sl_Htonl(SERVER_IP);
    AddrSize = sizeof(SlSockAddrIn_t);

    /* The return value is a positive number if successful; other wise negative */
    SockID = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, (uint32_t) ZERO);
    if (SockID < (int16_t) ZERO)
    {
        printf("Error in socket opening \r\n");
        /* error case*/
        return;
    }

    /* The return value is a number of characters sent;negative if not successful */
    Status = sl_SendTo(SockID, bsdBuffer_mau, BUFFER_SIZE * sizeof(int32_t), (uint32_t) ZERO, (SlSockAddr_t *) &Addr, AddrSize);

    /*Check if 0 transmitted bytes sent or error condition*/
    if (Status <= (int16_t) ZERO)
    {
        Status = sl_Close(SockID);
        if (Status < 0)
        {
            printf(" Error in closing socket after failing to send the UDP data \r\n");
            return;
        }
        printf("Error in sending data  \r\n");
        return;
    }
    Status = sl_Close(SockID);
    if (Status < 0)
    {
        printf("Error in closing socket after successfully sending the UDP data \r\n");
        return;
    }

    return;
}

  

My simple listener to receive data.

import socket

UDP_IP = "172.20.10.3" # my hotspot wifi IP
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print (data)

I tried so many ways to decode data from my Python server but I couldn't or decode to wrong value.I also asked my problem on Stackoverflow but I got no answer.That's would be great if you can help me.Thank you ! 

0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/5/18 2:16 PM as a reply to Nhan Tran Dinh.
Hello Nhan,

thank you for answering the questions and your shared code.

Your code looks mainly good to me. As I see, you initialize the socket ID, every time before you are about to send via UDP. This is not the root of your issue, but slows everything down and is not necessary.

I expect the issue to be a missing decoding step, after receiving the data via UDP, before it gets printed out. The UPD sending process affects the payload of the data in a way, it moves the bit positions from back to front and front to back.

So basically, your decoder needs to start with the very last bit and treat it as the first bit, the second last bit as the second and so on. Just to be sure, we are facing the right direction:
Which exact values do you get printed out?

Also, could you please explain the reason behind your listed UDP_IP address?

Please let me know if this was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/6/18 1:49 AM as a reply to Franjo Stjepandic.

Hi Franjo,

UDP_IP is server address.I set the same IP in header file 

#define WLAN_CONNECT_WPA_SSID               "HUAWEI-A3D7"       
#define WLAN_CONNECT_WPA_PASS               "01129521"     
#define SL_SECURITY_TYPE                SL_SEC_TYPE_WPA          		 
#define BUFFER_SIZE                     UINT8_C(3)
/** IP addressed of server side socket.
#define SERVER_IP                       UINT32_C(0xac140a03)                           /**< 172.20.10.3*/
#define SERVER_PORT                     UINT16_C(5005)                                  /**< Port number*/

I had already modified my Python code for clealy result.

import socket
import sys

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = ('172.20.10.3', 5005)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

while True:
    print('\nwaiting to receive message')
    data, address = sock.recvfrom(4096)

    print('received {} bytes from {}'.format(
        len(data), address))
    print(data)

and this is the Python console result

starting up on 172.20.10.3 port 5005

waiting to receive message
received 12 bytes from ('172.20.10.6', 65400)
b'\x98\x89\x01\x00\xeez\x00\x002\x00\x00\x00'

waiting to receive message
received 12 bytes from ('172.20.10.6', 62962)
b'\x9a\x89\x01\x00\xeez\x00\x002\x00\x00\x00'

I received data from my XDK IP but It's not expected values.

0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/6/18 3:43 PM as a reply to Nhan Tran Dinh.
Hello Nhan,

thank you for sharing further information on your server and client XDK implementation.

By setting up the SendDataOverUdp example with your modifcations, I was able to reproduce your error. Your XDK client implementation looks good and should provide the correct values. Please note, the data which runs over the bsdBuffer_mau array, splits up into 4 bytes for each sensor value. In addition, each sensor value's first two bytes get switched with the last two bytes of the value.

The receiving anmount of 12 bytes at your server implementation is also correct and relates to the three sensor values you send.

Now to the issue, followed by a way to solve it:
You directly print out the values over the print function, without running a previous necessary preprocessing step.

The solution would be:
  • Shift the last two bytes with the first two bytes of every pair of four bytes, for each sensor value
  • Use OR operations to combine the data of every byte in a pair of four bytes into a new variable of the type int32, for each sensor value
  • Print out the data as 32 bit integer over the print function

This should solve your issue on the server side since everything else is working as expected.

Please let me know if this was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/17/18 1:13 AM as a reply to Franjo Stjepandic.

Hi Franjo,

Thank you so much ! I have just implemented successful on server side after one week.I splited every 4 bytes for each sensor value and used convert method 

while True:
    print('\nwaiting to receive message')
    data, address = sock.recvfrom(4096)

    print('received {} bytes from {}'.format(
        len(data), address))
    pressure = int.from_bytes((data[0:4]),byteorder='little')
    print ("Pressure: ",pressure)
    temp = int.from_bytes((data[4:8]),byteorder='little')
    print ("Temp: ",temp)
    humid = int.from_bytes((data[8:12]),byteorder='little')
    print ("Humid: ",humid)

And I got my expected data

waiting to receive message
received 12 bytes from ('172.20.10.6', 53100)
Pressure:  100565
Temp:  27710
Humid:  51

 

0 (0 Votes)
RE: MSW Wifi connection for XDK
Answer
9/12/18 2:15 PM as a reply to Nhan Tran Dinh.
Hello Nhan,

thank you for providing the good news.

I am glad to hear that you were able to solve the issue and print out the correct data values server sided.

Furthermore, thank you for providing your server code. I guess there will be a lot of other users, which will benefit from it.

Please feel free to ask if you have any further questions.

kind regards,
Franjo
0 (0 Votes)