<\/span><\/h2>\r\n\r\n\r\n\r\nWell, we have some simplified versions of scripts that query the GPS module to obtain coordinates. Additionally, we have a separate script for fetching weather information based on coordinates from the weather service API, and we also have a chatbot. Now it’s time to expand the functionality of these scripts and integrate everything into a cohesive whole.<\/span><\/p>\r\n\r\n\r\n<\/span><\/p>\r\nThe workflow algorithm will be as follows:<\/span><\/p>\r\n\r\n\r\n<\/span><\/p>\r\n\r\nRun the main script: <\/span>1.1. Obtain coordinates from the GPS module. <\/span>1.2. Launch the next script and pass the coordinates to it.<\/span><\/li>\r\n\r\n\r\n\r\nThe next script receives coordinates from the previous one and: <\/span>2.1. Makes an API request to the weather service with the coordinates. <\/span>2.2. Parses the received response. <\/span>2.3. Sends the parsed response to Telegram.<\/span><\/li>\r\n<\/ol>\r\n\r\n\r\n<\/span><\/p>\r\nTaking into account the described algorithm above, the main script might look like this:<\/span><\/p>\r\n\r\n\r\n\r\nfrom gps import *\r\nimport time\r\nimport sys\r\n\r\ngpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE)\r\n\r\nwhile True:\r\n report = gpsd.next()\r\n if report['class'] == 'TPV':\r\n latitude = str(getattr(report,'lat',0.0))\r\n longitude = str(getattr(report,'lon',0.0))\r\n os.system(\"python api_forecast.py \" + latitude + \" \" + longitude)\r\n sys.exit()\r\n time.sleep(3)<\/code><\/pre>\r\n\r\n\r\n\r\nAs evident from the code, the script stores coordinates in variables and passes them as arguments when launching the next script. The sys<\/strong><\/code> module is used to execute the next script. After the successful execution of the subsequent script, the main script terminates its operation using the sys.exit()<\/strong><\/code> method.<\/span><\/p>\r\n\r\n\r\n<\/span><\/p>\r\nNow let’s consider the next script:<\/span><\/p>\r\n\r\n\r\n\r\n\r\n\r\nimport requests\r\nimport datetime\r\nimport sys\r\n\r\nTOKEN = \"TELEGRAM_BOT_TOKEN\"\r\nchat_id = \"CHAT_ID<\/strong>\"\r\napi_url = 'https:\/\/api.openweathermap.org\/data\/2.5\/onecall?'\r\n\r\nparams = dict(\r\n lat=sys.argv[1],\r\n lon=sys.argv[2],\r\n appid='YOUR_API_KEY<\/strong>',\r\n units='metric',\r\n exclude='minutely,daily',\r\n lang='uk'\r\n)\r\n\r\nresp = requests.get(url=api_url, params=params)\r\ndata = resp.json()\r\nforecast = ''\r\nlimit = 12\r\nindex = 0\r\nfor dt in data[\"hourly\"]:\r\n forecast += \"\u0427\u0430\u0441: \" + datetime.datetime.fromtimestamp(dt[\"dt\"]).strftime('%d-%m %H:%M') + \"\\nTemperature: \" + str(dt[\"temp\"]) + \"\\N{DEGREE SIGN}C.\" + \" Description: \" + dt[\"weather\"][0][\"description\"] + \"\\n\\n\"\r\n if index == limit:\r\n break\r\n index+=1\r\nmessage = \"Current weather:\\n\" + \"Time: \" + datetime.datetime.fromtimestamp(data[\"current\"][\"dt\"]).strftime('%d-%m %H:%M') + \"\\nTemperature: \" + str(data[\"current\"][\"temp\"]) + \"\\N{DEGREE SIGN}C.\" + \" Description: \" + data[\"current\"][\"weather\"][0][\"description\"] + \"\\n\\n\" + \"Today's wheather forcast:\\n\" + forecast\r\nurl = f\"https:\/\/api.telegram.org\/bot{TOKEN}\/sendMessage?chat_id={chat_id}&text={message}\"\r\nrequests.get(url).json()<\/code><\/pre>\r\n\r\n\r\n\r\nThe described script fetches the current weather from the weather service and provides an hourly forecast for the next 12 hours. It compiles this information into a message and sends it to Telegram. To read the arguments from the main script, the sys.argv<\/strong><\/code> method is utilized. The Telegram message looks like this:<\/span><\/p>\r\n\r\n\r\n\r\n
<\/figure><\/div>\r\n\r\n\r\nYou can further enhance this script to run regularly, for example, every day at 9 AM. This way, you’ll receive the current weather and the day’s forecast directly to your Telegram regularly \ud83d\ude42<\/span><\/p>\r\n <\/p>\r\n\r\n\r\n\r\n
I found the folloving materials very helpful:<\/strong> <\/span>1. https:\/\/github.com\/FranzTscharf\/Python-NEO-6M-GPS-Raspberry-Pi <\/span>2. https:\/\/openweathermap.org\/api\/one-call-api <\/span>3. https:\/\/ozzmaker.com\/using-python-with-a-gps-receiver-on-a-raspberry-pi\/<\/span><\/p>\r\n","protected":false},"excerpt":{"rendered":"At first, I simply wanted to connect the NEO-6M GPS module to my Raspberry Pi 4, but obtaining coordinates without…<\/p>\n","protected":false},"author":2,"featured_media":252,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cybocfi_hide_featured_image":"yes","footnotes":""},"categories":[879,811],"tags":[],"class_list":["post-636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-en","category-inernet-of-things"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/posts\/636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/comments?post=636"}],"version-history":[{"count":0,"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/posts\/636\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/media\/252"}],"wp:attachment":[{"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/media?parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/categories?post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmakerclub.com\/wp-json\/wp\/v2\/tags?post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}