Modified to run on Renesas GR Peach board

Dependencies:   EthernetInterface HTTP-Server Webpage mbed-rpc mbed-src

Fork of HTTPServer_echoback by Takuya Urakawa

Revision:
16:5d102be2566c
Parent:
15:eae1575da9ca
--- a/main.cpp	Fri Mar 08 02:24:33 2013 +0000
+++ b/main.cpp	Wed Oct 07 20:42:51 2015 +0000
@@ -1,96 +1,203 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
+#include "HTTPServer.h"
 #include <stdio.h>
 #include <string.h>
+//#include "webpage.h"
+
+
+#include "Adafruit_Sensor.h"
+#include "Adafruit_LSM303_U.h"
+#include "Adafruit_BMP085_U.h"
+#include "Adafruit_L3GD20_U.h"
+#include "Adafruit_10DOF.h"
+
 
 #define PORT   80
+#include <string>
+
+
+/* Assign a unique ID to the sensors */
+Adafruit_10DOF                dof   = Adafruit_10DOF();
+Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301L);
+Adafruit_LSM303_Mag_Unified   mag   = Adafruit_LSM303_Mag_Unified(30302L);
+Adafruit_BMP085_Unified       bmp   = Adafruit_BMP085_Unified(18001L);
+Adafruit_L3GD20_Unified       gyro  = Adafruit_L3GD20_Unified(20L);
+
+
+// Update this with the correct SLP for accurate altitude measurements
+float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
+
+mbed::I2C* i2c;
+Timer timer;
 
 EthernetInterface eth;
 
 TCPSocketServer svr;
-bool serverIsListened = false;
+bool serverIsListening = false;
 
 TCPSocketConnection client;
 bool clientIsConnected = false;
 
 DigitalOut led1(LED1); //server listning status
 DigitalOut led2(LED2); //socket connecting status
+DigitalOut led3(LED3); //last color
 
 Ticker ledTick;
+HTTPServer create_simple_server()
+{    
+    HTTPServer srv;
+    srv.add_request_handler("DELETE", new DeleteRequestHandler());
+    srv.add_request_handler("GET", new GetRequestHandler());
+    srv.add_request_handler("PUT", new PutRequestHandler());
+    return srv;
+}
+HTTPServer create_interactive_server()
+{
+    HTTPServer srv(new InteractiveHTMLFormatter());
+    srv.add_request_handler("GET", new ComplexRequestHandler());
+    return srv;
+}
+HTTPServer create_simple_interactive_server()
+{
+    HTTPServer srv(new InteractiveHTMLFormatter());
+    srv.add_request_handler("GET", new GetRequestHandler());
+    return srv;
+}
 
 void ledTickfunc()
 {
-    if(serverIsListened)  {
+    if(serverIsListening)  {
         led1 = !led1;
     } else {
         led1 = false;
     }
 }
 
+void setup(void)
+{
+
+  printf("Adafruit 10DOF Tester \r\n");
+
+  /* Initialise the sensors */
+  if(!accel.begin())
+  {
+    /* There was a problem detecting the ADXL345 ... check your connections */
+    printf("Ooops, no LSM303 detected ... Check your wiring!\r\n");
+    //while(1);
+  }
+   printf("Adafruit 10DOF Accel setup complete \r\n");
+ if(!mag.begin())
+  {
+    /* There was a problem detecting the LSM303 ... check your connections */
+    printf("Ooops, no LSM303 detected ... Check your wiring!\r\n");
+    //while(1);
+  }
+   printf("Adafruit 10DOF Mag setup complete \r\n");
+  if(!gyro.begin())
+  {
+    /* There was a problem detecting the L3GD20 ... check your connections */
+    printf("Ooops, no L3GD20 detected ... Check your wiring or I2C ADDR!\r\n");
+    //while(1);
+  }
+   printf("Adafruit 10DOF Gyro setup complete \r\n");
+
+  /* Display some basic information on this sensor */
+  //displaySensorDetails();
+}
+
+/**************************************************************************/
+/*!
+    @brief  Constantly check the roll/pitch/heading/altitude/temperature
+*/
+/**************************************************************************/
+void loop(void)
+{
+  sensors_event_t accel_event;
+  sensors_event_t mag_event;
+  sensors_vec_t   orientation;
+
+
+  /* Calculate pitch and roll from the raw accelerometer data */
+  accel.getEvent(&accel_event);
+  if (dof.accelGetOrientation(&accel_event, &orientation))
+  {
+    /* 'orientation' should have valid .roll and .pitch fields */
+    printf("Roll: %f \n\r",orientation.roll);
+    printf("Pitch: %f \n\r",orientation.pitch);
+  }
+  
+  /* Calculate the heading using the magnetometer */
+  mag.getEvent(&mag_event);
+  if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
+  {
+    /* 'orientation' should have valid .heading data now */
+    printf("Heading: %f\n\r", orientation.heading);
+  }
+ 
+  printf("\n\r");  
+}
+
+void getOrientation(char * xmlHeader){
+    sensors_event_t accel_event;
+    sensors_event_t mag_event;
+    sensors_event_t bmp_event;
+    sensors_vec_t   orientation;
+    //float horizontal;
+    //float vertical;
+    uint32_t altitude = 1000;//rand() % 300 +1;
+    uint32_t horizontal = 30;//rand() % 300 +1;
+    uint32_t vertical = 45;//rand() % 300 +1;
+    uint32_t heading = 33;//rand() % 300 +1;
+    uint32_t switch1 = 0;//rand() % 1;
+    uint32_t switch2 = 1;//rand() % 1;
+    //char echoHeader[256] = {};
+
+    //accel.getEvent(&accel_event);
+    //if (dof.accelGetOrientation(&accel_event, &orientation)){
+    //    horizontal = (uint32_t) orientation.roll;
+    //    vertical   = (uint32_t) orientation.pitch;
+    //}
+    sprintf(xmlHeader, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\r");
+    sprintf(xmlHeader, "<AHRS>\n\r");
+    sprintf(xmlHeader, "    <DATA>\n\r");
+    sprintf(xmlHeader, "        <HORIZONTAL>%d</HORIZONTAL>\n\r",horizontal);
+    sprintf(xmlHeader, "        <VERTICAL>%d</VERTICAL>\n\r",vertical);
+    sprintf(xmlHeader, "        <HEADING>%d</HEADING>\n\r",heading);
+    sprintf(xmlHeader, "        <SWITCH1>%d</SWITCH1>\n\r",switch1);
+    sprintf(xmlHeader, "        <SWITCH2>%d</SWITCH2>\n\r",switch2);
+    sprintf(xmlHeader, "        <COLOR>purple</COLOR>\n\r");
+    sprintf(xmlHeader, "    </DATA>\n\r");
+    sprintf(xmlHeader, "</AHRS>\n\r");
+    sprintf(xmlHeader, "\n\r");
+ //return echoHeader;
+ 
+}
+
 int main (void)
 {
     ledTick.attach(&ledTickfunc,0.5);
 
+    i2c = new mbed::I2C(I2C_SDA, I2C_SCL);
+   
+    setup();
+    loop();
+     
+    EthernetInterface eth;
     //setup ethernet interface
     eth.init(); //Use DHCP
     eth.connect();
+
     printf("IP Address is %s\n\r", eth.getIPAddress());
 
-    //setup tcp socket
-    if(svr.bind(PORT)< 0) {
-        printf("tcp server bind failed.\n\r");
+    HTTPServer srv = create_simple_interactive_server();//create_interactive_server();//create_simple_server();
+ 
+    if(!srv.init(PORT))
+    {
+        printf("Error while initializing the server\n");
+        eth.disconnect();
         return -1;
-    } else {
-        printf("tcp server bind successed.\n\r");
-        serverIsListened = true;
-    }
-
-    if(svr.listen(1) < 0) {
-        printf("tcp server listen failed.\n\r");
-        return -1;
-    } else {
-        printf("tcp server is listening...\n\r");
     }
-
-    //listening for http GET request
-    while (serverIsListened) {
-        //blocking mode(never timeout)
-        if(svr.accept(client)<0) {
-            printf("failed to accept connection.\n\r");
-        } else {
-            printf("connection success!\n\rIP: %s\n\r",client.get_address());
-            clientIsConnected = true;
-            led2 = true;
-            
-            while(clientIsConnected) {
-                char buffer[1024] = {};
-                switch(client.receive(buffer, 1023)) {
-                    case 0:
-                        printf("recieved buffer is empty.\n\r");
-                        clientIsConnected = false;
-                        break;
-                    case -1:
-                        printf("failed to read data from client.\n\r");
-                        clientIsConnected = false;
-                        break;
-                    default:
-                        printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
-                        if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) {
-                            printf("GET request incomming.\n\r");
-                            //setup http response header & data
-                            char echoHeader[256] = {};
-                            sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
-                            client.send(echoHeader,strlen(echoHeader));
-                            client.send(buffer,strlen(buffer));
-                            clientIsConnected = false;
-                            printf("echo back done.\n\r");
-                        }
-                        break;
-                }
-            }
-            printf("close connection.\n\rtcp server is listening...\n\r");
-            client.close();
-            led2 = false;
-        }
-    }
+    srv.run();
+ 
 }