Shop OBEX P1 Docs P2 Docs Learn Events
P1 WiFi module limitations for sockets and background tasks — Parallax Forums

P1 WiFi module limitations for sockets and background tasks

In my WiFi module html program I am thinking about implementing some javascript background tasks and some html socket server code. Anybody have any idea as to what limitations I would be running into, and could the P1 WiFi module handle something like that. I know there is a ram limitation for the files, what else?

Ray

«1

Comments

  • RsadeikaRsadeika Posts: 3,837

    I just tried using putty telnet to connect to my P1 WiFi module html program. I got a connection refused. I am using SimpleIDE for this stuff, is there some setup code for allowing a connection?

    Ray

  • iseriesiseries Posts: 1,492

    HTML requires a liitle more data then just connecting with putty. Also the port to access a web page is port 80 and not 22 or 23.

    You might want to try CURL.

    javascript runs in the browser so those things should work.

    Mike

  • RsadeikaRsadeika Posts: 3,837

    Thanks Mike for the lead. I discovered PycURL, at first glance, it looks a little bit intimidating with the POST and GET stuff, but I will do some deeper look see. I also found libtelnet, which I will see what that is all about. Both of these run under Python, so that fits into what I am working on.

    If this works out, that means I will probably be able to jam in more stuff into the WiFi module html file. I still have a lot of room left on the Simpleide C file, for this stuff.

    Ray

  • RsadeikaRsadeika Posts: 3,837

    I just checked out the cURL docs, it is a WEB SCRAPE tool. If you want to see the html code, this program will do it for you. I do not think that this tool will get me what I want and need.

    It looks like I will have to spend some time with javascript and sockets. I did have some success with sockets, in another program, but that was for moving a file from one computer to another. Now I have to figure out how to move a specific variable.

    Ray

  • iseriesiseries Posts: 1,492

    Took a crack at it and wrote some python code on the RPI and some C code on the P1 to pass data back and forth.

    I don't write in python so it took a little longer than I would like.

    Oh what fun.

    Here is the python program on the Raspberry PI:

    import socket
    
    HOST = '101.1.1.15'
    PORT = 23
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    s.connect((HOST, PORT))
    
    while True:
        command = input('Enter text: ')
        command = command + '\n'
        index = 0
        data = command.encode('utf-8')
        while index < len(data):
            sent = s.send(data[index:])
            if sent == 0:
                raise RuntimeError("Socket connection error")
            index = index + sent
    
        reply = s.recv(1024)
        if reply == b'terminate':
           break;
        print(reply)
    
    print('Good Bye')
    

    On the P1 I used this program:

    #include "simpletools.h"
    #include "fdserial.h"
    
    fdserial *cnn;
    char Buffer[1014];
    
    
    int main()
    {
      int i, j;
    
      cnn = fdserial_open(31, 30, FDSERIAL_MODE_NONE, 115200);
    
      memset(Buffer, 0, sizeof(Buffer));
    
      j = 0;
      while(1)
      {
        i = fdserial_rxCheck(cnn);
        if (i > 0)
        {
          if (i == '\n')
          {
            dprint(cnn, Buffer);
            j = 0;
          }
          else
          {        
            Buffer[j++] = i;
            Buffer[j] = 0;
          }
        }
      }  
    }
    

    The Python program takes in a text string and sends it over to the P1 through the WiFi module that is on the same network.
    In my case the Wifi module has a fixed IP address of 101.1.1.15. You would have to change this to what address your module has.

    Entering in the text 'terminate' will cause the Python program to end.

    Mike

  • RsadeikaRsadeika Posts: 3,837

    Looks good Mike. I tried it out and it works on my setup. Now I have to figure out how to have the Pi code send some data to the rpi. I did not know that Python had some code for using the socket stuff. I guess Python has just about everything covered, now if they could just get rid of that indentation stuff.

    Ray

  • iseriesiseries Posts: 1,492

    Next phase was to get an apache2 server running on the Raspberry Pi and run python programs.

    Done. works great.

    Now I need to build a web page to call the python program and build a python program to send request to the P1.

    Mike

  • RsadeikaRsadeika Posts: 3,837

    Mike your little program is working great. I tried some test runs with your setup, worked into my P1 WiFi program, and came up with some problems. The problem is that the P1 WiFi program uses 'wifi_start(31,30,115200,WX_ALL_COM)'. To talk too the rpi I need to also use 'serial_open(31,30,0,115200)', there is the conflict, both are trying to use the same port. I did not see any P1 C code that could mediate usage of that one port between the two requesting for use.

    I like the socket code on the Python side, that means I could talk to other machines on my network.

    Now thinking about your latest phase, I see you are going to get info from the P1 and have the rpi web page display that info. That sounds like an idea that would could work. Maybe I will have to try something like that, I was hoping to keep it as simple as possible.

    Ray

  • RsadeikaRsadeika Posts: 3,837

    I have also decided to install a web server on my rpi unit, which is now running. Now comes the hard part, creating the web page in conjunction with the Python program. It seems like the easy part now is getting the data on my P1 WiFi over to the Python program. The hard part is getting the data from the Python program to the web page. Still not sure how I will complete that.

    Ray

  • RsadeikaRsadeika Posts: 3,837

    Doing a WEB page for the Raspberry Pi is a PIA. To bad that the P1 WiFi module does not have more capabilities. I know somebody is thinking what kind of capabilities, to frustrated to enumerate at this time. I do know working with nano in sudo mode is a PIA, and creating an html file in /var/www/html is a PIA, and the list goes on.

    Ray

  • iseriesiseries Posts: 1,492

    Yes, I do not like working on the dark side. I'm a windows guy and I can do Linux but would rather not.

    I thought the RPi would do better at heavy lifting then the other way around. The Pi could just request data from the P1 as if it was an external device and get an immediate response.

    When I pass data back from the P1 or from windows I like to use JASON and encode the answer. This is a little more work but at least the data is readable almost any ware.

    On the PI you can just create a Python program that sends a request over to the P1 and not even use a web page until you have that working the way you want it.

    Yes, the request are being passed around on the Telnet port for the Wifi module. This is not a problem since it can handle both as long as you don't use the escape code in the request.

    Mike

  • ElectrodudeElectrodude Posts: 1,657
    edited 2024-03-13 05:51

    @Rsadeika said:
    Doing a WEB page for the Raspberry Pi is a PIA. To bad that the P1 WiFi module does not have more capabilities. I know somebody is thinking what kind of capabilities, to frustrated to enumerate at this time. I do know working with nano in sudo mode is a PIA, and creating an html file in /var/www/html is a PIA, and the list goes on.

    Ray

    You don't have to use nano, you don't have to edit as root, and you don't have to put your files in /var/www/html. Things in Linux are exactly as easy as you make them. Generally, if you just follow dumbed-down online tutorials and are afraid to change things to suit your taste, you'll only be making things more difficult for yourself.

    • Find an editor that works for you. Personally, nano is my least favorite Linux text editor.
    • Tell the HTTP server to load the files from where you want them, or change the permissions on the default location so you can edit them as a normal user.
  • RsadeikaRsadeika Posts: 3,837

    There has to be a better way of doing this, not sure what that is at the moment. I really do not want to create a full blown web server just to support a simple web page. The P1 WiFi module offers this. It is complex enough to deal with the html/javascript code as it is, but the Parallax Learn site gives some good instruction in how to do it. I have to give it some more thought before I get to deep into the web server.

    Ray

  • iseriesiseries Posts: 1,492

    I hear you; I was trying to simplify the process by removing the wires between the two units. If you have a Wifi connection, why not use it.

    What you have now should work either way.

    Start the Python code on the Raspberry PI to open a connection to the P1 through the Wifi telnet port to get data from the P1.

    On the P1 just process the web page like you are and then send it back through the same connection. The Wifi will only process request that are encoded for it and will not show up on the Raspberry Pi.

    Think of it as talking to two devices on the same connection and there addressed with different addresses.

    Now I think you need to encode your data better before sending it over to the RPi.

    Mike

  • RsadeikaRsadeika Posts: 3,837
    edited 2024-03-13 18:44

    I was just looking at the Wi-Fi Module docs and came across this:

    char wifi_send (int handle, char *data, int size)
    Transmit WebSocket/TCP data, or extended HTTP body (after REPLY command).

    int wifi_recv (int handle, char *data, int size)
    Retrieve incoming HTTP body or WebSocket/TCP data.

    Anybody know how this is to be used, in some WiFi C code. There used to be some forum members that were familiar with the WiFi stuff, but they have not been around for a while. Andy Lindsay is the author of this document and code but I do not think that he checks this forum anymore.

    Ray

  • Hi
    Don't know if this might be of interest?
    https://geoffg.net/webmite.html
    Dave

  • RsadeikaRsadeika Posts: 3,837

    Thanks Dave, looks very interesting.

    Ray

  • RsadeikaRsadeika Posts: 3,837

    I need C coders help. I think I figured out how to use the wifi_send() function. In my P1C wifi program, in the while() loop, which handles 'G' and 'P' functions, I added wifi_send(), which works as expected. The problem:

    sx=17.23 floating point
    wifi_send(handle,"%.2f",sx), sends a form of sx that the Python decode() does not understand, and throws an error.

    So, I tried:
    char newsx[5]
    sprint(newsx,"%.2f",sx)
    wifi_send(handle,"%c",newsx), sends a form of newsx as a "%" char. On the Python side, decode() does not have a problem with newsx.

    Because wifi_send() function sends a char/string form, I have to figure out how to convert a floating point number to a char/string form. I think my example is terse enough, and understandable. Any ideas, or did I do something wrong with sprint().

    Ray

  • iseriesiseries Posts: 1,492

    WiFi send expects a buffer or string object to send and not %c.

    i = sprintf(newsx, "%.2f", sx);
    wifi_send(handle, newsx, i);
    

    Mike

  • RsadeikaRsadeika Posts: 3,837
    edited 2024-03-14 13:25

    Thanks Mike, that works on the P1 C side. Now I am having problems on the Python side. The decode() works correctly, but the output gets weird.

    I get numbers like:
    12.23
    3
    12
    12.45
    45
    2
    and so on.
    Pi C code

        char newsx[5];
        i = sprint(newsx,"%.2f",vx);
        wifi_send(handle,newsx,i);
        pause(1000); 
    

    Python code

        while True:
            reply = s.recv(5)
            print(reply.decode())
            time.sleep(2)
    

    I believe the s.recv(5) sets the buffer for 5 bytes, and I think that buffer is being overrun.

    Ray

  • iseriesiseries Posts: 1,492
    edited 2024-03-14 14:05

    Don't understand the use of decode here.

    s.recv(max bytes) returns a byte array of data.

    to convert the byte data to a string you would use str(bytedata, 'ascii')
    and to convert a string to bytes you would use bytes(string, 'ascii')

    To make the floating data less floating you could use "%3.2f" --> 014.20

    while True:
        reply = s.recv(1024)
        print(str(reply, 'ascii')
        f = float(reply)
        print(f)
    

    Mike

  • RsadeikaRsadeika Posts: 3,837

    Thanks Mike. I got the Pi C program working correctly and I have the Python program working correctly.I ran the P1 program and the Python program at the same time, I checked the web page, and that seems to be working as expected. So, things are looking good so far. Now I have the P1 talking to the rpi, no wires, just a one way conversation. to be sure. Not sure if I want to make it a two way conversation, that is to be determined at a later date.

    Ray

  • iseriesiseries Posts: 1,492

    I tried to get two-way communication work but was unable to get the return value back from the Python using the Wifi library.

    I was able to get it to work with my own esp8266 library. Don't know what's going on there.

    Here is my test Python program that I used:

    import socket
    
    HOST = ""
    PORT = 3260
    
    HostName = socket.gethostname()
    HOST = socket.gethostbyname(HostName)
    print(HOST)
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
        server.bind((HOST, PORT))
        server.listen()
        conn, addr = server.accept()
        with conn:
            print(f"Connected by {addr}")
            while True:
                data = conn.recv(1024)
                if not data:
                    break
                if data == b'On':
                    print("Led set on")
                if data == b'Off':
                    print("LED set off")
                conn.sendall(b'Ok For Now')
    print('done')
    

    And this is the test program that follows an example from the WiFi library that doesn't seem to work.

    fdserial *cnn;
    char Buffer[1024];
    int event;
    char wifi_event;
    
    
    int main()
    {
      int i, j, k;
    
      cnn = wifi_start(31, 30, 115200, WX_ALL_COM);
      wifi_setBuffer(Buffer, sizeof(Buffer));
      memset(Buffer, 0, sizeof(Buffer));
    
      print("Press any key");
      j = fdserial_rxChar(cnn);
    
      i = wifi_connect("101.1.1.7", 3260);
    
      if (i > 0)
      {
        wifi_print(TCP, i, "%s", "Off");
        event = wifi_event;
    
        pause(2000);
        wifi_scan(TCP, i, "%s", Buffer);
        print(Buffer);
        print("\n");
    
        wifi_disconnect(i);
      }
      else
      {
        print("Send command failed\n");
      }
    
      print("Done\n");
    
      while (1)
      {
        pause(1000);
      }
    }
    

    Mike

  • RsadeikaRsadeika Posts: 3,837

    So now I have a problem that I am not sure how to solve. The WiFi module ip address gets getting switched, while it is running, not sure if this is the wifi module that is acting up or it is the router that is causing the problem. If I have to guess I would say it is the wifi module that is causing the problem, can anybody verify my assumption.

    Ray

  • iseriesiseries Posts: 1,492

    The IP address is assigned by the router. The router is configured with a range of addresses to assign to each device that connects to it. The router uses a lease time for that address and will not give it to another device until the lease time has expired.

    In my case I can tell the router that this device always gets this address, so the address is permanent.

    Devices are assigned a MAC address at the factory, so the router assigns addresses based on that.

    In the case of the Raspberry Pi it is possible to configure a fixed address on that interface and not use the address from the router.

    This is not the case for the WiFi module.

    Mike

  • RsadeikaRsadeika Posts: 3,837

    I think I fixed the problem with the P1 WiFi module. I did a deep dive into my router setup, and I did what Mike had suggested, setting the IP address to a permanent setting. Today I will test this to see if it holds. If it holds, then it is back to do some programming, hopefully I can get a two way communication between the P1 and the rpi via the socket.

    Ray

  • RsadeikaRsadeika Posts: 3,837

    I have been trying to get wifi_recv() to work, but no success, so far. Where is Andy when you need some guidance. Has anybody gotten wifi_recv() to work, and willing to share some examples.

    Ray

  • RsadeikaRsadeika Posts: 3,837

    I am now wondering if anybody has tried to move libwifi over to the flexprop, can this be done? Or, has anybody tried to create a web page with flexprop using the Parallax WiFi module and the P1 or P2? It seems that Mike and I are the only ones that are using SimpleIDE. If that is the case then I think that Parallax is just waiting for SimpleIDE to finally fall off the vine, and disappear on the ground.

    Or, maybe I got this all wrong, SimpleIDE is for doing simple things with the Activity board. If you want to do some of the more advanced stuff than you are on your own, or should be thinking about a different product. This is a very interesting predicament to be in. Still scratching my head.

    Ray

  • iseriesiseries Posts: 1,492

    I tried to get the wifi to work with the WiFi library but was unable to get it to work.

    I don't use that library as I have my own and yes, I have moved that library over to the P2.

    I have several application that use this wifi library that have been running for years now.

    This application wakes up every minute and sends a UDP packet from the Wifi module to a webserver that then collects the data for display.

    Weather Display site.

    I think my library is in the OBEX somewhere.

    The P2 stuff is in github only and I have several as there are no library function available for C programs on the P2.

    This is the program I used with my library to send and receive data from a python program:

    #include "simpletools.h"
    #include "fdserial.h"
    #include "esp8266.h"
    
    
    fdserial *fd;
    char Buffer[1024];
    
    
    int main()
    {
      int i;
    
      fd = esp8266_open(14, 13);
    
      print("Starting\n");
    
      i = esp8266_connect("101.1.1.7", 3260);
    
      esp8266_send(i, "On");
    
      esp8266_recv(i, Buffer, 1024);
    
      print(Buffer);
      printit(Buffer);
    
      print("done\n");
    
      esp8266_close(i);
    
      while(1)
      {
        pause(1000);
    
      }  
    }
    

    This is the Python program that I used to receive the request and return the answer:

    import socket
    
    HOST = ""
    PORT = 3260
    
    HostName = socket.gethostname()
    HOST = socket.gethostbyname(HostName)
    print(HOST)
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
        server.bind((HOST, PORT))
        server.listen()
        conn, addr = server.accept()
        with conn:
            print(f"Connected by {addr}")
            while True:
                data = conn.recv(1024)
                Msg = "What?";
                if not data:
                    break
                if data == b'On':
                    print("Led set on")
                    Msg = "Led Set On"
                if data == b'Off':
                    print("LED set off")
                    Msg = "Led Set Off"
                conn.sendall(bytes(Msg, 'ascii'))
    print('done')
    

    I ran the python program on my windows system but should be the same as the Raspberry Pi.

    Mike

  • RsadeikaRsadeika Posts: 3,837

    I guess I am a slow learner Mike, I will see if I can find it in OBEX. Your hints were way to subtle for me.

    Ray

Sign In or Register to comment.