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