You will need:
- an API key from your account on https://remocon.tv, going forward <api_key> is to be replaced with your API key. You get an API key right away by creating a robot in your account page.
Our API runs through websockets and returns JSON data.
It works just like any other HTTPS API out there that you are used to, except instead of making HTTPS requests you make a websocket connection, and then interpret the JSON data that you get back.
Interpreting the JSON data is very easy as you will see examples.
This means that it's very simple to set up, to use, and it's compatible with basically everything.
In Python and MicroPython:
In C and in Arduino:
That's it now get working.
The other side of interpreting data is to stream video to our API
This works by sending video streams to our server, that can then be viewed selectively and with a lot of flexibility.
Raspberry Pi Example
Open two terminal windows in your raspberry pi, run the following in each of them. The first streams the video, the second gets the commands from the website to control the motor and steering for a remote control car. To control anything else you just change the commands there.
First terminal window:
ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 \
-f mpegts -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 \
If that throws an error then try "sudo apt-get install ffmpeg" first, or reach out to us.
Second terminal window:
First create a file called websockets.py and copy this into it:
import RPi.GPIO as GPIO # for raspberry pi GPIO
import websockets # for web sockets
import json # https://www.w3schools.com/python/python_json.asp
# sending data in json format lets us send as much data
# as we want and in any order
apiKey = "<api_key>"
domain = "ws://control.remocon.tv/"
# set GPIO pin variables
# the standard Raspberry Pi 4 pins are: 17, 27, 22, 5, 6, 13, 26, 23, 24, 25, 12, 16
motor = 17
servo = 27
# raspberry pi GPIO setup
motorPWM = GPIO.PWM(motor, 50) # servo GPIO for PWM with 50Hz # <-- might have to
# play around with the frequency for the motor,
# for the servo 50 should work but for the motor I am not sure
servoPWM = GPIO.PWM(servo, 50) # servo GPIO for PWM with 50Hz
async def connectSocket():
url = domain+apiKey
async with websockets.connect(url) as websocket:
print('websocket connected to ' + url)
input = await websocket.recv()
print(message) # comment this out if you don't want to see every single message
data = json.loads(message)
if (data["type"] == 'keydown' and data["key"] == 87):
print('forward key pressed')
elif (data["type"] == 'keyup' and data["key"] == 87):
print('forward key released')
elif (data["type"] == 'keydown' and data["key"] == 83):
print('backward key pressed')
motorPWM.ChangeDutyCycle(0) # <-- not sure if there
# is a PWM duty cycle for reverse, motors
# should have it but I am not sure what it is
elif (data["type"] == 'keyup' and data["key"] == 83):
print('backward key released')
elif (data["type"] == 'keydown' and data["key"] == 65):
print('left key pressed')
elif (data["type"] == 'keyup' and data["key"] == 65):
print('left key released')
elif (data["type"] == 'keydown' and data["key"] == 68):
print('right key pressed')
elif (data["type"] == 'keyup' and data["key"] == 68):
print('right key released')
Then run this in the terminal window:
sudo apt-get install python3
sudo apt-get install python3-pip
pip3 install websockets
pip3 install RPi
python3 websockets.py # <-- this is the file we just created
And you're done, it should work. If you want to have these two run on startup then add them to the crontab file as cron jobs. The benefit of doing it this way is that you can see how it works and then customize what your Raspberry Pi does in response. When you run the websockets file it will show you every command, you can then change what's in the 'if' statements to make the Raspberry Pi do whatever you want.
How to make it run automatically on startup
Still writing this part, although you can do it with cron jobs. This is regarding the Raspberry Pi example above.
Why is this on Neocities rather than on some other documentation site?
It's because it's very easy to use and FOSS and ideologically good.
Last edited on 2021-04-11