![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A simple project for sending button input up to AT&T Flow
Dependencies: FXOS8700CQ MODSERIAL mbed
For instructions on using this program, see the AT&T Starter Kit tutorial on it.
main.cpp@71:bcbc072b85e1, 2016-08-12 (annotated)
- Committer:
- rfinn
- Date:
- Fri Aug 12 17:18:09 2016 +0000
- Revision:
- 71:bcbc072b85e1
- Parent:
- 70:e25fe7e5488a
updated comment;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rfinn | 68:d38834307b17 | 1 | #include "mbed.h" |
JMF | 0:9d5134074d84 | 2 | #include <cctype> |
JMF | 0:9d5134074d84 | 3 | #include <string> |
JMF | 2:0e2ef866af95 | 4 | #include "config_me.h" |
stefanrousseau | 61:f6b93129f954 | 5 | #include "cell_modem.h" |
stefanrousseau | 11:e6602513730f | 6 | #include "hardware.h" |
stefanrousseau | 11:e6602513730f | 7 | |
stefanrousseau | 61:f6b93129f954 | 8 | I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used |
stefanrousseau | 56:cb42ff383dab | 9 | MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes |
stefanrousseau | 63:90d7c69993cd | 10 | MODSERIAL mdm(PTD3, PTD2, 4096, 4096); |
stefanrousseau | 16:17c5916f2d12 | 11 | DigitalOut led_green(LED_GREEN); |
stefanrousseau | 16:17c5916f2d12 | 12 | DigitalOut led_red(LED_RED); |
stefanrousseau | 16:17c5916f2d12 | 13 | DigitalOut led_blue(LED_BLUE); |
JMF | 0:9d5134074d84 | 14 | |
rfinn | 68:d38834307b17 | 15 | // interrupts for buttons |
rfinn | 68:d38834307b17 | 16 | InterruptIn sw3(SW3); |
rfinn | 68:d38834307b17 | 17 | InterruptIn sw2(SW2); |
JMF | 0:9d5134074d84 | 18 | |
rfinn | 68:d38834307b17 | 19 | // flag - must be volatile as changes within ISR |
rfinn | 68:d38834307b17 | 20 | // g_ prefix makes it easier to distinguish it as global |
rfinn | 68:d38834307b17 | 21 | volatile int g_sw3_flag = 0; |
rfinn | 68:d38834307b17 | 22 | volatile int g_sw2_flag = 0; |
rfinn | 68:d38834307b17 | 23 | |
rfinn | 68:d38834307b17 | 24 | // SW2 event-triggered interrupt |
rfinn | 71:bcbc072b85e1 | 25 | // ISR = interrupt service routine |
rfinn | 71:bcbc072b85e1 | 26 | // down = button was pushed down/falling |
rfinn | 71:bcbc072b85e1 | 27 | // up = button was released/rising |
rfinn | 68:d38834307b17 | 28 | void sw2_isr_down() |
stefanrousseau | 3:26b3cc155f39 | 29 | { |
rfinn | 68:d38834307b17 | 30 | g_sw2_flag = 1; // set flag in ISR |
rfinn | 68:d38834307b17 | 31 | } |
stefanrousseau | 12:7c94ec5069dc | 32 | |
rfinn | 68:d38834307b17 | 33 | void sw2_isr_up() |
stefanrousseau | 3:26b3cc155f39 | 34 | { |
rfinn | 68:d38834307b17 | 35 | g_sw2_flag = 0; |
rfinn | 68:d38834307b17 | 36 | } |
rfinn | 68:d38834307b17 | 37 | |
rfinn | 68:d38834307b17 | 38 | // SW3 event-triggered interrupt |
rfinn | 68:d38834307b17 | 39 | void sw3_isr_down() |
rfinn | 68:d38834307b17 | 40 | { |
rfinn | 68:d38834307b17 | 41 | g_sw3_flag = 1; // set flag in ISR |
rfinn | 68:d38834307b17 | 42 | } |
rfinn | 68:d38834307b17 | 43 | |
rfinn | 68:d38834307b17 | 44 | void sw3_isr_up() |
stefanrousseau | 3:26b3cc155f39 | 45 | { |
rfinn | 68:d38834307b17 | 46 | g_sw3_flag = 0; |
rfinn | 68:d38834307b17 | 47 | } |
rfinn | 68:d38834307b17 | 48 | |
rfinn | 68:d38834307b17 | 49 | void GenerateModemString(char * modem_string, char btn1, char btn2) |
rfinn | 68:d38834307b17 | 50 | { |
rfinn | 68:d38834307b17 | 51 | sprintf(modem_string, "serial:%s button1:%c button2:%c\r\n", |
rfinn | 68:d38834307b17 | 52 | M2X_DEVICE_SERIAL, btn1, btn2 |
rfinn | 68:d38834307b17 | 53 | ); |
rfinn | 70:e25fe7e5488a | 54 | |
rfinn | 70:e25fe7e5488a | 55 | // HTTP method |
rfinn | 70:e25fe7e5488a | 56 | /*sprintf(modem_string, "GET %s%s?serial=%s&button1=%c&button2=%c %s%s\r\n\r\n", |
rfinn | 70:e25fe7e5488a | 57 | FLOW_BASE_PATH, FLOW_INPUT_NAME, M2X_DEVICE_NAME, |
rfinn | 70:e25fe7e5488a | 58 | btn1, btn2, |
rfinn | 70:e25fe7e5488a | 59 | FLOW_URL_TYPE, MY_SERVER_NAME); */ |
rfinn | 70:e25fe7e5488a | 60 | |
rfinn | 68:d38834307b17 | 61 | } //GenerateModemString |
rfinn | 68:d38834307b17 | 62 | |
stefanrousseau | 3:26b3cc155f39 | 63 | |
stefanrousseau | 16:17c5916f2d12 | 64 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 65 | //* Set the RGB LED's Color |
rfinn | 68:d38834307b17 | 66 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
stefanrousseau | 16:17c5916f2d12 | 67 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 68 | void SetLedColor(unsigned char ucColor) |
stefanrousseau | 16:17c5916f2d12 | 69 | { |
stefanrousseau | 16:17c5916f2d12 | 70 | //Note that when an LED is on, you write a 0 to it: |
stefanrousseau | 16:17c5916f2d12 | 71 | led_red = !(ucColor & 0x1); //bit 0 |
stefanrousseau | 16:17c5916f2d12 | 72 | led_green = !(ucColor & 0x2); //bit 1 |
stefanrousseau | 16:17c5916f2d12 | 73 | led_blue = !(ucColor & 0x4); //bit 2 |
stefanrousseau | 16:17c5916f2d12 | 74 | } //SetLedColor() |
stefanrousseau | 16:17c5916f2d12 | 75 | |
stefanrousseau | 16:17c5916f2d12 | 76 | //******************************************************************************************************************************************** |
rfinn | 68:d38834307b17 | 77 | //* Process the JSON response. In this example we are only extracting a LED color. |
stefanrousseau | 16:17c5916f2d12 | 78 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 79 | bool parse_JSON(char* json_string) |
stefanrousseau | 16:17c5916f2d12 | 80 | { |
stefanrousseau | 16:17c5916f2d12 | 81 | char* beginquote; |
stefanrousseau | 16:17c5916f2d12 | 82 | char token[] = "\"LED\":\""; |
stefanrousseau | 16:17c5916f2d12 | 83 | beginquote = strstr(json_string, token ); |
rfinn | 68:d38834307b17 | 84 | if ((beginquote != 0)) { |
stefanrousseau | 16:17c5916f2d12 | 85 | char cLedColor = beginquote[strlen(token)]; |
stefanrousseau | 64:09004cd610df | 86 | PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor); |
rfinn | 68:d38834307b17 | 87 | switch(cLedColor) { |
rfinn | 68:d38834307b17 | 88 | case 'O': { |
rfinn | 68:d38834307b17 | 89 | //Off |
stefanrousseau | 16:17c5916f2d12 | 90 | SetLedColor(0); |
stefanrousseau | 16:17c5916f2d12 | 91 | break; |
stefanrousseau | 16:17c5916f2d12 | 92 | } |
rfinn | 68:d38834307b17 | 93 | case 'R': { |
rfinn | 68:d38834307b17 | 94 | //Red |
stefanrousseau | 16:17c5916f2d12 | 95 | SetLedColor(1); |
stefanrousseau | 16:17c5916f2d12 | 96 | break; |
stefanrousseau | 16:17c5916f2d12 | 97 | } |
rfinn | 68:d38834307b17 | 98 | case 'G': { |
rfinn | 68:d38834307b17 | 99 | //Green |
stefanrousseau | 16:17c5916f2d12 | 100 | SetLedColor(2); |
stefanrousseau | 16:17c5916f2d12 | 101 | break; |
stefanrousseau | 16:17c5916f2d12 | 102 | } |
rfinn | 68:d38834307b17 | 103 | case 'Y': { |
rfinn | 68:d38834307b17 | 104 | //Yellow |
stefanrousseau | 16:17c5916f2d12 | 105 | SetLedColor(3); |
stefanrousseau | 16:17c5916f2d12 | 106 | break; |
stefanrousseau | 16:17c5916f2d12 | 107 | } |
rfinn | 68:d38834307b17 | 108 | case 'B': { |
rfinn | 68:d38834307b17 | 109 | //Blue |
stefanrousseau | 16:17c5916f2d12 | 110 | SetLedColor(4); |
stefanrousseau | 16:17c5916f2d12 | 111 | break; |
stefanrousseau | 16:17c5916f2d12 | 112 | } |
rfinn | 68:d38834307b17 | 113 | case 'M': { |
rfinn | 68:d38834307b17 | 114 | //Magenta |
stefanrousseau | 16:17c5916f2d12 | 115 | SetLedColor(5); |
stefanrousseau | 16:17c5916f2d12 | 116 | break; |
stefanrousseau | 16:17c5916f2d12 | 117 | } |
rfinn | 68:d38834307b17 | 118 | case 'T': { |
rfinn | 68:d38834307b17 | 119 | //Turquoise |
stefanrousseau | 16:17c5916f2d12 | 120 | SetLedColor(6); |
stefanrousseau | 16:17c5916f2d12 | 121 | break; |
stefanrousseau | 16:17c5916f2d12 | 122 | } |
rfinn | 68:d38834307b17 | 123 | case 'W': { |
rfinn | 68:d38834307b17 | 124 | //White |
stefanrousseau | 16:17c5916f2d12 | 125 | SetLedColor(7); |
stefanrousseau | 16:17c5916f2d12 | 126 | break; |
stefanrousseau | 16:17c5916f2d12 | 127 | } |
rfinn | 68:d38834307b17 | 128 | default: { |
stefanrousseau | 16:17c5916f2d12 | 129 | break; |
stefanrousseau | 16:17c5916f2d12 | 130 | } |
stefanrousseau | 16:17c5916f2d12 | 131 | } //switch(cLedColor) |
stefanrousseau | 16:17c5916f2d12 | 132 | return true; |
rfinn | 68:d38834307b17 | 133 | } else { |
stefanrousseau | 16:17c5916f2d12 | 134 | return false; |
stefanrousseau | 16:17c5916f2d12 | 135 | } |
stefanrousseau | 16:17c5916f2d12 | 136 | } //parse_JSON |
stefanrousseau | 16:17c5916f2d12 | 137 | |
rfinn | 68:d38834307b17 | 138 | void send_button_data(char btn1, char btn2) |
rfinn | 68:d38834307b17 | 139 | { |
rfinn | 68:d38834307b17 | 140 | char modem_string[512]; |
rfinn | 68:d38834307b17 | 141 | GenerateModemString(&modem_string[0], btn1, btn2); |
rfinn | 68:d38834307b17 | 142 | char myJsonResponse[512]; |
rfinn | 68:d38834307b17 | 143 | if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0])) { |
rfinn | 68:d38834307b17 | 144 | if (true) { |
rfinn | 68:d38834307b17 | 145 | //ledOnce = 1; |
rfinn | 68:d38834307b17 | 146 | SetLedColor(0x2); //Green |
rfinn | 68:d38834307b17 | 147 | } |
rfinn | 68:d38834307b17 | 148 | parse_JSON(&myJsonResponse[0]); |
rfinn | 68:d38834307b17 | 149 | } |
rfinn | 68:d38834307b17 | 150 | } |
rfinn | 68:d38834307b17 | 151 | |
rfinn | 68:d38834307b17 | 152 | int main() |
rfinn | 68:d38834307b17 | 153 | { |
rfinn | 68:d38834307b17 | 154 | //static unsigned ledOnce = 0; |
fkellermavnet | 20:27a4f27254d0 | 155 | |
stefanrousseau | 61:f6b93129f954 | 156 | pc.baud(115200); |
rfinn | 68:d38834307b17 | 157 | PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n"); |
rfinn | 68:d38834307b17 | 158 | PRINTF(GRN "Cellular IoT Buttons!\r\n\r\n"); |
JMF | 0:9d5134074d84 | 159 | |
rfinn | 68:d38834307b17 | 160 | // SW have a pull-up resistor, so the pin will be at 3.3 V by default |
rfinn | 68:d38834307b17 | 161 | // and fall to 0 V when pressed. We therefore need to look for a falling edge |
rfinn | 68:d38834307b17 | 162 | // on the pin to fire the interrupt |
rfinn | 68:d38834307b17 | 163 | sw3.fall(&sw3_isr_down); |
rfinn | 68:d38834307b17 | 164 | sw3.rise(&sw3_isr_up); |
rfinn | 68:d38834307b17 | 165 | sw2.fall(&sw2_isr_down); |
rfinn | 68:d38834307b17 | 166 | sw2.rise(&sw2_isr_up); |
rfinn | 68:d38834307b17 | 167 | // since SW have an external pull-up, we should disable to internal pull-down |
rfinn | 68:d38834307b17 | 168 | // resistor that is enabled by default using InterruptIn |
rfinn | 68:d38834307b17 | 169 | sw3.mode(PullNone); |
rfinn | 68:d38834307b17 | 170 | sw2.mode(PullNone); |
stefanrousseau | 11:e6602513730f | 171 | |
stefanrousseau | 61:f6b93129f954 | 172 | // Set LED to RED until init finishes |
stefanrousseau | 61:f6b93129f954 | 173 | SetLedColor(0x1); //Red |
JMF | 0:9d5134074d84 | 174 | // Initialize the modem |
stefanrousseau | 64:09004cd610df | 175 | PRINTF("\r\n"); |
stefanrousseau | 61:f6b93129f954 | 176 | cell_modem_init(); |
stefanrousseau | 61:f6b93129f954 | 177 | // Set LED BLUE for partial init |
stefanrousseau | 61:f6b93129f954 | 178 | SetLedColor(0x4); //Blue |
JMF | 0:9d5134074d84 | 179 | |
JMF | 2:0e2ef866af95 | 180 | // Send and receive data perpetually |
JMF | 2:0e2ef866af95 | 181 | while(1) { |
rfinn | 68:d38834307b17 | 182 | if (g_sw3_flag) { |
rfinn | 68:d38834307b17 | 183 | printf("%s\n","SW3 Button"); |
rfinn | 68:d38834307b17 | 184 | led_green = 0; |
rfinn | 68:d38834307b17 | 185 | send_button_data('0','1'); |
rfinn | 68:d38834307b17 | 186 | } else { |
rfinn | 68:d38834307b17 | 187 | led_green = 1; |
rfinn | 68:d38834307b17 | 188 | } |
rfinn | 68:d38834307b17 | 189 | |
rfinn | 68:d38834307b17 | 190 | if (g_sw2_flag) { |
rfinn | 68:d38834307b17 | 191 | printf("%s\n","SW2 Button"); |
rfinn | 68:d38834307b17 | 192 | led_blue = 0; |
rfinn | 68:d38834307b17 | 193 | send_button_data('1','0'); |
rfinn | 68:d38834307b17 | 194 | } else { |
rfinn | 68:d38834307b17 | 195 | led_blue = 1; |
rfinn | 68:d38834307b17 | 196 | } |
rfinn | 68:d38834307b17 | 197 | |
rfinn | 68:d38834307b17 | 198 | // put the MCU to sleep until an interrupt wakes it up |
rfinn | 68:d38834307b17 | 199 | sleep(); |
stefanrousseau | 3:26b3cc155f39 | 200 | } //forever loop |
JMF | 0:9d5134074d84 | 201 | } |