By: Jimmy H (jh225iw) Estimated time: 5 hours ## Project overview While there are tech for connection IoT devices and sending data, they might me limited by geographical location or simply doesn´t exists. This project uses "stuff" that most have. A powerbank and regular mobile phone for sending weather data like humidity and temperature. ## Objective The objective is to familiarize the reader (and author :smiley:) of this project with the IoT enviroment with a project that is managable for a beginner. This project hopefully gives the insight of both build the hardware, connecting it to internet and using a cloud solution for storing/displaying data in a friendly UI. ## Material | Component | Description | Price (EUR) | | -------- | -------- | -------| | [Raspberry Pie Pico WH](https://www.electrokit.com/produkt/raspberry-pi-pico-wh/) | Microcontoller used in this project. | 10 [DTH11](https://www.m.nu/sensorer-matinstrument/dht11-basic-temperature-humidity-sensor-extras-1?gclid=CjwKCAjwzJmlBhBBEiwAEJyLu34clUEWT2mnNj6OJg3_W338yE5_VXkur3q7tWCAK_uRZxttCd35fxoCNZUQAvD_BwE) | Temperature and humidity sensor | 4.6 [Breadboard](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) | Board for connecting sensor and microcontroller without soldering.|4.5 |[*LCD-1602 display](https://www.amazon.se/-/en/Freenove-Display-Compatible-Arduino-Raspberry/dp/B0B76Z83Y4/ref=sr_1_9?keywords=lcd%2B1602&qid=1688664961&sprefix=lcd%2B1602%2Caps%2C120&sr=8-9&th=1) | Display for showing temp and humidity | 11.5 |[Powerbank/Battery](https://www.electrokit.com/produkt/powerbank-2500mah-usb/) | For powering up Pico (remotly) | 15.4 |Mobile Phone| Any phone that can create a wifi hotspot. | - |[M2M wires](https://www.electrokit.com/produkt/labbsladd-40-pin-30cm-hane-hane/)| For connecting DHT11 to the breadboard | 4.5 |[M2F wires](https://www.electrokit.com/produkt/labbsladd-40-pin-30cm-hona-hane/)| For connecting LCD display to breadboard | 4.5 [USB cabel](https://www.electrokit.com/produkt/usb-kabel-a-hane-mini-b-hane-5p-1-8m/) | For developing and power to Pico.|3.5 :::info *The LCD-1602 in this example is with integrated I2C interface. ::: ## Computer setup At this point there are two things that must be done. - [ ] Setup the Pico with the latest firmware - [ ] Setup a local development eviroment so you can send data to the Pico. If you comfratble with **VSCode** you can go with "**Pymakr**" extension. However, i would highly recommend that you don´t use this extension. It has a very low rating. And there are good reasons for it. I would recommend "**Thonny**". To install "**Thonny**" follow these excellent stp by step instructions from "Raspberry pi org": [Thonny install instructions](https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/2) Now it´s time to flash your Pico with the latest firmware (MicroPython): [MicroPython firmware to Pico](https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/3) ## Putting everything together The below picture is an image created by "**fritzing**". You can find the software [here](https://fritzing.org/) ### DTH11 (from left) pin 32 GP27 (signal). pin 36 (power 3v3). ground to breadboard ground rail. ### LCD 1602 (from top) ground to breadbaord ground rail. pin 40 (power 5v). SDA to pin 9 (I2C1 SDA) SCL to pin 10 (I2C1 SCL) ### Pico pin 38 to ground rail. ![](https://hackmd.io/_uploads/SkFepo-tn.png) ## Platform The platform for this project is [ubidot](https://ubidots.com/). It has a very mature platform for recieving, storing and displaying data from IoT devices. For transmission of the data i choose HTTP in a RESTful way. It suits this project well because at this point all Pico is doing is collecting data and posting it to ubidots. Unfortunally there is no "free" account. But there is a "try" account. ## The code The code is separated in 2 main files. The config.py for values that you might want to edit. And the main.py that runs the application. main.py also uses [lcd1602 library](https://toptechboy.com/tag/library/) and a [dht library](https://github.com/iot-lnu/applied-iot/blob/master/1DV027/DHT11-pico-w/dht.py). config.py ```python= config = { 'ssid' : 'PicoMobile', 'password' : 'PASSWORD', 'ubidots_token' : 'token', 'ubidots_sleep' : 5, 'ubidots_device' : 'pico' } ``` main.py Imports ```python= from lcd1602 import LCD import utime as time import dht import machine import network from config import config from time import sleep import urequests as requests ``` sending data to ubidot using JSON (HTTP POST) ```python=+ def sendData(device, temp, hum): try: url = "https://industrial.api.ubidots.com/" url = url + "api/v1.6/devices/" + device headers = {"X-Auth-Token": config["ubidots_token"], "Content-Type": "application/json"} #Labels for "dots" data = {"temperature": temp, "humidity" : hum} if data is not None: req = requests.post(url=url, headers=headers, json=data) return req.json() else: print("error") pass except: pass ``` Try to connect phone wifi ```python=+ def tryToConnect(): tries = 0 print('connecting to network...') #wlan might be active wlan.active(False) wlan.active(True) # set power mode to get WiFi power-saving off (if needed) wlan.config(pm = 0xa11140) lcd.write(0, 0, "Connecting to") lcd.write(0, 1, "SSID: " + config["ssid"]) wlan.connect(config["ssid"], config["password"]) print('Waiting for connection on ' + config["ssid"], end='') # Check if it is connected otherwise wait while not wlan.isconnected(): print('.', end='') tries += 1 if tries == 10: break sleep(1) print('moving on') lcd.clear() #Double check connection if wlan.isconnected(): lcd.write(0, 0, "Connected to") lcd.write(0, 1, "SSID: " + config["ssid"]) else: lcd.write(0, 0, "Connection fail") lcd.write(0, 1, "SSID: " + config["ssid"]) #Sleep in order for the text to show up on LCD sleep(3) lcd.clear() # Print the IP assigned by router ip = wlan.ifconfig()[0] print('\nConnected on {}'.format(ip)) return ip ``` Always running mainloop ```python=+ def mainloop(): print('mainloop') temperature = "0" humidity = "0" while True: try: tempSensor.measure() temperature = tempSensor.temperature humidity = tempSensor.humidity tempText = 'Temp: ' + str(temperature) + ' C' humText = 'Humi: ' + str(humidity) + ' %' lcd.write(0, 0, tempText) lcd.write(0,1, humText) sendData(config["ubidots_device"], temperature, humidity) sleep(config["ubidots_sleep"]) except: sleep(1) pass ``` Code that runs at start (this code should be the last rows in main.py) ```python=+ lcd=LCD() tempSensor = dht.DHT11(machine.Pin(27)) wlan = network.WLAN(network.STA_IF) if not wlan.isconnected(): print('not connected') tryToConnect() mainloop() ``` ## Transmitting the data / connectivity Before we can send data. We need to set up a wifi hotspot with the phone. This is usually done in "settings/network and internet/Hotspot and tethering/Portable hotspot" (Android). Make sure **Hotspot name** is the same name as in config.py (**ssid**) and that **Hotspot password** is the same in config.py (**password**) Then activate the hotspot. :::warning Only use WPA2 security and make sure to use 2.4 GHz. ::: ![](https://hackmd.io/_uploads/rJIC7wVY3.png) When hotspot is enabled you can connect the powerbank to the Pico. The display will inform you if Pico was enable to connect to the phones hotspot. If it fails. Take out the battery and plug it in again. In this case we try to send data every 5 seconds. That can be changed in the config.py file ('ubidots_sleep' : 5). The below diagram shows connection flow. ![](https://hackmd.io/_uploads/HkRHFpWK2.png) ## Presenting the data For visualisation and data storage [ubidots](https://www.ubidots.com) is used. [Ubidots basics](https://help.ubidots.com/en/articles/854333-ubidots-basics-devices-variables-dashboards-and-alerts) In this case we create a device called "pw" and that device will have 2 variabels **humidity** and **temperature** ![](https://hackmd.io/_uploads/SJDsKDEKn.png) Then in the actual dashbboard we connect widgets with those two variabels. ![](https://hackmd.io/_uploads/H1EuvC-tn.png) ## Finalizing the design This project and "tutorial" was done as a school project for Kalmar University (IoT) in the summer of 2023. I hade no previous experience with sensors or the Pico or MicroPython. Everything went pretty ok. But as previously stated earlier i had a lot of problems with the "Pymakr" extension for VSCode. Unfortunally it ate up alot of time. But it was a really fun beginner project. Hopefully i can keep build up on the IoT knowlegde obtained from Kalmar university. Below is a video showcasing the project in the woods. [![](https://hackmd.io/_uploads/H1ky_qGF3.png) ](https://photos.app.goo.gl/jwBJBTFMkYu6XWkk8) [Video demostration](https://photos.app.goo.gl/jwBJBTFMkYu6XWkk8) Final design picture ![](https://hackmd.io/_uploads/r1qMB6bFn.jpg)