Sumit Kumar
Published © MIT

The Holidays Traveller

Start your new year by sightseeing at your own pace.

BeginnerProtip2 hours99
The Holidays Traveller

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1

Software apps and online services

Tuya Cloud Services
Tuya IoT

Story

Read more

Schematics

Working

Connection

Same for all raspberry pi

Code

Raspberry pi code

Python
import RPi.GPIO as GPIO
import time
from tuya_iot import (
    TuyaOpenAPI,
    AuthType,
)
from urllib.request import urlopen
import json
import wikipedia

url = 'http://ipinfo.io/json'
# Cloud project authorization info
ACCESS_ID = 'ecnthtncb7d2tpmuzlzs'
ACCESS_KEY = 'b75ce52xxxxxxxxxxxxxxxxxx'

# Select an endpoint base on your project availability zone
# For more info: https://developer.tuya.com/en/docs/iot/api-request?id=Ka4a8uuo1j4t4
ENDPOINT = "https://openapi.tuyain.com"

# Project configuration for authorized account
USERNAME = 'test1@gmail.com'  # email address or phone number
PASSWORD = 'Job894455'

SMS_TEMPLATE_ID = "SMS_2460072921"  # SMS template ID
EMAIl_TEMPLATE_ID = "MAIL_9584653696"  # Email template ID

# Initialization of tuya openapi
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.CUSTOM)
print(openapi.connect(USERNAME, PASSWORD))

# Setup the Pin with Internal pullups enabled and PIN in reading mode. 
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)

# Add our function to execute when the button pressed event happens 
GPIO.add_event_detect(18, GPIO.FALLING, callback = SendMsg, bouncetime = 2000)

def SendMsg():
    response = urlopen(url)
    data = json.load(response)
    print(data)
    IP = data['ip']

    # Get location of device through IP
    location = openapi.get(f'/v1.0/iot-03/locations/ip?ip={IP}')
    print(location)
    location = location['result']
    latitude, longitude = location['latitude'], location['longitude']

    landmarks = wikipedia.geosearch(latitude, longitude, results=5)
    landmarksListToStr = ' '.join(map(str, landmarks))

    # get weather based on geo location
    weather_url = f'/v2.0/iot-03/weather/current?lat={latitude}&lon={longitude}'
    weather = openapi.get(weather_url)
    print(weather)
    condition = weather['result']['current_weather']['condition']
    air_quality = weather['result']['air_quality']['aqi']
    print(condition, air_quality)

    params = {
        "landmarks": landmarksListToStr,
        "condition": condition,
        "aqi": air_quality
    }

    payload_json = json.dumps(params)
    print(payload_json)

    email_sent = openapi.post("/v1.0/iot-03/messages/mails/actions/push", dict({"to_address": "gadecito@ryteto.me",
                                                                                "template_id": "MAIL_9584653696",
                                                                                "template_param": payload_json,
                                                                                "reply_to_addr}))

    sms_sent = openapi.post("/v1.0/iot-03/messages/sms/actions/push", dict({"country_code": "91",
                                                                            "phone": "748000000",
                                                                            "template_id": "SMS_2460072921",
                                                                            "template_param": payload_json,
                                                                            "sign_name": ""
                                                                            }))

    print(email_sent)
    print(sms_sent)                                                                  }))

    sms_sent = openapi.post("/v1.0/iot-03/messages/sms/actions/push", dict({"country_code": "91",
                                                                            "phone": "748000000",
                                                                            "template_id": "SMS_2460072921",
                                                                            "template_param": payload_json,
                                                                            "sign_name": ""
                                                                            }))

    print(email_sent)
    print(sms_sent)

Basic python working code

Python
from tuya_iot import (
    TuyaOpenAPI,
    AuthType,
)
from urllib.request import urlopen
import json
import wikipedia

url = 'http://ipinfo.io/json'
response = urlopen(url)
data = json.load(response)
print(data)
IP = data['ip']


# Cloud project authorization info
ACCESS_ID = 'ecnthtncb7d2tpmuzlzs'
ACCESS_KEY = 'b75ce52xxxxxxxxxxxxxxxxxx'

# Select an endpoint base on your project availability zone
# For more info: https://developer.tuya.com/en/docs/iot/api-request?id=Ka4a8uuo1j4t4
ENDPOINT = "https://openapi.tuyain.com"

# Project configuration
USERNAME = 'test1@gmail.com'  # email address or phone number
PASSWORD = 'Job894455'

SMS_TEMPLATE_ID = "SMS_2460072921"  # SMS template ID
EMAIl_TEMPLATE_ID = "MAIL_9584653696"  # Email template ID

# Initialization of tuya openapi
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.CUSTOM)
print(openapi.connect(USERNAME, PASSWORD))

# Get location of device through IP
location = openapi.get(f'/v1.0/iot-03/locations/ip?ip={IP}')
print(location)
location = location['result']
latitude, longitude = location['latitude'], location['longitude']

landmarks = wikipedia.geosearch(latitude, longitude, results=5)
landmarksListToStr = ' '.join(map(str, landmarks))

# get weather based on geo location
weather_url = f'/v2.0/iot-03/weather/current?lat={latitude}&lon={longitude}'
weather = openapi.get(weather_url)
print(weather)
condition = weather['result']['current_weather']['condition']
air_quality = weather['result']['air_quality']['aqi']
print(condition, air_quality)

params = {
    "landmarks": landmarksListToStr,
    "condition": condition,
    "aqi": air_quality
}

payload_json = json.dumps(params)
print(payload_json)

email_sent = openapi.post("/v1.0/iot-03/messages/mails/actions/push", dict({"to_address": "gadecito@ryteto.me",
                                                                            "template_id": "MAIL_9584653696",
                                                                            "template_param": payload_json,
                                                                            "reply_to_address": ""
                                                                            }))

sms_sent = openapi.post("/v1.0/iot-03/messages/sms/actions/push", dict({"country_code": "91",
                                                                        "phone": "748000000",
                                                                        "template_id": "SMS_2460072921",
                                                                        "template_param": payload_json,
                                                                        "sign_name": ""
                                                                        }))

print(email_sent)
print(sms_sent)

Requirements

Tex
wikipedia==1.4.0
tuya-iot-py-sdk==0.6.6
setuptools==58.5.3

Credits

Sumit Kumar

Sumit Kumar

32 projects • 94 followers
19 y/o. My daily routine involves dealing with electronics, code, distributed storage and cloud APIs.

Comments