This software example demonstrates the downlink capabilities of the SIGFOX network.
Dependencies: QW_Sensors mbed
Fork of HelloWorld - QW Development kit by
Revision 1:897a1b3f0955, committed 2016-04-18
- Comitter:
- quicksand
- Date:
- Mon Apr 18 10:44:39 2016 +0000
- Parent:
- 0:49858c4c3500
- Child:
- 2:a4a68a858624
- Commit message:
- This example now sends the measured temperature along with the button press!
Changed in this revision
| QW_Sensors.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QW_Sensors.lib Mon Apr 18 10:44:39 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/LievenHollevoet/code/QW_Sensors/#7fb6f774bf68
--- a/main.cpp Fri Oct 30 14:10:26 2015 +0000
+++ b/main.cpp Mon Apr 18 10:44:39 2016 +0000
@@ -1,84 +1,171 @@
#include "mbed.h"
+#include "math.h"
+#include "LinearTempSensor.h"
+/* The 4 onboard LEDs */
DigitalOut LED_0 (PB_6);
DigitalOut LED_1 (PA_7);
DigitalOut LED_2 (PA_6);
DigitalOut LED_3 (PA_5);
-InterruptIn SW1(PB_10);
-InterruptIn SW2(PA_8);
-Ticker hartbeat;
+/* The 2 user buttons */
+InterruptIn SW1(PA_8);
+InterruptIn SW2(PB_10);
+
+/*Temperature sensor */
+LinearTempSensor sensor(PA_0, 40);
-typedef struct{
- bool busy;
- char button;
-} TX_BUTTON;
-
-TX_BUTTON Button_tx;
+/* Function prototypes */
+void sw1interrupt();
+void sw2interrupt();
+void beat();
+void sertmout();
+bool modem_command_check_ok(char * command);
+void modem_setup();
+void txData(uint8_t btn);
-void beat() {
- LED_0 = !LED_0;
-}
-
-//Virtual serial port over USB
+bool ser_timeout = false;
+
+Ticker heartbeat;
+
+/* Serial port over USB */
Serial pc(USBTX, USBRX);
+
+/* Serial connection to sigfox modem */
Serial modem(PA_9, PA_10);
-void sw1interrupt(){
- pc.printf("Button 1 pressed, sending sigfox message (command is AT$SF=42 55 54 54 4f 4e 20 31 00 00 00 00,2,0)\n");
- modem.printf("AT$SF=42 55 54 54 4f 4e 20 31 00 00 00 00,2,0\n");
- LED_1 = 0;
- wait(0.25);
- // Flush the echo of the command:
- while(modem.readable()) modem.getc();
- Button_tx.busy = true;
- Button_tx.button = 1;
-}
+
+int main()
+{
-void sw2interrupt(){
- pc.printf("Button 2 pressed, sending sigfox message (command is AT$SF=42 55 54 54 4f 4e 20 32 00 00 00 00,2,0)\n");
- modem.printf("AT$SF=42 55 54 54 4f 4e 20 32 00 00 00 00,2,0\n");
- LED_2 = 0;
- wait(0.25);
- // Flush the echo of the command:
- while(modem.readable()) modem.getc();
- Button_tx.busy = true;
- Button_tx.button = 2;
-}
+ /* Setup TD120x */
+ wait(3);
+ modem_setup();
+ /* Test temperature sensor */
+ float vOut = sensor.Sense();
+ pc.printf("\n\rMCP9700 reading: Vout: %.2f mV", vOut);
-int main() {
-
+ /* Turn off all LED */
LED_0 = 1;
LED_1 = 1;
LED_2 = 1;
LED_3 = 1;
- hartbeat.attach(&beat, 0.5);
- Button_tx.busy = false;
- Button_tx.button = 0;
- SW2.fall(&sw1interrupt);
- SW1.fall(&sw2interrupt);
- char responsebuffer[2];
- char c;
- while(1) {
+
+ /* Blinking LED */
+ heartbeat.attach(&beat, 0.5);
+
+ /* Setup button interrupts */
+ SW2.fall(&sw2interrupt);
+ SW1.fall(&sw1interrupt);
+
+ while(1) {
if(pc.readable()) {
modem.putc(pc.getc());
}
-
if(modem.readable()) {
- c = modem.getc();
- responsebuffer[0] = responsebuffer[1];
- responsebuffer[1] = c;
- if(Button_tx.busy)
- {
- if(responsebuffer[0] == 'O' && responsebuffer[1] == 'K' )
- {
- // Everything went fine, turn off the LED
- Button_tx.busy = false;
- if(Button_tx.button == 1) LED_1 = 1;
- if(Button_tx.button == 2) LED_2 = 1;
- }
- }
- pc.putc(c);
+ pc.putc(modem.getc());
}
}
}
+
+void beat()
+{
+ LED_0 = !LED_0;
+}
+
+/* Button 1 ISR */
+void sw1interrupt()
+{
+ pc.printf("\n\rButton 1 pressed\n\r");
+ LED_1 = 0;
+ txData(1);
+}
+
+/* Button 2 ISR */
+void sw2interrupt()
+{
+ pc.printf("\n\rButton 2 pressed\n\r");
+ LED_2 = 0;
+ txData(2);
+}
+
+
+/* TX data over Sigfox */
+void txData (uint8_t btn)
+{
+
+ float tAvg;
+ char command[32];
+ tAvg = sensor.GetAverageTemp();
+ char temperature[6] ="";
+ sprintf(temperature, "%3.1f", tAvg);
+ for(int i = 0; i < 5; i++)
+ if(temperature[i]==0) temperature[i] = ' ';
+ sprintf(command, "AT$SF=42544e%x20%x%x%x%x%x43,2,0\n", btn+48, temperature[0],temperature[1],temperature[2],temperature[3],temperature[4]);
+ pc.printf("Sending pressed button %d and temperature %s C over Sigfox.\n", btn, temperature);
+ pc.printf("using modem command: %s", command);
+ modem_command_check_ok(command);
+ LED_1 = 1;
+ LED_2 = 1;
+}
+
+void modem_setup()
+{
+ /* Reset to factory defaults */
+ if(modem_command_check_ok("AT&F")) {
+ pc.printf("Factory reset succesfull\r\n");
+ } else {
+ pc.printf("Factory reset TD120x failed\r\n");
+ }
+ /* Disable local echo */
+ modem.printf("ATE0\n");
+ if(modem_command_check_ok("ATE0")) {
+ pc.printf("Local echo disabled\r\n");
+ }
+ /* Write to mem */
+ if(modem_command_check_ok("AT&W")) {
+ pc.printf("Settings saved!\r\n");
+ }
+}
+
+/* ISR for serial timeout */
+void sertmout()
+{
+ ser_timeout = true;
+}
+
+bool modem_command_check_ok(char * command)
+{
+ /* first clear serial data buffers */
+ while(modem.readable()) modem.getc();
+ /* Timeout for response of the modem */
+ Timeout tmout;
+ ser_timeout = false;
+ /* Buffer for incoming data */
+ char responsebuffer[6];
+ /* Flag to set when we get 'OK' response */
+ bool ok = false;
+ bool error = false;
+ /* Print command to TD120x */
+ modem.printf(command);
+ /* Newline to activate command */
+ modem.printf("\n");
+ /* Wait untill serial feedback, max 3 seconds before timeout */
+ tmout.attach(&sertmout, 3.0);
+ while(!modem.readable()&& ser_timeout == false);
+ while(!ok && !ser_timeout && !error) {
+ if(modem.readable()) {
+ for(int i = 0; i < 5; i++) {
+ responsebuffer[i] = responsebuffer[i+1];
+ }
+ responsebuffer[5] = modem.getc();
+ if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' ) {
+ ok = true;
+ } else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' ) {
+ error = true;
+ }
+ }
+ }
+ tmout.detach();
+ return ok;
+}
\ No newline at end of file
