1

I have following situation (and problem):

Setup:

Raspberry Pi Zero W, connected to a Windows client via USB-OTG in Ethernet emulation mode.

The Pi is controlling a single servo motor, via Python Scripts.

Goal:

I need to be able to execute the python scripts, which control the servo. Execution must be triggered from the Windows client via a Batch-file.

What I've tried:

As the Pi is connected via Ethernet emulation, it gets a (static) 169.x.x.x IP. I am able to connect to the Pi via SSH.

I tried writing a batch-file which does the following:

  1. Connect to Raspberry Pi (with given credentials) via "plink.exe"
  2. Execute Python script.

Problem:

While my solution generally works, it is way too slow. From executing the batch-file to the actual control of the servo-motor it needs about 8-10 Seconds.

This is mainly because of the SSH authentification process.

If I am connected to the Pi via Putty and execute the script directly, the motor reacts almost instantly.

Possible solution I have thought of:

I have thought of follwing - but am unable to find a way to realize it:

  1. Create shared folder on Windows Client
  2. (permanently) mount shared folder on Raspberry
  3. Some kind of Script or daemon running on the Raspberry monitors the folder (every Second or so)
  4. If folder contains file with name "rotate_right.xy": -> Execute "rotate_right.py" and delete "rotate_right.xy"
  5. Batch file on Windows Client: create file "rotate_right.xy"

As I am no coder whatsoever, I am (currently) unable to solve this. Maybe my thinking process is faulty - or there are more "elegant" ways to solve this -- I really hope someone can help me with this (or push me in the right direction)

Thanks in advance, Iring

  • This thread might help raspberrypi.org/forums/viewtopic.php?t=44945 in fact I would maybe go to the Raspberry Pi forum with this. – Michael Harvey Sep 22 '18 at 9:11
  • This did it for some people (but read the whole thread): appending UseDNS=no to "/etc/ssh/sshd_config" and rebooting. – Michael Harvey Sep 22 '18 at 9:14
  • @MichaelHarvey Thanks for the tip!! "UseDNS no" definitly made an improvement! I am now down to 3 seconds - which is way more useable! – iringsson Sep 22 '18 at 9:18
  • unfortunately, the client runs on Windows 7 (embedded). The client is a POS checkout terminal with an RFID reader for payment. The servo is used to put on a card or remove the card from the reader - for remote testing purposes – iringsson Sep 22 '18 at 9:24
  • @MichaelHarvey and Iringsson, please note that all relevant information should preferably by included in either the question or answers not long comment sections. If you like, please feel free to edit accordingly. I guess that the results now "hidden" in the comments should be part of the answer. Comments might be deleted and should preferably not turn into a long back-and-forth discussion - please use chat in that case. Thanks. – Ghanima Sep 22 '18 at 15:54
2

A disclaimer: you are using a Zero W. I do not have a Raspberry Pi Zero, but on my home LAN I do have an original (first) model Pi B, using a USB wi-fi adapter, and also a model 3 B+ on ethernet, both running Raspbian Stretch. I tried the following from a Windows 10 command prompt (use %%N for the FOR metavariable in a batch script):

for /L %N in (1,1,5) do @plink -ssh -pw password pi@192.168.0.X "date +%T.%3N"

With the pi B I got these times back:
11:00:53.537
11:00:55.475
11:00:57.438
11:00:59.407
11:01:01.338 - around 1.9 seconds delay.

The model 3 B+ gave me
10:05:39.715
10:05:40.097
10:05:40.522
10:05:40.916
10:05:41.305 - slightly under 0.4 seconds.

At the raspberrypi.org forums I found this thread: Unusually slow ssh login where a suggested action was to edit /etc/ssh/sshd_config to show "UseDNS no" and "UsePAM no". I did this and got the model B down to 1.5 seconds, and the 3 B+ down to 0.25 seconds.

I do not know how much time your Python module takes to load.

I have also seen another very recent (Sep 2018) thread: Slow ssh response on RPI zero w where people say that using raspi-update can result in an unstable kernel version (i.e. greater than 4.14.62 at this time) such as 4.14.67 and 4.14.68, which can result in slow ssh response in the Zero W. The correct update method is given as sudo apt update && sudo apt full-upgrade -y

0

Just make a python script that runs all the time, and opens a UDP socket (as a server), and processes incoming packets. Then write its client-side pair to send these packets from your PC. (untested code as I don't have your setup):

import socket

UDP_IP = "0.0.0.0"
UDP_PORT = 5555

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes, doesn't matter here
    print(data)
    #just parse your packet, and set the servo based on that

And the client side:

import socket
UDP_IP = "169.x.x.x" #you could use broadcast as well
UDP_PORT = 5555
MESSAGE = bytearray([0x00,0xff]) #this is the data we send now.. can come from a command line parameter or whatever
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

You can encode in your message anything.. different channels for multiple servos, authentication, checksum, commands, etc.. UDP is connectionless, and it has very low latency. You can call your client-side code from a batch file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.