brendanvanbreda
Published © MIT

Python Weather Station on CW02(ESP32) and Ubitdots

Quick build of a weather station that uses CW02(ESP32) programmed in Python with Zerynth and Ubidots IoT platform.

IntermediateFull instructions provided1 hour1,679
Python Weather Station on CW02(ESP32) and Ubitdots

Things used in this project

Hardware components

SW01
XinaBox SW01
×1
IP01
XinaBox IP01
×1
CW02
XinaBox CW02
×1
XC10
XinaBox XC10
×1

Software apps and online services

Zerynth Studio
Zerynth Studio
Ubidots
Ubidots

Story

Read more

Schematics

XinaBox CW02 pin mapping

Code

main.py

Python
Replace the code in main.py
###########################################################
# Weather Station with Ubitdots
# Created December 2018
# Author - Brendan van Breda (vanbredabrendan@gmail.com)
###########################################################

import streams
import json
from wireless import wifi

# import SW01 sensor library
from xinabox.sw01 import sw01

# SW01 instance
SW01 = sw01.SW01(I2C0)

# choose a wifi chip supporting secure sockets
from espressif.esp32net import esp32wifi as wifi_driver

# import ubidots iot module
from ubidots.iot import iot

# import helpers functions to easily device configuration
import helpers

# SET DEVICE CONFIGURATION INSIDE THE FOLLOWING JSON FILE
new_resource('device.conf.json')

# define a callback for period updates
def period_callback(value):
    global publish_period
    print('requested publish period:', int(value))
    publish_period = int(value)

streams.serial()
wifi_driver.auto_init()

sleep(1000)

print('connecting to wifi...')
try:
    # FOR THIS EXAMPLE TO WORK, "Network-Name" AND "Wifi-Password" MUST BE SET
    # TO MATCH YOUR ACTUAL NETWORK CONFIGURATION
    wifi.link("Network-Name",wifi.WIFI_WPA2,"Wifi-Password")
    print("Connected to Wi-Fi")
except Exception as e:
    print("ooops, something wrong while connecting :(", e)
    while True:
        sleep(1000)

device_conf = helpers.load_device_conf()
publish_period = 10000   # post data to Ubitdots every 10 seconds

print('connecting to mqtt broker...')
try:
    # create ubidots iot device instance, connect to mqtt broker, set variable update callback and start mqtt reception loop
    device = iot.Device(device_conf['device_label'], device_conf['user_type'], device_conf['api_token'])
    device.mqtt.connect()
    print("Connected to mqtt broker")
except Exception as e:
   print("ooops, something went wrong :(", e)
   while True:
       sleep(1000)
       
device.on_variable_update(device_conf['device_label'], 'publish_period', period_callback, json=False)
device.mqtt.loop()

while True:
    print("Publish SW01")
    tempC = SW01.getTempC()		    # return temp in degree celcius
    humid = SW01.getHumidity()	    # return humidity in percentage
    pres = SW01.getPressure()/10	# return pressure in kPa
    device.publish({ 'value': tempC }, variable='temperature')
    print("Temperature: ",tempC," C")
    device.publish({ 'value': humid }, variable='humidity')
    print("Humidity: ",humid," %")
    device.publish({ 'value': pres }, variable='pressure')
    print("Pressure: ",pres," kPa")
    sleep(publish_period)   # sleep for 10 seconds
    

Credits

brendanvanbreda

brendanvanbreda

1 project • 7 followers
Keyboard Warrior and Electrical Engineer by Night.
Thanks to Karimhamdy1.

Comments