Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
TOI - Things on Internet
Published © GPL3+

Python On ESP32: WiFi Test

Using Python to connect an industrial ESP32-based board to an HTTP Request & Response Service to get the current UTC time.

BeginnerProtip1 hour2,535
Python On ESP32: WiFi Test

Things used in this project

Hardware components

ESP32S
Espressif ESP32S
×1
4zerobox
×1

Software apps and online services

Zerynth Studio
Zerynth Studio

Story

Read more

Schematics

board-press-release_HkhljSvrY1.png

Code

Python on ESP32 using Zerynth - Get Time

Python
# 4zerobox - DEMO Get Time

import streams
import json
# import the wifi interface
from wireless import wifi
import requests

from espressif.esp32net import esp32wifi as wifi_driver
wifi_driver.auto_init()
streams.serial()

print("Establishing Link...")
try:
    wifi.link("SSID",wifi.WIFI_WPA2,"password")
except Exception as e:
    print("ooops, something wrong while linking :(", e)
    while True:
        sleep(1000)

# let's try to connect to http://now.httpbin.org/ to get the current UTC time
for i in range(3):
    try:
        print("Trying to connect...")
        # we need to impersonate a web browser: as easy as setting the http user-agent header
        user_agent = {"user-agent":"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"}
        # go get that time!
        # url resolution and http protocol handling are hidden inside the requests module
        response = requests.get("http://now.httpbin.org/")
        # let's check the http response status: if different than 200, something went wrong
        print("Http Status:",response.status)
        # if we get here, there has been no exception, exit the loop
        break
    except Exception as e:
        print(e)

try:
    # check status and print the result
    print("Success!!")
    print("-------------")
    print("And the result is:",response.content)
    print("-------------")
    js = json.loads(response.content)
    print("Date:",js["now"]["rfc2822"][:16])
    print("Time:",js["now"]["rfc2822"][17:])
except Exception as e:
    print("ooops, something very wrong! :(",e)

Credits

TOI - Things on Internet
4 projects • 20 followers
TOI is the creator of 4zerobox: the Industrial Toolkit for IoT Solution Providers. Live now on Kickstarter: https://bit.ly/get-4zerobox
Contact

Comments

Please log in or sign up to comment.