termux × AWS Send smartphone location information to AWS with IoT

Introduction

My wife told me to line up when I left the office, but honestly it's a hassle. I wondered if I could automatically notify that I had left the workplace, so I sent the location information to AWS for the time being.

図1.png

environment

Smartphone: Kyocera (KTV44) OS:Android 9

Smartphone (termux) side

Install termux on Android using Google Play. https://play.google.com/store/apps/details?id=com.termux&hl=ja

Since it is difficult to input on the keyboard with a smartphone, After installation, enter the following command to build the environment for ssh.

$ termux-setup-storage #Allow termux to use android storage.
$ pkg install openssh #Install ssh (both server and client)
$ sshd #Start ssh server
$ passwd #password setting
$ whoami #Confirm user name (make a note of it)
$ ifconfig #Check the IP address of your smartphone (make a note of it)

By the way, the IP address displayed by termux is the same as the IP address of the smartphone itself, so if you know it in advance, you do not need to configure ifconfig.

Next, connect to your smartphone with ssh from your PC to make it easier to work. Be careful here. The default TCP port number for ssh is 22, but for termux 8022 is specified. Therefore, specify the port number with the -p option. PC (Ubuntu) side

$ ssh -p 8022  (User name)@(IP address of smartphone)

If you ssh for the first time Are you sure you want to continue connecting (yes/no)? Answer yes. After that, enter the password set on your smartphone.

Since background processing is prohibited in the initial state, the ssh connection will be interrupted each time the smartphone goes to sleep. By selecting "RELEASE WAKE LOCK" on your smartphone, background processing will be performed.

Screenshot_20201015-214307.png

Once you can use ssh, we will continue to build the environment.

$ pkg install git #Install git for AWS IoT Core to be used later.
$ pkg install python #Install Python as well.
$ pkg install vim-python #Also install vim for file editing.
$ pkg install termux-api #termux-Install api as well. Required to get GPS information.

After the installation system is completed, the next step is to set up AWS Iot Core. Refer to this ↓ article, register the thing called "android01", and perform the connection test from the smartphone to AWS. https://qiita.com/zakuzakuzaki/items/e30d63598ca1d6c0f2a9

I was surprised because I was able to do it as it was. termux is amazing.

Use the termux-location command in "termux-api" to get the location information. https://wiki.termux.com/wiki/Termux-location

$ termux-location -p network #Output the current position that the smartphone is observing
{
  "latitude": 	35.6359322,
  "longitude": 139.8786311,
  "altitude": 43.5,
  "accuracy": 23.26799964904785,
  "vertical_accuracy": 3.299999237060547,
  "bearing": 0.0,
  "speed": 0.0,
  "elapsedMs": 97,
  "provider": "network"
}

-p is an option to select the location information acquisition method, and select from [gps / network / passive]. The default is gps, but for some reason I couldn't do it on my smartphone, so I chose network. (No error, wait for a long time.) By the way, even if you change your smartphone from Wi-Fi connection to 3G connection, you can not get location information unless you select network.

Probably a problem with the permissions of the termux app, but I couldn't solve it, so I left it. Normally, android can give usage rights such as folders, cameras, GPS, etc. for each application on the application setting screen. Termux doesn't come up with permission options in the first place, so there's no way to grant it.

After confirming that the location information can be obtained, save it in json format for easy processing.

$ termux-location -p network |sed -e "4,10d"|tr -d '\n'|tr -d ' '| sed -s s/,}/}/g > location.json
$ cat location.json 
{"latitude":35.6359322,"longitude":139.8786311}#Confirm that it was shaped nicely

What you are doing ・ Get location information ・ Deleted lines 4-10 (other than latitude and longitude) -Delete line breaks -Delete space -Delete the last "," (comma) -Save location.json in the current directory is. I think it's okay to send all the information as it is, but I cut off unnecessary data in order to reduce the amount of communication as much as possible.

Next, create a program to publish your location. Create "jsonPub.py" which is a modified version of the last part of "basicPubSub.py", a sample file in aws-iot-device-sdk-python. Make sure to load and publish location.json, which contains location information.

Before editing

basicPubSub.py (line 112 and after)


# Publish to the same topic in a loop forever
loopCount = 0
while True:
    if args.mode == 'both' or args.mode == 'publish':
        message = {}
        message['message'] = args.message
        message['sequence'] = loopCount
        messageJson = json.dumps(message)
        myAWSIoTMQTTClient.publish(topic, messageJson, 1)
        if args.mode == 'publish':
            print('Published topic %s: %s\n' % (topic, messageJson))
        loopCount += 1
    time.sleep(1)

After editing

jsonPub.py (line 112 and after)


# Publish to the same topic in a loop forever
with open("location.json", "rb") as load_file:#Reading location information file
    location = bytearray(load_file.read())#Convert to bytearray type for MQTT transmission
loopCount = 0
while True:
    if args.mode == 'both' or args.mode == 'publish':
        myAWSIoTMQTTClient.publish(topic, location, 1)#Publish location information
        if args.mode == 'publish':
            print('Published topic %s: %s\n' % (topic, location))#For confirmation
        loopCount += 1
    time.sleep(1)

This completes the construction on the smartphone side.

AWS side settings

Next, set the cloud side. It just changes the policy.

From AWS IoT> Policies, add topics that you can publish and subscribe to. This time I added a topic named "location". When you're done editing, "Save as new version". Failure to do this will result in a connection error.

0.PNG

test

Go to AWS IoT> Tests, enter the topic name (location), and then click Subscribe to Topic.

1.PNG

Run jsonPub.py in your terminal. Specify the topic name with the -t option at the end of the command.

python jsonPub.py  -e (Please match your ARN)-ats.iot.ap-northeast-1.amazonaws.com -r root-CA.crt -c android01.cert.pem -k android01.private.key -t location

Confirm that the following message is output to the terminal. I was able to subscribe to the location information that my smartphone published to AWS from AWS.


Received a new message: 
b'{"latitude":35.6359322,"longitude":139.8786311}'
from topic: 
location
--------------

In addition, on the AWS side, make sure that you subscribe to the location topic. 2.PNG

Thank you for your support.

in conclusion

We have built an environment to send the location information of the smartphone to AWS. Next, I'd like you to use lamda etc. to automatically notify you of leaving the office and check the departure time of the return train. By the way, please be assured that the location information displayed on this page is not my home.

The file created this time is posted on github. https://github.com/zakuzakuzaki/zaki-aws/tree/main/iot

Reference URL

Thank you for your cooperation.    I tried to prepare the development environment on the Android terminal https://qiita.com/leosuke/items/b83425d5a6730aa4cf94

Create a mobile repository that you can carry with Termux https://wlog.flatlib.jp/item/1859

Read JSON in Python https://qiita.com/kikuchiTakuya/items/53990fca06fb9ba1d8a7

Recommended Posts

termux × AWS Send smartphone location information to AWS with IoT
Visualize location information with Basemap
Send images taken with ESP32-WROOM-32 to AWS (API Gateway → Lambda → S3)
HTML email with image to send with python
Send CSS compressed to Gzip with Flask
Send data to DRF API with Vue.js
I want to play with aws with python
Connect to s3 with AWS Lambda Python
How to use folium (visualization of location information)
Send a message to LINE with Python (LINE Notify)
How to send a message to LINE with curl
Get the latest AMI information with the AWS CLI
Send an email to Spushi's address with python
Send data from Raspberry Pi using AWS IOT
Easily log in to AWS with multiple accounts
Sample to send slack notification with python lambda
Route guidance to iPhone photography location with Pythonista
Python mock to try AWS IoT Device Shadow
AWS Step Functions to learn with a sample
Getting started with AWS IoT easily in Python
Send msgpack with ajax to flask (werkzeug) environment
Python script to get note information with REAPER
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)