Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.00 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BNO055.h>
  4. #include <utility/imumaths.h>
  5.  
  6. #include <Ethernet.h>
  7. #include <MQTT.h>
  8.  
  9.  
  10. #define OUTPUT_JSON_QUATERNION
  11.  
  12. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  13. byte ip[] = {192, 168, 1, 177};  // <- change to match your network
  14.  
  15. EthernetClient net;
  16. MQTTClient client;
  17.  
  18. unsigned long lastMillis = 0
  19.  
  20. /* Set the delay between fresh samples */
  21. #define BNO055_SAMPLERATE_DELAY_MS (100)
  22.  
  23. Adafruit_BNO055 bno = Adafruit_BNO055(55);
  24.  
  25. /**************************************************************************/
  26. /*
  27.     Displays some basic information on this sensor from the unified
  28.     sensor API sensor_t type (see Adafruit_Sensor for more information)
  29. */
  30. /**************************************************************************/
  31. void displaySensorDetails(void)
  32. {
  33.   sensor_t sensor;
  34.   bno.getSensor(&sensor);
  35.   Serial.println("------------------------------------");
  36.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  37.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  38.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  39.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" xxx");
  40.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" xxx");
  41.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" xxx");
  42.   Serial.println("------------------------------------");
  43.   Serial.println("");
  44.   delay(500);
  45. }
  46.  
  47. /**************************************************************************/
  48. /*
  49.     Arduino setup function (automatically called at startup)
  50. */
  51. /**************************************************************************/
  52. void setup(void)
  53. {
  54.   Serial.begin(115200);
  55.   Ethernet.begin(mac, ip);
  56.  
  57.   // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
  58.   // You need to set the IP address directly.
  59.   client.begin("10.218.128.28", net);
  60.   client.onMessage(messageReceived);
  61.  
  62.   connect();
  63.  
  64.   Serial.println("Orientation Sensor Test"); Serial.println("");
  65.  
  66.   /* Initialise the sensor */
  67.   if(!bno.begin())
  68.   {
  69.     /* There was a problem detecting the BNO055 ... check your connections */
  70.     Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
  71.     while(1);
  72.   }
  73.    
  74.   delay(1000);
  75.  
  76.   /* Use external crystal for better accuracy */
  77.   bno.setExtCrystalUse(true);
  78.    
  79.   /* Display some basic information on this sensor */
  80.   displaySensorDetails();
  81. }
  82.  
  83. /**************************************************************************/
  84. /*
  85.     Arduino loop function, called once 'setup' is complete (your own code
  86.     should go here)
  87. */
  88. /**************************************************************************/
  89. void loop(void)
  90. {
  91.     StaticJsonBuffer<500> jsonBuffer;
  92.     String jsonqQaternion, jsonimu;
  93.   /* Get a new sensor event */
  94.   sensors_event_t event;
  95.   bno.getEvent(&event);
  96.  
  97.   /* Board layout:
  98.          +----------+
  99.          |         *| RST   PITCH  ROLL  HEADING
  100.      ADR |*        *| SCL
  101.      INT |*        *| SDA     ^            /->
  102.      PS1 |*        *| GND     |            |
  103.      PS0 |*        *| 3VO     Y    Z-->    \-X
  104.          |         *| VIN
  105.          +----------+
  106.   */
  107.  
  108.   /* The processing sketch expects data as roll, pitch, heading */
  109.   Serial.print(F("Orientation: "));
  110.   Serial.print((float)event.orientation.x);
  111.   Serial.print(F(" "));
  112.   Serial.print((float)event.orientation.y);
  113.   Serial.print(F(" "));
  114.   Serial.print((float)event.orientation.z);
  115.   Serial.println(F(""));
  116.  
  117.   /* Also send calibration data for each sensor. */
  118.   uint8_t sys, gyro, accel, mag = 0;
  119.   bno.getCalibration(&sys, &gyro, &accel, &mag);
  120.   Serial.print(F("Calibration: "));
  121.   Serial.print(sys, DEC);
  122.   Serial.print(F(" "));
  123.   Serial.print(gyro, DEC);
  124.   Serial.print(F(" "));
  125.   Serial.print(accel, DEC);
  126.   Serial.print(F(" "));
  127.   Serial.println(mag, DEC);
  128.  
  129.   if (!client.connected()) {
  130.     connect();
  131.   }
  132.  
  133.   // publish a message roughly every second.
  134.   if (millis() - lastMillis > 100) {
  135.     lastMillis = millis();
  136.      lastMillis_imu = millis();
  137. #ifdef OUTPUT_JSON_QUATERNION
  138.  
  139.           Quaternion q = bno.getQuat();        
  140.           JsonObject& root = jsonBuffer.createObject();
  141.           JsonArray& data = root.createNestedArray("data");
  142.           data.add(q.w);
  143.           data.add(q.x);
  144.           data.add(q.y);
  145.           data.add(q.z);
  146.           root.printTo(jsonqQaternion);
  147.           Serial.println(JsonOutput);
  148.           client.publish("quaternion",jsonqQaternion );
  149. #endif
  150.   }
  151.  
  152.   delay(BNO055_SAMPLERATE_DELAY_MS);
  153. }
  154. void connect() {
  155.   Serial.print("connecting...");
  156.   while (!client.connect("arduino", "try", "try")) {
  157.     Serial.print(".");
  158.     delay(1000);
  159.   }
  160.  
  161.   Serial.println("\nconnected!");
  162.  
  163.   client.subscribe("/hello");
  164.   // client.unsubscribe("/hello");
  165. }
  166.  
  167. void messageReceived(String &topic, String &payload) {
  168.   Serial.println("incoming: " + topic + " - " + payload);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement