1

i'm trying to send the coordinates of mouse connected to a raspberry pi to an ESP8266 in the same network. I just wrote this bash script

#!/bin/bash

device='/dev/input/event1'

mouseX="*(REL_X), value*"
mouseY="*(REL_Y), value*"


evtest "$device" | while read line; do
  case $line in
  ($mouseX) X=${line##*value } 
  curl 'http://192.168.0.4/ricevuto?X='"$X" &
  ;;
     
  ($mouseY) Y=${line##*value } 
   curl 'http://192.168.0.4/ricevuto?Y='"$Y" &
  ;;
  esac

done

the command "evtest" capture the mouse movements and the script extract the coordinates, curl send the data.

It works, but it's really SLOW! With the "&" at the end of the curls is faster but sometimes the coordinates are messed up... Is there a way to establish a connection and just transmit data without make a request everytime?

Just to explain my final goal: i'm trying to use a mouse, connected to a raspberry pi, on multiple devices: in this case the receiver (esp8266) will be connected to a arduino leonardo that can recreate the mouse movement on an android TV. Thanks for the help or any other simpler solution!

  • 1
    Of course there is _a way_, but you'd also have to adapt the receiving code, which you didn't show. – Armali May 24 '21 at 15:14
  • 1
    Sorry about that, of course I know that i have to adapt the receiver code, my question is more "general", asking the method, and not the code. After a day of test I reach almost good results with MQTT, but of course with the same "architecture" – Andrea Aghemo May 24 '21 at 16:43
  • It sounds like you're already working on a sophisticated solution - I'd have, for a start, just suggested a simple approach with piping through `sed` and `netcat`, but MQTT might be more promising. I don't know what you mean by _"architecture"_. Surely it's not optimal to have a shell loop which handles individual lines. – Armali May 24 '21 at 16:51
  • 1
    It means that the code is the same but with "mosquitto_pub" instead of curl. Thanks for the suggestion, I'll check for these commands! – Andrea Aghemo May 24 '21 at 17:51

1 Answers1

0

Here's a sketch of how the shell loop could be avoided:

evtest "$device" | sed -un 's/.*(REL_\([XY]\)), value /\1=/p' | …

The can be a command like netcat or mosquitto_pub -l. The above will generate messages like X=2 or Y=1, but the message format can be changed by using some other replacement than \1=.

Armali
  • 18,255
  • 14
  • 57
  • 171