# Tutorial on how to build a Smart Weather Reporter ### Smart Weather Reporter Anis Bourbia (ab225vg) Approximate time - 9 hours This project tutorial walks you through how to build a smart weather reporter. The application of the device can vary depending on the need. It could for example be used to measure outside or inside temperature. For my use case, I decided to use it in a greenhouse to get continuous insight into temperature data. ## Objective The objective of the project was to measure, store and visualize temperature data from a greenhouse. This would make it possible to monitor temperature from anywhere and make sure the temperature is optimal. The project can in the future be extended to measure air composition, humidity, and other data which would give additional insight into the climate inside of the greenhouse. ## Material | Name | Type | Link | Price | | -------- | -------- | -------- | -------- | | LoPy4 | Microcontroller | [Store link](https://pycom.io/product/lopy4/) | 38.45€ | | Expansion Board 3.0 | Expansionboard | [Store link](https://pycom.io/product/expansion-board-3-0/) | 17.60€ | | MCP9700 | Tempreturesensor | [Store link](https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/) | 8 SEK | | Breadboard | ------------------ | [Store link](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) | 59 SEK | | Jumperwire | ------------------ | [Store link](https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/) | 39 SEK | | USB - USB-C | ------------------ | [Store link](https://www.amazon.se/Premiumcord-Kabel-3-1-Kontakt-Kontakt-Till/dp/B07NSQ56DZ/ref=asc_df_B07NSQ56DZ/) | 24.46 SEK | | Batteryholder | ------------------ | [Store link](https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare-och-jst-kontakt/) | 29 SEK | The table above lists all material needed for this project. Note that everything needed is included in the course bundle found [here!](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle) ![](https://i.imgur.com/fUyvKBl.png =300x) ![](https://i.imgur.com/daEANyu.jpg =300x) ![](https://i.imgur.com/rNkUuB2.png =300x) ![](https://i.imgur.com/B6haT8j.jpg =300x) ![](https://i.imgur.com/jEUDNTA.jpg =300x) ![](https://i.imgur.com/CVN3bQE.png =300x) ![](https://i.imgur.com/XWMxMCU.png =300x) ## Computer setup (Approximation 2 hours) For this project, either Visual Studio Code or Atom does work to upload and test code. The project structure should follow the structure shown in the image below. ![](https://i.imgur.com/b1AD8Ch.png) ### If your preferred IDE is **Atom** follow these steps: * Navigate to Preferences/Settings * Click on install in the left side menu * Search for Pymakr in the search bar * Click install * Once installed a terminal with information should appear * On the left side of the terminal there are buttons to run, upload and download your code. ### If your preferred **Visual Studio Code** follow these steps: * Navigate to Extensions in the left side menu * Search for Pymakr in the search bar * Click install * Once installed buttons to run, upload, and download your code should appear on the bottom of the page. The options are also visible when right-clicking the file. ## Putting everything together (Approximation 4 hours) The cabling for this project is very simple. We connect power and ground from the Pycom device to the breadboard. We do also connect a cable(Green as shown on the image) that is responsible to retrieve the sensor data. We do not need to use any resistors while using this sensor. ![](https://i.imgur.com/kmsvCaW.png) ## Platform (Approximation 1 hours) This project uses the Adafruit.com platform to store data. Adafruit is an IoT cloud platform that has tools and support for protocols such as MQTT and HTTP to send and store data. The platform is free it helps with scalability. ## The code (Approximation 2 hours) Note that the MQTT library is required for this project. Create a file in your lib folder and name it "mqtt.py". Copy file content from this [link](https://github.com/pycom/pycom-libraries/blob/master/lib/mqtt/mqtt.py) and paste the contents in your local mqtt.py file. Create a “main.py” file in your project folder and copy-paste the code from below. Make sure to go through the comments and change credentials where the comments tell you to. The code below connects to WiFi and the Adafruit API to send data. When the device is connected it runs a while loop every 10 minutes that get temperature data from the sensor and sends it via MQTT. ``` import machine import time import network from mqtt import MQTTClient def sub_cb(topic, msg): print(msg) adc = machine.ADC() apin = adc.channel(pin='P16') wlan = WLAN(mode=WLAN.STA) #Replace "Wifi_NAME" and "Wifi_PASSWORD" with actual wifi credentials. wlan.connect('WIFI_NAME', auth=(network.WLAN.WPA2, 'WIFI_PASSWORD')) while not wlan.isconnected(): machine.idle() print("Connected to WiFi\n") #Add Adafruit credentials below. NOTE that password should be the API-Key and #not the actual user password. You can find your API-Key under account settings. client = MQTTClient("", "io.adafruit.com",user="", password="", port=1883) client.set_callback(sub_cb) client.connect() #Replace <Name> with your first name used to register to Adafruit. client.subscribe(topic="<Name>/feeds/temps") while True: millivolts = apin.voltage() celsius = (millivolts - 500.0) / 10.0 #Once again replace <Name> with registered first name. client.publish(topic="<user>/feeds/temps", msg="ON") print(celsius) time.sleep(600) ``` ## Transmitting the data/connectivity For this project, I decided that WiFi made the most sense when choosing wireless protocol. The device will in my use case always have access to a strong WiFi connection which made it easy to pick WiFi as the protocol. Data is sent every 10 minutes. For data transport protocol I felt that MQTT would make it easier to transition into collecting more data points which I intend on doing later on. MQTT is also very easy to use with Adafruit. ## Presenting the data All data that the Pycom device sends through the MQTT protocol gets saved to Adafruit. Adafruit visualizes the data with a graph and also gives the option to download and export all your data. ![](https://i.imgur.com/DBz9HvX.png) ## Finalizing the design ![](https://i.imgur.com/emiSFw1.jpg =600x) The project is very simple with only one type of data collected. In hindsight, I could have added an additional sensor to make the project more interesting. I could have also done some sort of data processing that would also give the project additional depth instead of only reporting and storing temperature data.