Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds bluetooth and wifi data collection #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,57 +1,123 @@
/*
* Reads in the x, y, and z accelerometer values from the LIS3DH and a "record gesture" button
* state and prints the following CSV to serial: timestamp, x, y, z, buttonState
*
* By Jon E. Froehlich
* @jonfroehlich
* http://makeabilitylab.io
*
*/
Reads in the x, y, and z accelerometer values from the LIS3DH and a "record gesture" button
state and prints the following CSV to serial: timestamp, x, y, z, buttonState

By Jon E. Froehlich
@jonfroehlich
http://makeabilitylab.io

*/

#include <WiFi.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include "BluetoothSerial.h" //Header File for Serial Bluetooth, will be added by default into Arduino

// Settings
enum CommunicationMode {
RECORDER_SERIAL,
RECORDER_BLUETOOTH,
RECORDER_WIFI
};
// By default we use serial
const CommunicationMode CURRENT_MODE = RECORDER_SERIAL; // CHANGE THIS IF YOU WANT A DIFFERENT MODE

// -------------------------------------------
// Serial info
const int SERIAL_BAUD_RATE = 115200;

// -------------------------------------------
// Bluetooth settings
const char* ARDUINO_BT_NAME = "ESP32 Gesture Recorder";
// Bluetooth uses the serial baud rate and port index above

// -------------------------------------------
// WIFI
// If using wifi, make sure to put your credentials there if you're using WIFI mode
const char* WIFI_SSID = "";
const char* WIFI_PASS = "";
const char* WIFI_HOST_IP_ADDRESS = ""; // The IP address of the host you want to connect to
const int WIFI_HOST_PORT = 10002; // The IP address of the host you want to connect to

// Used for LIS3DH hardware & software SPI
#define LIS3DH_CS 10
Adafruit_LIS3DH lis = Adafruit_LIS3DH();

const boolean INCLUDE_TIMESTAMP = true; // print out timestamp to serial
const int BUTTON_INPUT_PIN = 21; // hooked up with pull-up configuration
const int SERIAL_BAUD_RATE = 115200; // make sure this matches the value in GestureRecorder.pde
const int DELAY_MS = 10; // the loop delay
const int BUTTON_INPUT_PIN = 21; // hooked up with pull-up configuration
const int DELAY_MS = 10; // the loop delay

void setup() {
// Common data objects
BluetoothSerial ESP_BT; //Object for Bluetooth
// Use WiFiClient class to create TCP connections
WiFiClient ESP_WIFI;

void setup()
{
Serial.begin(SERIAL_BAUD_RATE);
Serial.println("Initializing accelerometer...");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
if (!lis.begin(0x18))
{ // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1) yield();
while (1)
yield();
}
Serial.println("LIS3DH found!");

lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!

Serial.print("Range = ");
Serial.print(2 << lis.getRange());
Serial.println("G");

// Setup Bluetooth if we want
if (CURRENT_MODE == RECORDER_BLUETOOTH) {
if (!ESP_BT.begin(ARDUINO_BT_NAME)) {
Serial.println("We failed to start Bluetooth");
}
ESP_BT.setPin("1234");
Serial.println("Bluetooth started");
while (!ESP_BT.isReady()) {
Serial.println("Waiting for Bluetooth...");
vTaskDelay(1000); // this is similar to yield and allows the device to go to low power mode
}
}
else if (CURRENT_MODE == RECORDER_WIFI) {
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting.");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
vTaskDelay(500); // this is similar to yield and allows the device to go to low power mode
}
Serial.print("WiFi connected - IP address: ");
Serial.println(WiFi.localIP());
if (!ESP_WIFI.connect(WIFI_HOST_IP_ADDRESS, WIFI_HOST_PORT)) {
Serial.print("Failed to connect to ");
Serial.print(WIFI_HOST_IP_ADDRESS);
Serial.print(":");
Serial.print(WIFI_HOST_PORT);
Serial.println();
while(1) vTaskDelay(5000);
}
Serial.println("Connected to host");
ESP_WIFI.println("Hello");
}

pinMode(BUTTON_INPUT_PIN, INPUT_PULLUP);
}

void loop() {

// Read accel data
lis.read();

void serial_output()
{
int buttonVal = digitalRead(BUTTON_INPUT_PIN);

if(INCLUDE_TIMESTAMP){
if (INCLUDE_TIMESTAMP)
{
Serial.print(millis());
Serial.print(", ");
}

Serial.print(lis.x);
Serial.print(", ");
Serial.print(lis.y);
Expand All @@ -60,8 +126,68 @@ void loop() {
Serial.print(", ");
Serial.print(!buttonVal); // because pull-up
Serial.println();
}

void bluetooth_output()
{
int buttonVal = digitalRead(BUTTON_INPUT_PIN);

if (INCLUDE_TIMESTAMP)
{
ESP_BT.print(millis());
ESP_BT.print(", ");
}
ESP_BT.print(lis.x);
ESP_BT.print(", ");
ESP_BT.print(lis.y);
ESP_BT.print(", ");
ESP_BT.print(lis.z);
ESP_BT.print(", ");
ESP_BT.print(!buttonVal); // because pull-up
ESP_BT.println();
}

void wifi_output()
{
int buttonVal = digitalRead(BUTTON_INPUT_PIN);

if (INCLUDE_TIMESTAMP)
{
ESP_WIFI.print(millis());
ESP_WIFI.print(", ");
}
ESP_WIFI.print(lis.x);
ESP_WIFI.print(", ");
ESP_WIFI.print(lis.y);
ESP_WIFI.print(", ");
ESP_WIFI.print(lis.z);
ESP_WIFI.print(", ");
ESP_WIFI.print(!buttonVal); // because pull-up
ESP_WIFI.println();
}

void loop()
{

// Read accel data
lis.read();

// Then output our stuff
switch (CURRENT_MODE)
{
case RECORDER_SERIAL:
serial_output();
break;
case RECORDER_BLUETOOTH:
bluetooth_output();
break;
case RECORDER_WIFI:
wifi_output();
break;
}

if(DELAY_MS > 0){
if (DELAY_MS > 0)
{
delay(DELAY_MS);
}
}
27 changes: 27 additions & 0 deletions Processing/GestureRecorder/Arduino/LIS3DHGestureRecorder/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SERIAL MODE

# BLUETOOTH MODE
Starting the device in this mode will create a new bluetooth device named by default ESP32 Gesture Recorder.
You can change it if you really like.
On windows, the easiest way to add this is as follows:
1. go to bluetooth settings.
2. On the right side it says "More Bluetooth Options".
3. A new dialog box will open and it will say "Com Ports" in one of the tabs at the top.
4. Go to that tab (see image below):

![dialog box](DialogBox.PNG "The COM Ports Dialog box")

5. Click Add
6. A new dialog will pop up, giving you different options (it may take a moment to come up)
7. Select outgoing since your PC will be initializing the connection
8. Select the bluetooth device in the dropdown for "Device that will use the com port".
It will be ESP32 Gesture Recorder unless you changed it
9. Hit okay
10. Take note of the name of the COM port that was created.

# WIFI MODE
Start the processing server with WIFI turned on.
It will spit out a "Started the server at 192.168.x.x"
Put that address in the arduino sketch and upload it to your ESP32.
Make sure to open up the port you've selected in your firewall for the computer running processing.
I had trouble with this even when I added explicit rules.
Loading