|
| 1 | +--- |
| 2 | +title: "MKR FOX 1200 Weather Monitor" |
| 3 | +description: "Turn your MKR FOX 1200 into a battery-powered weather monitor that you can deploy EVERYWHERE. " |
| 4 | +coverImage: "assets/socials_mkrfox1200_bVlDZFCxco.jpg" |
| 5 | +tags: [radio, weather] |
| 6 | +author: "Arduino_Genuino" |
| 7 | +difficulty: advanced |
| 8 | +source: "https://create.arduino.cc/projecthub/Arduino_Genuino/mkr-fox-1200-weather-monitor-6a94e2" |
| 9 | +--- |
| 10 | + |
| 11 | +## Components and Supplies |
| 12 | + |
| 13 | +- [Arduino MKR Fox 1200](https://store.arduino.cc/arduino-mkrfox1200) |
| 14 | +- [SparkFun Atmospheric Sensor Breakout - BME280](https://www.sparkfun.com/products/13676) |
| 15 | +- [TSL2561 digital luminosity light sensor](https://store.arduino.cc/tsl2561-digital-luminosity-light-sensor) |
| 16 | +- [HTU 1D](https://www.sparkfun.com/products/retired/12064) |
| 17 | + |
| 18 | +## Apps and Online Services |
| 19 | + |
| 20 | +- [Arduino Web Editor](https://create.arduino.cc/editor) |
| 21 | +- [ThingSpeak API](https://github.com/iobridge/ThingSpeak) |
| 22 | +- [Sigfox](http://makers.sigfox.com/) |
| 23 | + |
| 24 | +## About This Project |
| 25 | + |
| 26 | +This project shows how to turn a **MKR FOX 1200** and a bunch of I2C sensors into a simple, battery-powered weather station. |
| 27 | + |
| 28 | +We will use: |
| 29 | + |
| 30 | +* the onboard temperature sensor (low precision, ± 1 °C) |
| 31 | +* HTU21D I2C sensor to get humidity level |
| 32 | +* Bosch BMP280 to get the barometric pressure and precise temperature |
| 33 | +* TSL2561 light sensor to get luminosity |
| 34 | + |
| 35 | +### Hardware Setup |
| 36 | + |
| 37 | +The hardware connections for this project are quite simple: all the sensors use I2C interfaces and run at 3.3V. |
| 38 | + |
| 39 | +They need four wires from the MKR FOX 1200: GND, 3.3V, SDA on Pin 11 and SCL on Pin 12. The same four connections can be shared in parallel with the three sensors because each of them has a specific I2C address and therefore the relevant libraries and functions will take care of discriminating each of them. |
| 40 | + |
| 41 | +The whole project can be powered by two AA or AAA alkaline batteries connected in series to generate 3V and wired to the screw connection terminal block on the board. |
| 42 | + |
| 43 | +### Sigfox Messages |
| 44 | + |
| 45 | +Since the Sigfox network can send a maximum of 140 messages per day, we'll optimize the readings and send data in compact binary format. |
| 46 | + |
| 47 | +Data packing is important because the maximum size of a message you can send is 12 bytes. A float occupies 4 bytes, so we'll need to pack our readings into some smaller representation. |
| 48 | + |
| 49 | +The float format is very wide (you can represent numbers as big as 3.402823e+38 (!) ) and we don't need the whole data space; we can then decide safe limits (based on where we are going to deploy the station) and restore the original representation in the backend. |
| 50 | + |
| 51 | +### Configuration of the Cloud Services |
| 52 | + |
| 53 | +We'll use two Cloud services: |
| 54 | + |
| 55 | +* [Sigfox backend](http://backend.sigfox.com/) |
| 56 | +* [Thingspeak](https://thingspeak.com/) |
| 57 | + |
| 58 | +Register your board on Sigfox backend using PAC and ID provided by [FirstConfiguration example](https://www.arduino.cc/en/Tutorial/SigFoxFirstConfiguration) (can be skipped if you already did that for a previous project). |
| 59 | + |
| 60 | +Both need to be configured to talk each other and interpret the data in the right format. Register to both and create a couple of *channels* on Thingspeak. The configuration is reported in the image below. |
| 61 | + |
| 62 | +The first channel we configure will be the one which receives the raw data (`readChannelID`), while the second one will collect the re-scaled data (`writeChannelID`). |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +It's about time to upload the sketch using Arduino Create Editor. The default sketch lets you test extensively all the infrastructure before deploying on the field. |
| 67 | + |
| 68 | +Remember to declare: |
| 69 | + |
| 70 | +```arduino |
| 71 | +oneshot = false |
| 72 | +``` |
| 73 | + |
| 74 | +when you are confident that all the backend infrastructure setup is correct. The sketch waits for the serial port, so we won't launch it now. Instead, open the [SigFox backend](http://backend.sigfox.com/) and configure a callback: |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +You will see a windows like this: |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +The relevant configurations are reported below. Make sure the callback is setup as: |
| 92 | + |
| 93 | +* TYPE: `DATA/UPLINK` |
| 94 | +* Channel: `URL` |
| 95 | +* Http method: `GET` |
| 96 | + |
| 97 | +As Custom Payload, set: |
| 98 | + |
| 99 | +```arduino |
| 100 | +status::uint:8 temp1::int:16:little-endian temp2::int:16:little-endian press::uint:16:little-endian hum::uint:16:little-endian light::uint:16:little-endian lastMsg::uint:8 |
| 101 | +``` |
| 102 | + |
| 103 | +This represents the structure we packed in the sketch. We are assigning a variable to each field, so we can reference them in the HTTP callback. |
| 104 | + |
| 105 | +As URL Pattern, set: |
| 106 | + |
| 107 | +```arduino |
| 108 | +https://api.thingspeak.com/update?api_key=XXXXXXXXXXXX&field1={customData#temp1}&field2={customData#press}&field3={customData#temp2}&field4={customData#hum}&field5={customData#light}&field6={customData#status}&field7={customData#lastMsg} |
| 109 | +``` |
| 110 | + |
| 111 | +Once configured, your windows should be like this: |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +Remember to change the API key `api_key`with the one provided by Thingspeak as `Write API Key`for `Channel1`. |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +Save and exit. We can now open the serial port and observe the first message being sent. Hooray! |
| 122 | + |
| 123 | +### From Raw to Graphics |
| 124 | + |
| 125 | +The setup we configured will route our data to the Thingspeak backend, but they are still in raw format; to reconvert them, let's use [Thingspeak analysis tool ](https://thingspeak.com/apps/matlab_analyses)with this snippet |
| 126 | + |
| 127 | +```arduino |
| 128 | +% TODO - Replace the [] with channel ID to read data from: |
| 129 | +readChannelID = []; |
| 130 | +% TODO - Enter the Read API Key between the '' below: |
| 131 | +readAPIKey = 'T6UK7XO6A4H2AGT7'; |
| 132 | +% TODO - Replace the [] with channel ID to write data to: |
| 133 | +writeChannelID = []; |
| 134 | +% TODO - Enter the Write API Key between the '' below: |
| 135 | +writeAPIKey = 'XU4TGY261P6B5USN'; |
| 136 | +%% Read Data %% |
| 137 | +data = thingSpeakRead(readChannelID, 'ReadKey', readAPIKey,'OutputFormat','table'); |
| 138 | +analyzedData = data; |
| 139 | +%% Analyze Data %% |
| 140 | +INT16_t_MAX = 32767; |
| 141 | +UINT16_t_MAX = 65536; |
| 142 | +analyzedData.('Temperature1') = data.('Temperature1') / INT16_t_MAX * 120 ; |
| 143 | +analyzedData.('Temperature2') = data.('Temperature2') / INT16_t_MAX * 120 ; |
| 144 | +analyzedData.('Pressure') = data.('Pressure') / UINT16_t_MAX * 200000 ; |
| 145 | +analyzedData.('Light') = data.('Light') / UINT16_t_MAX * 100000 ; |
| 146 | +analyzedData.('Humidity') = data.('Humidity') / UINT16_t_MAX * 110 ; |
| 147 | +%% Write Data %% |
| 148 | +thingSpeakWrite(writeChannelID, analyzedData, 'WriteKey', writeAPIKey); |
| 149 | +%% Schedule action: React -> every 10 minutes |
| 150 | +``` |
| 151 | + |
| 152 | +By replacing the **channel IDs** with our own we can finally convert back the raw data into actual values. Remember to schedule the analysis action every 10 minutes using **React** or it will never be executed! |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +## Complete Sketch |
| 168 | + |
| 169 | +<iframe src='https://create.arduino.cc/editor/Arduino_Genuino/f025cb10-c6a7-41d5-911a-0ecad82bbcbb/preview?embed&snippet' style='height:510px;width:100%;margin:10px 0' frameborder='0'></iframe> |
0 commit comments