Ever tried adding country, city and country code lists into your project? Some find it too difficult but let us make it more simpler with a single API call for all Country, Country Symbol and Codes, City and other relevant details. I am sure after this tutorial you will never have to hunt for other APIs for Country, city info tasks for future projects.
What is Tuya? Why do I need it?
Tuya is a leading global IoT Cloud Platform that connects the intelligent needs of brands, OEMs, developers, and retail chains. The platform provides developers with a one-stop IoT PaaS-level solution that contains hardware development tools, global cloud services, and smart business platform development, Tuyaoffering comprehensive ecosystem empowerment from technology to marketing channels to build the world’s leading IoT Cloud Platform. It's Cloud APIs makes it powerful enough to connect clients all over the globe.
Let's begin.
Prerequisites
Tuya Developer Account. Get it here -
https://developer.tuya.com/en/TuyaOpenAPI. To install,
pip install tuya-iot-py-sdk
Setting up Tuya
Add a cloud project and select your respective zone based data centre.
After, the project is created - Go to Service API tab and authorize Country and City Info API
Then go to Users tab and add your email and password details that you will use to authorise with python APIs.
Check the Overview tab for Access_ID and Access_Keys to be used with Python APIs.
Once these steps are complete you can move on to code part.
Tuya API Explorer Environment
Tuya also provide API explore where you can try out the API requests before moving on to code,
It's easy to test and try out APIs without any setups and installations,
Python SDK for Tuya Cloud API requests
First, we have to import TuyaOpenAPI from Tuya Connecter that we installed before. We are also using streamlit for UI layouts.
from tuya_connector import (
TuyaOpenAPI
)
import streamlit as st
Next comes authorizing details (below details are available on your Overview Tab of Tuya Cloud Project
ACCESS_ID = "acbe*************123"
ACCESS_KEY = "abcderf*************"
API_ENDPOINT = "https://openapi.tuyain.com"
USERNAME = 'test@gmail.com' # email address or phone number
PASSWORD = 'Job123'
Check your endpoint is correct according to your region,
Once those details are ready, it's time to connect TuyaOpenAPI
# Initialization of tuya openapi
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.CUSTOM)
# on printing you would get response and uid for other usecases
print(openapi.connect(USERNAME, PASSWORD))
Understanding the Country and City Request API, first let us try to get country code list through the API. The only parameter we need is "lang": "en"
country_codes = openapi.get("/v3.0/iot-03/all-countries", dict({"lang": "en"}))
st.json(country_codes)
Let's also try some other API, which would reveal some more details of the country and city based upon location sensor data (latitude and longitude). The below API GET request, the required parameters are "lat" and "lon"(I used some random data)
city_info = openapi.get("/v1.0/iot-03/cities/positions", dict({"lat": "27.1751", "lon": "78.0421",
}))
st.json(city_info)
Using the above API you get the following details:
city_name
province_name
parent_city_name
country_name
What next?
Basically there are lots of fun stuff you can do with Tuya IoT and Cloud Platform. Need inspiration, check out my previous projects with Tuya Platforms:
- https://steptostem.com/4809/tuya-link-sdk-iot-smart-environment-sensing-and-humidifier/
- https://steptostem.com/3553/aquamon-aquaponics-and-fish-tank-monitoring-with-tuya-iot/
- https://steptostem.com/3360/getting-started-with-arduino-iot-control-with-tuya-iot-platform/
- https://steptostem.com/3688/hack-your-own-smart-gadgets-using-tuya-cloud-platform/
Top comments (0)