Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BNO055.h>
- #include <utility/imumaths.h>
- #include <Ethernet.h>
- #include <MQTT.h>
- #define OUTPUT_JSON_QUATERNION
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- byte ip[] = {192, 168, 1, 177}; // <- change to match your network
- EthernetClient net;
- MQTTClient client;
- unsigned long lastMillis = 0
- /* Set the delay between fresh samples */
- #define BNO055_SAMPLERATE_DELAY_MS (100)
- Adafruit_BNO055 bno = Adafruit_BNO055(55);
- /**************************************************************************/
- /*
- Displays some basic information on this sensor from the unified
- sensor API sensor_t type (see Adafruit_Sensor for more information)
- */
- /**************************************************************************/
- 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);
- }
- /**************************************************************************/
- /*
- Arduino setup function (automatically called at startup)
- */
- /**************************************************************************/
- void setup(void)
- {
- Serial.begin(115200);
- Ethernet.begin(mac, ip);
- // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
- // You need to set the IP address directly.
- client.begin("10.218.128.28", net);
- client.onMessage(messageReceived);
- connect();
- Serial.println("Orientation Sensor Test"); Serial.println("");
- /* 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(1000);
- /* Use external crystal for better accuracy */
- bno.setExtCrystalUse(true);
- /* Display some basic information on this sensor */
- displaySensorDetails();
- }
- /**************************************************************************/
- /*
- Arduino loop function, called once 'setup' is complete (your own code
- should go here)
- */
- /**************************************************************************/
- void loop(void)
- {
- StaticJsonBuffer<500> jsonBuffer;
- String jsonqQaternion, jsonimu;
- /* Get a new sensor event */
- sensors_event_t event;
- bno.getEvent(&event);
- /* Board layout:
- +----------+
- | *| RST PITCH ROLL HEADING
- ADR |* *| SCL
- INT |* *| SDA ^ /->
- PS1 |* *| GND | |
- PS0 |* *| 3VO Y Z--> \-X
- | *| VIN
- +----------+
- */
- /* The processing sketch expects data as roll, pitch, heading */
- Serial.print(F("Orientation: "));
- Serial.print((float)event.orientation.x);
- Serial.print(F(" "));
- Serial.print((float)event.orientation.y);
- Serial.print(F(" "));
- Serial.print((float)event.orientation.z);
- Serial.println(F(""));
- /* Also send calibration data for each sensor. */
- uint8_t sys, gyro, accel, mag = 0;
- bno.getCalibration(&sys, &gyro, &accel, &mag);
- Serial.print(F("Calibration: "));
- Serial.print(sys, DEC);
- Serial.print(F(" "));
- Serial.print(gyro, DEC);
- Serial.print(F(" "));
- Serial.print(accel, DEC);
- Serial.print(F(" "));
- Serial.println(mag, DEC);
- if (!client.connected()) {
- connect();
- }
- // publish a message roughly every second.
- if (millis() - lastMillis > 100) {
- lastMillis = millis();
- lastMillis_imu = millis();
- #ifdef OUTPUT_JSON_QUATERNION
- Quaternion q = bno.getQuat();
- JsonObject& root = jsonBuffer.createObject();
- JsonArray& data = root.createNestedArray("data");
- data.add(q.w);
- data.add(q.x);
- data.add(q.y);
- data.add(q.z);
- root.printTo(jsonqQaternion);
- Serial.println(JsonOutput);
- client.publish("quaternion",jsonqQaternion );
- #endif
- }
- delay(BNO055_SAMPLERATE_DELAY_MS);
- }
- void connect() {
- Serial.print("connecting...");
- while (!client.connect("arduino", "try", "try")) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("\nconnected!");
- client.subscribe("/hello");
- // client.unsubscribe("/hello");
- }
- void messageReceived(String &topic, String &payload) {
- Serial.println("incoming: " + topic + " - " + payload);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement