To smooth the data we’ll be using the ‘averageValue’ function. Our temperature sensor is connected to the analog A0 input, and since we’ll be sampling the data at a very high frequency we need to smooth out the data, in order to prevent spiking. This will be our notifying RX characteristic that our iOS app will read from. We can only send 20 bytes at a time, and once we get our temperature data (formatted as a string), we write it into our object. In this method, we get the current state of the BLE, and if it’s connected we send our temperature data. So all our processing is done within the loop method. On Arduino, the setup method is executed once when the Arduino is started, and the loop method is executed constantly. In the setup method, we set the device name, and start our BLE chip. Let’s go over the BLE code first.Īt the top of the class, we create an instance of the BLE UART with the corresponding pins. BLE breakout circuit and the temperature sensor. Readings = analogRead(inputPin) įloat voltage = (sensorVal / 1024.0) * 5.0 įloat temperature = (voltage. The code is not that long, so I’ll just show the whole source file here: #include Īdafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST) įor (int thisReading = 0 thisReading \"")) Serial.print((char *)sendbuffer) Serial.println("\"") īTLEserial.write(sendbuffer, sendbuffersize) The only thing that’s left is to write some code and upload it to our Arduino board. Now we have the hardware wired up, and all the libraries we need. Adafruit has a great tutorial on how to do this, so go and check it out. Once you installed the IDE, you’ll need to install the Adafruit libraries. They have a great guide on their website on how to set up your IDE. To write software for Arduino you’ll need to install Arduino IDE, it’s available for Mac, so you can just go to their website, and download it. Now that we have everything wired up, let’s write some Arduino code. If you didn’t, that’s ok, just wire the sensor output, to the analog A0 input on the Arduino. If you bought the kit I recommended you’ll find the diagram with the example code in the book included in the kit. This is the wiring diagram from their website: If you got the same kit as I do, you can go to Adafruit website, and follow the instructions on how to wire the BLE to your Arduino. You can see this depicted in the following diagram: We’ll use this in our project, we’ll be writing the temperature data in our characteristic, and it will be read automatically by our iOS app. What this means is, every time their value changes, they notify the observer (iOS app, in our case). BLE is very different than your standard Bluetooth, and if you want to learn more about it, I would recommend a book called Getting Started with Bluetooth Low Energy.īLE is very structured, you have your peripheral (the device), which can contain many services, a service can contain many characteristics. It’s very popular in the IoT projects, it allows you to create a local network of connected devices. It’s a Bluetooth standard that’s using very low energy to transmit small chunks of data over short distances. Bluetooth Low EnergyīLE stands for Bluetooth Low Energy, sometimes called Bluetooth Smart, or Bluetooth 4.1. You could use any other circuit, but I used this one, and the Arduino sketch is written for this circuit. If you’re interested in learning more about Arduino, and how to use it, Arduino for Dummies is a great book to start you off.Īnother piece of hardware you’ll need is a BLE breakout circuit by Adafruit. In this pack, you’ll have pretty much everything you’ll need to start working with it, along with a nice example book. Arduinoįor this project to work, you’ll need an Arduino, there are many versions of it, but I would recommend you get the official starter pack from amazon, check it out here. In the first part of this post, we’ll talk about the Arduino, and in the second part about the iOS app. We’ll be sending a string instead of a float with temperature using the BLE, this way, you can reuse the circuit for some other projects you might derive out of this one. Don’t worry if some of these things are unfamiliar to you, I’ll try to give you step-by-step instructions on how to create this. On the iOS side, we’ll be using CoreBluetooth. For this project, we’ll be using an Arduino with a BLE breakout circuit. Today we’ll create something cool, a temperature sensor and an iOS app that connects to it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |