Search code examples
pythonpython-3.xraspberry-pitelnettelnetlib

Why won't this code work in python but when I telnet It does?


What I am trying to do it make a GUI to control a raspberry pi bot. I have telnet to wirelessly connect to it. When I telnet to it from a terminal on my computer, I can run echo 1=230 > /dev/servoblaster to make one of the servos move.

However, when using that same piece of code in the python GUI, it does not make the servo move, but I don't get an error message either. At one point it was all working but then suddenly it stopped.

Below is a simplified version of the python script that I am using for my GUI:

from tkinter import *
import telnetlib

bot = telnetlib.Telnet("192.168.1.128")
bot.read_until(b"login: ")
bot.write(("pi \n").encode('ascii'))
bot.read_until(b"Password: ")
bot.write(("pi \n").encode('ascii'))
bot.write(("cd /home/pi/pibits/ServoBlaster/user \n").encode('ascii'))
bot.write(("sudo ./servod \n").encode('ascii'))
bot.write(("cd \n").encode('ascii'))

bot.write(("echo 1=230 > /dev/servoblaster \n").encode('ascii'))

Would someone be able to help me work out why when I connect to the bot from a terminal I can control it but when running the same command with the above code nothing happens, not even an error message.

Just in case this is of any use, I know the above code is at least reaching the wireless receiver of my raspberry pi because the lights on it flash every time it receives a signal.

Thanks


Solution

  • Your space in the username and password commands are keeping your telnet session from actually logging into the pi bot. The spaces aren't ignored.

    You need:

    bot.read_until(b"login: ")
    bot.write("pi\n".encode('ascii'))
    bot.read_until(b"Password: ")
    bot.write("pi\n".encode('ascii'))