#include #include #include #include #include #include #include /* ================================================================== BNO055 Code Goes Here. ================================================================== */ // Set the delay between fresh samples */ #define BNO055_SAMPLERATE_DELAY_MS (100) // Check I2C device address and correct line below (by default address is 0x29 or 0x28) // id, address Adafruit_BNO055 bno = Adafruit_BNO055(12345, 0x28); //0x28 works for new BNO-055 // Declare valuables String BNO_val; /* ================================================================== Websocket Code Goes Here. ==================================================================*/ // Replace with your network credentials const char* ssid = "RosalieMINI"; const char* password = "00110011"; const int WSDELAY = 500; // Define valuables bool ledState = 0; const int ledPin = 14; //GPIO PIN // Create AsyncWebServer object on port 80 AsyncWebServer server(8080); AsyncWebSocket ws("/ws"); const char index_html[] PROGMEM = R"rawliteral( ESP Web Server ESP Web Server

ESP WebSocket Server

Output - GPIO 14

state: %STATE%

)rawliteral"; /* ================================================================== BNO055 Code Goes Here. ================================================================== */ void displaySensorDetails(void) { sensor_t sensor; bno.getSensor(&sensor); // Serial.println("------------------------------------"); // Serial.print ("Sensor: "); Serial.println(sensor.name); // Serial.print ("Driver Ver: "); Serial.println(sensor.version); // Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); // Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" xxx"); // Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" xxx"); // Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" xxx"); // Serial.println("------------------------------------"); // Serial.println(""); // delay(500); } void displaySensorStatus(void) { /* Get the system status values (mostly for debugging purposes) */ uint8_t system_status, self_test_results, system_error; system_status = self_test_results = system_error = 0; bno.getSystemStatus(&system_status, &self_test_results, &system_error); } void displayCalStatus(void) { /* Get the four calibration values (0..3) */ /* Any sensor data reporting 0 should be ignored, */ /* 3 means 'fully calibrated" */ uint8_t system, gyro, accel, mag; system = gyro = accel = mag = 0; bno.getCalibration(&system, &gyro, &accel, &mag); } /* ================================================================== BNO055 orientation / linearAccel / magnetometer datas ==================================================================*/ //void printEvent(sensors_event_t* event) { // double x = -1000000, y = -1000000 , z = -1000000; //dumb values, easy to spot problem // if (event->type == SENSOR_TYPE_ORIENTATION) { // Serial.print("Orient:"); // x = event->orientation.x; // y = event->orientation.y; // z = event->orientation.z; // } // else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) { // Serial.print("Mag:"); // x = event->magnetic.x; // y = event->magnetic.y; // z = event->magnetic.z; // } // else (event->type == SENSOR_TYPE_LINEAR_ACCELERATION) { // Serial.print("Linear:"); // x = event->acceleration.x; // y = event->acceleration.y; // z = event->acceleration.z; // } //} /* ================================================================== Websocket Code Goes Here. ================================================================== */ void notifyClients() { ws.textAll(String(BNO_val)); //ws.textAll(String(ledState)); } void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) { AwsFrameInfo *info = (AwsFrameInfo*)arg; if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) { data[len] = 0; if (strcmp((char*)data, "toggle") == 0) { ledState = !ledState; notifyClients(); } } } void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { switch (type) { case WS_EVT_CONNECT: Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str()); break; case WS_EVT_DISCONNECT: Serial.printf("WebSocket client #%u disconnected\n", client->id()); break; case WS_EVT_DATA: handleWebSocketMessage(arg, data, len); break; case WS_EVT_PONG: case WS_EVT_ERROR: break; } } void initWebSocket() { ws.onEvent(onEvent); server.addHandler(&ws); } String processor(const String& var){ Serial.println(var); if(var == "STATE"){ if (ledState){ return "ON"; } else{ return "OFF"; } } } void setup(void) { /* ================================================================== BNO055 Code Goes Here. ==================================================================*/ Serial.begin(115200); /* Initialise the sensor */ if(!bno.begin()) { /* There was a problem detecting the BNO055 ... check your connections */ Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); while(1); } delay(5000); /* Display some basic information on this sensor */ displaySensorDetails(); /* Optional: Display current status */ displaySensorStatus(); bno.setExtCrystalUse(true); /* ================================================================== Websocket Code Goes Here. ================================================================== */ // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } // Print ESP Local IP Address Serial.println(WiFi.localIP()); initWebSocket(); // Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); // Start server server.begin(); } void loop(void) { /* ================================================================== BNO055 Code Goes Here. ==================================================================*/ /* Get a new sensor event */ sensors_event_t event; bno.getEvent(&event); /* Orientation*/ int x_orient_val = round(event.orientation.x); int y_orient_val = round(event.orientation.y); int z_orient_val = round(event.orientation.z); /* LinearAcceration*/ int x_linearaccel_val = round(event.acceleration.x); int y_linearaccel_val = round(event.acceleration.y); int z_linearaccel_val = round(event.acceleration.z); /* Magnetic*/ int x_magnetic_val = round(event.magnetic.x); int y_magnetic_val = round(event.magnetic.y); int z_magnetic_val = round(event.magnetic.z); String orientation_val = "orientation_val: "; String linearaccel_val = "inearaccel_val: "; String magnetic_val = "magnetic_val: "; String sensor_val = " "; /* GET DATA */ ///* All - text + number */ // String XYZ = orientation_val + x_orient_val +","+ y_orient_val +","+ z_orient_val + " " + // linearaccel_val + x_linearaccel_val +","+ y_linearaccel_val +","+ z_linearaccel_val + " " + // magnetic_val + x_magnetic_val +","+ y_magnetic_val +","+ z_magnetic_val; ///* All - number only */ // String XYZ = sensor_val + x_orient_val +","+ y_orient_val +","+ z_orient_val + " " // + x_linearaccel_val +","+ y_linearaccel_val +","+ z_linearaccel_val + " " // + x_magnetic_val +","+ y_magnetic_val +","+ z_magnetic_val; /* Orientation - number only */ String XYZ = sensor_val + x_orient_val +","+ y_orient_val +","+ z_orient_val; // ///* LinearAcceleration - number only */ // String XYZ = sensor_val + x_linearaccel_val +","+ y_linearaccel_val +","+ z_linearaccel_val; // ///* Magnetic - number only */ // String XYZ = sensor_val + x_magnetic_val +","+ y_magnetic_val +","+ z_magnetic_val; Serial.println(XYZ); BNO_val=XYZ; /* Wait the specified delay before requesting nex data */ delay(BNO055_SAMPLERATE_DELAY_MS); /* ================================================================== Websocket Code Goes Here. ==================================================================*/ ws.cleanupClients(); ws.textAll(String(BNO_val)); delay(WSDELAY); }