A first code example to get you started with the QW (GPS) Shield for SIGFOX development. It provides a serial connection to the modem over usb and transmits a SIGFOX message upon a button press.

Dependencies:   mbed QW_Sensors

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "math.h"
00003 #include "LinearTempSensor.h"
00004 
00005 /* The 4 onboard LEDs */
00006 DigitalOut LED_0 (PB_6);
00007 DigitalOut LED_1 (PA_7);
00008 DigitalOut LED_2 (PA_6);
00009 DigitalOut LED_3 (PA_5);
00010 
00011 /* The 2 user buttons */
00012 InterruptIn SW1(PA_8);
00013 InterruptIn SW2(PB_10);
00014 
00015 /*Temperature sensor */
00016 LinearTempSensor sensor(PA_0, 5);
00017 
00018 /* Function prototypes */
00019 void sw1interrupt();
00020 void sw2interrupt();
00021 void beat();
00022 void sertmout();
00023 bool modem_command_check_ok(char * command);
00024 void modem_setup();
00025 void txData(uint8_t btn);
00026 bool ser_timeout = false;
00027 
00028 Ticker heartbeat;
00029 
00030 /* Serial port over USB */
00031 Serial pc(USBTX, USBRX);
00032 
00033 /* Serial connection to sigfox modem */
00034 Serial modem(PA_9, PA_10);
00035 
00036 
00037 int main()
00038 {
00039 
00040     /* Setup TD120x */
00041     wait(3);
00042     modem_setup();
00043     /* Test temperature sensor */
00044     float vOut = sensor.Sense();
00045     pc.printf("\n\rMCP9700 reading:  Vout: %.2f mV", vOut);
00046 
00047     /* Turn off all LED */
00048     LED_0 = 1;
00049     LED_1 = 1;
00050     LED_2 = 1;
00051     LED_3 = 1;
00052 
00053     /* Blinking LED */
00054     heartbeat.attach(&beat, 0.5);
00055 
00056     /* Setup button interrupts */
00057     SW2.fall(&sw2interrupt);
00058     SW1.fall(&sw1interrupt);
00059 
00060     while(1) {
00061         if(pc.readable()) {
00062             modem.putc(pc.getc());
00063         }
00064         if(modem.readable()) {
00065             pc.putc(modem.getc());
00066         }
00067     }
00068 }
00069 
00070 void beat()
00071 {
00072     LED_0 = !LED_0;
00073 }
00074 
00075 /* Button 1 ISR */
00076 void sw1interrupt()
00077 {
00078     pc.printf("\n\rButton 1 pressed\n\r");
00079     LED_1 = 0;
00080     txData(1);
00081 }
00082 
00083 /* Button 2 ISR */
00084 void sw2interrupt()
00085 {
00086     pc.printf("\n\rButton 2 pressed\n\r");
00087     LED_2 = 0;
00088     txData(2);
00089 }
00090 
00091 
00092 /* TX data over Sigfox */
00093 void txData (uint8_t btn)
00094 {
00095     float    tAvg;
00096     char     command[32];
00097     sensor.Sense();
00098     tAvg = sensor.GetAverageTemp();
00099     char temperature[6] ="";
00100     sprintf(temperature, "%3.1f", tAvg);
00101     for(int i = 0; i < 5; i++)
00102         if(temperature[i]==0) temperature[i] = ' ';
00103     sprintf(command, "AT$SF=0142544e%x20%x%x%x%x%x43,2,0\n", btn+48, temperature[0],temperature[1],temperature[2],temperature[3],temperature[4]);
00104     pc.printf("Sending pressed button %d and temperature %s C over Sigfox.\n", btn, temperature);
00105     pc.printf("using modem command: %s", command);
00106     modem_command_check_ok(command);
00107     LED_1 = 1;
00108     LED_2 = 1;
00109 }
00110 
00111 void modem_setup()
00112 {
00113     /* Reset to factory defaults */
00114     if(modem_command_check_ok("AT&F")) {
00115         pc.printf("Factory reset succesfull\r\n");
00116     } else {
00117         pc.printf("Factory reset TD120x failed\r\n");
00118     }
00119     /* Disable local echo */
00120     modem.printf("ATE0\n");
00121     if(modem_command_check_ok("ATE0")) {
00122         pc.printf("Local echo disabled\r\n");
00123     }
00124     /* Write to mem */
00125     if(modem_command_check_ok("AT&W")) {
00126         pc.printf("Settings saved!\r\n");
00127     }
00128 }
00129 
00130 /* ISR for serial timeout */
00131 void sertmout()
00132 {
00133     ser_timeout = true;
00134 }
00135 
00136 bool modem_command_check_ok(char * command)
00137 {
00138     /* first clear serial data buffers */
00139     while(modem.readable()) modem.getc();
00140     /* Timeout for response of the modem */
00141     Timeout tmout;
00142     ser_timeout = false;
00143     // check if ok or error is received
00144     char * readyString = "OK";
00145     char * readyStringPtr;
00146     readyStringPtr = readyString;
00147     char * errorString = "ERROR";
00148     char * errorStringPtr;
00149     errorStringPtr = errorString;
00150     /* Flag to set when we get 'OK' response */
00151     bool ok = false;
00152     bool error = false;
00153     /* Print command to TD120x */
00154     modem.printf(command);
00155     /* Newline to activate command */
00156     modem.printf("\n");
00157     /* Wait untill serial feedback, max 10 seconds before timeout */
00158     tmout.attach(&sertmout, 10.0);
00159     int c;
00160     while (!ser_timeout  && !ok && !error) {
00161         if(modem.readable()) {
00162             c = modem.getc();
00163             if ( (char)c == *readyStringPtr ) readyStringPtr++;
00164             else readyStringPtr = readyString;
00165             if ( *readyStringPtr == 0 ) {
00166                 ok = true;
00167             }
00168             if ( (char)c == *errorStringPtr ) errorStringPtr++;
00169             else errorStringPtr = errorString;
00170             if ( *errorStringPtr == 0 ) {
00171                 error = true;
00172             }
00173         }
00174     }
00175     tmout.detach();
00176     if(ser_timeout) return false;
00177     else return ok;
00178 }