http_post_message demonstrates TCP/IP TXMODE via HTTP POST of telemetry data

http_post_message.py

HTTP POST DEMO USING Python TCPIP TX MODE

This script shows how to use an HTTP POST to send telemetry data via HTTP using Python TCPIP as the telemetry mode.

The script will generate an HTTP POST header which includes some custom information (in this case, battery volage and station name)

Below is an example of the HTTP header generated by the script, as well as the content “This is a test message”:

POST / HTTP/1.1 Host: httpbin.org Content-Type: text/plain Content-Length: 22 Battery-Voltage: 10.70 Station-Name: Sutron XLink

This is a test message

If you are using a version of LinkComm which will not let you set the TXMODE to Python TCPIP (but are running a version of firmware that supports it) you will need to click the “Test Script File…” button and the script will set the necessary fields. If you need to change the setup after that, be sure to click “Test Script File…” button again.

The public test server httpbin.org is used to perform the test transmission and verify the result. The last result can be displayed by running the script task display_results.

With the following TX1 setup, this script will transmit sensor data via secure HTTPS:

!TX1 Enable=On
!TX1 Radio Type=Cell
!TX1 Kind=Scheduled
!TX1 Label=HTTP POST
!TX1 Scheduled Interval=01:00:00
!TX1 Scheduled Time=00:00:00
!TX1 Data Source=Measurement
!TX1 Format=CSV
!TX1 Custom Script Format=Off
!TX1 Mode=Python TCPIP
!TX1 Secure=On
!TX1 Use Certificate=Off
!TX1 Mode Function=send_message_http
!TX1 Main Server=httpbin.org
!TX1 Backup Server=
!TX1 Server Port=443
!TX1 Server Username=
!TX1 Server Password=
!TX1 Server Path=post
http_post_message.display_results()

Display the last results received from the HTTP Server

http_post_message.receive_data(socket, amount=1024, iterations=120)

Receive data from a socket.

This function calls socket.recv(amount) up to ‘iterations’ times, concatenating and returning the result. It exits early if after receiving data on one iteration it doesn’t receive data on the next.

Parameters:
  • socket (socket.socket) – The socket object to receive data from.
  • amount (int, optional) – The maximum amount of data to be received at once, defaults to 1024.
  • iterations (int, optional) – The maximum number of iterations to attempt receiving data, defaults to 10.
Returns:

The concatenated data received from the socket.

Return type:

bytes

http_post_message.send_message_http(socket, message, file_name)

Function to send a telemetry message via HTTP POST to httpbin.org.

Parameters:
  • socket – Open socket for sending/receiving data.
  • message – the telemetry data we want to send, generated using standard formatting
  • file_name – unused as we send the message
Returns:

0 if HTTP POST was accepted and acknowledged, 1 if there was an issue.