# Using ESP32 OLED to get Tilt data



## N3MIS15 (14/2/18)

I was hesitant to post this as the code is very bare bones, has absolutely zero error handling and probably totally the wrong way to go about this. I did however get working exactly what i was after.







For $18~ on aliexpress and a bit of dodgy code I can now check my tilt hydrometer readings at a glance.

Here is the Arduino "code" if anyone cares.

```
#include <stdlib.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include "SSD1306.h" // https://github.com/ThingPulse/esp8266-oled-ssd1306

SSD1306  display(0x3c, 5, 4);
int scanTime = 5; //In seconds

void setup() {
  display.init();
  display.flipScreenVertically();
  display.clear();
}

void loop() {
  display.clear();
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_16);
  display.drawString(0, 0, "Scanning..."); // Line 1
  display.display();
  BLEDevice::init("");
  BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
  BLEScanResults foundDevices = pBLEScan->start(scanTime);
  BLEAdvertisedDevice tilt = foundDevices.getDevice(0);

  char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)tilt.getManufacturerData().data(), tilt.getManufacturerData().length());
  String manufacturerData = pHex;
  String UUID = manufacturerData.substring(8, 40);

  const char* xTemp = (manufacturerData.substring(40, 42) + manufacturerData.substring(42, 44)).c_str();
  long tempF = strtol(xTemp, NULL, 16);
  float tempC = (float(tempF-32)*0.55555555555555555555555555555556);
  const char* xGravity = (manufacturerData.substring(44, 46) + manufacturerData.substring(46, 48)).c_str();
  long gravityThousand = strtol(xGravity, NULL, 16);

  // Lets get the colour, because why not.
  String tiltColour = "";
 
  if(UUID.substring(6, 7).equals("1")) {
    tiltColour = "Red";
  }
  else if (UUID.substring(6, 7).equals("2")) {
    tiltColour = "Green";
  }
  else if (UUID.substring(6, 7).equals("3")) {
    tiltColour = "Black";
  }
  else if (UUID.substring(6, 7).equals("4")) {
    tiltColour = "Purple";
  }
  else if (UUID.substring(6, 7).equals("5")) {
    tiltColour = "Orange";
  }
  else if (UUID.substring(6, 7).equals("6")) {
    tiltColour = "Blue";
  }
  else if (UUID.substring(6, 7).equals("7")) {
    tiltColour = "Yellow";
  }
  else if (UUID.substring(6, 7).equals("8")) {
    tiltColour = "Pink";
  }
 
  display.clear();
  display.drawString(0, 0, "Tilt " + tiltColour); // Line 1
  display.drawString(0, 16, "Gravity: " + String(float(gravityThousand)/1000, 3)); // Line 2
  display.drawString(0, 32, "Temp: " + String(tempC) + "C"); // Line 3
  //display.drawString(0, 48, "Line 4"); // Line 4
  display.display();

  delay(3600000);
}
```

It takes the first bluetooth device it finds and attempts to pull tilt data from it. If it succeeds, the device sleeps for 1 hour before scanning again. If it does not succeed the device keeps scanning until it does.


----------



## mr_wibble (15/2/18)

That's an interesting little machine, I like it.
What type of "Board" do you compile this against? ESP8266?

It doesn't build for me though, do I need a special version of something?

I'm trying with the latest Arduino (1.8.5), latest ESP8266 "board", and BLE library fresh from github.

arduino-1.8.5/libraries/ESP32_BLE_Arduino-master/src/BLEDevice.h:10:23: fatal error: sdkconfig.h: No such file or directory​
Obviously I can go re-writing, but it Works For Your right?


----------



## SmallBatchBru (28/2/18)

This is what I've been trying to do! Any idea how we could sift through all of the bluetooth beacons that are on in my house? Is there a way to pull a specific manufacturer id? UUID?


----------

