AMMP demonstration using Freescale FRDM-KL25Z and Adafruit CC3000 Arduino shield

Dependencies:   MMA8451Q NVIC_set_all_priorities TSI cc3000_hostdriver_mbedsocket mbed

Fork of AxedaGo-Freescale by Axeda Corp

Committer:
AxedaCorp
Date:
Thu Jun 26 16:56:32 2014 +0000
Revision:
7:95a9dbe089e2
Parent:
6:aeed0b3ebc34
url update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:fe1445e57b7d 1 /* mbed Microcontroller Library
AxedaCorp 6:aeed0b3ebc34 2 * Copyright (c) 2006-2013 ARM Limited
AxedaCorp 6:aeed0b3ebc34 3 *
AxedaCorp 6:aeed0b3ebc34 4 * Licensed under the Apache License, Version 2.0 (the "License");
AxedaCorp 6:aeed0b3ebc34 5 * you may not use this file except in compliance with the License.
AxedaCorp 6:aeed0b3ebc34 6 * You may obtain a copy of the License at
AxedaCorp 6:aeed0b3ebc34 7 *
AxedaCorp 6:aeed0b3ebc34 8 * http://www.apache.org/licenses/LICENSE-2.0
AxedaCorp 6:aeed0b3ebc34 9 *
AxedaCorp 6:aeed0b3ebc34 10 * Unless required by applicable law or agreed to in writing, software
AxedaCorp 6:aeed0b3ebc34 11 * distributed under the License is distributed on an "AS IS" BASIS,
AxedaCorp 6:aeed0b3ebc34 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AxedaCorp 6:aeed0b3ebc34 13 * See the License for the specific language governing permissions and
AxedaCorp 6:aeed0b3ebc34 14 * limitations under the License.
AxedaCorp 6:aeed0b3ebc34 15 */
Kojto 0:fe1445e57b7d 16 #include "mbed.h"
Kojto 0:fe1445e57b7d 17 #include "cc3000.h"
Kojto 0:fe1445e57b7d 18 #include "main.h"
asmithmd1 4:3c2e21757a9c 19 #include "TSISensor.h"
asmithmd1 4:3c2e21757a9c 20 #include "MMA8451Q.h" //accelerometer
asmithmd1 4:3c2e21757a9c 21 #include "TCPSocketConnection.h"
Kojto 0:fe1445e57b7d 22
Kojto 0:fe1445e57b7d 23
Kojto 0:fe1445e57b7d 24 using namespace mbed_cc3000;
Kojto 0:fe1445e57b7d 25
asmithmd1 4:3c2e21757a9c 26 /* cc3000 module declaration specific for user's board */
asmithmd1 4:3c2e21757a9c 27 #if (MY_BOARD == WIFI_SHEILD_ADAFRUIT)
asmithmd1 4:3c2e21757a9c 28 cc3000 wifi(PTA12, PTA5, PTD0, SPI(PTD2, PTD3, PTD1), PORTA_IRQn);
asmithmd1 4:3c2e21757a9c 29 Serial pc(USBTX, USBRX); // tx, rx
Kojto 0:fe1445e57b7d 30 #else
Kojto 0:fe1445e57b7d 31
Kojto 0:fe1445e57b7d 32 #endif
Kojto 0:fe1445e57b7d 33
Kojto 0:fe1445e57b7d 34 tUserFS user_info;
asmithmd1 4:3c2e21757a9c 35
Kojto 0:fe1445e57b7d 36 char str[512];
Kojto 0:fe1445e57b7d 37
asmithmd1 4:3c2e21757a9c 38 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
asmithmd1 4:3c2e21757a9c 39
asmithmd1 4:3c2e21757a9c 40 DigitalOut rled(LED_RED);
asmithmd1 4:3c2e21757a9c 41 DigitalOut gled(LED_GREEN);
asmithmd1 4:3c2e21757a9c 42 // DO NOT Use LED_BLUE - pin PTD1 already in use by WiFi chip
asmithmd1 4:3c2e21757a9c 43
asmithmd1 4:3c2e21757a9c 44
Kojto 0:fe1445e57b7d 45
Kojto 0:fe1445e57b7d 46 /**
AxedaCorp 6:aeed0b3ebc34 47 * \brief Print cc3000 information
AxedaCorp 6:aeed0b3ebc34 48 * \param none
AxedaCorp 6:aeed0b3ebc34 49 * \return none
AxedaCorp 6:aeed0b3ebc34 50 */
Kojto 0:fe1445e57b7d 51 void print_cc3000_info() {
Kojto 0:fe1445e57b7d 52 uint8_t myMAC[8];
Kojto 0:fe1445e57b7d 53
Kojto 0:fe1445e57b7d 54 printf("MAC address + cc3000 info \r\n");
Kojto 0:fe1445e57b7d 55 wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 0:fe1445e57b7d 56 wifi.get_mac_address(myMAC);
Kojto 0:fe1445e57b7d 57 printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
Kojto 0:fe1445e57b7d 58
Kojto 0:fe1445e57b7d 59 printf(" FTC %i \r\n",user_info.FTC);
Kojto 0:fe1445e57b7d 60 printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]);
Kojto 0:fe1445e57b7d 61 printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]);
Kojto 0:fe1445e57b7d 62 printf(" DRV_VER %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]);
Kojto 0:fe1445e57b7d 63 printf(" FW_VER %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]);
Kojto 0:fe1445e57b7d 64 }
Kojto 0:fe1445e57b7d 65
Kojto 0:fe1445e57b7d 66 /**
AxedaCorp 6:aeed0b3ebc34 67 * \brief Connect to SSID with a timeout
AxedaCorp 6:aeed0b3ebc34 68 * \param ssid Name of SSID
AxedaCorp 6:aeed0b3ebc34 69 * \param key Password
AxedaCorp 6:aeed0b3ebc34 70 * \param sec_mode Security mode
AxedaCorp 6:aeed0b3ebc34 71 * \return none
AxedaCorp 6:aeed0b3ebc34 72 */
Kojto 0:fe1445e57b7d 73 void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) {
Kojto 1:58cd57bddd3a 74 printf("Connecting to SSID: %s. Timeout is 10s. \r\n",ssid);
Kojto 0:fe1445e57b7d 75 if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) {
Kojto 3:ec0169cbc94d 76 printf(" Connected. \r\n");
Kojto 0:fe1445e57b7d 77 } else {
Kojto 1:58cd57bddd3a 78 printf(" Connection timed-out (error). Please restart. \r\n");
Kojto 0:fe1445e57b7d 79 while(1);
Kojto 0:fe1445e57b7d 80 }
Kojto 0:fe1445e57b7d 81 }
Kojto 0:fe1445e57b7d 82
Kojto 0:fe1445e57b7d 83
Kojto 0:fe1445e57b7d 84 /**
AxedaCorp 6:aeed0b3ebc34 85 * \brief HTTP client demo
AxedaCorp 6:aeed0b3ebc34 86 * \param none
AxedaCorp 6:aeed0b3ebc34 87 * \return int
AxedaCorp 6:aeed0b3ebc34 88 */
Kojto 0:fe1445e57b7d 89 int main() {
Kojto 0:fe1445e57b7d 90
asmithmd1 4:3c2e21757a9c 91 rled = 1;
asmithmd1 4:3c2e21757a9c 92 gled = 1;
asmithmd1 4:3c2e21757a9c 93 TSISensor touchSensor;
AxedaCorp 7:95a9dbe089e2 94 const char* SERVER_ADDRESS = "216.34.120.52"; //IP address for toolbox-stage-connect.axeda.com is 216.34.120.53
AxedaCorp 6:aeed0b3ebc34 95 //IP address for toolbox-connect.axeda.com is 216.34.120.52
AxedaCorp 6:aeed0b3ebc34 96 const int SERVER_PORT = 80;
AxedaCorp 5:c99347399fb7 97 char *MODEL = "Freescale";
AxedaCorp 6:aeed0b3ebc34 98 char *SERIAL_NUM = "nlr__jrogers_axeda_com___6243311";
AxedaCorp 6:aeed0b3ebc34 99 int http_cmd_sz=800;
AxedaCorp 6:aeed0b3ebc34 100 char http_cmd[http_cmd_sz];
AxedaCorp 6:aeed0b3ebc34 101 int buffer_sz=300;
AxedaCorp 6:aeed0b3ebc34 102 char buffer[buffer_sz];
asmithmd1 4:3c2e21757a9c 103 int returnCode = 0;
AxedaCorp 6:aeed0b3ebc34 104 int weight=0;
asmithmd1 4:3c2e21757a9c 105 TCPSocketConnection socket;
asmithmd1 4:3c2e21757a9c 106
AxedaCorp 6:aeed0b3ebc34 107
asmithmd1 4:3c2e21757a9c 108 float accels[3];
asmithmd1 4:3c2e21757a9c 109 float resting,reading;
asmithmd1 4:3c2e21757a9c 110 float THRESHOLD = 0.15;
asmithmd1 4:3c2e21757a9c 111 float position;
asmithmd1 4:3c2e21757a9c 112
asmithmd1 4:3c2e21757a9c 113
asmithmd1 4:3c2e21757a9c 114 pc.printf("Starting up...\n");
asmithmd1 4:3c2e21757a9c 115
asmithmd1 4:3c2e21757a9c 116
asmithmd1 4:3c2e21757a9c 117 pc.printf("Before wifi.start...\n");
Kojto 0:fe1445e57b7d 118 wifi.start(0);
Kojto 2:bc6c789cac58 119 printf("cc3000 HTTP client demo. \r\n");
Kojto 0:fe1445e57b7d 120 print_cc3000_info();
Kojto 0:fe1445e57b7d 121
Kojto 0:fe1445e57b7d 122 printf("Attempting SSID Connection. \r\n");
asmithmd1 4:3c2e21757a9c 123
Kojto 0:fe1445e57b7d 124 wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
asmithmd1 4:3c2e21757a9c 125
Kojto 0:fe1445e57b7d 126 connect_to_ssid(SSID, AP_KEY, AP_SECURITY);
asmithmd1 4:3c2e21757a9c 127
asmithmd1 4:3c2e21757a9c 128 printf("send DHCP request \r\n");
asmithmd1 4:3c2e21757a9c 129 gled =0;
asmithmd1 4:3c2e21757a9c 130
asmithmd1 4:3c2e21757a9c 131
Kojto 0:fe1445e57b7d 132 while (wifi.is_dhcp_configured() == false) {
asmithmd1 4:3c2e21757a9c 133
Kojto 0:fe1445e57b7d 134 wait_ms(500);
Kojto 0:fe1445e57b7d 135 printf(" Waiting for dhcp to be set. \r\n");
asmithmd1 4:3c2e21757a9c 136
Kojto 0:fe1445e57b7d 137 }
asmithmd1 4:3c2e21757a9c 138
asmithmd1 4:3c2e21757a9c 139 gled = 1; rled = 0;
Kojto 0:fe1445e57b7d 140
Kojto 0:fe1445e57b7d 141 tNetappIpconfigRetArgs ipinfo2;
Kojto 0:fe1445e57b7d 142 wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
Kojto 0:fe1445e57b7d 143 printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
AxedaCorp 5:c99347399fb7 144 printf(" subnet mask = %d.%d.%d.%d \r\n", ipinfo2.aucSubnetMask[3], ipinfo2.aucSubnetMask[2], ipinfo2.aucSubnetMask[1], ipinfo2.aucSubnetMask[0]);
AxedaCorp 5:c99347399fb7 145 printf(" gateway = %d.%d.%d.%d \r\n", ipinfo2.aucDefaultGateway[3], ipinfo2.aucDefaultGateway[2], ipinfo2.aucDefaultGateway[1], ipinfo2.aucDefaultGateway[0]);
AxedaCorp 5:c99347399fb7 146 printf(" DNS IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucDNSServer[3], ipinfo2.aucDNSServer[2], ipinfo2.aucDNSServer[1], ipinfo2.aucDNSServer[0]);
Kojto 0:fe1445e57b7d 147
AxedaCorp 6:aeed0b3ebc34 148
AxedaCorp 6:aeed0b3ebc34 149
AxedaCorp 6:aeed0b3ebc34 150
asmithmd1 4:3c2e21757a9c 151 wait(0.7);
asmithmd1 4:3c2e21757a9c 152 acc.getAccAllAxis( accels );
asmithmd1 4:3c2e21757a9c 153 resting = abs(accels[0]) + abs(accels[1]) + abs(accels[2]);
asmithmd1 4:3c2e21757a9c 154 printf("Set up resting accelerometer - %.2f\r\n", resting);
asmithmd1 4:3c2e21757a9c 155 wait(0.1);
Kojto 1:58cd57bddd3a 156
AxedaCorp 6:aeed0b3ebc34 157
AxedaCorp 6:aeed0b3ebc34 158
asmithmd1 4:3c2e21757a9c 159 while(1)
Kojto 0:fe1445e57b7d 160 {
Kojto 1:58cd57bddd3a 161
asmithmd1 4:3c2e21757a9c 162 acc.getAccAllAxis( accels );
asmithmd1 4:3c2e21757a9c 163 reading = abs(accels[0]) + abs(accels[1]) + abs(accels[2]);
asmithmd1 4:3c2e21757a9c 164 if ( abs( resting - reading ) > THRESHOLD ){
asmithmd1 4:3c2e21757a9c 165 wait(0.1);
asmithmd1 4:3c2e21757a9c 166 position = touchSensor.readPercentage();
asmithmd1 4:3c2e21757a9c 167 printf( "BUMPED resting %.2f read %.2f position %.2f \r\n", resting, reading, position );
asmithmd1 4:3c2e21757a9c 168 rled = 1;
asmithmd1 4:3c2e21757a9c 169 gled = 1;
asmithmd1 4:3c2e21757a9c 170
asmithmd1 4:3c2e21757a9c 171 printf("Before connect\r\n");
AxedaCorp 6:aeed0b3ebc34 172 //socket.connect(SERVER_ADDRESS, SERVER_PORT);
asmithmd1 4:3c2e21757a9c 173 while ( socket.connect(SERVER_ADDRESS, SERVER_PORT) < 0) {
AxedaCorp 6:aeed0b3ebc34 174 printf("Unable to connect to (%s) on port (%d) \r\n", SERVER_ADDRESS, SERVER_PORT);
asmithmd1 4:3c2e21757a9c 175 wait(1);
asmithmd1 4:3c2e21757a9c 176 printf(" - retring to connect");
asmithmd1 4:3c2e21757a9c 177 }
asmithmd1 4:3c2e21757a9c 178 printf( "connected");
AxedaCorp 6:aeed0b3ebc34 179 weight = position * 300;
AxedaCorp 6:aeed0b3ebc34 180 printf("\n%d\n", weight);
AxedaCorp 6:aeed0b3ebc34 181 snprintf(http_cmd, http_cmd_sz, "POST /ammp/data/1/%s!%s HTTP/1.1\r\nContent-type: application/json\r\nContent-Length: 52\r\n\r\n{\"data\":[{\"dataItems\":{\"weight\":%d}}]}\r\n\r\n", MODEL, SERIAL_NUM, weight);
AxedaCorp 6:aeed0b3ebc34 182 socket.send_all(http_cmd, http_cmd_sz-1);
asmithmd1 4:3c2e21757a9c 183
AxedaCorp 6:aeed0b3ebc34 184 returnCode = socket.receive(buffer, buffer_sz-1);
asmithmd1 4:3c2e21757a9c 185
asmithmd1 4:3c2e21757a9c 186 buffer[returnCode] = '\0';
asmithmd1 4:3c2e21757a9c 187 printf("Sent->%s<-\r\n",http_cmd);
asmithmd1 4:3c2e21757a9c 188 printf("Received %d chars from server:\n\r%s\n", returnCode, buffer);
AxedaCorp 6:aeed0b3ebc34 189
asmithmd1 4:3c2e21757a9c 190 socket.close();
asmithmd1 4:3c2e21757a9c 191 printf("Socket closed\r\n");
asmithmd1 4:3c2e21757a9c 192 rled = 0;
asmithmd1 4:3c2e21757a9c 193 gled = 0;
AxedaCorp 6:aeed0b3ebc34 194
AxedaCorp 6:aeed0b3ebc34 195 //memset null
AxedaCorp 6:aeed0b3ebc34 196 memset(buffer, '\0', buffer_sz);
AxedaCorp 6:aeed0b3ebc34 197 memset(http_cmd, '\0', http_cmd_sz);
asmithmd1 4:3c2e21757a9c 198 }
AxedaCorp 6:aeed0b3ebc34 199
Kojto 0:fe1445e57b7d 200 }
asmithmd1 4:3c2e21757a9c 201
Kojto 0:fe1445e57b7d 202 }