Sigfox Communication library, allowing you to use any kind of UART able Sigfox Transmitter

Dependencies:   SoftSerial

Dependents:   RUCHE2-CODES RUCHE2-CODES_correctionpoids RUCHE2-CODES

Files at this revision

API Documentation at this revision

Comitter:
adrien3d
Date:
Fri Mar 11 08:24:52 2016 +0000
Child:
1:39a0558a3a3a
Commit message:
First version

Changed in this revision

millis.cpp Show annotated file Show diff for this revision Revisions of this file
millis.h Show annotated file Show diff for this revision Revisions of this file
sigfox.cpp Show annotated file Show diff for this revision Revisions of this file
sigfox.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/millis.cpp	Fri Mar 11 08:24:52 2016 +0000
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * This file is part of the millis library.                                    *
+ *                                                                             *
+ * millis is free software: you can redistribute it and/or                     *
+ * modify it under the terms of the GNU General Public License as              *
+ * published by the Free Software Foundation, either version 3 of              *
+ * the License, or any later version.                                          *
+ *                                                                             *
+ * millis is distributed in the hope that it will be useful,                   *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
+ * GNU Lesser General Public License for more details.                         *
+ *                                                                             *
+ * millis is distributed in the hope that it will be useful,                   *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
+ * GNU Lesser General Public License for more details.                         *
+ *                                                                             *
+ * You should have received a copy of the GNU Lesser General Public            *
+ * License along with millis. If not, see                                      *
+ * <http://www.gnu.org/licenses/>.                                             *
+ ******************************************************************************/
+
+/*
+ *  Copyright:  DFRobot
+ *  name:       millis
+ *  version:    1.0
+ *  Author:     lisper (lisper.li@dfrobot.com)
+ *  Date:       2014-10-30
+ *  Description:    millis library for mbed
+ */
+
+#include "mbed.h"
+#include "millis.h"
+
+static volatile uint32_t millisValue = 0;
+
+static  Ticker ticker;
+
+void millisTicker ()
+{
+    millisValue ++;
+}
+
+uint32_t millis ()
+{
+    return millisValue;
+}
+
+void setMillis (uint32_t theValue) {
+    millisValue = theValue;
+}
+
+void startMillis () {
+    ticker.attach (millisTicker, 0.001);    
+}
+
+void stopMillis () {
+    ticker.detach ();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/millis.h	Fri Mar 11 08:24:52 2016 +0000
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * This file is part of the millis library.                                    *
+ *                                                                             *
+ * millis is free software: you can redistribute it and/or                     *
+ * modify it under the terms of the GNU General Public License as              *
+ * published by the Free Software Foundation, either version 3 of              *
+ * the License, or any later version.                                          *
+ *                                                                             *
+ * millis is distributed in the hope that it will be useful,                   *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
+ * GNU Lesser General Public License for more details.                         *
+ *                                                                             *
+ * millis is distributed in the hope that it will be useful,                   *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
+ * GNU Lesser General Public License for more details.                         *
+ *                                                                             *
+ * You should have received a copy of the GNU Lesser General Public            *
+ * License along with millis. If not, see                                      *
+ * <http://www.gnu.org/licenses/>.                                             *
+ ******************************************************************************/
+
+/*
+ *  Copyright:  DFRobot
+ *  name:       millis
+ *  version:    1.0
+ *  Author:     lisper (lisper.li@dfrobot.com)
+ *  Date:       2014-10-30
+ *  Description:    millis library for mbed
+ */
+
+#include "mbed.h"
+
+void millisTicker ();
+
+uint32_t millis ();
+
+void startMillis ();
+
+void stopMillis ();
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sigfox.cpp	Fri Mar 11 08:24:52 2016 +0000
@@ -0,0 +1,200 @@
+#include "mbed.h"
+#include "millis.h"
+#include "sigfox.h"
+
+/* sigfox library
+ *
+ * Copyright (c) Adrien Chapelet for IoTech.fr
+ *
+ * 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.
+ */
+ 
+ /*
+    This library allows you to use any kind of Sigfox Transmitter, all you have to do is configure pins and baudrate, and check syntax is conform to your datasheet.
+ */
+ 
+//---------------------------------------------------------------------------------------------//
+//Constructors
+Sigfox_::Sigfox_()  {
+    Serial device(PIN_RX, PIN_TX);
+    _lastSend=-1;
+}
+/*Sigfox_::Sigfox_(PinMode TX, PinMode RX)  {
+    Serial device(p8, p7); 
+}*/
+
+//Functions
+uint8_t Sigfox_::_nextReturn() {
+    Serial device(PIN_RX, PIN_TX);     
+    char fstChar = device.getc();
+    while(device.getc() != ';');
+    return fstChar;
+}
+
+void Sigfox_::begin () {
+    Serial device(PIN_RX, PIN_TX); 
+    device.putc((uint8_t)'\0');
+    device.putc((uint8_t)';');
+    
+    //while(device.available() < 3);
+
+    device.getc(); //'K'
+    device.getc(); //'O'
+    device.getc(); //';'
+}
+
+bool Sigfox_::isReady() {
+    Serial device(PIN_RX, PIN_TX);
+
+    unsigned long currentTime = millis();
+    if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) {
+        return false;
+    }
+    // Time is ok, ask the modem's status
+    device.putc((uint8_t)'\0');
+    device.putc((uint8_t)'A');
+    device.putc((uint8_t)'T');
+    device.putc('\r');
+    //device.putc((uint8_t)';');
+
+    //return _nextReturn() == OK;
+    return true;
+/*
+    device.putc((uint8_t)'A');
+    device.putc((uint8_t)'T');
+
+    uint8_t response[8] = {0};
+    uint8_t i = 0;
+    //while(!device.available());
+    while(device.peek() != ';') {
+        response[i] = device.read();
+        while(!device.available());
+        ++i;
+    }
+    response[0]=device.getc(); //'K'
+    response[1]=device.getc(); //'O'
+    response[2]=device.getc(); //';'
+    if ((response[0]=='O') && (response[1]=='K'))
+        return OK;*/
+}
+ 
+bool Sigfox_::send(const void* data, uint8_t len) {
+    uint8_t* bytes = (uint8_t*)data;
+    _lastSend = millis();
+    
+    Serial device(PIN_RX, PIN_TX);
+    device.putc((uint8_t)'\0');
+    device.putc((uint8_t)'A');
+    device.putc((uint8_t)'T');
+    device.putc((uint8_t)'$');
+    device.putc((uint8_t)'S');
+    device.putc((uint8_t)'F');
+    device.putc((uint8_t)'=');
+    
+    for(uint8_t i = 0; i < len; ++i) {
+        device.putc(bytes[i]);
+    }
+    device.putc('\r');
+
+    uint8_t ok = _nextReturn();
+    if(ok == OK) {
+        _nextReturn(); //SENT
+        return true;
+    }
+    return false;
+}
+ 
+unsigned long Sigfox_::getID() {
+    unsigned long id = 0;
+    Serial device(PIN_RX, PIN_TX);
+    device.putc((uint8_t)'\0');
+    device.putc((uint8_t)'A');
+    device.putc((uint8_t)'T');
+    device.putc((uint8_t)'$');
+    device.putc((uint8_t)'I');
+    device.putc((uint8_t)'=');
+    device.putc((uint8_t)'1');
+    device.putc((uint8_t)'0');
+    device.putc('\r');
+    
+    uint8_t response[8] = {0};
+    uint8_t i = 0;
+    for (i = 0; i<7;++i) {
+        response[i] = device.getc();
+        ++i;
+    }
+    /*uint8_t response[8] = {0};
+    while(device.getc() != '\0') {
+        response[i] = device.getc();
+        while(!device.readable());
+        ++i;
+    }*/
+    device.getc(); //';'
+    for(uint8_t j = 0; j < i; ++j) {
+        id += response[j] << ((i-3-j) * 8);
+    }
+    return id;
+}
+
+unsigned long Sigfox_::getPAC() {
+    unsigned long id = 0;
+    Serial device(PIN_RX, PIN_TX);
+    device.putc((uint8_t)'\0');
+    device.putc((uint8_t)'A');
+    device.putc((uint8_t)'T');
+    device.putc((uint8_t)'$');
+    device.putc((uint8_t)'I');
+    device.putc((uint8_t)'=');
+    device.putc((uint8_t)'1');
+    device.putc((uint8_t)'1');
+    device.putc('\r');
+    
+    uint8_t response[8] = {0};
+    uint8_t i = 0;
+    for (i = 0; i<7;++i) {
+        response[i] = device.getc();
+        ++i;
+    }
+    
+    for(uint8_t j = 0; j < i; ++j) {
+        id += response[j] << ((i-3-j) * 16);
+    }
+    return true;
+}
+
+bool Sigfox_::setPowerMode(uint8_t power) {
+    Serial device(PIN_RX, PIN_TX);
+    device.putc((uint8_t)'\0');
+    device.putc((uint8_t)'A');
+    device.putc((uint8_t)'T');
+    device.putc((uint8_t)'$');
+    device.putc((uint8_t)'I');
+    device.putc((uint8_t)'=');
+    device.putc(power);
+    device.putc('\r');
+
+    //return _nextReturn() == OK;
+    return 1;
+}
+
+//------------------------------------------------------------------------------------------------//
+//Destructors
+
+/*sigfox::~sf() {
+}*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sigfox.h	Fri Mar 11 08:24:52 2016 +0000
@@ -0,0 +1,39 @@
+#ifndef SIGFOX_H
+#define SIGFOX_H
+
+#include "mbed.h"
+
+//Define your baudrate and pins here
+#define BAUDRATE      9600
+#define PIN_RX       p7
+#define PIN_TX       p8
+
+class Sigfox_ {
+
+public:
+    Sigfox_();
+    //Sigfox_(PinMode TX, PinMode RX);
+    
+    void begin();
+    bool isReady();
+    bool send(const void* data, uint8_t len);
+    unsigned long getID();
+    unsigned long getPAC();
+    bool setPowerMode(uint8_t power);
+
+    enum RETURN_CODE {
+        OK = 'O',
+        KO = 'K',
+        SENT = 'S'
+    };
+    
+private:
+    //Serial device(p9, p10); 
+    //Serial _serial;
+    unsigned long _lastSend;
+
+    uint8_t _nextReturn();
+};
+
+extern Sigfox_ Sigfox;
+#endif
\ No newline at end of file