5 Replies Latest reply on Oct 12, 2021 8:47 AM by mraker

    Beaglebone Black -debian 10.10- starting python code

    mraker

      Hello, i have a beaglebone black. i have two python code.

      One to communicate on uart with hercules. With crontab I can run this code automatically on boot and test it with hercules.

       

      But,

      Initially my code is not working even though I followed the same steps for tcp communication.

       

      I follow these steps for crontab.

       

      crontab -e

      @reboot sudo  python /root/python_code.py &

       

      -----------------------------------

      not working at startup code

      --------------------------------------

      import socket
      import time
      
      
      host = '192.168.1.80'
      port = 23
      
      
      while True:
              TestServer = socket.socket()
              TestServer.bind((host,port))
              TestServer.listen(1)
              c, addr = TestServer.accept()
              print ('Connection from: ' + str(addr))
              while True:
                      data = c.recv(1024)
                      if not data:
                              break
                      print "data: " + data
                      #data = str(data).upper()
                      data = "feedback: " +data+ " data received \n"
                      print "sending feedback"
                      c.send(str(data))
              c.close()
      
              time.sleep(1)
      
        • Re: Beaglebone Black -debian 10.10- starting python code
          shabaz

          Hi Omer,

           

          I don't know the answer, but I know how to start troubleshooting this, and possible workarounds.

          To start troubleshooting, check the log files (could be /var/log folder or similar) since there may be syslog output there indicating the issue. It could be as simple as you attempting to bind when the network is not up yet (I have no idea).

          For workaround, I'd investigate systemd.. I don't know how popular crontab is, but perhaps systemd is a more modern way to do it.

          When I had to do this for paid work, I didn't use crontab either, I used /etc/init.d, and then wrote code to handle failure conditions like restarting if things crashed (and cleanup, logging etc). But I'd write some systemd scripts if I had to do it now.

          I'm not saying it is better or worse, there are advantages/disadvantages for all methods, just something to consider if you have not already considered it and ruled it in or out for specific reasons.

          1 of 1 people found this helpful
            • Re: Beaglebone Black -debian 10.10- starting python code
              mraker

              Thank for reply,

               

              But i learned something new.

              I doubted if the code was working and I wrote the uart activation line to test it.

               

              When the beaglebone starts I check my uart pins and I can see that the uart is active. But i have no idea why the following parts are not working.

              Also when i run the code manually in command line it works.

               

              Probably the code needs to be edited but I couldn't find how to do it.

               

              When the beaglebone starts;

              UART1 Working

              UART2 Working

              but

              UART4 NOT Working

               

              TestServer.bind((host,port)) this part not work

               

               

               

              Thanks.

               

               

               

               

              MY CODE:

               

              import Adafruit_BBIO.UART as UART

              import socket

              import time

               

               

              host = '192.168.1.80'

              port = 8888

              UART.setup("UART1")

               

               

              while True:

                      UART.setup("UART2")

                      time.sleep(0.5)

                      TestServer = socket.socket()

                      TestServer.bind((host,port))

                      UART.setup("UART4")

                      TestServer.listen(10)

                      c, addr = TestServer.accept()

                      print ('Connection from: ' + str(addr))

               

               

                      while True:

                              data = c.recv(1024)

                              if not data:

                                      break

                              print ("data: " + str(data))

                              newdata = "feedback: " + str(data)+ " data received"

                              print ("sending feedback")

                              newdata=newdata.encode('utf-8')

                              #c.sendall(newdata.encode('utf-8')) 

                              c.send(newdata)

                      c.close()

               

               

                      time.sleep(0.5)

                • Re: Beaglebone Black -debian 10.10- starting python code
                  mraker

                  I think the problem is as you first said.
                  At boot, the ip starts as 127.0.1.1.
                  I'm looking into how to change this on boot.

                    • Re: Beaglebone Black -debian 10.10- starting python code
                      shabaz

                      Hi Omer,

                       

                      Regarding the point:

                      mraker  wrote:

                      I'm looking into how to change this on boot.

                      It's not a change of value or anything, it's more a case of things need time to be brought up, and your job may be running too soon.

                      I can't help with resolving it, but if you're sure this is the issue, (and you can try logging in your code, or touching a file, etc., to get feedback about what is really the issue, so that it's definite), then you'd need to investigate how to delay running or schedule it to run later. I know nothing about crontab though.

                      Also, if you look at your code, there's zero error-handling. The code needs designing so that if the network is not up, it pauses and reattempts several times, at the least.For help, you may need to also ask on Linux forums, since it is more related to that, although you may get lucky and find a Linux expert here eventually too - it's not me unfortunately : (

                        • Re: Beaglebone Black -debian 10.10- starting python code
                          mraker

                          Hello Shabaz,

                           

                          This time I will ask a different question.

                          I connect beaglebone black to computer via mini usb.

                           

                          I am sending a data using hercules. for example '1'. When the beaglebone black receives the data, I want it to send the message "led on" as a feedback.

                           

                          I am able to do this when I run the code after connecting serially and logging in. but what I want is that when I connect the card to usb, the code will run automatically without doing anything and it will give me feedback when I send the "1" data.

                           

                          I guess my code doesn't work automatically because I'm not logged in.

                          How should I continue?