Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:69df63eeb91e, 2017-03-14 (annotated)
- Committer:
- quicksand
- Date:
- Tue Mar 14 14:54:35 2017 +0000
- Revision:
- 4:69df63eeb91e
- Parent:
- 3:4da15d6e1429
- Child:
- 5:ca670603d63d
Updated the function that sends out the SIGFOX command to be more robust.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 0:49858c4c3500 | 26 | |
| quicksand | 1:897a1b3f0955 | 27 | bool ser_timeout = false; |
| quicksand | 1:897a1b3f0955 | 28 | |
| quicksand | 1:897a1b3f0955 | 29 | Ticker heartbeat; |
| quicksand | 1:897a1b3f0955 | 30 | |
| quicksand | 1:897a1b3f0955 | 31 | /* Serial port over USB */ |
| quicksand | 0:49858c4c3500 | 32 | Serial pc(USBTX, USBRX); |
| quicksand | 1:897a1b3f0955 | 33 | |
| quicksand | 1:897a1b3f0955 | 34 | /* Serial connection to sigfox modem */ |
| quicksand | 0:49858c4c3500 | 35 | Serial modem(PA_9, PA_10); |
| quicksand | 0:49858c4c3500 | 36 | |
| quicksand | 1:897a1b3f0955 | 37 | |
| quicksand | 1:897a1b3f0955 | 38 | int main() |
| quicksand | 1:897a1b3f0955 | 39 | { |
| quicksand | 0:49858c4c3500 | 40 | |
| quicksand | 1:897a1b3f0955 | 41 | /* Setup TD120x */ |
| quicksand | 1:897a1b3f0955 | 42 | wait(3); |
| quicksand | 1:897a1b3f0955 | 43 | modem_setup(); |
| quicksand | 1:897a1b3f0955 | 44 | /* Test temperature sensor */ |
| quicksand | 1:897a1b3f0955 | 45 | float vOut = sensor.Sense(); |
| quicksand | 1:897a1b3f0955 | 46 | pc.printf("\n\rMCP9700 reading: Vout: %.2f mV", vOut); |
| quicksand | 0:49858c4c3500 | 47 | |
| quicksand | 1:897a1b3f0955 | 48 | /* Turn off all LED */ |
| quicksand | 0:49858c4c3500 | 49 | LED_0 = 1; |
| quicksand | 0:49858c4c3500 | 50 | LED_1 = 1; |
| quicksand | 0:49858c4c3500 | 51 | LED_2 = 1; |
| quicksand | 0:49858c4c3500 | 52 | LED_3 = 1; |
| quicksand | 1:897a1b3f0955 | 53 | |
| quicksand | 1:897a1b3f0955 | 54 | /* Blinking LED */ |
| quicksand | 1:897a1b3f0955 | 55 | heartbeat.attach(&beat, 0.5); |
| quicksand | 1:897a1b3f0955 | 56 | |
| quicksand | 1:897a1b3f0955 | 57 | /* Setup button interrupts */ |
| quicksand | 1:897a1b3f0955 | 58 | SW2.fall(&sw2interrupt); |
| quicksand | 1:897a1b3f0955 | 59 | SW1.fall(&sw1interrupt); |
| quicksand | 1:897a1b3f0955 | 60 | |
| quicksand | 1:897a1b3f0955 | 61 | while(1) { |
| quicksand | 0:49858c4c3500 | 62 | if(pc.readable()) { |
| quicksand | 0:49858c4c3500 | 63 | modem.putc(pc.getc()); |
| quicksand | 0:49858c4c3500 | 64 | } |
| quicksand | 0:49858c4c3500 | 65 | if(modem.readable()) { |
| quicksand | 1:897a1b3f0955 | 66 | pc.putc(modem.getc()); |
| quicksand | 0:49858c4c3500 | 67 | } |
| quicksand | 0:49858c4c3500 | 68 | } |
| quicksand | 0:49858c4c3500 | 69 | } |
| quicksand | 1:897a1b3f0955 | 70 | |
| quicksand | 1:897a1b3f0955 | 71 | void beat() |
| quicksand | 1:897a1b3f0955 | 72 | { |
| quicksand | 1:897a1b3f0955 | 73 | LED_0 = !LED_0; |
| quicksand | 1:897a1b3f0955 | 74 | } |
| quicksand | 1:897a1b3f0955 | 75 | |
| quicksand | 1:897a1b3f0955 | 76 | /* Button 1 ISR */ |
| quicksand | 1:897a1b3f0955 | 77 | void sw1interrupt() |
| quicksand | 1:897a1b3f0955 | 78 | { |
| quicksand | 1:897a1b3f0955 | 79 | pc.printf("\n\rButton 1 pressed\n\r"); |
| quicksand | 1:897a1b3f0955 | 80 | LED_1 = 0; |
| quicksand | 1:897a1b3f0955 | 81 | txData(1); |
| quicksand | 1:897a1b3f0955 | 82 | } |
| quicksand | 1:897a1b3f0955 | 83 | |
| quicksand | 1:897a1b3f0955 | 84 | /* Button 2 ISR */ |
| quicksand | 1:897a1b3f0955 | 85 | void sw2interrupt() |
| quicksand | 1:897a1b3f0955 | 86 | { |
| quicksand | 1:897a1b3f0955 | 87 | pc.printf("\n\rButton 2 pressed\n\r"); |
| quicksand | 1:897a1b3f0955 | 88 | LED_2 = 0; |
| quicksand | 1:897a1b3f0955 | 89 | txData(2); |
| quicksand | 1:897a1b3f0955 | 90 | } |
| quicksand | 1:897a1b3f0955 | 91 | |
| quicksand | 1:897a1b3f0955 | 92 | |
| quicksand | 1:897a1b3f0955 | 93 | /* TX data over Sigfox */ |
| quicksand | 1:897a1b3f0955 | 94 | void txData (uint8_t btn) |
| quicksand | 1:897a1b3f0955 | 95 | { |
| quicksand | 1:897a1b3f0955 | 96 | float tAvg; |
| quicksand | 1:897a1b3f0955 | 97 | char command[32]; |
| quicksand | 2:a4a68a858624 | 98 | sensor.Sense(); |
| quicksand | 1:897a1b3f0955 | 99 | tAvg = sensor.GetAverageTemp(); |
| quicksand | 1:897a1b3f0955 | 100 | char temperature[6] =""; |
| quicksand | 1:897a1b3f0955 | 101 | sprintf(temperature, "%3.1f", tAvg); |
| quicksand | 1:897a1b3f0955 | 102 | for(int i = 0; i < 5; i++) |
| quicksand | 1:897a1b3f0955 | 103 | if(temperature[i]==0) temperature[i] = ' '; |
| quicksand | 3:4da15d6e1429 | 104 | 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 | 105 | pc.printf("Sending pressed button %d and temperature %s C over Sigfox.\n", btn, temperature); |
| quicksand | 1:897a1b3f0955 | 106 | pc.printf("using modem command: %s", command); |
| quicksand | 1:897a1b3f0955 | 107 | modem_command_check_ok(command); |
| quicksand | 1:897a1b3f0955 | 108 | LED_1 = 1; |
| quicksand | 1:897a1b3f0955 | 109 | LED_2 = 1; |
| quicksand | 1:897a1b3f0955 | 110 | } |
| quicksand | 1:897a1b3f0955 | 111 | |
| quicksand | 1:897a1b3f0955 | 112 | void modem_setup() |
| quicksand | 1:897a1b3f0955 | 113 | { |
| quicksand | 1:897a1b3f0955 | 114 | /* Reset to factory defaults */ |
| quicksand | 1:897a1b3f0955 | 115 | if(modem_command_check_ok("AT&F")) { |
| quicksand | 1:897a1b3f0955 | 116 | pc.printf("Factory reset succesfull\r\n"); |
| quicksand | 1:897a1b3f0955 | 117 | } else { |
| quicksand | 1:897a1b3f0955 | 118 | pc.printf("Factory reset TD120x failed\r\n"); |
| quicksand | 1:897a1b3f0955 | 119 | } |
| quicksand | 1:897a1b3f0955 | 120 | /* Disable local echo */ |
| quicksand | 1:897a1b3f0955 | 121 | modem.printf("ATE0\n"); |
| quicksand | 1:897a1b3f0955 | 122 | if(modem_command_check_ok("ATE0")) { |
| quicksand | 1:897a1b3f0955 | 123 | pc.printf("Local echo disabled\r\n"); |
| quicksand | 1:897a1b3f0955 | 124 | } |
| quicksand | 1:897a1b3f0955 | 125 | /* Write to mem */ |
| quicksand | 1:897a1b3f0955 | 126 | if(modem_command_check_ok("AT&W")) { |
| quicksand | 1:897a1b3f0955 | 127 | pc.printf("Settings saved!\r\n"); |
| quicksand | 1:897a1b3f0955 | 128 | } |
| quicksand | 1:897a1b3f0955 | 129 | } |
| quicksand | 1:897a1b3f0955 | 130 | |
| quicksand | 1:897a1b3f0955 | 131 | /* ISR for serial timeout */ |
| quicksand | 1:897a1b3f0955 | 132 | void sertmout() |
| quicksand | 1:897a1b3f0955 | 133 | { |
| quicksand | 1:897a1b3f0955 | 134 | ser_timeout = true; |
| quicksand | 1:897a1b3f0955 | 135 | } |
| quicksand | 1:897a1b3f0955 | 136 | |
| quicksand | 1:897a1b3f0955 | 137 | bool modem_command_check_ok(char * command) |
| quicksand | 1:897a1b3f0955 | 138 | { |
| quicksand | 1:897a1b3f0955 | 139 | /* first clear serial data buffers */ |
| quicksand | 1:897a1b3f0955 | 140 | while(modem.readable()) modem.getc(); |
| quicksand | 1:897a1b3f0955 | 141 | /* Timeout for response of the modem */ |
| quicksand | 1:897a1b3f0955 | 142 | Timeout tmout; |
| quicksand | 1:897a1b3f0955 | 143 | ser_timeout = false; |
| quicksand | 4:69df63eeb91e | 144 | // check if ok or error is received |
| quicksand | 4:69df63eeb91e | 145 | char * readyString = "OK"; |
| quicksand | 4:69df63eeb91e | 146 | char * readyStringPtr; |
| quicksand | 4:69df63eeb91e | 147 | readyStringPtr = readyString; |
| quicksand | 4:69df63eeb91e | 148 | char * errorString = "ERROR"; |
| quicksand | 4:69df63eeb91e | 149 | char * errorStringPtr; |
| quicksand | 4:69df63eeb91e | 150 | errorStringPtr = errorString; |
| quicksand | 1:897a1b3f0955 | 151 | /* Flag to set when we get 'OK' response */ |
| quicksand | 1:897a1b3f0955 | 152 | bool ok = false; |
| quicksand | 1:897a1b3f0955 | 153 | bool error = false; |
| quicksand | 1:897a1b3f0955 | 154 | /* Print command to TD120x */ |
| quicksand | 1:897a1b3f0955 | 155 | modem.printf(command); |
| quicksand | 1:897a1b3f0955 | 156 | /* Newline to activate command */ |
| quicksand | 1:897a1b3f0955 | 157 | modem.printf("\n"); |
| quicksand | 4:69df63eeb91e | 158 | /* Wait untill serial feedback, max 10 seconds before timeout */ |
| quicksand | 4:69df63eeb91e | 159 | tmout.attach(&sertmout, 10.0); |
| quicksand | 4:69df63eeb91e | 160 | int c; |
| quicksand | 4:69df63eeb91e | 161 | while(!ser_timeout && !ok && !error) |
| quicksand | 4:69df63eeb91e | 162 | if(modem.readable()) |
| quicksand | 4:69df63eeb91e | 163 | while (((c = modem.getc()) > 0) && !ser_timeout && !ok && !error) { |
| quicksand | 4:69df63eeb91e | 164 | if ( (char)c == *readyStringPtr ) readyStringPtr++; |
| quicksand | 4:69df63eeb91e | 165 | else readyStringPtr = readyString; |
| quicksand | 4:69df63eeb91e | 166 | if ( *readyStringPtr == 0 ) { |
| quicksand | 4:69df63eeb91e | 167 | ok = true; |
| quicksand | 4:69df63eeb91e | 168 | } |
| quicksand | 4:69df63eeb91e | 169 | if ( (char)c == *errorStringPtr ) errorStringPtr++; |
| quicksand | 4:69df63eeb91e | 170 | else errorStringPtr = errorString; |
| quicksand | 4:69df63eeb91e | 171 | if ( *errorStringPtr == 0 ) { |
| quicksand | 4:69df63eeb91e | 172 | error = true; |
| quicksand | 4:69df63eeb91e | 173 | } |
| quicksand | 1:897a1b3f0955 | 174 | } |
| quicksand | 1:897a1b3f0955 | 175 | tmout.detach(); |
| quicksand | 4:69df63eeb91e | 176 | if(ser_timeout) return false; |
| quicksand | 4:69df63eeb91e | 177 | else return ok; |
| quicksand | 1:897a1b3f0955 | 178 | } |
QW SIGFOX Development Kit