Commit 5dfa8e38 authored by Patrick's avatar Patrick

inital commit of sensor code

parent ed688d1d
.pioenvs
.piolibdeps
.clang_complete
.gcc-flags.json
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < http://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < http://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < http://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choice one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#
#
# Template #1: General project. Test it using existing `platformio.ini`.
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
#
# script:
# - platformio run
#
# Template #2: The project is intended to by used as a library with examples
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
#
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
This directory is intended for the project specific (private) libraries.
PlatformIO will compile them to static libraries and link to executable file.
The source code of each library should be placed in separate directory, like
"lib/private_lib/[here are source files]".
For example, see how can be organized `Foo` and `Bar` libraries:
|--lib
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |- readme.txt --> THIS FILE
|- platformio.ini
|--src
|- main.c
Then in `src/main.c` you should use:
#include <Foo.h>
#include <Bar.h>
// rest H/C/CPP code
PlatformIO will find your libraries automatically, configure preprocessor's
include paths and build them.
More information about PlatformIO Library Dependency Finder
- http://docs.platformio.org/page/librarymanager/ldf.html
Red-Peg
-------
Arduino library for communication with the [red-peg shield](https://git.defproc.co.uk/ea/red-peg).
# Functions
To use the library, include it and create the constructor with a name that you
can remember.
```
#include <red_peg.h>
red_peg RP;
```
To use the library, call the `begin` function during `setup()`:
```
RP.begin();
```
This allows a new variable type that stores all the received data from the
red-peg slave controller: `t_SensorData`
And the functions available are:
* `t_SensorData returnedValue = RP.get(SENSOR_NAME);`
* `RP.sensorsOn()`
* `RP.sensorsOff()`
A set of helper functions will work on the `returnedValue` data to give more
useful results:
* `RP.print_data(returnedValue)`
* `RP.degC(returnedValue)`
* `RP.volts(returnedValue)`
* `RP.mA(returnedValue)`
* `RP.level(returnedValue, max_level)`
Sensor control is made by asking for one of the sensor values. `SENSOR_NAME`
options are:
* `RTC`
* `ADC1` which is equal to `MA4_20`
* `ADC2` which is equal to `ANA`
* `ADC3` which is equal to `TMP`
* `SDI` (unused)
#include <Lewis.h>
Lewis Morse;
void setup() {
// receive on pin 2, transmit on pin 13, 10 WPM
Morse.begin(2, 13, 10);
}
void loop() {
Morse.write('k');
delay(2000);
}
#include <Wire.h>
#include <MCP342x.h>
/*
Read a TMP36 sensor from MCP3428, channel 3
*/
#define BAUD 115200
// 0x68 is the default address for all MCP342x devices
// on Red-Peg, the MCP3428 address is set to 0x6F
uint8_t address = 0x6F;
MCP342x adc = MCP342x(address);
void setup(void)
{
Serial.begin(BAUD);
Serial.println("starting temp_mon");
Wire.begin();
// Reset devices
MCP342x::generalCallReset();
delay(1); // MC342x needs 300us to settle, wait 1ms
// Check device present
Wire.requestFrom(address, (uint8_t)1);
if (!Wire.available()) {
Serial.print("No device found at address ");
Serial.println(address, HEX);
while (1)
;
} else {
Serial.println("MCP3248 found");
}
}
void loop(void)
{
long value = 0;
MCP342x::Config status;
// Initiate a conversion; convertAndRead() will wait until it can be read
uint8_t err = adc.convertAndRead(MCP342x::channel3, MCP342x::oneShot,
MCP342x::resolution16, MCP342x::gain1,
1000000, value, status);
if (err) {
Serial.print("Convert error: ");
Serial.println(err);
}
else {
float voltage = value * (2.045 / 32575.0);
float temp = (voltage-0.5) * 100;
Serial.print("Value: ");
Serial.print(value);
Serial.print(", ");
Serial.print(voltage);
Serial.print("V, ");
Serial.print(temp);
Serial.print(" degC");
Serial.println();
}
delay(1000);
}
#include <Wire.h>
#include <MCP342x.h>
/*
Read a TMP36 sensor from MCP3428, channel 3
*/
#define BAUD 115200
// 0x68 is the default address for all MCP342x devices
// on Red-Peg, the MCP3428 address is set to 0x6F
uint8_t address = 0x6F;
MCP342x adc = MCP342x(address);
void setup(void)
{
Serial.begin(BAUD);
Serial.println("starting temp_mon");
Wire.begin();
// Reset devices
MCP342x::generalCallReset();
delay(1); // MC342x needs 300us to settle, wait 1ms
// Check device present
Wire.requestFrom(address, (uint8_t)1);
if (!Wire.available()) {
Serial.print("No device found at address ");
Serial.println(address, HEX);
while (1)
;
} else {
Serial.println("MCP3248 found");
}
}
void loop(void)
{
long value = 0;
MCP342x::Config status;
// Initiate a conversion; convertAndRead() will wait until it can be read
uint8_t err = adc.convertAndRead(MCP342x::channel3, MCP342x::oneShot,
MCP342x::resolution16, MCP342x::gain1,
1000000, value, status);
if (err) {
Serial.print("Convert error: ");
Serial.println(err);
}
else {
float voltage = value * (2.0 / 32575.0);
float temp = (voltage-0.5) * 100;
Serial.print("Value: ");
Serial.print(value);
Serial.print(", ");
Serial.print(voltage);
Serial.print("V, ");
Serial.print(temp);
Serial.print(" degC");
Serial.println();
}
delay(1000);
}
#define SENSOR_ACTIVE_PIN 6
#define BAUD 115200
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(BAUD);
Serial.println(F("start sensor_blink"));
delay(100);
pinMode(SENSOR_ACTIVE_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(SENSOR_ACTIVE_PIN, HIGH);
delay(1000); // wait for a second
digitalWrite(SENSOR_ACTIVE_PIN, LOW);
delay(1000); // wait for a second
}
#include <red_peg.h>
red_peg RP;
void setup() {
Serial.begin(BAUD);
Serial.println("starting rp_ma4_20");
RP.begin();
}
void loop() {
RP.sensorsOn();
delay(100);
t_SensorData depth = RP.get(MA4_20);
RP.sensorsOff();
if (depth.sensor == MA4_20) {
Serial.print("Value: ");
Serial.print(depth.reading);
Serial.print(", ");
Serial.print(RP.mA(depth));
Serial.print(" mA, ");
Serial.print(RP.level(depth, 7000L));
Serial.print(" mm");
Serial.println();
} else {
Serial.println("no return from ADC");
}
delay(1000);
}
#include <red_peg.h>
red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
}
void loop() {
RP.sensorsOn();
t_SensorData depth = RP.get(MA4_20);
RP.sensorsOff();
if (depth.sensor == MA4_20) {
Serial.print("Value: ");
Serial.print(depth.reading);
Serial.print(", ");
Serial.print(RP.mA(depth));
Serial.print(" mA, ");
Serial.print(RP.level(depth, 7000));
Serial.print(" mm");
Serial.println();
} else {
Serial.println("no return from ADC");
}
delay(1000);
}
/*
* Reading the MB 7060 sensor to give distance readings
*
* It is recommended to providing an external power supply
* as the maxbotix ultrasound sensors can draw more than
* the 0.5A that a USB port is able to supply
*
* Powering from USB only may lead to some crashes/restarts
* because of the current draw
*
*/
#include <red_peg.h>
red_peg RP;
void setup() {
delay(2000);
RP.begin();
Serial.begin(BAUD);
Serial.println("starting rp_maxbotix");
}
void loop() {
RP.sensorsOn();
// wait long enough for the ultrasound to start up
delay(500);
t_SensorData depth = RP.get(ANA);
RP.sensorsOff();
if (depth.sensor == ANA) {
RP.print_data(depth);
Serial.print("Value: ");
Serial.print(depth.reading);
Serial.print(", ");
Serial.print(RP.volts(depth));
Serial.print(" V, ");
Serial.print(RP.distance(depth));
Serial.print(" cm");
Serial.println();
delay(5000);
}
}
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <red_peg.h>
red_peg RP;
void setup () {
Serial.begin(115200);
Serial.println(F("start rp_rtc_ds1307"));
RP.begin();
RP.sensorsOn();
delay(100);
}
void loop () {
t_SensorData the_time = RP.get(RTC);
if (the_time.sensor == RTC) {
// print the current time in ISO 8601 format
Serial.print(the_time.y);
Serial.print("-");
Serial.print(the_time.m);
Serial.print("-");
Serial.print(the_time.d);
Serial.print("T");
Serial.print(the_time.hh);
Serial.print(":");
Serial.print(the_time.mm);
Serial.print(":");
Serial.print(the_time.ss); // DS1307 has whole secconds only
// assume UTC timestamp
Serial.print("Z");
Serial.println();
} else {
Serial.println(F("no RTC message received. \nLast message is:"));
RP.print_data(the_time);
}
delay(10000);
}
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <red_peg.h>
red_peg RP;
void setup () {
Serial.begin(115200);
Serial.println(F("start rp_rtc_ds1307"));
RP.begin();
}
void loop () {
RP.sensorsOn();
delay(100);
t_SensorData the_time = RP.get(RTC);
RP.sensorsOff();
if (the_time.sensor == RTC) {
// print the current time in ISO 8601 format
Serial.print(the_time.y);
Serial.print("-");
Serial.print(the_time.m);
Serial.print("-");
Serial.print(the_time.d);
Serial.print("T");
Serial.print(the_time.hh);
Serial.print(":");
Serial.print(the_time.mm);
Serial.print(":");
Serial.print(the_time.ss); // DS1307 has whole secconds only
// assume UTC timestamp
Serial.print("Z");
Serial.println();
} else {
Serial.println(F("no RTC message received. \nLast message is:"));
RP.print_data(the_time);
}
delay(5000);
}
#include <SPI.h>
#include <SdFat.h>
SdFat SD;
File myFile;
#include <red_peg.h>
red_peg RP;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(BAUD);
Serial.println("starting rp_sd_logger");
RP.begin();
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
pinMode(SD_SS, OUTPUT);
if (!SD.begin(SD_SS)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
RP.sensorsOn();
delay(100);
t_SensorData temp = RP.get(TMP);
RP.sensorsOff();
if (temp.sensor == TMP) {
myFile = SD.open("readings.csv", FILE_WRITE);
if (myFile) {
Serial.print("Writing to readings.csv: ");
// write the timestamp to the file
myFile.print(temp.y);
myFile.print("-");
myFile.print(temp.m);
myFile.print("-");
myFile.print(temp.d);
myFile.print("-");
myFile.print("T");
myFile.print(temp.hh);
myFile.print(":");
myFile.print(temp.mm);
myFile.print(":");
myFile.print(temp.ss);
myFile.print("Z");
// plus a comma delimeter
myFile.print(", ");
// followed by the temperature
myFile.print(RP.degC(temp));
// and a new line
myFile.println();
// close the file:
myFile.close();
// Then print what we sent to the Serial port
RP.print_data(temp);
Serial.println("done.");
// and wait 5 seconds
delay(5000);
}
}
}
#include <red_peg.h>
red_peg RP;
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(BAUD);
Serial.println(F("start rp_sensor_active"));
RP.begin();
}
// the loop function runs over and over again forever
void loop() {
RP.sensorsOn();
delay(1000); // wait for a second
RP.sensorsOff();
delay(1000); // wait for a second
}
#include <red_peg.h>
red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
RP.sensorsOn();
delay(100);
}
void loop() {
t_SensorData temp = RP.get(TMP);
if (temp.sensor == TMP) {
Serial.print("Value: ");
Serial.print(temp.reading);
Serial.print(", ");
Serial.print(RP.volts(temp));
Serial.print("V, ");
Serial.print(RP.degC(temp));
Serial.print(" degC");
Serial.println();
} else {
Serial.println("no return from TMP");
}
delay(5000);
}
#include <red_peg.h>
red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
}
void loop() {
t_SensorData temp_sensor = RP.get(TMP);
if (temp_sensor.the_sensor == TMP) {
Serial.print("Value: ");
Serial.print(temp_sensor.reading);
Serial.print(", ");
Serial.print(RP.volts(temp_sensor));
Serial.print("V, ");
Serial.print(RP.degC(temp_sensor));
Serial.print(" degC");
Serial.println();
} else {
Serial.println("no return from TMP");
}
delay(5000);
}
#include <red_peg.h>
red_peg RP;
void setup() {
RP.begin();
RP.sensorsOn();
delay(100);
Serial.begin(BAUD);
Serial.println("starting rp_version_check");
}