LoginSignup
9
3

More than 3 years have passed since last update.

GPSマルチユニットSORACOM Edition で位置情報(Googleマップ)をLINEに通知する

Posted at

やったこと

GPSマルチユニットSORACOM Editionのデータを、SORACOM Funk経由でAWS Lambdaに送り、位置情報をLINE Notifyを使用して通知する。位置情報はGoogleマップのリンクで。使用する言語は、Python。

GPS マルチユニット SORACOM Edition とは

以下、公式サイトより引用

GPS マルチユニット SORACOM Editionは、「位置情報(GPS)」「温度」「湿度」「加速度」の4つのセンサーと充電式バッテリーを内蔵し、 セルラーLPWAであるLTE-M通信がご利用可能なデバイスです。

詳細については、以下を参照ください。
(公式)GPS マルチユニット SORACOM Edition とは
GPSマルチユニット SORACOM Editionで遊ぶ

SORACOM Funkとは

クラウドサービスの Function を直接実行できるサービスです。
今回は、AWS Lambdaを実行します。

以下参照ください
(公式)SORACOM Funk とは
SORACOM Funkを使ってみた。

LINE Notifyとは

以下、公式サイトより

Webサービスからの通知をLINEで受信
Webサービスと連携すると、LINEが提供する公式アカウント"LINE Notify"から通知が届きます。
複数のサービスと連携でき、グループでも通知を受信することが可能です。

以下参照
Python で LINE にメッセージを送信(LINE Notify)

AWS LambdaでLINE Notifyを呼び出す

SORACOM、AWS Lambda、LINE Notifyの設定については、省略します。

以下を AWS Lambdaに。
Python 3.6
時刻も通知してます。

lambda_function.py
import os
import requests
import datetime

def lambda_handler(event, context):
    lat = event['lat']     #緯度
    lon = event['lon']     #経度

# 現在日時を取得(日本時間)
    now = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=9))) 

# 位置情報が取得できない場合
    if lat == None or lon == None:
      msg = now.strftime('%Y-%m-%d') + " " + now.strftime('%H:%M:%S') + " " + "位置情報を取得できませんでした"
      line_notify(msg)
      return

# Google マップで表示
    mapurl = "https://maps.google.co.jp/maps?q=" + str(lat) + "," + str(lon)  # google map用
    msg = now.strftime('%Y-%m-%d') + " " + now.strftime('%H:%M:%S') + " " +  "現在地はここです" + " " + mapurl
    line_notify(msg)

# LINE Notifyに通知
def line_notify(msg):
    url = "https://notify-api.line.me/api/notify"
    headers = {"Authorization" : "Bearer "+ "<LINEのTOKENを設定>"}
    data = {"message" : msg}
    s3 = requests.Session()
    r3 = s3.post(url, data=data, headers=headers)

結果

IMG_2719.jpg
※文字列[SORACOM GPS]は、LINE Notify内で設定

位置情報が取得できない場合は以下

IMG_2720.jpg

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3