element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
BeagleBoard
  • Products
  • Dev Tools
  • Single-Board Computers
  • BeagleBoard
  • More
  • Cancel
BeagleBoard
Forum Beaglebone Black -debian 10.10- starting python code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
BeagleBoard requires membership for participation - click to join
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 5 replies
  • Subscribers 98 subscribers
  • Views 1589 views
  • Users 0 members are here
  • debian
  • python
  • BeagleBone Black
  • linux
Related

Beaglebone Black -debian 10.10- starting python code

mraker
mraker over 3 years ago

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)

  • Sign in to reply
  • Cancel
  • shabaz
    shabaz over 3 years ago

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mraker
    mraker over 3 years ago in reply to shabaz

    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)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mraker
    mraker over 3 years ago in reply to 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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 3 years ago in reply to mraker

    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 : (

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mraker
    mraker over 3 years ago in reply to shabaz

    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?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube