We have upgraded the community system as part of the upgrade a password reset is required for all users before login in.

[Solved] How to stop Python oledExp writing to STDOUT/STDERR ?



  • This is my second project using Python 2 oledExp and I'm having the same problem as before. When I SSH into the Onion2 and run my script I get this:

    ./clock.py 
    > Setting display to ON
    > Initializing display  
    > Writing 'Wed 10 Jul 2019' to display
    > Writing '08:03 +0100' to display
    > Writing 'Local Time BST' to display
    

    and the output also appears on the display, as planned. is there any way that I can stop the above appearing? I just need it to appear on the display. Here is what I've tried so far:

    within the script:
    
    old_stderr = sys.stderr
    old_stdout = sys.stdout
    sys.stderr = open(os.devnull, 'w')
    sys.stdout = open(os.devnull, 'w')
    

    and in crontab:

    crontab -l
    */1 * * * * /root/clock.py >/dev/null 2>&1
    #
    

    and here's the top of the script with a write statement:

    oledExp.setDisplayPower(1)
    oledExp.driverInit()
    oledExp.setBrightness(127)
    oledExp.setTextColumns()
    oledExp.setDisplayMode(0)
    oledExp.setVerbosity(0)
    oledExp.clear()
    ...
    oledExp.setCursor(0,0)
    oledExp.write("the time is now...")
    


  • @peter-garner-0 I think you need move oled.setVerbosity to the top of your code, it seems to default to debug verbosity so it is reporting all of your actions prior to you setting verbosity to Info level. The C library calls OnionPrint inside the OledExp code and this relies upon the verbosity setting.

    I haven't tested this on Python but this is the case in C.



  • Got it! Thanks for that. I actually had to do it in this order:

    oledExp.setVerbosity(-1)
    oledExp.driverInit()
    oledExp.write("Hello World")
    

    I wasn't expecting to have to set the verbosity before the init but there you go...

    Actually, oledExp.setVerbosity() doesn't seem to be mentioned for the Python module, but it makes sense given it's C origins.



  • @peter-garner-0 the first line in the C code of oledDriverInit is:

    onionPrint(ONION_SEVERITY_INFO, "> Initializing display\n");

    So you need vernbosity set to < INFO before calling init.



Looks like your connection to Community was lost, please wait while we try to reconnect.