Modified to run on Renesas GR Peach board
Dependencies: EthernetInterface HTTP-Server Webpage mbed-rpc mbed-src
Fork of HTTPServer_echoback by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "HTTPServer.h" 00004 #include <stdio.h> 00005 #include <string.h> 00006 //#include "webpage.h" 00007 00008 00009 #include "Adafruit_Sensor.h" 00010 #include "Adafruit_LSM303_U.h" 00011 #include "Adafruit_BMP085_U.h" 00012 #include "Adafruit_L3GD20_U.h" 00013 #include "Adafruit_10DOF.h" 00014 00015 00016 #define PORT 80 00017 #include <string> 00018 00019 00020 /* Assign a unique ID to the sensors */ 00021 Adafruit_10DOF dof = Adafruit_10DOF(); 00022 Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301L); 00023 Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302L); 00024 Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(18001L); 00025 Adafruit_L3GD20_Unified gyro = Adafruit_L3GD20_Unified(20L); 00026 00027 00028 // Update this with the correct SLP for accurate altitude measurements 00029 float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; 00030 00031 mbed::I2C* i2c; 00032 Timer timer; 00033 00034 EthernetInterface eth; 00035 00036 TCPSocketServer svr; 00037 bool serverIsListening = false; 00038 00039 TCPSocketConnection client; 00040 bool clientIsConnected = false; 00041 00042 DigitalOut led1(LED1); //server listning status 00043 DigitalOut led2(LED2); //socket connecting status 00044 DigitalOut led3(LED3); //last color 00045 00046 Ticker ledTick; 00047 HTTPServer create_simple_server() 00048 { 00049 HTTPServer srv; 00050 srv.add_request_handler("DELETE", new DeleteRequestHandler()); 00051 srv.add_request_handler("GET", new GetRequestHandler()); 00052 srv.add_request_handler("PUT", new PutRequestHandler()); 00053 return srv; 00054 } 00055 HTTPServer create_interactive_server() 00056 { 00057 HTTPServer srv(new InteractiveHTMLFormatter()); 00058 srv.add_request_handler("GET", new ComplexRequestHandler()); 00059 return srv; 00060 } 00061 HTTPServer create_simple_interactive_server() 00062 { 00063 HTTPServer srv(new InteractiveHTMLFormatter()); 00064 srv.add_request_handler("GET", new GetRequestHandler()); 00065 return srv; 00066 } 00067 00068 void ledTickfunc() 00069 { 00070 if(serverIsListening) { 00071 led1 = !led1; 00072 } else { 00073 led1 = false; 00074 } 00075 } 00076 00077 void setup(void) 00078 { 00079 00080 printf("Adafruit 10DOF Tester \r\n"); 00081 00082 /* Initialise the sensors */ 00083 if(!accel.begin()) 00084 { 00085 /* There was a problem detecting the ADXL345 ... check your connections */ 00086 printf("Ooops, no LSM303 detected ... Check your wiring!\r\n"); 00087 //while(1); 00088 } 00089 printf("Adafruit 10DOF Accel setup complete \r\n"); 00090 if(!mag.begin()) 00091 { 00092 /* There was a problem detecting the LSM303 ... check your connections */ 00093 printf("Ooops, no LSM303 detected ... Check your wiring!\r\n"); 00094 //while(1); 00095 } 00096 printf("Adafruit 10DOF Mag setup complete \r\n"); 00097 if(!gyro.begin()) 00098 { 00099 /* There was a problem detecting the L3GD20 ... check your connections */ 00100 printf("Ooops, no L3GD20 detected ... Check your wiring or I2C ADDR!\r\n"); 00101 //while(1); 00102 } 00103 printf("Adafruit 10DOF Gyro setup complete \r\n"); 00104 00105 /* Display some basic information on this sensor */ 00106 //displaySensorDetails(); 00107 } 00108 00109 /**************************************************************************/ 00110 /*! 00111 @brief Constantly check the roll/pitch/heading/altitude/temperature 00112 */ 00113 /**************************************************************************/ 00114 void loop(void) 00115 { 00116 sensors_event_t accel_event; 00117 sensors_event_t mag_event; 00118 sensors_vec_t orientation; 00119 00120 00121 /* Calculate pitch and roll from the raw accelerometer data */ 00122 accel.getEvent(&accel_event); 00123 if (dof.accelGetOrientation(&accel_event, &orientation)) 00124 { 00125 /* 'orientation' should have valid .roll and .pitch fields */ 00126 printf("Roll: %f \n\r",orientation.roll); 00127 printf("Pitch: %f \n\r",orientation.pitch); 00128 } 00129 00130 /* Calculate the heading using the magnetometer */ 00131 mag.getEvent(&mag_event); 00132 if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation)) 00133 { 00134 /* 'orientation' should have valid .heading data now */ 00135 printf("Heading: %f\n\r", orientation.heading); 00136 } 00137 00138 printf("\n\r"); 00139 } 00140 00141 void getOrientation(char * xmlHeader){ 00142 sensors_event_t accel_event; 00143 sensors_event_t mag_event; 00144 sensors_event_t bmp_event; 00145 sensors_vec_t orientation; 00146 //float horizontal; 00147 //float vertical; 00148 uint32_t altitude = 1000;//rand() % 300 +1; 00149 uint32_t horizontal = 30;//rand() % 300 +1; 00150 uint32_t vertical = 45;//rand() % 300 +1; 00151 uint32_t heading = 33;//rand() % 300 +1; 00152 uint32_t switch1 = 0;//rand() % 1; 00153 uint32_t switch2 = 1;//rand() % 1; 00154 //char echoHeader[256] = {}; 00155 00156 //accel.getEvent(&accel_event); 00157 //if (dof.accelGetOrientation(&accel_event, &orientation)){ 00158 // horizontal = (uint32_t) orientation.roll; 00159 // vertical = (uint32_t) orientation.pitch; 00160 //} 00161 sprintf(xmlHeader, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\r"); 00162 sprintf(xmlHeader, "<AHRS>\n\r"); 00163 sprintf(xmlHeader, " <DATA>\n\r"); 00164 sprintf(xmlHeader, " <HORIZONTAL>%d</HORIZONTAL>\n\r",horizontal); 00165 sprintf(xmlHeader, " <VERTICAL>%d</VERTICAL>\n\r",vertical); 00166 sprintf(xmlHeader, " <HEADING>%d</HEADING>\n\r",heading); 00167 sprintf(xmlHeader, " <SWITCH1>%d</SWITCH1>\n\r",switch1); 00168 sprintf(xmlHeader, " <SWITCH2>%d</SWITCH2>\n\r",switch2); 00169 sprintf(xmlHeader, " <COLOR>purple</COLOR>\n\r"); 00170 sprintf(xmlHeader, " </DATA>\n\r"); 00171 sprintf(xmlHeader, "</AHRS>\n\r"); 00172 sprintf(xmlHeader, "\n\r"); 00173 //return echoHeader; 00174 00175 } 00176 00177 int main (void) 00178 { 00179 ledTick.attach(&ledTickfunc,0.5); 00180 00181 i2c = new mbed::I2C(I2C_SDA, I2C_SCL); 00182 00183 setup(); 00184 loop(); 00185 00186 EthernetInterface eth; 00187 //setup ethernet interface 00188 eth.init(); //Use DHCP 00189 eth.connect(); 00190 00191 printf("IP Address is %s\n\r", eth.getIPAddress()); 00192 00193 HTTPServer srv = create_simple_interactive_server();//create_interactive_server();//create_simple_server(); 00194 00195 if(!srv.init(PORT)) 00196 { 00197 printf("Error while initializing the server\n"); 00198 eth.disconnect(); 00199 return -1; 00200 } 00201 srv.run(); 00202 00203 }
Generated on Tue Jul 12 2022 19:53:07 by 1.7.2