Skip to content

Instantly share code, notes, and snippets.

@PattieC4ke
Created April 13, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PattieC4ke/d902865aecbb70adf32bcb022d797cee to your computer and use it in GitHub Desktop.
Save PattieC4ke/d902865aecbb70adf32bcb022d797cee to your computer and use it in GitHub Desktop.
// Light Sensor
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
// BLE
#include <SPI.h>
#include <EEPROM.h>
#include <boards.h>
#include <RBL_nRF8001.h>
// GPS
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
#include <TinyGPS++.h> // Include the TinyGPS++ library
TinyGPSPlus tinyGPS; // Create a TinyGPSPlus object
#define GPS_BAUD 9600 // GPS module baud rate. GP3906 defaults to 9600.
// If you're using an Arduino Uno, Mega, RedBoard, or any board that uses the
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
#include <SoftwareSerial.h>
#define ARDUINO_GPS_RX 7 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 6 // GPS RX, Arduino TX pin
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial
// Set gpsPort to either ssGPS if using SoftwareSerial or Serial1 if using an
// Arduino with a dedicated hardware serial port
#define gpsPort ssGPS // Alternatively, use Serial1 on the Leonardo
/**************************************************************************/
/*
Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
Serial.println("SLIMT"); Serial.println("");
// GPS
gpsPort.begin(GPS_BAUD);
// Light Sensor
if(!tsl.begin())
{
/* There was a problem detecting the TSL2561 ... check your connections */
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
/* Setup the sensor gain and integration time */
configureSensor();
// BLE
ble_set_pins(9,8);
ble_set_name("SLIMT");
ble_begin();
/* We're ready to go! */
Serial.println("Set Up Done");
}
/**************************************************************************/
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
/**************************************************************************/
unsigned char buffer[] = {};
unsigned char len;
void loop(void)
{
ble_do_events();
if ( ble_connected() )
{
// LIGHT SENSOR
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
if (event.light)
{
Serial.print(event.light); Serial.println(" lux");
}
else
{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
Serial.println("Sensor overload");
}
delay(250);
// GPS
// print position, altitude, speed, time/date, and satellites:
printGPSInfo();
//BLE
double LAT = tinyGPS.location.lat();
double LONG = tinyGPS.location.lng();
double LUX = event.light;
String COMMA = ",";
String Data = "";
String LATData = "";
String LONGData ="";
String LUXData ="";
if(LAT>0){
LATData = "+" + String(LAT,6)
}else{
LATData = String(LAT,6)
}
if(LONG>0){
LONGData = "+" + String(LONG,6);
}else{
LONGData = String(LONG,6);
}
LUXData = String(LUX,1);
Data += LUXData;
Data += COMMA;
Data += LATData;
Data += COMMA;
Data += LONGData;
Data.toCharArray(buffer,25); /* Adds the data*/
Serial.println(Data); /* print to Monitor */
ble_write_bytes(buffer,25); /* write to BLE */
ble_do_events();
if ( ble_available() )
{
while ( ble_available() )
{
Serial.write(ble_read());
}
Serial.println();
}
// "Smart delay" looks for GPS data while the Arduino's not doing anything else
smartDelay(1000);
delay(5000);
}
}
/***************************************************************************
*
*
*
*
*
*
*
* METHODS
*
*
*
*
*
*
*
*
**************************************************************************/
/**************************************************************************/
/*
* LIGHT SENSOR METHODS
*/
/**************************************************************************/
void displaySensorDetails(void)
{
sensor_t sensor;
tsl.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(" lux");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above! */
Serial.println("------------------------------------");
Serial.print ("Gain: "); Serial.println("Auto");
Serial.print ("Timing: "); Serial.println("13 ms");
Serial.println("------------------------------------");
}
/**************************************************************************/
/*
* GPS METHODS
*/
/**************************************************************************/
void printGPSInfo()
{
// Print latitude, longitude, altitude in feet, course, speed, date, time,
// and the number of visible satellites.
Serial.print("Lat: "); Serial.println(tinyGPS.location.lat(), 6);
Serial.print("Long: "); Serial.println(tinyGPS.location.lng(), 6);
Serial.print("Alt: "); Serial.println(tinyGPS.altitude.feet());
Serial.print("Course: "); Serial.println(tinyGPS.course.deg());
Serial.print("Speed: "); Serial.println(tinyGPS.speed.mph());
Serial.print("Date: "); printDate();
Serial.print("Time: "); printTime();
Serial.print("Sats: "); Serial.println(tinyGPS.satellites.value());
Serial.println();
}
// This custom version of delay() ensures that the tinyGPS object
// is being "fed". From the TinyGPS++ examples.
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
// If data has come in from the GPS module
while (gpsPort.available())
tinyGPS.encode(gpsPort.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
} while (millis() - start < ms);
}
// printDate() formats the date into dd/mm/yy.
void printDate()
{
Serial.print(tinyGPS.date.day());
Serial.print("/");
Serial.print(tinyGPS.date.month());
Serial.print("/");
Serial.println(tinyGPS.date.year());
}
// printTime() formats the time into "hh:mm:ss", and prints leading 0's
// where they're called for.
void printTime()
{
if ((tinyGPS.time.hour()-6) < 0)
Serial.print(tinyGPS.time.hour()+24-6);
else
Serial.print(tinyGPS.time.hour()-6);
Serial.print(":");
if (tinyGPS.time.minute() < 10) Serial.print('0');
Serial.print(tinyGPS.time.minute());
Serial.print(":");
if (tinyGPS.time.second() < 10) Serial.print('0');
Serial.println(tinyGPS.time.second());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment