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

HelloWorld QW Development kit

Preloaded code example

The QW development kits ship with this code example. This example allows you to talk straight to the TD1208 modem when using a virtual com port (note: local echo is off). This code-example also sends a SIGFOX message whenever you press a button. The message contains the button number and the measured environment temperature. The first byte is always 0x01.

/media/uploads/quicksand/packetformat.jpg

More information and other example code can be found on the component page by clicking the link below: https://developer.mbed.org/components/QW-SIGFOX-Development-Kit/

Committer:
quicksand
Date:
Tue Mar 14 15:08:58 2017 +0000
Revision:
5:ca670603d63d
Parent:
4:69df63eeb91e
Child:
6:ac11cebb397e
Small update eliminating unnecessary if-clause.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quicksand 0:49858c4c3500 1 #include "mbed.h"
quicksand 1:897a1b3f0955 2 #include "math.h"
quicksand 1:897a1b3f0955 3 #include "LinearTempSensor.h"
quicksand 0:49858c4c3500 4
quicksand 1:897a1b3f0955 5 /* The 4 onboard LEDs */
quicksand 0:49858c4c3500 6 DigitalOut LED_0 (PB_6);
quicksand 0:49858c4c3500 7 DigitalOut LED_1 (PA_7);
quicksand 0:49858c4c3500 8 DigitalOut LED_2 (PA_6);
quicksand 0:49858c4c3500 9 DigitalOut LED_3 (PA_5);
quicksand 0:49858c4c3500 10
quicksand 1:897a1b3f0955 11 /* The 2 user buttons */
quicksand 1:897a1b3f0955 12 InterruptIn SW1(PA_8);
quicksand 1:897a1b3f0955 13 InterruptIn SW2(PB_10);
quicksand 1:897a1b3f0955 14
quicksand 1:897a1b3f0955 15 /*Temperature sensor */
quicksand 2:a4a68a858624 16 LinearTempSensor sensor(PA_0, 5);
quicksand 0:49858c4c3500 17
quicksand 1:897a1b3f0955 18 /* Function prototypes */
quicksand 1:897a1b3f0955 19 void sw1interrupt();
quicksand 1:897a1b3f0955 20 void sw2interrupt();
quicksand 1:897a1b3f0955 21 void beat();
quicksand 1:897a1b3f0955 22 void sertmout();
quicksand 1:897a1b3f0955 23 bool modem_command_check_ok(char * command);
quicksand 1:897a1b3f0955 24 void modem_setup();
quicksand 1:897a1b3f0955 25 void txData(uint8_t btn);
quicksand 1:897a1b3f0955 26 bool ser_timeout = false;
quicksand 1:897a1b3f0955 27
quicksand 1:897a1b3f0955 28 Ticker heartbeat;
quicksand 1:897a1b3f0955 29
quicksand 1:897a1b3f0955 30 /* Serial port over USB */
quicksand 0:49858c4c3500 31 Serial pc(USBTX, USBRX);
quicksand 1:897a1b3f0955 32
quicksand 1:897a1b3f0955 33 /* Serial connection to sigfox modem */
quicksand 0:49858c4c3500 34 Serial modem(PA_9, PA_10);
quicksand 0:49858c4c3500 35
quicksand 1:897a1b3f0955 36
quicksand 1:897a1b3f0955 37 int main()
quicksand 1:897a1b3f0955 38 {
quicksand 0:49858c4c3500 39
quicksand 1:897a1b3f0955 40 /* Setup TD120x */
quicksand 1:897a1b3f0955 41 wait(3);
quicksand 1:897a1b3f0955 42 modem_setup();
quicksand 1:897a1b3f0955 43 /* Test temperature sensor */
quicksand 1:897a1b3f0955 44 float vOut = sensor.Sense();
quicksand 1:897a1b3f0955 45 pc.printf("\n\rMCP9700 reading: Vout: %.2f mV", vOut);
quicksand 0:49858c4c3500 46
quicksand 1:897a1b3f0955 47 /* Turn off all LED */
quicksand 0:49858c4c3500 48 LED_0 = 1;
quicksand 0:49858c4c3500 49 LED_1 = 1;
quicksand 0:49858c4c3500 50 LED_2 = 1;
quicksand 0:49858c4c3500 51 LED_3 = 1;
quicksand 1:897a1b3f0955 52
quicksand 1:897a1b3f0955 53 /* Blinking LED */
quicksand 1:897a1b3f0955 54 heartbeat.attach(&beat, 0.5);
quicksand 1:897a1b3f0955 55
quicksand 1:897a1b3f0955 56 /* Setup button interrupts */
quicksand 1:897a1b3f0955 57 SW2.fall(&sw2interrupt);
quicksand 1:897a1b3f0955 58 SW1.fall(&sw1interrupt);
quicksand 1:897a1b3f0955 59
quicksand 1:897a1b3f0955 60 while(1) {
quicksand 0:49858c4c3500 61 if(pc.readable()) {
quicksand 0:49858c4c3500 62 modem.putc(pc.getc());
quicksand 0:49858c4c3500 63 }
quicksand 0:49858c4c3500 64 if(modem.readable()) {
quicksand 1:897a1b3f0955 65 pc.putc(modem.getc());
quicksand 0:49858c4c3500 66 }
quicksand 0:49858c4c3500 67 }
quicksand 0:49858c4c3500 68 }
quicksand 1:897a1b3f0955 69
quicksand 1:897a1b3f0955 70 void beat()
quicksand 1:897a1b3f0955 71 {
quicksand 1:897a1b3f0955 72 LED_0 = !LED_0;
quicksand 1:897a1b3f0955 73 }
quicksand 1:897a1b3f0955 74
quicksand 1:897a1b3f0955 75 /* Button 1 ISR */
quicksand 1:897a1b3f0955 76 void sw1interrupt()
quicksand 1:897a1b3f0955 77 {
quicksand 1:897a1b3f0955 78 pc.printf("\n\rButton 1 pressed\n\r");
quicksand 1:897a1b3f0955 79 LED_1 = 0;
quicksand 1:897a1b3f0955 80 txData(1);
quicksand 1:897a1b3f0955 81 }
quicksand 1:897a1b3f0955 82
quicksand 1:897a1b3f0955 83 /* Button 2 ISR */
quicksand 1:897a1b3f0955 84 void sw2interrupt()
quicksand 1:897a1b3f0955 85 {
quicksand 1:897a1b3f0955 86 pc.printf("\n\rButton 2 pressed\n\r");
quicksand 1:897a1b3f0955 87 LED_2 = 0;
quicksand 1:897a1b3f0955 88 txData(2);
quicksand 1:897a1b3f0955 89 }
quicksand 1:897a1b3f0955 90
quicksand 1:897a1b3f0955 91
quicksand 1:897a1b3f0955 92 /* TX data over Sigfox */
quicksand 1:897a1b3f0955 93 void txData (uint8_t btn)
quicksand 1:897a1b3f0955 94 {
quicksand 1:897a1b3f0955 95 float tAvg;
quicksand 1:897a1b3f0955 96 char command[32];
quicksand 2:a4a68a858624 97 sensor.Sense();
quicksand 1:897a1b3f0955 98 tAvg = sensor.GetAverageTemp();
quicksand 1:897a1b3f0955 99 char temperature[6] ="";
quicksand 1:897a1b3f0955 100 sprintf(temperature, "%3.1f", tAvg);
quicksand 1:897a1b3f0955 101 for(int i = 0; i < 5; i++)
quicksand 1:897a1b3f0955 102 if(temperature[i]==0) temperature[i] = ' ';
quicksand 3:4da15d6e1429 103 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]);
quicksand 1:897a1b3f0955 104 pc.printf("Sending pressed button %d and temperature %s C over Sigfox.\n", btn, temperature);
quicksand 1:897a1b3f0955 105 pc.printf("using modem command: %s", command);
quicksand 1:897a1b3f0955 106 modem_command_check_ok(command);
quicksand 1:897a1b3f0955 107 LED_1 = 1;
quicksand 1:897a1b3f0955 108 LED_2 = 1;
quicksand 1:897a1b3f0955 109 }
quicksand 1:897a1b3f0955 110
quicksand 1:897a1b3f0955 111 void modem_setup()
quicksand 1:897a1b3f0955 112 {
quicksand 1:897a1b3f0955 113 /* Reset to factory defaults */
quicksand 1:897a1b3f0955 114 if(modem_command_check_ok("AT&F")) {
quicksand 1:897a1b3f0955 115 pc.printf("Factory reset succesfull\r\n");
quicksand 1:897a1b3f0955 116 } else {
quicksand 1:897a1b3f0955 117 pc.printf("Factory reset TD120x failed\r\n");
quicksand 1:897a1b3f0955 118 }
quicksand 1:897a1b3f0955 119 /* Disable local echo */
quicksand 1:897a1b3f0955 120 modem.printf("ATE0\n");
quicksand 1:897a1b3f0955 121 if(modem_command_check_ok("ATE0")) {
quicksand 1:897a1b3f0955 122 pc.printf("Local echo disabled\r\n");
quicksand 1:897a1b3f0955 123 }
quicksand 1:897a1b3f0955 124 /* Write to mem */
quicksand 1:897a1b3f0955 125 if(modem_command_check_ok("AT&W")) {
quicksand 1:897a1b3f0955 126 pc.printf("Settings saved!\r\n");
quicksand 1:897a1b3f0955 127 }
quicksand 1:897a1b3f0955 128 }
quicksand 1:897a1b3f0955 129
quicksand 1:897a1b3f0955 130 /* ISR for serial timeout */
quicksand 1:897a1b3f0955 131 void sertmout()
quicksand 1:897a1b3f0955 132 {
quicksand 1:897a1b3f0955 133 ser_timeout = true;
quicksand 1:897a1b3f0955 134 }
quicksand 1:897a1b3f0955 135
quicksand 1:897a1b3f0955 136 bool modem_command_check_ok(char * command)
quicksand 1:897a1b3f0955 137 {
quicksand 1:897a1b3f0955 138 /* first clear serial data buffers */
quicksand 1:897a1b3f0955 139 while(modem.readable()) modem.getc();
quicksand 1:897a1b3f0955 140 /* Timeout for response of the modem */
quicksand 1:897a1b3f0955 141 Timeout tmout;
quicksand 1:897a1b3f0955 142 ser_timeout = false;
quicksand 4:69df63eeb91e 143 // check if ok or error is received
quicksand 4:69df63eeb91e 144 char * readyString = "OK";
quicksand 4:69df63eeb91e 145 char * readyStringPtr;
quicksand 4:69df63eeb91e 146 readyStringPtr = readyString;
quicksand 4:69df63eeb91e 147 char * errorString = "ERROR";
quicksand 4:69df63eeb91e 148 char * errorStringPtr;
quicksand 4:69df63eeb91e 149 errorStringPtr = errorString;
quicksand 1:897a1b3f0955 150 /* Flag to set when we get 'OK' response */
quicksand 1:897a1b3f0955 151 bool ok = false;
quicksand 1:897a1b3f0955 152 bool error = false;
quicksand 1:897a1b3f0955 153 /* Print command to TD120x */
quicksand 1:897a1b3f0955 154 modem.printf(command);
quicksand 1:897a1b3f0955 155 /* Newline to activate command */
quicksand 1:897a1b3f0955 156 modem.printf("\n");
quicksand 4:69df63eeb91e 157 /* Wait untill serial feedback, max 10 seconds before timeout */
quicksand 4:69df63eeb91e 158 tmout.attach(&sertmout, 10.0);
quicksand 4:69df63eeb91e 159 int c;
quicksand 4:69df63eeb91e 160 while(!ser_timeout && !ok && !error)
quicksand 5:ca670603d63d 161 while (modem.readable() && !ser_timeout && !ok && !error) {
quicksand 5:ca670603d63d 162 c = modem.getc();
quicksand 5:ca670603d63d 163 if ( (char)c == *readyStringPtr ) readyStringPtr++;
quicksand 5:ca670603d63d 164 else readyStringPtr = readyString;
quicksand 5:ca670603d63d 165 if ( *readyStringPtr == 0 ) {
quicksand 5:ca670603d63d 166 ok = true;
quicksand 1:897a1b3f0955 167 }
quicksand 5:ca670603d63d 168 if ( (char)c == *errorStringPtr ) errorStringPtr++;
quicksand 5:ca670603d63d 169 else errorStringPtr = errorString;
quicksand 5:ca670603d63d 170 if ( *errorStringPtr == 0 ) {
quicksand 5:ca670603d63d 171 error = true;
quicksand 5:ca670603d63d 172 }
quicksand 5:ca670603d63d 173 }
quicksand 1:897a1b3f0955 174 tmout.detach();
quicksand 4:69df63eeb91e 175 if(ser_timeout) return false;
quicksand 4:69df63eeb91e 176 else return ok;
quicksand 5:ca670603d63d 177 }