This blog is a record of what I do, what I think and what I read. If you are interested to know about my professional experience, you can refer to the other blog of mine. http://admi2camac.blogspot.com/
Wednesday, 26 April 2023
CoreElectronics 1: Connecting to the Internet
# A simple example that:
# - Connects to a WiFi Network defined by "ssid" and "password"
# - Performs a GET request (loads a webpage)
# - Queries the current time from a server
import network # handles connecting to WiFi
import urequests # handles making and servicing network requests
import utime
# Connect to network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# Fill in your network name (ssid) and password here:
ssid = 'xxxxx'
password = 'xxxxx'
wlan.connect(ssid, password)
# Example 1. Make a GET request for google.com and print HTML
# Print the html content from google.com
print("1. Querying google.com:")
r = urequests.get("http://www.google.com")
print(r.content)
r.close()
# Example 2. urequests can also handle basic json support! Let's get the current time from a server
print("\n\n2. Querying the current GMT+0 time:")
r = urequests.get("http://date.jsontest.com") # Server that returns the current GMT+0 time.
#print the tuple
print(r.json())
# Get the 'date and time' value from the JSON response
date_str = r.json()['date']
time_str = r.json()['time'][:-3] # Remove the last 3 characters (' PM' or ' AM')
# Print only the time part
print("Today's date GMT+0 is ", date_str.split(' '))
print("Current time GMT+0 is ",time_str.split(' ')) #Adding [0] will remove the [AM/PM] string
#https://core-electronics.com.au/guides/raspberry-pi-pico-w-connect-to-the-internet/
#https://www.youtube.com/watch?v=GiT3MzRzG48&t=306s
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment