A.IR Shield Nano RPi Python script: Difference between revisions

From AnalysIR WiKi
Jump to navigation Jump to search
No edit summary
(3 intermediate revisions by the same user not shown)
Line 72: Line 72:
* '''servAIR.py''' - is the name of the script and is located in your active directory.
* '''servAIR.py''' - is the name of the script and is located in your active directory.
* '''9449''' - is the port number to use for communications & ideally should be the same number that you use for AnalysIR.
* '''9449''' - is the port number to use for communications & ideally should be the same number that you use for AnalysIR.
* Experienced users will be able to run from other locations, force running in the background(''python servAIR.py 9449 &'') and pipe script output to a log file.
* Experienced users will be able to run from other locations and force running in the background(''python servAIR.py 9449 &'').
To run the script run the following command: '''''python servAIR.py 9449 &'''''. Depending on configuration, some users may need to type '''''sudo python servAIR.py 9449 &'''''.
To run the script run the following command: '''''python servAIR.py 9449 &'''''. Depending on configuration, some users may need to type '''''sudo python servAIR.py 9449 &'''''.


Line 92: Line 92:
* Control your IR devices using Amazon Alexa/Echo, Google Assistant/Home, Apple Siri or equivalent voice devices.
* Control your IR devices using Amazon Alexa/Echo, Google Assistant/Home, Apple Siri or equivalent voice devices.
* Control your IR devices using MQTT or similar brokers.
* Control your IR devices using MQTT or similar brokers.
* Log all IR activity to your RPi, PC or remote server


== Can I use this script in my own project or product? ==
== Can I use this script in my own project or product? ==
Yes, there are no restrictions on using the script in your own commercial or hobby(non-commercial) projects, provided it is used together with an official A.IR shield purchased from AnalysIR or an authorised re-seller/agent. For other uses please contact us directly. We always appreciate when users provide a link back to '''[https://www.AnalysIR.com/blog/ our site]'''.
Yes, there are no restrictions on using the script in your own commercial or hobby(non-commercial) projects, provided it is used together with an official A.IR shield purchased from AnalysIR or an authorised re-seller/agent. For other uses please contact us directly. We always appreciate when users credit AnalysIr as author and provide a link back to '''[https://www.AnalysIR.com/blog/ our site]'''.


== Getting Support ==
== Getting Support ==
If you need additional support please open a new issue in our '''[https://www.analysir.com/blog/irforum/ IRforum]''' - under ''Products:Support Requests''.
If you need additional support please register & open a new issue in our '''[https://www.analysir.com/blog/irforum/ IRforum]''' - under ''Products:Support Requests''.

Revision as of 05:37, 5 December 2017

A.IR Shield Nano with Raspberry Pi
A.IR Shield Nano with RPi

The A.IR Shield Nano is a versatile IR tranceiver built on top of the Arduino Nano. It connects to a PC (Windows, Linux, RPi etc) via the included serialUSB port connector and features advanced IR circuitry for sending & receiving IR signals. It also records the IR carrier frequency with every signal.

This page describes an example PYTHON (2.7) script for using the shield on the RPi via HTTP POST requests. The script can also be used on any LINUX system along with Windows or OSX, subject to PYTHON 2.7 compatability.

About the A.IR Shield Nano

This is described elsewhere as follows:

About the Script

The script source code, is available to everone who has purchased an A.IR Shield Nano. Just contact us for the download link anytime after purchase. You will require PYTHON 2.7 to run the script and it should be installed on your RPi or other supported platform. We are happy to provide script specific support, but you should be prepared to troubleshoot your own PYTHON & RPi installation by yourself.

Supported Platforms

We have tested using an RPi running the latest Jesse updates as of Nov 2017. It should also run on other PYTHON 2.7 platforms (Linux, Windows, OSX etc.) with little or no adjustments. The script assumes the use of AnalysIR to accept received signals. In this context AnalysIR can be replaced by any other properly configured HTTP web server. Signals can be sent using a properly configured HTTP POST, which can be sent from any location or platform - using a browser or application.

Sending & Receiving IR

The script is written to send IR based on web POST requests. One end of each of these is supported by the Script/Shield. By default the other end is supported by AnalysIR. To integrate your own project or product, you will need to implement a HTTP post request to send an IR signal via the script. You will also need to implement a HTTP server to handle a POST request when signals are received.

You may customise the script to only support either sending or receiving of IR signals, instead of the default of both.

We have provided an example web page with a HTML POST form, which can be used as a way to test sending signals using the script and as a basis for your own implemetation. An example of this form is available via: Sending Test signals from a HTML POST form

Example HTTP POST requests

Here we provide example POST requests, captured from Wireshark to assist you if you need to create your own application.

Signal received by shield POSTed to PC/Server from Python Script:

POST /analysir.signal HTTP/1.1
Host: 192.168.0.233:9449
Content-Length: 497
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.4.3 CPython/2.7.9 Linux/4.9.35+
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded

format=air&channel=1&analysir=%2438369%3A9037%2C4364%2C649%2C472%2C669%2C448%2C669%2C448%2C641%2C472%2C673%2C444%2C645%2C472%2C669%2C452%2C641%2C1592%2C645%2C472%2C669%2C1560%2C677%2C1564%2C665%2C1568%2C673%2C1560%2C621%2C1616%2C645%2C1588%2C645%2C472%2C673%2C444%2C677%2C1560%2C665%2C448%2C645%2C1592%2C645%2C1588%2C673%2C440%2C649%2C472%2C645%2C472%2C645%2C1588%2C645%2C472%2C673%2C1560%2C649%2C472%2C697%2C420%2C641%2C1592%2C645%2C1588%2C673%2C1564%2C645%2C40724%2C9009%2C2180%2C645%2C%3B%0D%0A

Signal sent by shield POSTed from PC/Server to Python Script:

POST /analysir.signal HTTP/1.1
Host: 192.168.137.210:9449
Referer: http://www.analysir.com/sendsignal.html
Content-Type: application/x-www-form-urlencoded
Content-Length: 468
Connection: keep-alive
Cache-Control: max-age=0

analysir=%2438%3A9000%2C4500%2C560%2C560%2C560%2C560%2C560%2C1690%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C1690%2C560%2C1690%2C560%2C560%2C560%2C1690%2C560%2C1690%2C560%2C1690%2C560%2C1690%2C560%2C1690%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C1690%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C560%2C1690%2C560%2C1690%2C560%2C1690%2C560%2C560%2C560%2C1690%2C560%2C1690%2C560%2C1690%2C560%2C1690%2C560%2C39915%2C9000%2C2325%2C560%2C%3B

Note: the use of analysir.signal as the webpage address and that the body of the request is url encloded. We have not included example traces of HTTP requests as they are all entered directly via the browser(or curl etc.).

Sending Signals stored in the script

We have also provided for storing pre-recorded signals within the script and sending them via HTTP request using only the signal number, as follows:

http://192.168.137.217:9449/?signalNo=1
http://192.168.137.217:9449/?signalNo=2
....
http://192.168.137.217:9449/?signalNo=20
....
http://192.168.137.217:9449/?signalNo=n

You can customis the script with your own signals and there is no limit on the number of signals you can store for sending (within reason). An error will be reported if you reference a non-existing signal.A possible enhancement would be to reference the stored signals by name (e..g. TV OFF, TV2 VOL+ or AC ON) instead of number or indeed both.

Installing the Script

Create a directory named servAIR on your RPi and copy the file servAIR.py into that directory. The script will write to log files (servAIR.log.txt & requestLogAIR.txt) for saving script INFO messages and HTTP request logs. Of course this logging can be easily extended by the user.

Running the Script

The script should be executed as follows: python servAIR.py 9449 where:

  • python - starts a python process to run the script
  • servAIR.py - is the name of the script and is located in your active directory.
  • 9449 - is the port number to use for communications & ideally should be the same number that you use for AnalysIR.
  • Experienced users will be able to run from other locations and force running in the background(python servAIR.py 9449 &).

To run the script run the following command: python servAIR.py 9449 &. Depending on configuration, some users may need to type sudo python servAIR.py 9449 &.

Stop the Script running

The best way to stop the script is to issue the following request via a web browser or CURL

http://192.168.0.217:9449/?exitCMD=1

(Change the IP Address & port number to match your own configuration. Experienced users may want to add in additional support for a password on this command)

Using the Script in your own projects or products

Using this servAIR script, it is now a small step to create your own full featured IR remote control project or product. Most of the hard work is done for you and all you need to do is to create your own web based application that leverages the functionality provided by the script and the A.IR Shield Nano.

Project ideas include:

  • Control your IR devices using your smart Phone (iPhone, Android etc)
  • Control your IR devices using your Tablet(iPad, Android etc)
  • Control your IR devices using your IoT application or device (ESP8266, ESP32, Particle Photon etc)
  • Control your IR devices using IFTTT
  • Control your IR devices using Amazon Alexa/Echo, Google Assistant/Home, Apple Siri or equivalent voice devices.
  • Control your IR devices using MQTT or similar brokers.
  • Log all IR activity to your RPi, PC or remote server

Can I use this script in my own project or product?

Yes, there are no restrictions on using the script in your own commercial or hobby(non-commercial) projects, provided it is used together with an official A.IR shield purchased from AnalysIR or an authorised re-seller/agent. For other uses please contact us directly. We always appreciate when users credit AnalysIr as author and provide a link back to our site.

Getting Support

If you need additional support please register & open a new issue in our IRforum - under Products:Support Requests.