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