/* * Sigfox.ino * Example sketch for Meteo Station using Sigfox * * Website : www.pcdemano.com * Author : Pharizna * Create Time: 22/05/2018 * Change Log : * * The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include "conversions.h" // Set oneshot to false to trigger continuous mode when you finisched setting up the whole flow int oneshot = false; // float temperature; float pressure; float atm; float altitude; Barometer myBarometer; #define STATUS_OK 0 /* ATTENTION - the structure we are going to send MUST be declared "packed" otherwise we'll get padding mismatch on the sent data - see http://www.catb.org/esr/structure-packing/#_structure_alignment_and_padding for more details */ typedef struct __attribute__ ((packed)) sigfox_message { uint8_t status; int16_t moduleTemperature; int16_t pcdmTemperature; uint16_t pcdmPressure; uint16_t pcdmAtm; uint16_t pcdmAltitude; uint8_t lastMessageStatus; // Empty in this version } SigfoxMessage; // stub for message which will be sent SigfoxMessage msg; void setup(){ if (oneshot == true) { // Wait for the serial Serial.begin(9600); while (!Serial) {} } if (!SigFox.begin()) { // Something is really wrong, try rebooting // Reboot is useful if we are powering the board using an unreliable power source // (eg. solar panels or other energy harvesting methods) reboot(); } //Send module to standby until we need to send a message SigFox.end(); if (oneshot == true) { // Enable debug prints and LED indication if we are testing SigFox.debug(); } myBarometer.init(); } void loop() { // Every 15 minutes, read all the sensors and send them // Let's try to optimize the data format // Only use floats as intermediate representaion, don't send them directly float temperature = myBarometer.bmp085GetTemperature(myBarometer.bmp085ReadUT()); //Get the temperature, bmp085ReadUT MUST be called first msg.pcdmTemperature = convertoFloatToInt16(temperature, 50, -5); float pressure = myBarometer.bmp085GetPressure(myBarometer.bmp085ReadUP());//Get the pressure msg.pcdmPressure = convertoFloatToUInt16(pressure, 150000); float altitude = myBarometer.calcAltitude(pressure); //Uncompensated caculation - in Meters msg.pcdmAltitude = convertoFloatToUInt16(altitude, 1000); float atm = pressure / 101325*1024; msg.pcdmAtm = convertoFloatToInt16(atm, 1200, 900); // Start the module SigFox.begin(); // Wait at least 100ms after first configuration delay(100); // We can only read the module temperature before SigFox.end() float itemperature = SigFox.internalTemperature(); msg.moduleTemperature = convertoFloatToInt16(itemperature, 45, -5); if (oneshot == true) { Serial.println("Pressure: " + String(pressure)); Serial.println("External temperature: " + String(temperature)); Serial.println("Internal temp: " + String(itemperature)); Serial.println("Altitude: " + String(altitude)); Serial.println("Atm: " + String(atm)); } // Clears all pending interrupts SigFox.status(); delay(1); SigFox.beginPacket(); SigFox.write((uint8_t*)&msg, 12); msg.lastMessageStatus = SigFox.endPacket(); if (oneshot == true) { Serial.println("Status: " + String(msg.lastMessageStatus)); Serial.println (""); } SigFox.end(); if (oneshot == true) { // spin forever, so we can test that the backend is behaving correctly while (1) {} } //Sleep for 15 minutes LowPower.sleep(15 * 60 * 1000); } void reboot() { NVIC_SystemReset(); while (1); }