Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z revert by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 15:59:38 2014 +0000
Revision:
1:5ad12c581db4
Parent:
0:65004368569c
url ip switch
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 #include "mbed.h"
AxedaCorp 0:65004368569c 2 #include "MTSSerial.h"
AxedaCorp 0:65004368569c 3 #include "Wifi.h"
AxedaCorp 0:65004368569c 4 #include "TSISensor.h"
AxedaCorp 0:65004368569c 5 #include "MMA8451Q.h"
AxedaCorp 0:65004368569c 6 #include "MAG3110.h"
AxedaCorp 0:65004368569c 7 #include "USBHostCam.h"
AxedaCorp 0:65004368569c 8 #include "axToolkit.h"
AxedaCorp 0:65004368569c 9
AxedaCorp 0:65004368569c 10 /*********************************************/
AxedaCorp 0:65004368569c 11 /* In the following lines please input: */
AxedaCorp 0:65004368569c 12 /* on line 17: your serial number */
AxedaCorp 0:65004368569c 13 /* on line 18: your wifi SSID */
AxedaCorp 0:65004368569c 14 /* on line 19: your wifi password/key */
AxedaCorp 0:65004368569c 15 /* on line 20: your security type (WPA... */
AxedaCorp 0:65004368569c 16 /*********************************************/
AxedaCorp 0:65004368569c 17
AxedaCorp 0:65004368569c 18 #define YOUR_SERIAL_NUM "your_serial_here_123"
AxedaCorp 0:65004368569c 19 #define SSID "yourWifi"
AxedaCorp 0:65004368569c 20 #define AP_Security "SECURITYKEYHERE"
AxedaCorp 0:65004368569c 21 #define AP_Key WPA2 //security type examples: NONE, WEP64, WEP128, WPA, WPA2
AxedaCorp 0:65004368569c 22
AxedaCorp 0:65004368569c 23
AxedaCorp 0:65004368569c 24 /*********************************************/
AxedaCorp 0:65004368569c 25 /*Axeda connections */
AxedaCorp 0:65004368569c 26 /*********************************************/
AxedaCorp 0:65004368569c 27
AxedaCorp 0:65004368569c 28 #define MODEL "Freescale46" //using Freescale as a placeholder
AxedaCorp 1:5ad12c581db4 29 #define SERVER_NAME "216.34.120.53" //IP = 216.34.120.53/52 stage/public URL=toolbox-stage-connect.axeda.com
AxedaCorp 0:65004368569c 30 #define PORT_NUM 80
AxedaCorp 0:65004368569c 31
AxedaCorp 0:65004368569c 32
AxedaCorp 0:65004368569c 33 using namespace mts;
AxedaCorp 0:65004368569c 34
AxedaCorp 0:65004368569c 35 //Hardware Support Stuff
AxedaCorp 0:65004368569c 36 // Include support for on-board green and red LEDs
AxedaCorp 0:65004368569c 37 #define LED_ON 0
AxedaCorp 0:65004368569c 38 #define LED_OFF 1
AxedaCorp 0:65004368569c 39
AxedaCorp 0:65004368569c 40 DigitalOut greenLED(LED_GREEN);
AxedaCorp 0:65004368569c 41 DigitalOut redLED(LED_RED);
AxedaCorp 0:65004368569c 42 // Include support for onboard pushbuttons (value = 0 when pressed)
AxedaCorp 0:65004368569c 43 DigitalIn sw1(PTC3);
AxedaCorp 0:65004368569c 44 DigitalIn sw3(PTC12);
AxedaCorp 0:65004368569c 45 TSISensor slider;
AxedaCorp 0:65004368569c 46 AnalogIn lightSense(PTE22);
AxedaCorp 0:65004368569c 47 #define MMA8451_I2C_ADDRESS (0x1d<<1)
AxedaCorp 0:65004368569c 48 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
AxedaCorp 0:65004368569c 49 MAG3110 mag(PTE25, PTE24);
AxedaCorp 0:65004368569c 50 //Camera support
AxedaCorp 0:65004368569c 51 /*
AxedaCorp 0:65004368569c 52 #if defined(TARGET_KL46Z)
AxedaCorp 0:65004368569c 53 uint8_t image_buf[1024*24];
AxedaCorp 0:65004368569c 54 #elif defined(TARGET_KL25Z)
AxedaCorp 0:65004368569c 55 uint8_t image_buf[1024*12];
AxedaCorp 0:65004368569c 56 #endif */
AxedaCorp 0:65004368569c 57
AxedaCorp 0:65004368569c 58
AxedaCorp 0:65004368569c 59 int main()
AxedaCorp 0:65004368569c 60 {
AxedaCorp 0:65004368569c 61 TSISensor touchSensor;
AxedaCorp 0:65004368569c 62 //float accels[3];
AxedaCorp 0:65004368569c 63 //float resting;
AxedaCorp 0:65004368569c 64 int SEVERITY;
AxedaCorp 0:65004368569c 65 //int HURT_BUTTON_STATE=0;
AxedaCorp 0:65004368569c 66 //int OK_BUTTON_STATE=0;
AxedaCorp 0:65004368569c 67 double status=0;
AxedaCorp 0:65004368569c 68
AxedaCorp 0:65004368569c 69 //Some light I/O setup:
AxedaCorp 0:65004368569c 70 redLED=LED_OFF;
AxedaCorp 0:65004368569c 71 greenLED=LED_OFF;
AxedaCorp 0:65004368569c 72 // Turn on pull up resistors on pushbutton inputs
AxedaCorp 0:65004368569c 73 sw1.mode(PullUp);
AxedaCorp 0:65004368569c 74 sw3.mode(PullUp);
AxedaCorp 0:65004368569c 75
AxedaCorp 0:65004368569c 76 //Setup the Camera:
AxedaCorp 0:65004368569c 77 /* int size=0;
AxedaCorp 0:65004368569c 78 USBHostCam* cam = new USBHostCam(_320x240);
AxedaCorp 0:65004368569c 79 if (!cam->connect()) {
AxedaCorp 0:65004368569c 80 error("WebCam not found.\n");
AxedaCorp 0:65004368569c 81 greenLED=LED_ON;
AxedaCorp 0:65004368569c 82 }
AxedaCorp 0:65004368569c 83 else { redLED=LED_ON; } */
AxedaCorp 0:65004368569c 84
AxedaCorp 0:65004368569c 85 //Set the network parameters
AxedaCorp 0:65004368569c 86 std::string ssid = SSID;
AxedaCorp 0:65004368569c 87 std::string securityKey = AP_Security;
AxedaCorp 0:65004368569c 88 Wifi::SecurityType securityType = Wifi::AP_Key;
AxedaCorp 0:65004368569c 89
AxedaCorp 0:65004368569c 90 //Wait for wifi module to boot up
AxedaCorp 0:65004368569c 91 for (int i = 10; i >= 0; i = i - 2) {
AxedaCorp 0:65004368569c 92 wait(2);
AxedaCorp 0:65004368569c 93 printf("Waiting %d seconds...\n\r", i);
AxedaCorp 0:65004368569c 94 }
AxedaCorp 0:65004368569c 95
AxedaCorp 0:65004368569c 96 //Setup serial interface to WiFi module
AxedaCorp 0:65004368569c 97 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
AxedaCorp 0:65004368569c 98 serial->baud(9600);
AxedaCorp 0:65004368569c 99
AxedaCorp 0:65004368569c 100 //Setup Wifi class
AxedaCorp 0:65004368569c 101 Wifi* wifi = Wifi::getInstance();
AxedaCorp 0:65004368569c 102 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");
AxedaCorp 0:65004368569c 103 wifi->sendBasicCommand("set comm size 1460", 500, CR);
AxedaCorp 0:65004368569c 104 wifi->sendBasicCommand("set comm time 6000", 500, CR);
AxedaCorp 0:65004368569c 105 // wifi->sendBasicCommand("set comm match "+EOT, 3, CR);
AxedaCorp 0:65004368569c 106 //Setup and check connection
AxedaCorp 0:65004368569c 107 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str());
AxedaCorp 0:65004368569c 108 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
AxedaCorp 0:65004368569c 109 while (! wifi->connect()) {
AxedaCorp 0:65004368569c 110 printf("Connect: Failure\r\n");
AxedaCorp 0:65004368569c 111 wait(1);
AxedaCorp 0:65004368569c 112 }
AxedaCorp 0:65004368569c 113 printf("Connected To Wifi!");
AxedaCorp 0:65004368569c 114
AxedaCorp 0:65004368569c 115 //Now we setup the Axeda Service:
AxedaCorp 0:65004368569c 116 ax_platform sbox;
AxedaCorp 0:65004368569c 117 ax_deviceID thisDevice;
AxedaCorp 0:65004368569c 118 ax_dataSet *ds[1];
AxedaCorp 0:65004368569c 119 // ax_file pic;
AxedaCorp 0:65004368569c 120
AxedaCorp 0:65004368569c 121 ax_createPlatform(&sbox, SERVER_NAME, NULL, PORT_NUM);
AxedaCorp 0:65004368569c 122 ax_data_createModelSerialDeviceId(&thisDevice, MODEL, YOUR_SERIAL_NUM, NULL);
AxedaCorp 0:65004368569c 123 // ax_createFile(&pic, "mypic.jpg", "pic", sizeof(image_buf), (unsigned char*)&image_buf);
AxedaCorp 0:65004368569c 124
AxedaCorp 0:65004368569c 125 /*wait(0.7);
AxedaCorp 0:65004368569c 126 acc.getAccAllAxis( accels );
AxedaCorp 0:65004368569c 127 resting = abs(accels[0]) + abs(accels[1]) + abs(accels[2]);
AxedaCorp 0:65004368569c 128 printf("Set up resting accelerometer - %.2f\r\n", resting);
AxedaCorp 0:65004368569c 129 wait(0.1);*/
AxedaCorp 0:65004368569c 130
AxedaCorp 0:65004368569c 131 //Ping
AxedaCorp 0:65004368569c 132 while (true) {
AxedaCorp 0:65004368569c 133 ds[0]=(ax_dataSet *)malloc(sizeof(ax_dataSet));
AxedaCorp 0:65004368569c 134 printf("\n\n\r ready \n\n");
AxedaCorp 0:65004368569c 135 SEVERITY=0;
AxedaCorp 0:65004368569c 136 //ax_data_createSet(ds[0], 0, AX_NO_PRIORITY);
AxedaCorp 0:65004368569c 137 //HURT_BUTTON_STATE=sw1.mode(PullUp);
AxedaCorp 0:65004368569c 138 //OK_BUTTON_STATE=sw3.mode(PullUp);
AxedaCorp 0:65004368569c 139 sw3.mode(PullUp);
AxedaCorp 0:65004368569c 140 sw1.mode(PullUp);
AxedaCorp 0:65004368569c 141 while(sw3&&sw1){
AxedaCorp 0:65004368569c 142 status=0;
AxedaCorp 0:65004368569c 143 greenLED=LED_ON;
AxedaCorp 0:65004368569c 144 wait(0.1);
AxedaCorp 0:65004368569c 145 greenLED=LED_OFF;
AxedaCorp 0:65004368569c 146 wait(0.1);
AxedaCorp 0:65004368569c 147 if (!sw3){
AxedaCorp 0:65004368569c 148 status=2;
AxedaCorp 0:65004368569c 149 }
AxedaCorp 0:65004368569c 150 else{
AxedaCorp 0:65004368569c 151 status=1;
AxedaCorp 0:65004368569c 152 }
AxedaCorp 0:65004368569c 153 }
AxedaCorp 0:65004368569c 154
AxedaCorp 0:65004368569c 155 //SEND button push (hurt or ok)
AxedaCorp 0:65004368569c 156 ax_data_addAnalogToSet(ds[0], "status", status);
AxedaCorp 0:65004368569c 157
AxedaCorp 0:65004368569c 158 while(SEVERITY<=1){
AxedaCorp 0:65004368569c 159 SEVERITY=100*touchSensor.readPercentage();
AxedaCorp 0:65004368569c 160 redLED=LED_ON;
AxedaCorp 0:65004368569c 161 wait(0.1);
AxedaCorp 0:65004368569c 162 redLED=LED_OFF;
AxedaCorp 0:65004368569c 163 wait(0.1);
AxedaCorp 0:65004368569c 164
AxedaCorp 0:65004368569c 165 }
AxedaCorp 0:65004368569c 166
AxedaCorp 0:65004368569c 167 SEVERITY=100*touchSensor.readPercentage();
AxedaCorp 0:65004368569c 168
AxedaCorp 0:65004368569c 169 printf("\n\r%d\n",SEVERITY);
AxedaCorp 0:65004368569c 170 //Send data about severity (left to right position on touchpad)
AxedaCorp 0:65004368569c 171 ax_data_addAnalogToSet(ds[0], "severity",SEVERITY);
AxedaCorp 0:65004368569c 172
AxedaCorp 0:65004368569c 173 //Send data about orientation
AxedaCorp 0:65004368569c 174
AxedaCorp 0:65004368569c 175 ax_data_addAnalogToSet(ds[0], "accel_x", acc.getAccX());
AxedaCorp 0:65004368569c 176 ax_data_addAnalogToSet(ds[0], "accel_y", acc.getAccY());
AxedaCorp 0:65004368569c 177 ax_data_addAnalogToSet(ds[0], "accel_z", acc.getAccZ());
AxedaCorp 0:65004368569c 178
AxedaCorp 0:65004368569c 179 printf("\n\r%f\n", acc.getAccX());
AxedaCorp 0:65004368569c 180 printf("\n\r%f\n", acc.getAccY());
AxedaCorp 0:65004368569c 181 printf("\n\r%f\n", acc.getAccZ());
AxedaCorp 0:65004368569c 182
AxedaCorp 0:65004368569c 183 ax_platform_sendData(&sbox, &thisDevice, ds, 1);
AxedaCorp 0:65004368569c 184 ax_data_destroySet(ds[0]);
AxedaCorp 0:65004368569c 185 /*
AxedaCorp 0:65004368569c 186 size = cam->readJPEG(image_buf, sizeof(image_buf));
AxedaCorp 0:65004368569c 187 ax_platform_upload(&sbox, &thisDevice, &pic);
AxedaCorp 0:65004368569c 188 */
AxedaCorp 0:65004368569c 189 // printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed");
AxedaCorp 0:65004368569c 190 wait(10);
AxedaCorp 0:65004368569c 191 }
AxedaCorp 0:65004368569c 192 }