# Send temperature data over WIFI ==Sebastian Mikkelsen Toth (sm223ps)== ## Overview and timeframe This turtorial will cover how to connect a temperature censor to the LoPy4 and send the temperature data to the plattform Pybytes over WIFI. Also, a LED connected to the LoPy4 will be turned on if the temperature exceeds 30^o^C. Given that you already are connected to pybytes, this project takes around 60 minutes to finish if you follow this turtorial. ## Objective I chose to do this project because it was easy and could be done without spending too much time. Still, it is a useful project, because now I can get the temperature from anywhere (with a WIFI-connection). Also, I can quickly see if the temperature is above 30^o^C. It is a good 'first project' as it it not too hard, and it lets you experience IOT without buying a lot of extra censors. Also, there is not many things that can stuggle or go wrong. Therefore this will hopefully be a smooth ride to learn before starting with harder projects. ## Material For this project, the material listed in the table below are needed. I bought the *[LoPy4 and censor bundle](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)* for 949 SEK and the [battery case with JST-connection](https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare-och-jst-kontakt/) for 29 SEK at *electrokit*. In total, this was 978 SEK. | Item | Picture | Description | |:------------------ | ------- | ----- | | Lopy4 |![](https://i.imgur.com/YTu8Zrl.jpg =80x)|Microcontroller| | Expansion board |![](https://i.imgur.com/f4RQ4Zl.jpg =80x)|Connects LoPy4 to computer and censors| | Battery case |![](https://i.imgur.com/Q2DaQvL.jpg =70x)|Gives power to the device when not connected to computer| | Temperature censor |![](https://i.imgur.com/nScUBWi.jpg =40x)|Gives an Voltage proportional to the temperature (measures temperature)| | Resistor (330kΩ) |![](https://i.imgur.com/6P2cNcO.jpg =60x)|To control currents (Ohm's law)| | Cables (at least 3)|![](https://i.imgur.com/jmlV6oU.jpg =70x)|Connects the LoPy4 with the censors| | Breadboard |![](https://i.imgur.com/GbtBXkr.jpg =150x)|For connecting everything to eachother| | USB-cable |![](https://i.imgur.com/C60AJ5c.jpg =70x)|Connects to computer for setup and programming| ## Computer setup #### Update firmware Close all programs that can interfere with the USB-port where you connect the Pycom device. Then follow the [Pycom's instuctions on how to update the firmwere](https://docs.pycom.io/updatefirmware/device/). #### Visual Studio Code I chose to work on the IDE called VScode. This is because I already got this IDE installed on my computer and I got experience with this IDE from before. Another IDE that can be used is Atom. VScode can be installed from [here](https://code.visualstudio.com/). #### Install Pymakr In VScode (follow in figure 1): 1. Click on 'extensions' 2. Search for 'Pymakr' 3. Install Pymakr ![](https://i.imgur.com/7gl4qG1.png) *Figure 1: installing Pymakr* #### Install Node.js Install Node.js from [here](https://nodejs.org/en/). ## Putting everything together The temperature censor is connected according to figure 2 below. This means that, when facing the flat side, the left pin will be connected to the 3V3 pin on the Pycom device. 3V3 gives the 3 V output from the device to the censor. The middle pin (the output voltage of the censor) will be connected to an input, in this case P16, that measures the voltage. And the right pin will be connected to ground (GND). ![](https://i.imgur.com/4iST2b4.png =110x) *Figure 2: Pin configuration* A 330 Ω resistor is needed between the LED and ground to restrict the current through the LED. The outher pin of the LED is connected to 3V3. Below is one schematic overwiev of how everything is connected (figure 3), and also a picture of everything (figure 4). ![](https://i.imgur.com/L1TKZIk.jpg) *Figure 3: Schematic overwiev of the circuit* ![](https://i.imgur.com/8hsBmbq.jpg) *Figure 4: Pictiure of the circuit* ## Plattform #### Pybytes Create an account and connect your pycom device to Pybytes according to [these instructions](https://docs.pycom.io/pybytes/gettingstarted/). On this plattform, the data sent can be viewed in tables or charts. In this cse, we present the data in a line chart. To see the data sent, follow the steps in figure 5: 1. Click on devices and add your device according to [these instructions](https://docs.pycom.io/pybytes/connect/). Then select the device. 2. Click on signals. 3. Click on the first signal. Here you can also add more signals, and the change the signal in the code. ![](https://i.imgur.com/edXrFmQ.png) *Figure 5: View data in Pybytes* A line chart can be added by clicking *create new display* in the top left corner in figure 6. ![](https://i.imgur.com/WdMDewA.png) *figure 6: table of measurements* ## The code Create two fils in a folder for the project, named boot.py and main.py. It will look like it does in figure 7 below. ![](https://i.imgur.com/PI4L8cj.png =200x) *Figure 7: files in the folder* The code for this project is simple, and there is no need for code in the *boot*-file. This code below is placed in the *main*-file. In the code, it is easy to change the sleeptime (i.e how freaquent the data will be measured and sent), and the temperature limit where the LED should light up. Before the infinite while-loop, assign the Output and input pins used a variable name. Then these will remain through all loops. *while True* will loop 'forever', so the code inside this loop will repeat until the device shuts off. As mentioned earlier, the temperature censor gives out a voltage proportional the the temperature change. In order to get a value in Celsius, we substract 500 from the voltage measurement (in millivoltage) and divide by 10. Celsius = $\frac{millivolts - 500}{10}$ The if-statements determines wheter the LED should light up or not. Then the data is sent to *signal 1*, and shold appear in Pybytes. The temperature is also printed the terminal. Then the device sleeps as many seconds as given, and the code in the loop repeats. ``` =Python import machine import time sleeptime = 900 tempwarning = 30 adc = machine.ADC() apin = adc.channel(pin='P16') led = machine.Pin('P12', machine.Pin.OUT, pull = machine.Pin.PULL_DOWN) while True: millivolts = apin.voltage() celsius = (millivolts - 500) / 10 if celsius > tempwarning: led.value(1) if celsius < tempwarning: led.value(0) pybytes.send_signal(3, celsius) print("sending: {}".format(celsius)) time.sleep(sleeptime) ``` #### Upload code to device With the Pycom device connected, click on 'upload' (see figure 8). Then the code will upload to the device, and the decive will start measuring temperature and send data. ![](https://i.imgur.com/FlVN6xG.png) *Figure 8: Upload code to device* ## Transmitting the data/connectivity Since the device is place in inside a house for now, the data is sent via WIFI to Pybytes. The protocol used to send the data is MQTT (Message queuing telemery transport), wich is using minimal network bandwidth. As the temperature does not varying very fast, it is enough to send the data every 30 or 15 minutes in this case. ## Presenting the data The data is presented in a line chart at Pybytes. Figure 9 below shows an example of how it looks like (here the temperature measurement was sent more frequently to show how it is presented, but for the application it's enough to send data every 30 minutes or so). The line chart makes it possible to see what the temperature has been recently as well as the current temperature. The data is stored in Pybytes for 30 days. ![](https://i.imgur.com/2datSyI.png) *Figure 9: Presentation of the data in Pybytes* The tabel can also be put on the dashboard, which is useful if you have more censors and want to see everything. The dashboard is shown below in figure 10. ![](https://i.imgur.com/2j4cuHr.png) *Figure 10: The dashboard* ## Finalizing the design Below is a figure of the final product (figure 11) and how thedata is visualized (figure 12). ![](https://i.imgur.com/8hsBmbq.jpg) *Figure 11: Pictiure of the circuit* ![](https://i.imgur.com/2datSyI.png) *Figure 12: Presentation of the data in Pybytes* For me, this project went smoothly and no problems occured. It is relatively easy and is a good beginning for learning about IOT.