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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MTSSerial.h"
00003 #include "Wifi.h"
00004 #include "TSISensor.h"
00005 #include "MMA8451Q.h"
00006 #include "MAG3110.h"
00007 #include "USBHostCam.h"
00008 #include "axToolkit.h"
00009 
00010 /*********************************************/
00011 /* In the following lines please input:      */
00012 /* on line 17: your serial number            */
00013 /* on line 18: your wifi SSID                */
00014 /* on line 19: your wifi password/key        */
00015 /* on line 20: your security type (WPA...    */
00016 /*********************************************/
00017 
00018 #define YOUR_SERIAL_NUM "your_serial_here_123"
00019 #define SSID "yourWifi"
00020 #define AP_Security "SECURITYKEYHERE"
00021 #define AP_Key WPA2             //security type examples: NONE, WEP64, WEP128, WPA, WPA2
00022 
00023 
00024 /*********************************************/
00025 /*Axeda connections                          */
00026 /*********************************************/
00027 
00028 #define MODEL           "Freescale46"                       //using Freescale as a placeholder
00029 #define SERVER_NAME     "216.34.120.53" //IP = 216.34.120.53/52 stage/public URL=toolbox-stage-connect.axeda.com
00030 #define PORT_NUM        80
00031 
00032 
00033 using namespace mts;
00034 
00035 //Hardware Support Stuff
00036 // Include support for on-board green and red LEDs
00037 #define LED_ON  0
00038 #define LED_OFF 1
00039 
00040 DigitalOut greenLED(LED_GREEN);
00041 DigitalOut redLED(LED_RED);
00042 // Include support for onboard pushbuttons (value = 0 when pressed)
00043 DigitalIn  sw1(PTC3);
00044 DigitalIn  sw3(PTC12);
00045 TSISensor slider;
00046 AnalogIn  lightSense(PTE22);
00047 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00048 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
00049 MAG3110 mag(PTE25, PTE24);
00050 //Camera support
00051 /*
00052 #if defined(TARGET_KL46Z)
00053 uint8_t image_buf[1024*24];
00054 #elif defined(TARGET_KL25Z)
00055 uint8_t image_buf[1024*12];
00056 #endif */
00057 
00058 
00059 int main()
00060 {
00061     TSISensor touchSensor;
00062     //float accels[3];
00063     //float resting;
00064     int SEVERITY;
00065     //int HURT_BUTTON_STATE=0;
00066     //int OK_BUTTON_STATE=0;
00067     double status=0;
00068     
00069 //Some light I/O setup:    
00070     redLED=LED_OFF;
00071     greenLED=LED_OFF; 
00072 // Turn on pull up resistors on pushbutton inputs
00073     sw1.mode(PullUp);
00074     sw3.mode(PullUp);
00075 
00076 //Setup the Camera: 
00077  /*   int size=0;
00078     USBHostCam* cam = new USBHostCam(_320x240);
00079     if (!cam->connect()) {
00080         error("WebCam not found.\n");
00081         greenLED=LED_ON;
00082         }
00083     else { redLED=LED_ON; }  */
00084      
00085     //Set the network parameters
00086     std::string ssid = SSID;
00087     std::string securityKey = AP_Security;
00088     Wifi::SecurityType securityType = Wifi::AP_Key;
00089 
00090     //Wait for wifi module to boot up
00091     for (int i = 10; i >= 0; i = i - 2) {
00092         wait(2);
00093         printf("Waiting %d seconds...\n\r", i);
00094     }
00095 
00096     //Setup serial interface to WiFi module
00097     MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
00098     serial->baud(9600);
00099 
00100     //Setup Wifi class
00101     Wifi* wifi = Wifi::getInstance();
00102     printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");
00103     wifi->sendBasicCommand("set comm size 1460", 500, CR);
00104     wifi->sendBasicCommand("set comm time 6000", 500, CR);
00105   //  wifi->sendBasicCommand("set comm match "+EOT, 3, CR);
00106     //Setup and check connection
00107     printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str());
00108     printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
00109     while (! wifi->connect()) {
00110         printf("Connect: Failure\r\n");
00111         wait(1);
00112     }
00113     printf("Connected To Wifi!");
00114     
00115 //Now we setup the Axeda Service:
00116     ax_platform sbox;
00117     ax_deviceID thisDevice;
00118     ax_dataSet *ds[1];
00119  //   ax_file pic;
00120 
00121     ax_createPlatform(&sbox, SERVER_NAME, NULL, PORT_NUM); 
00122     ax_data_createModelSerialDeviceId(&thisDevice, MODEL, YOUR_SERIAL_NUM, NULL);
00123   //  ax_createFile(&pic, "mypic.jpg", "pic", sizeof(image_buf), (unsigned char*)&image_buf);       
00124    
00125     /*wait(0.7);
00126     acc.getAccAllAxis( accels );
00127     resting = abs(accels[0]) + abs(accels[1]) + abs(accels[2]);
00128     printf("Set up resting accelerometer - %.2f\r\n", resting);
00129     wait(0.1);*/
00130  
00131     //Ping 
00132     while (true) {
00133         ds[0]=(ax_dataSet *)malloc(sizeof(ax_dataSet));
00134         printf("\n\n\r ready \n\n");
00135         SEVERITY=0; 
00136         //ax_data_createSet(ds[0], 0, AX_NO_PRIORITY);     
00137         //HURT_BUTTON_STATE=sw1.mode(PullUp);
00138         //OK_BUTTON_STATE=sw3.mode(PullUp);
00139         sw3.mode(PullUp);
00140         sw1.mode(PullUp);
00141         while(sw3&&sw1){
00142             status=0;
00143             greenLED=LED_ON;
00144             wait(0.1);
00145             greenLED=LED_OFF;
00146             wait(0.1);
00147                 if (!sw3){
00148                 status=2;
00149                 }
00150                     else{
00151                         status=1;
00152                     }
00153             }  
00154         
00155         //SEND button push (hurt or ok)    
00156         ax_data_addAnalogToSet(ds[0], "status", status);
00157                
00158         while(SEVERITY<=1){
00159             SEVERITY=100*touchSensor.readPercentage();
00160             redLED=LED_ON;
00161             wait(0.1);
00162             redLED=LED_OFF;
00163             wait(0.1);
00164             
00165             }
00166         
00167         SEVERITY=100*touchSensor.readPercentage();
00168 
00169         printf("\n\r%d\n",SEVERITY);
00170         //Send data about severity (left to right position on touchpad)
00171         ax_data_addAnalogToSet(ds[0], "severity",SEVERITY);    
00172         
00173         //Send data about orientation     
00174 
00175         ax_data_addAnalogToSet(ds[0], "accel_x", acc.getAccX());
00176         ax_data_addAnalogToSet(ds[0], "accel_y", acc.getAccY());
00177         ax_data_addAnalogToSet(ds[0], "accel_z", acc.getAccZ());
00178         
00179         printf("\n\r%f\n", acc.getAccX());
00180         printf("\n\r%f\n", acc.getAccY());
00181         printf("\n\r%f\n", acc.getAccZ());
00182     
00183         ax_platform_sendData(&sbox, &thisDevice, ds, 1);
00184         ax_data_destroySet(ds[0]);
00185     /*
00186         size = cam->readJPEG(image_buf, sizeof(image_buf));
00187         ax_platform_upload(&sbox, &thisDevice, &pic);
00188         */
00189     //    printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed");
00190         wait(10);
00191     }
00192 }