mirror of
https://github.com/Xinyuan-LilyGO/LilyGo-T-Call-SIM800.git
synced 2026-07-27 19:56:23 +00:00
Added deplibs
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
Thank you for opening an issue on an Adafruit Arduino library repository. To
|
||||
improve the speed of resolution please review the following guidelines and
|
||||
common troubleshooting steps below before creating the issue:
|
||||
|
||||
- **Do not use GitHub issues for troubleshooting projects and issues.** Instead use
|
||||
the forums at http://forums.adafruit.com to ask questions and troubleshoot why
|
||||
something isn't working as expected. In many cases the problem is a common issue
|
||||
that you will more quickly receive help from the forum community. GitHub issues
|
||||
are meant for known defects in the code. If you don't know if there is a defect
|
||||
in the code then start with troubleshooting on the forum first.
|
||||
|
||||
- **If following a tutorial or guide be sure you didn't miss a step.** Carefully
|
||||
check all of the steps and commands to run have been followed. Consult the
|
||||
forum if you're unsure or have questions about steps in a guide/tutorial.
|
||||
|
||||
- **For Arduino projects check these very common issues to ensure they don't apply**:
|
||||
|
||||
- For uploading sketches or communicating with the board make sure you're using
|
||||
a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes
|
||||
very hard to tell the difference between a data and charge cable! Try using the
|
||||
cable with other devices or swapping to another cable to confirm it is not
|
||||
the problem.
|
||||
|
||||
- **Be sure you are supplying adequate power to the board.** Check the specs of
|
||||
your board and plug in an external power supply. In many cases just
|
||||
plugging a board into your computer is not enough to power it and other
|
||||
peripherals.
|
||||
|
||||
- **Double check all soldering joints and connections.** Flakey connections
|
||||
cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints.
|
||||
|
||||
- **Ensure you are using an official Arduino or Adafruit board.** We can't
|
||||
guarantee a clone board will have the same functionality and work as expected
|
||||
with this code and don't support them.
|
||||
|
||||
If you're sure this issue is a defect in the code and checked the steps above
|
||||
please fill in the following fields to provide enough troubleshooting information.
|
||||
You may delete the guideline and text above to just leave the following details:
|
||||
|
||||
- Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE**
|
||||
|
||||
- Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO
|
||||
VERSION HERE**
|
||||
|
||||
- List the steps to reproduce the problem below (if possible attach a sketch or
|
||||
copy the sketch code in too): **LIST REPRO STEPS BELOW**
|
||||
@@ -0,0 +1,26 @@
|
||||
Thank you for creating a pull request to contribute to Adafruit's GitHub code!
|
||||
Before you open the request please review the following guidelines and tips to
|
||||
help it be more easily integrated:
|
||||
|
||||
- **Describe the scope of your change--i.e. what the change does and what parts
|
||||
of the code were modified.** This will help us understand any risks of integrating
|
||||
the code.
|
||||
|
||||
- **Describe any known limitations with your change.** For example if the change
|
||||
doesn't apply to a supported platform of the library please mention it.
|
||||
|
||||
- **Please run any tests or examples that can exercise your modified code.** We
|
||||
strive to not break users of the code and running tests/examples helps with this
|
||||
process.
|
||||
|
||||
Thank you again for contributing! We will try to test and integrate the change
|
||||
as soon as we can, but be aware we have many GitHub repositories to manage and
|
||||
can't immediately respond to every request. There is no need to bump or check in
|
||||
on a pull request (it will clutter the discussion of the request).
|
||||
|
||||
Also don't be worried if the request is closed or not integrated--sometimes the
|
||||
priorities of Adafruit's GitHub code (education, ease of use) might not match the
|
||||
priorities of the pull request. Don't fret, the open source community thrives on
|
||||
forks and GitHub makes it easy to keep your changes in a forked repo.
|
||||
|
||||
After reviewing the guidelines above you can delete this text from the pull request.
|
||||
@@ -0,0 +1,32 @@
|
||||
name: Arduino Library CI
|
||||
|
||||
on: [pull_request, push, repository_dispatch]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
repository: adafruit/ci-arduino
|
||||
path: ci
|
||||
|
||||
- name: pre-install
|
||||
run: bash ci/actions_install.sh
|
||||
|
||||
- name: test platforms
|
||||
run: python3 ci/build_platform.py main_platforms
|
||||
|
||||
- name: clang
|
||||
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .
|
||||
|
||||
- name: doxygen
|
||||
env:
|
||||
GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }}
|
||||
PRETTYNAME : "Adafruit BMP085 Library"
|
||||
run: bash ci/doxy_gen_and_deploy.sh
|
||||
@@ -0,0 +1 @@
|
||||
{"type": "library", "name": "Adafruit BMP085 Library", "version": "1.2.2", "spec": {"owner": "adafruit", "id": 525, "name": "Adafruit BMP085 Library", "requirements": null, "uri": null}}
|
||||
@@ -0,0 +1,311 @@
|
||||
/*!
|
||||
* @file Adafruit_BMP085.cpp
|
||||
*
|
||||
* @mainpage Adafruit BMP085 Library
|
||||
*
|
||||
* @section intro_sec Introduction
|
||||
*
|
||||
* This is a library for the Adafruit BMP085/BMP180 Barometric Pressure + Temp
|
||||
* sensor
|
||||
*
|
||||
* Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout
|
||||
* ----> http://www.adafruit.com/products/391
|
||||
* ----> http://www.adafruit.com/products/1603
|
||||
*
|
||||
* These displays use I2C to communicate, 2 pins are required to
|
||||
* interface
|
||||
* Adafruit invests time and resources providing this open source code,
|
||||
* please support Adafruit and open-source hardware by purchasing
|
||||
* products from Adafruit!
|
||||
*
|
||||
* @section author Author
|
||||
*
|
||||
* Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
* Updated by Samy Kamkar for cross-platform support.
|
||||
*
|
||||
* @section license License
|
||||
*
|
||||
* BSD license, all text above must be included in any redistribution
|
||||
*/
|
||||
|
||||
#include "Adafruit_BMP085.h"
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
|
||||
Adafruit_BMP085::Adafruit_BMP085() { i2c_dev = nullptr; }
|
||||
|
||||
bool Adafruit_BMP085::begin(uint8_t mode, TwoWire *wire) {
|
||||
if (mode > BMP085_ULTRAHIGHRES)
|
||||
mode = BMP085_ULTRAHIGHRES;
|
||||
oversampling = mode;
|
||||
|
||||
if (i2c_dev) {
|
||||
delete i2c_dev; // remove old interface
|
||||
}
|
||||
|
||||
i2c_dev = new Adafruit_I2CDevice(BMP085_I2CADDR, wire);
|
||||
|
||||
if (!i2c_dev->begin()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (read8(0xD0) != 0x55)
|
||||
return false;
|
||||
|
||||
/* read calibration data */
|
||||
ac1 = read16(BMP085_CAL_AC1);
|
||||
ac2 = read16(BMP085_CAL_AC2);
|
||||
ac3 = read16(BMP085_CAL_AC3);
|
||||
ac4 = read16(BMP085_CAL_AC4);
|
||||
ac5 = read16(BMP085_CAL_AC5);
|
||||
ac6 = read16(BMP085_CAL_AC6);
|
||||
|
||||
b1 = read16(BMP085_CAL_B1);
|
||||
b2 = read16(BMP085_CAL_B2);
|
||||
|
||||
mb = read16(BMP085_CAL_MB);
|
||||
mc = read16(BMP085_CAL_MC);
|
||||
md = read16(BMP085_CAL_MD);
|
||||
#if (BMP085_DEBUG == 1)
|
||||
Serial.print("ac1 = ");
|
||||
Serial.println(ac1, DEC);
|
||||
Serial.print("ac2 = ");
|
||||
Serial.println(ac2, DEC);
|
||||
Serial.print("ac3 = ");
|
||||
Serial.println(ac3, DEC);
|
||||
Serial.print("ac4 = ");
|
||||
Serial.println(ac4, DEC);
|
||||
Serial.print("ac5 = ");
|
||||
Serial.println(ac5, DEC);
|
||||
Serial.print("ac6 = ");
|
||||
Serial.println(ac6, DEC);
|
||||
|
||||
Serial.print("b1 = ");
|
||||
Serial.println(b1, DEC);
|
||||
Serial.print("b2 = ");
|
||||
Serial.println(b2, DEC);
|
||||
|
||||
Serial.print("mb = ");
|
||||
Serial.println(mb, DEC);
|
||||
Serial.print("mc = ");
|
||||
Serial.println(mc, DEC);
|
||||
Serial.print("md = ");
|
||||
Serial.println(md, DEC);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int32_t Adafruit_BMP085::computeB5(int32_t UT) {
|
||||
int32_t X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) >> 15;
|
||||
int32_t X2 = ((int32_t)mc << 11) / (X1 + (int32_t)md);
|
||||
return X1 + X2;
|
||||
}
|
||||
|
||||
uint16_t Adafruit_BMP085::readRawTemperature(void) {
|
||||
write8(BMP085_CONTROL, BMP085_READTEMPCMD);
|
||||
delay(5);
|
||||
#if BMP085_DEBUG == 1
|
||||
Serial.print("Raw temp: ");
|
||||
Serial.println(read16(BMP085_TEMPDATA));
|
||||
#endif
|
||||
return read16(BMP085_TEMPDATA);
|
||||
}
|
||||
|
||||
uint32_t Adafruit_BMP085::readRawPressure(void) {
|
||||
uint32_t raw;
|
||||
|
||||
write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));
|
||||
|
||||
if (oversampling == BMP085_ULTRALOWPOWER)
|
||||
delay(5);
|
||||
else if (oversampling == BMP085_STANDARD)
|
||||
delay(8);
|
||||
else if (oversampling == BMP085_HIGHRES)
|
||||
delay(14);
|
||||
else
|
||||
delay(26);
|
||||
|
||||
raw = read16(BMP085_PRESSUREDATA);
|
||||
|
||||
raw <<= 8;
|
||||
raw |= read8(BMP085_PRESSUREDATA + 2);
|
||||
raw >>= (8 - oversampling);
|
||||
|
||||
/* this pull broke stuff, look at it later?
|
||||
if (oversampling==0) {
|
||||
raw <<= 8;
|
||||
raw |= read8(BMP085_PRESSUREDATA+2);
|
||||
raw >>= (8 - oversampling);
|
||||
}
|
||||
*/
|
||||
|
||||
#if BMP085_DEBUG == 1
|
||||
Serial.print("Raw pressure: ");
|
||||
Serial.println(raw);
|
||||
#endif
|
||||
return raw;
|
||||
}
|
||||
|
||||
int32_t Adafruit_BMP085::readPressure(void) {
|
||||
int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
|
||||
uint32_t B4, B7;
|
||||
|
||||
UT = readRawTemperature();
|
||||
UP = readRawPressure();
|
||||
|
||||
#if BMP085_DEBUG == 1
|
||||
// use datasheet numbers!
|
||||
UT = 27898;
|
||||
UP = 23843;
|
||||
ac6 = 23153;
|
||||
ac5 = 32757;
|
||||
mc = -8711;
|
||||
md = 2868;
|
||||
b1 = 6190;
|
||||
b2 = 4;
|
||||
ac3 = -14383;
|
||||
ac2 = -72;
|
||||
ac1 = 408;
|
||||
ac4 = 32741;
|
||||
oversampling = 0;
|
||||
#endif
|
||||
|
||||
B5 = computeB5(UT);
|
||||
|
||||
#if BMP085_DEBUG == 1
|
||||
Serial.print("X1 = ");
|
||||
Serial.println(X1);
|
||||
Serial.print("X2 = ");
|
||||
Serial.println(X2);
|
||||
Serial.print("B5 = ");
|
||||
Serial.println(B5);
|
||||
#endif
|
||||
|
||||
// do pressure calcs
|
||||
B6 = B5 - 4000;
|
||||
X1 = ((int32_t)b2 * ((B6 * B6) >> 12)) >> 11;
|
||||
X2 = ((int32_t)ac2 * B6) >> 11;
|
||||
X3 = X1 + X2;
|
||||
B3 = ((((int32_t)ac1 * 4 + X3) << oversampling) + 2) / 4;
|
||||
|
||||
#if BMP085_DEBUG == 1
|
||||
Serial.print("B6 = ");
|
||||
Serial.println(B6);
|
||||
Serial.print("X1 = ");
|
||||
Serial.println(X1);
|
||||
Serial.print("X2 = ");
|
||||
Serial.println(X2);
|
||||
Serial.print("B3 = ");
|
||||
Serial.println(B3);
|
||||
#endif
|
||||
|
||||
X1 = ((int32_t)ac3 * B6) >> 13;
|
||||
X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
|
||||
X3 = ((X1 + X2) + 2) >> 2;
|
||||
B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
|
||||
B7 = ((uint32_t)UP - B3) * (uint32_t)(50000UL >> oversampling);
|
||||
|
||||
#if BMP085_DEBUG == 1
|
||||
Serial.print("X1 = ");
|
||||
Serial.println(X1);
|
||||
Serial.print("X2 = ");
|
||||
Serial.println(X2);
|
||||
Serial.print("B4 = ");
|
||||
Serial.println(B4);
|
||||
Serial.print("B7 = ");
|
||||
Serial.println(B7);
|
||||
#endif
|
||||
|
||||
if (B7 < 0x80000000) {
|
||||
p = (B7 * 2) / B4;
|
||||
} else {
|
||||
p = (B7 / B4) * 2;
|
||||
}
|
||||
X1 = (p >> 8) * (p >> 8);
|
||||
X1 = (X1 * 3038) >> 16;
|
||||
X2 = (-7357 * p) >> 16;
|
||||
|
||||
#if BMP085_DEBUG == 1
|
||||
Serial.print("p = ");
|
||||
Serial.println(p);
|
||||
Serial.print("X1 = ");
|
||||
Serial.println(X1);
|
||||
Serial.print("X2 = ");
|
||||
Serial.println(X2);
|
||||
#endif
|
||||
|
||||
p = p + ((X1 + X2 + (int32_t)3791) >> 4);
|
||||
#if BMP085_DEBUG == 1
|
||||
Serial.print("p = ");
|
||||
Serial.println(p);
|
||||
#endif
|
||||
return p;
|
||||
}
|
||||
|
||||
int32_t Adafruit_BMP085::readSealevelPressure(float altitude_meters) {
|
||||
float pressure = readPressure();
|
||||
return (int32_t)(pressure / pow(1.0 - altitude_meters / 44330, 5.255));
|
||||
}
|
||||
|
||||
float Adafruit_BMP085::readTemperature(void) {
|
||||
int32_t UT, B5; // following ds convention
|
||||
float temp;
|
||||
|
||||
UT = readRawTemperature();
|
||||
|
||||
#if BMP085_DEBUG == 1
|
||||
// use datasheet numbers!
|
||||
UT = 27898;
|
||||
ac6 = 23153;
|
||||
ac5 = 32757;
|
||||
mc = -8711;
|
||||
md = 2868;
|
||||
#endif
|
||||
|
||||
B5 = computeB5(UT);
|
||||
temp = (B5 + 8) >> 4;
|
||||
temp /= 10;
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
float Adafruit_BMP085::readAltitude(float sealevelPressure) {
|
||||
float altitude;
|
||||
|
||||
float pressure = readPressure();
|
||||
|
||||
altitude = 44330 * (1.0 - pow(pressure / sealevelPressure, 0.1903));
|
||||
|
||||
return altitude;
|
||||
}
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
uint8_t Adafruit_BMP085::read8(uint8_t a) {
|
||||
uint8_t ret;
|
||||
|
||||
// send 1 byte, reset i2c, read 1 byte
|
||||
i2c_dev->write_then_read(&a, 1, &ret, 1, true);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t Adafruit_BMP085::read16(uint8_t a) {
|
||||
uint8_t retbuf[2];
|
||||
uint16_t ret;
|
||||
|
||||
// send 1 byte, reset i2c, read 2 bytes
|
||||
// we could typecast uint16_t as uint8_t array but would need to ensure proper
|
||||
// endianness
|
||||
i2c_dev->write_then_read(&a, 1, retbuf, 2, true);
|
||||
|
||||
// write_then_read uses uint8_t array
|
||||
ret = retbuf[1] | (retbuf[0] << 8);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Adafruit_BMP085::write8(uint8_t a, uint8_t d) {
|
||||
// send d prefixed with a (a d [stop])
|
||||
i2c_dev->write(&d, 1, true, &a, 1);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*!
|
||||
* @file Adafruit_BMP085.h
|
||||
*
|
||||
* This is a library for the Adafruit BMP085/BMP180 Barometric Pressure + Temp
|
||||
* sensor
|
||||
*
|
||||
* Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout
|
||||
* ----> http://www.adafruit.com/products/391
|
||||
* ----> http://www.adafruit.com/products/1603
|
||||
*
|
||||
* These displays use I2C to communicate, 2 pins are required to
|
||||
* interface
|
||||
* Adafruit invests time and resources providing this open source code,
|
||||
* please support Adafruit and open-source hardware by purchasing
|
||||
* products from Adafruit!
|
||||
*
|
||||
* Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
* BSD license, all text above must be included in any redistribution
|
||||
*/
|
||||
|
||||
#ifndef ADAFRUIT_BMP085_H
|
||||
#define ADAFRUIT_BMP085_H
|
||||
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#define BMP085_DEBUG 0 //!< Debug mode
|
||||
|
||||
#define BMP085_I2CADDR 0x77 //!< BMP085 I2C address
|
||||
|
||||
#define BMP085_ULTRALOWPOWER 0 //!< Ultra low power mode
|
||||
#define BMP085_STANDARD 1 //!< Standard mode
|
||||
#define BMP085_HIGHRES 2 //!< High-res mode
|
||||
#define BMP085_ULTRAHIGHRES 3 //!< Ultra high-res mode
|
||||
#define BMP085_CAL_AC1 0xAA //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_AC2 0xAC //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_AC3 0xAE //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_AC4 0xB0 //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_AC5 0xB2 //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_AC6 0xB4 //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_B1 0xB6 //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_B2 0xB8 //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_MB 0xBA //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_MC 0xBC //!< R Calibration data (16 bits)
|
||||
#define BMP085_CAL_MD 0xBE //!< R Calibration data (16 bits)
|
||||
|
||||
#define BMP085_CONTROL 0xF4 //!< Control register
|
||||
#define BMP085_TEMPDATA 0xF6 //!< Temperature data register
|
||||
#define BMP085_PRESSUREDATA 0xF6 //!< Pressure data register
|
||||
#define BMP085_READTEMPCMD 0x2E //!< Read temperature control register value
|
||||
#define BMP085_READPRESSURECMD 0x34 //!< Read pressure control register value
|
||||
|
||||
/*!
|
||||
* @brief Main BMP085 class
|
||||
*/
|
||||
class Adafruit_BMP085 {
|
||||
public:
|
||||
Adafruit_BMP085();
|
||||
/*!
|
||||
* @brief Starts I2C connection
|
||||
* @param mode Mode to set, ultra high-res by default
|
||||
* @param wire The I2C interface to use, defaults to Wire
|
||||
* @return Returns true if successful
|
||||
*/
|
||||
bool begin(uint8_t mode = BMP085_ULTRAHIGHRES, TwoWire *wire = &Wire);
|
||||
/*!
|
||||
* @brief Gets the temperature over I2C from the BMP085
|
||||
* @return Returns the temperature
|
||||
*/
|
||||
float readTemperature(void);
|
||||
/*!
|
||||
* @brief Gets the pressure over I2C from the BMP085
|
||||
* @return Returns the pressure
|
||||
*/
|
||||
int32_t readPressure(void);
|
||||
/*!
|
||||
* @brief Calculates the pressure at sea level
|
||||
* @param altitude_meters Current altitude (in meters)
|
||||
* @return Returns the calculated pressure at sea level
|
||||
*/
|
||||
int32_t readSealevelPressure(float altitude_meters = 0);
|
||||
/*!
|
||||
* @brief Reads the altitude
|
||||
* @param sealevelPressure Pressure at sea level, measured in pascals
|
||||
* @return Returns the altitude
|
||||
*/
|
||||
float readAltitude(float sealevelPressure = 101325); // std atmosphere
|
||||
/*!
|
||||
* @brief Reads the raw temperature
|
||||
* @return Returns the raw temperature
|
||||
*/
|
||||
uint16_t readRawTemperature(void);
|
||||
/*!
|
||||
* @brief Reads the raw pressure
|
||||
* @return Returns the raw pressure
|
||||
*/
|
||||
uint32_t readRawPressure(void);
|
||||
|
||||
private:
|
||||
int32_t computeB5(int32_t UT);
|
||||
uint8_t read8(uint8_t addr);
|
||||
uint16_t read16(uint8_t addr);
|
||||
void write8(uint8_t addr, uint8_t data);
|
||||
|
||||
Adafruit_I2CDevice *i2c_dev;
|
||||
uint8_t oversampling;
|
||||
|
||||
int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
|
||||
uint16_t ac4, ac5, ac6;
|
||||
};
|
||||
|
||||
#endif // ADAFRUIT_BMP085_H
|
||||
@@ -0,0 +1,32 @@
|
||||
# Adafruit BMP085 Library [](https://github.com/adafruit/Adafruit-BMP085-Library/actions)[](http://adafruit.github.io/Adafruit-BMP085-Library/html/index.html)
|
||||
|
||||
This is a library for the Adafruit BMP085/BMP180 Barometric Pressure + Temp sensor
|
||||
|
||||
Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout
|
||||
----> http://www.adafruit.com/products/391
|
||||
----> http://www.adafruit.com/products/1603
|
||||
|
||||
These displays use I2C to communicate, 2 pins are required to interface
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Check out the links above for our tutorials and wiring diagrams
|
||||
|
||||
Requires the https://github.com/adafruit/Adafruit_BusIO library for I2C abstraction
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
BSD license, all text above must be included in any redistribution
|
||||
|
||||
To download. click the DOWNLOAD ZIP button, rename the uncompressed folder Adafruit_BMP085.
|
||||
Check that the Adafruit_BMP085 folder contains Adafruit_BMP085.cpp and Adafruit_BMP085.h
|
||||
|
||||
Place the Adafruit_BMP085 library folder your arduinosketchfolder/libraries/ folder.
|
||||
You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
||||
|
||||
We also have a great tutorial on Arduino library installation at:
|
||||
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <Adafruit_BMP085.h>
|
||||
|
||||
/***************************************************
|
||||
This is an example for the BMP085 Barometric Pressure & Temp Sensor
|
||||
|
||||
Designed specifically to work with the Adafruit BMP085 Breakout
|
||||
----> https://www.adafruit.com/products/391
|
||||
|
||||
These pressure and temperature sensors use I2C to communicate, 2 pins
|
||||
are required to interface
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
BSD license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
// Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
|
||||
// Connect GND to Ground
|
||||
// Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
|
||||
// Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
|
||||
// EOC is not used, it signifies an end of conversion
|
||||
// XCLR is a reset pin, also not used here
|
||||
|
||||
Adafruit_BMP085 bmp;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
if (!bmp.begin()) {
|
||||
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
|
||||
while (1) {}
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print("Temperature = ");
|
||||
Serial.print(bmp.readTemperature());
|
||||
Serial.println(" *C");
|
||||
|
||||
Serial.print("Pressure = ");
|
||||
Serial.print(bmp.readPressure());
|
||||
Serial.println(" Pa");
|
||||
|
||||
// Calculate altitude assuming 'standard' barometric
|
||||
// pressure of 1013.25 millibar = 101325 Pascal
|
||||
Serial.print("Altitude = ");
|
||||
Serial.print(bmp.readAltitude());
|
||||
Serial.println(" meters");
|
||||
|
||||
Serial.print("Pressure at sealevel (calculated) = ");
|
||||
Serial.print(bmp.readSealevelPressure());
|
||||
Serial.println(" Pa");
|
||||
|
||||
// you can get a more precise measurement of altitude
|
||||
// if you know the current sea level pressure which will
|
||||
// vary with weather and such. If it is 1015 millibars
|
||||
// that is equal to 101500 Pascals.
|
||||
Serial.print("Real altitude = ");
|
||||
Serial.print(bmp.readAltitude(101500));
|
||||
Serial.println(" meters");
|
||||
|
||||
Serial.println();
|
||||
delay(500);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
name=Adafruit BMP085 Library
|
||||
version=1.2.2
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=A powerful but easy to use BMP085/BMP180 Library
|
||||
paragraph=A powerful but easy to use BMP085/BMP180 Library
|
||||
category=Sensors
|
||||
url=https://github.com/adafruit/Adafruit-BMP085-Library
|
||||
architectures=*
|
||||
depends=Adafruit Unified Sensor, Adafruit BusIO
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
Thank you for opening an issue on an Adafruit Arduino library repository. To
|
||||
improve the speed of resolution please review the following guidelines and
|
||||
common troubleshooting steps below before creating the issue:
|
||||
|
||||
- **Do not use GitHub issues for troubleshooting projects and issues.** Instead use
|
||||
the forums at http://forums.adafruit.com to ask questions and troubleshoot why
|
||||
something isn't working as expected. In many cases the problem is a common issue
|
||||
that you will more quickly receive help from the forum community. GitHub issues
|
||||
are meant for known defects in the code. If you don't know if there is a defect
|
||||
in the code then start with troubleshooting on the forum first.
|
||||
|
||||
- **If following a tutorial or guide be sure you didn't miss a step.** Carefully
|
||||
check all of the steps and commands to run have been followed. Consult the
|
||||
forum if you're unsure or have questions about steps in a guide/tutorial.
|
||||
|
||||
- **For Arduino projects check these very common issues to ensure they don't apply**:
|
||||
|
||||
- For uploading sketches or communicating with the board make sure you're using
|
||||
a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes
|
||||
very hard to tell the difference between a data and charge cable! Try using the
|
||||
cable with other devices or swapping to another cable to confirm it is not
|
||||
the problem.
|
||||
|
||||
- **Be sure you are supplying adequate power to the board.** Check the specs of
|
||||
your board and plug in an external power supply. In many cases just
|
||||
plugging a board into your computer is not enough to power it and other
|
||||
peripherals.
|
||||
|
||||
- **Double check all soldering joints and connections.** Flakey connections
|
||||
cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints.
|
||||
|
||||
- **Ensure you are using an official Arduino or Adafruit board.** We can't
|
||||
guarantee a clone board will have the same functionality and work as expected
|
||||
with this code and don't support them.
|
||||
|
||||
If you're sure this issue is a defect in the code and checked the steps above
|
||||
please fill in the following fields to provide enough troubleshooting information.
|
||||
You may delete the guideline and text above to just leave the following details:
|
||||
|
||||
- Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE**
|
||||
|
||||
- Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO
|
||||
VERSION HERE**
|
||||
|
||||
- List the steps to reproduce the problem below (if possible attach a sketch or
|
||||
copy the sketch code in too): **LIST REPRO STEPS BELOW**
|
||||
@@ -0,0 +1,26 @@
|
||||
Thank you for creating a pull request to contribute to Adafruit's GitHub code!
|
||||
Before you open the request please review the following guidelines and tips to
|
||||
help it be more easily integrated:
|
||||
|
||||
- **Describe the scope of your change--i.e. what the change does and what parts
|
||||
of the code were modified.** This will help us understand any risks of integrating
|
||||
the code.
|
||||
|
||||
- **Describe any known limitations with your change.** For example if the change
|
||||
doesn't apply to a supported platform of the library please mention it.
|
||||
|
||||
- **Please run any tests or examples that can exercise your modified code.** We
|
||||
strive to not break users of the code and running tests/examples helps with this
|
||||
process.
|
||||
|
||||
Thank you again for contributing! We will try to test and integrate the change
|
||||
as soon as we can, but be aware we have many GitHub repositories to manage and
|
||||
can't immediately respond to every request. There is no need to bump or check in
|
||||
on a pull request (it will clutter the discussion of the request).
|
||||
|
||||
Also don't be worried if the request is closed or not integrated--sometimes the
|
||||
priorities of Adafruit's GitHub code (education, ease of use) might not match the
|
||||
priorities of the pull request. Don't fret, the open source community thrives on
|
||||
forks and GitHub makes it easy to keep your changes in a forked repo.
|
||||
|
||||
After reviewing the guidelines above you can delete this text from the pull request.
|
||||
@@ -0,0 +1,33 @@
|
||||
name: Arduino Library CI
|
||||
|
||||
on: [pull_request, push, repository_dispatch]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
repository: adafruit/ci-arduino
|
||||
path: ci
|
||||
|
||||
- name: Install the prerequisites
|
||||
run: bash ci/actions_install.sh
|
||||
|
||||
- name: Check for correct code formatting with clang-format
|
||||
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .
|
||||
|
||||
- name: Check for correct documentation with doxygen
|
||||
env:
|
||||
GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }}
|
||||
PRETTYNAME : "Adafruit Bus IO Library"
|
||||
run: bash ci/doxy_gen_and_deploy.sh
|
||||
|
||||
- name: Test the code on supported platforms
|
||||
run: python3 ci/build_platform.py main_platforms zero feather32u4
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"type": "library", "name": "Adafruit BusIO", "version": "1.14.3", "spec": {"owner": "adafruit", "id": 6214, "name": "Adafruit BusIO", "requirements": null, "uri": null}}
|
||||
@@ -0,0 +1,365 @@
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
|
||||
#if !defined(SPI_INTERFACES_COUNT) || \
|
||||
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
|
||||
|
||||
/*!
|
||||
* @brief Create a register we access over an I2C Device (which defines the
|
||||
* bus and address)
|
||||
* @param i2cdevice The I2CDevice to use for underlying I2C access
|
||||
* @param reg_addr The address pointer value for the I2C/SMBus register, can
|
||||
* be 8 or 16 bits
|
||||
* @param width The width of the register data itself, defaults to 1 byte
|
||||
* @param byteorder The byte order of the register (used when width is > 1),
|
||||
* defaults to LSBFIRST
|
||||
* @param address_width The width of the register address itself, defaults
|
||||
* to 1 byte
|
||||
*/
|
||||
Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
|
||||
uint16_t reg_addr,
|
||||
uint8_t width,
|
||||
uint8_t byteorder,
|
||||
uint8_t address_width) {
|
||||
_i2cdevice = i2cdevice;
|
||||
_spidevice = nullptr;
|
||||
_addrwidth = address_width;
|
||||
_address = reg_addr;
|
||||
_byteorder = byteorder;
|
||||
_width = width;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Create a register we access over an SPI Device (which defines the
|
||||
* bus and CS pin)
|
||||
* @param spidevice The SPIDevice to use for underlying SPI access
|
||||
* @param reg_addr The address pointer value for the SPI register, can
|
||||
* be 8 or 16 bits
|
||||
* @param type The method we use to read/write data to SPI (which is not
|
||||
* as well defined as I2C)
|
||||
* @param width The width of the register data itself, defaults to 1 byte
|
||||
* @param byteorder The byte order of the register (used when width is > 1),
|
||||
* defaults to LSBFIRST
|
||||
* @param address_width The width of the register address itself, defaults
|
||||
* to 1 byte
|
||||
*/
|
||||
Adafruit_BusIO_Register::Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice,
|
||||
uint16_t reg_addr,
|
||||
Adafruit_BusIO_SPIRegType type,
|
||||
uint8_t width,
|
||||
uint8_t byteorder,
|
||||
uint8_t address_width) {
|
||||
_spidevice = spidevice;
|
||||
_spiregtype = type;
|
||||
_i2cdevice = nullptr;
|
||||
_addrwidth = address_width;
|
||||
_address = reg_addr;
|
||||
_byteorder = byteorder;
|
||||
_width = width;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Create a register we access over an I2C or SPI Device. This is a
|
||||
* handy function because we can pass in nullptr for the unused interface,
|
||||
* allowing libraries to mass-define all the registers
|
||||
* @param i2cdevice The I2CDevice to use for underlying I2C access, if
|
||||
* nullptr we use SPI
|
||||
* @param spidevice The SPIDevice to use for underlying SPI access, if
|
||||
* nullptr we use I2C
|
||||
* @param reg_addr The address pointer value for the I2C/SMBus/SPI register,
|
||||
* can be 8 or 16 bits
|
||||
* @param type The method we use to read/write data to SPI (which is not
|
||||
* as well defined as I2C)
|
||||
* @param width The width of the register data itself, defaults to 1 byte
|
||||
* @param byteorder The byte order of the register (used when width is > 1),
|
||||
* defaults to LSBFIRST
|
||||
* @param address_width The width of the register address itself, defaults
|
||||
* to 1 byte
|
||||
*/
|
||||
Adafruit_BusIO_Register::Adafruit_BusIO_Register(
|
||||
Adafruit_I2CDevice *i2cdevice, Adafruit_SPIDevice *spidevice,
|
||||
Adafruit_BusIO_SPIRegType type, uint16_t reg_addr, uint8_t width,
|
||||
uint8_t byteorder, uint8_t address_width) {
|
||||
_spidevice = spidevice;
|
||||
_i2cdevice = i2cdevice;
|
||||
_spiregtype = type;
|
||||
_addrwidth = address_width;
|
||||
_address = reg_addr;
|
||||
_byteorder = byteorder;
|
||||
_width = width;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write a buffer of data to the register location
|
||||
* @param buffer Pointer to data to write
|
||||
* @param len Number of bytes to write
|
||||
* @return True on successful write (only really useful for I2C as SPI is
|
||||
* uncheckable)
|
||||
*/
|
||||
bool Adafruit_BusIO_Register::write(uint8_t *buffer, uint8_t len) {
|
||||
|
||||
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
|
||||
(uint8_t)(_address >> 8)};
|
||||
|
||||
if (_i2cdevice) {
|
||||
return _i2cdevice->write(buffer, len, true, addrbuffer, _addrwidth);
|
||||
}
|
||||
if (_spidevice) {
|
||||
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
|
||||
// very special case!
|
||||
|
||||
// pass the special opcode address which we set as the high byte of the
|
||||
// regaddr
|
||||
addrbuffer[0] =
|
||||
(uint8_t)(_address >> 8) & ~0x01; // set bottom bit low to write
|
||||
// the 'actual' reg addr is the second byte then
|
||||
addrbuffer[1] = (uint8_t)(_address & 0xFF);
|
||||
// the address appears to be a byte longer
|
||||
return _spidevice->write(buffer, len, addrbuffer, _addrwidth + 1);
|
||||
}
|
||||
|
||||
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
|
||||
addrbuffer[0] &= ~0x80;
|
||||
}
|
||||
if (_spiregtype == ADDRBIT8_HIGH_TOWRITE) {
|
||||
addrbuffer[0] |= 0x80;
|
||||
}
|
||||
if (_spiregtype == AD8_HIGH_TOREAD_AD7_HIGH_TOINC) {
|
||||
addrbuffer[0] &= ~0x80;
|
||||
addrbuffer[0] |= 0x40;
|
||||
}
|
||||
return _spidevice->write(buffer, len, addrbuffer, _addrwidth);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write up to 4 bytes of data to the register location
|
||||
* @param value Data to write
|
||||
* @param numbytes How many bytes from 'value' to write
|
||||
* @return True on successful write (only really useful for I2C as SPI is
|
||||
* uncheckable)
|
||||
*/
|
||||
bool Adafruit_BusIO_Register::write(uint32_t value, uint8_t numbytes) {
|
||||
if (numbytes == 0) {
|
||||
numbytes = _width;
|
||||
}
|
||||
if (numbytes > 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// store a copy
|
||||
_cached = value;
|
||||
|
||||
for (int i = 0; i < numbytes; i++) {
|
||||
if (_byteorder == LSBFIRST) {
|
||||
_buffer[i] = value & 0xFF;
|
||||
} else {
|
||||
_buffer[numbytes - i - 1] = value & 0xFF;
|
||||
}
|
||||
value >>= 8;
|
||||
}
|
||||
return write(_buffer, numbytes);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Read data from the register location. This does not do any error
|
||||
* checking!
|
||||
* @return Returns 0xFFFFFFFF on failure, value otherwise
|
||||
*/
|
||||
uint32_t Adafruit_BusIO_Register::read(void) {
|
||||
if (!read(_buffer, _width)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t value = 0;
|
||||
|
||||
for (int i = 0; i < _width; i++) {
|
||||
value <<= 8;
|
||||
if (_byteorder == LSBFIRST) {
|
||||
value |= _buffer[_width - i - 1];
|
||||
} else {
|
||||
value |= _buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Read cached data from last time we wrote to this register
|
||||
* @return Returns 0xFFFFFFFF on failure, value otherwise
|
||||
*/
|
||||
uint32_t Adafruit_BusIO_Register::readCached(void) { return _cached; }
|
||||
|
||||
/*!
|
||||
* @brief Read a buffer of data from the register location
|
||||
* @param buffer Pointer to data to read into
|
||||
* @param len Number of bytes to read
|
||||
* @return True on successful write (only really useful for I2C as SPI is
|
||||
* uncheckable)
|
||||
*/
|
||||
bool Adafruit_BusIO_Register::read(uint8_t *buffer, uint8_t len) {
|
||||
uint8_t addrbuffer[2] = {(uint8_t)(_address & 0xFF),
|
||||
(uint8_t)(_address >> 8)};
|
||||
|
||||
if (_i2cdevice) {
|
||||
return _i2cdevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
|
||||
}
|
||||
if (_spidevice) {
|
||||
if (_spiregtype == ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE) {
|
||||
// very special case!
|
||||
|
||||
// pass the special opcode address which we set as the high byte of the
|
||||
// regaddr
|
||||
addrbuffer[0] =
|
||||
(uint8_t)(_address >> 8) | 0x01; // set bottom bit high to read
|
||||
// the 'actual' reg addr is the second byte then
|
||||
addrbuffer[1] = (uint8_t)(_address & 0xFF);
|
||||
// the address appears to be a byte longer
|
||||
return _spidevice->write_then_read(addrbuffer, _addrwidth + 1, buffer,
|
||||
len);
|
||||
}
|
||||
if (_spiregtype == ADDRBIT8_HIGH_TOREAD) {
|
||||
addrbuffer[0] |= 0x80;
|
||||
}
|
||||
if (_spiregtype == ADDRBIT8_HIGH_TOWRITE) {
|
||||
addrbuffer[0] &= ~0x80;
|
||||
}
|
||||
if (_spiregtype == AD8_HIGH_TOREAD_AD7_HIGH_TOINC) {
|
||||
addrbuffer[0] |= 0x80 | 0x40;
|
||||
}
|
||||
return _spidevice->write_then_read(addrbuffer, _addrwidth, buffer, len);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Read 2 bytes of data from the register location
|
||||
* @param value Pointer to uint16_t variable to read into
|
||||
* @return True on successful write (only really useful for I2C as SPI is
|
||||
* uncheckable)
|
||||
*/
|
||||
bool Adafruit_BusIO_Register::read(uint16_t *value) {
|
||||
if (!read(_buffer, 2)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_byteorder == LSBFIRST) {
|
||||
*value = _buffer[1];
|
||||
*value <<= 8;
|
||||
*value |= _buffer[0];
|
||||
} else {
|
||||
*value = _buffer[0];
|
||||
*value <<= 8;
|
||||
*value |= _buffer[1];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Read 1 byte of data from the register location
|
||||
* @param value Pointer to uint8_t variable to read into
|
||||
* @return True on successful write (only really useful for I2C as SPI is
|
||||
* uncheckable)
|
||||
*/
|
||||
bool Adafruit_BusIO_Register::read(uint8_t *value) {
|
||||
if (!read(_buffer, 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*value = _buffer[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Pretty printer for this register
|
||||
* @param s The Stream to print to, defaults to &Serial
|
||||
*/
|
||||
void Adafruit_BusIO_Register::print(Stream *s) {
|
||||
uint32_t val = read();
|
||||
s->print("0x");
|
||||
s->print(val, HEX);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Pretty printer for this register
|
||||
* @param s The Stream to print to, defaults to &Serial
|
||||
*/
|
||||
void Adafruit_BusIO_Register::println(Stream *s) {
|
||||
print(s);
|
||||
s->println();
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Create a slice of the register that we can address without
|
||||
* touching other bits
|
||||
* @param reg The Adafruit_BusIO_Register which defines the bus/register
|
||||
* @param bits The number of bits wide we are slicing
|
||||
* @param shift The number of bits that our bit-slice is shifted from LSB
|
||||
*/
|
||||
Adafruit_BusIO_RegisterBits::Adafruit_BusIO_RegisterBits(
|
||||
Adafruit_BusIO_Register *reg, uint8_t bits, uint8_t shift) {
|
||||
_register = reg;
|
||||
_bits = bits;
|
||||
_shift = shift;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Read 4 bytes of data from the register
|
||||
* @return data The 4 bytes to read
|
||||
*/
|
||||
uint32_t Adafruit_BusIO_RegisterBits::read(void) {
|
||||
uint32_t val = _register->read();
|
||||
val >>= _shift;
|
||||
return val & ((1 << (_bits)) - 1);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write 4 bytes of data to the register
|
||||
* @param data The 4 bytes to write
|
||||
* @return True on successful write (only really useful for I2C as SPI is
|
||||
* uncheckable)
|
||||
*/
|
||||
bool Adafruit_BusIO_RegisterBits::write(uint32_t data) {
|
||||
uint32_t val = _register->read();
|
||||
|
||||
// mask off the data before writing
|
||||
uint32_t mask = (1 << (_bits)) - 1;
|
||||
data &= mask;
|
||||
|
||||
mask <<= _shift;
|
||||
val &= ~mask; // remove the current data at that spot
|
||||
val |= data << _shift; // and add in the new data
|
||||
|
||||
return _register->write(val, _register->width());
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief The width of the register data, helpful for doing calculations
|
||||
* @returns The data width used when initializing the register
|
||||
*/
|
||||
uint8_t Adafruit_BusIO_Register::width(void) { return _width; }
|
||||
|
||||
/*!
|
||||
* @brief Set the default width of data
|
||||
* @param width the default width of data read from register
|
||||
*/
|
||||
void Adafruit_BusIO_Register::setWidth(uint8_t width) { _width = width; }
|
||||
|
||||
/*!
|
||||
* @brief Set register address
|
||||
* @param address the address from register
|
||||
*/
|
||||
void Adafruit_BusIO_Register::setAddress(uint16_t address) {
|
||||
_address = address;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Set the width of register address
|
||||
* @param address_width the width for register address
|
||||
*/
|
||||
void Adafruit_BusIO_Register::setAddressWidth(uint16_t address_width) {
|
||||
_addrwidth = address_width;
|
||||
}
|
||||
|
||||
#endif // SPI exists
|
||||
@@ -0,0 +1,105 @@
|
||||
#ifndef Adafruit_BusIO_Register_h
|
||||
#define Adafruit_BusIO_Register_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if !defined(SPI_INTERFACES_COUNT) || \
|
||||
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
|
||||
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
|
||||
typedef enum _Adafruit_BusIO_SPIRegType {
|
||||
ADDRBIT8_HIGH_TOREAD = 0,
|
||||
/*!<
|
||||
* ADDRBIT8_HIGH_TOREAD
|
||||
* When reading a register you must actually send the value 0x80 + register
|
||||
* address to the device. e.g. To read the register 0x0B the register value
|
||||
* 0x8B is sent and to write 0x0B is sent.
|
||||
*/
|
||||
AD8_HIGH_TOREAD_AD7_HIGH_TOINC = 1,
|
||||
|
||||
/*!<
|
||||
* ADDRBIT8_HIGH_TOWRITE
|
||||
* When writing to a register you must actually send the value 0x80 +
|
||||
* the register address to the device. e.g. To write to the register 0x19 the
|
||||
* register value 0x99 is sent and to read 0x19 is sent.
|
||||
*/
|
||||
ADDRBIT8_HIGH_TOWRITE = 2,
|
||||
|
||||
/*!<
|
||||
* ADDRESSED_OPCODE_LOWBIT_TO_WRITE
|
||||
* Used by the MCP23S series, we send 0x40 |'rd with the opcode
|
||||
* Then set the lowest bit to write
|
||||
*/
|
||||
ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE = 3,
|
||||
|
||||
} Adafruit_BusIO_SPIRegType;
|
||||
|
||||
/*!
|
||||
* @brief The class which defines a device register (a location to read/write
|
||||
* data from)
|
||||
*/
|
||||
class Adafruit_BusIO_Register {
|
||||
public:
|
||||
Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice, uint16_t reg_addr,
|
||||
uint8_t width = 1, uint8_t byteorder = LSBFIRST,
|
||||
uint8_t address_width = 1);
|
||||
|
||||
Adafruit_BusIO_Register(Adafruit_SPIDevice *spidevice, uint16_t reg_addr,
|
||||
Adafruit_BusIO_SPIRegType type, uint8_t width = 1,
|
||||
uint8_t byteorder = LSBFIRST,
|
||||
uint8_t address_width = 1);
|
||||
|
||||
Adafruit_BusIO_Register(Adafruit_I2CDevice *i2cdevice,
|
||||
Adafruit_SPIDevice *spidevice,
|
||||
Adafruit_BusIO_SPIRegType type, uint16_t reg_addr,
|
||||
uint8_t width = 1, uint8_t byteorder = LSBFIRST,
|
||||
uint8_t address_width = 1);
|
||||
|
||||
bool read(uint8_t *buffer, uint8_t len);
|
||||
bool read(uint8_t *value);
|
||||
bool read(uint16_t *value);
|
||||
uint32_t read(void);
|
||||
uint32_t readCached(void);
|
||||
bool write(uint8_t *buffer, uint8_t len);
|
||||
bool write(uint32_t value, uint8_t numbytes = 0);
|
||||
|
||||
uint8_t width(void);
|
||||
|
||||
void setWidth(uint8_t width);
|
||||
void setAddress(uint16_t address);
|
||||
void setAddressWidth(uint16_t address_width);
|
||||
|
||||
void print(Stream *s = &Serial);
|
||||
void println(Stream *s = &Serial);
|
||||
|
||||
private:
|
||||
Adafruit_I2CDevice *_i2cdevice;
|
||||
Adafruit_SPIDevice *_spidevice;
|
||||
Adafruit_BusIO_SPIRegType _spiregtype;
|
||||
uint16_t _address;
|
||||
uint8_t _width, _addrwidth, _byteorder;
|
||||
uint8_t _buffer[4]; // we won't support anything larger than uint32 for
|
||||
// non-buffered read
|
||||
uint32_t _cached = 0;
|
||||
};
|
||||
|
||||
/*!
|
||||
* @brief The class which defines a slice of bits from within a device register
|
||||
* (a location to read/write data from)
|
||||
*/
|
||||
class Adafruit_BusIO_RegisterBits {
|
||||
public:
|
||||
Adafruit_BusIO_RegisterBits(Adafruit_BusIO_Register *reg, uint8_t bits,
|
||||
uint8_t shift);
|
||||
bool write(uint32_t value);
|
||||
uint32_t read(void);
|
||||
|
||||
private:
|
||||
Adafruit_BusIO_Register *_register;
|
||||
uint8_t _bits, _shift;
|
||||
};
|
||||
|
||||
#endif // SPI exists
|
||||
#endif // BusIO_Register_h
|
||||
@@ -0,0 +1,313 @@
|
||||
#include "Adafruit_I2CDevice.h"
|
||||
|
||||
//#define DEBUG_SERIAL Serial
|
||||
|
||||
/*!
|
||||
* @brief Create an I2C device at a given address
|
||||
* @param addr The 7-bit I2C address for the device
|
||||
* @param theWire The I2C bus to use, defaults to &Wire
|
||||
*/
|
||||
Adafruit_I2CDevice::Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire) {
|
||||
_addr = addr;
|
||||
_wire = theWire;
|
||||
_begun = false;
|
||||
#ifdef ARDUINO_ARCH_SAMD
|
||||
_maxBufferSize = 250; // as defined in Wire.h's RingBuffer
|
||||
#elif defined(ESP32)
|
||||
_maxBufferSize = I2C_BUFFER_LENGTH;
|
||||
#else
|
||||
_maxBufferSize = 32;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initializes and does basic address detection
|
||||
* @param addr_detect Whether we should attempt to detect the I2C address
|
||||
* with a scan. 99% of sensors/devices don't mind but once in a while, they spaz
|
||||
* on a scan!
|
||||
* @return True if I2C initialized and a device with the addr found
|
||||
*/
|
||||
bool Adafruit_I2CDevice::begin(bool addr_detect) {
|
||||
_wire->begin();
|
||||
_begun = true;
|
||||
|
||||
if (addr_detect) {
|
||||
return detected();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief De-initialize device, turn off the Wire interface
|
||||
*/
|
||||
void Adafruit_I2CDevice::end(void) {
|
||||
// Not all port implement Wire::end(), such as
|
||||
// - ESP8266
|
||||
// - AVR core without WIRE_HAS_END
|
||||
// - ESP32: end() is implemented since 2.0.1 which is latest at the moment.
|
||||
// Temporarily disable for now to give time for user to update.
|
||||
#if !(defined(ESP8266) || \
|
||||
(defined(ARDUINO_ARCH_AVR) && !defined(WIRE_HAS_END)) || \
|
||||
defined(ARDUINO_ARCH_ESP32))
|
||||
_wire->end();
|
||||
_begun = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Scans I2C for the address - note will give a false-positive
|
||||
* if there's no pullups on I2C
|
||||
* @return True if I2C initialized and a device with the addr found
|
||||
*/
|
||||
bool Adafruit_I2CDevice::detected(void) {
|
||||
// Init I2C if not done yet
|
||||
if (!_begun && !begin()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// A basic scanner, see if it ACK's
|
||||
_wire->beginTransmission(_addr);
|
||||
if (_wire->endTransmission() == 0) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("Detected"));
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("Not detected"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write a buffer or two to the I2C device. Cannot be more than
|
||||
* maxBufferSize() bytes.
|
||||
* @param buffer Pointer to buffer of data to write. This is const to
|
||||
* ensure the content of this buffer doesn't change.
|
||||
* @param len Number of bytes from buffer to write
|
||||
* @param prefix_buffer Pointer to optional array of data to write before
|
||||
* buffer. Cannot be more than maxBufferSize() bytes. This is const to
|
||||
* ensure the content of this buffer doesn't change.
|
||||
* @param prefix_len Number of bytes from prefix buffer to write
|
||||
* @param stop Whether to send an I2C STOP signal on write
|
||||
* @return True if write was successful, otherwise false.
|
||||
*/
|
||||
bool Adafruit_I2CDevice::write(const uint8_t *buffer, size_t len, bool stop,
|
||||
const uint8_t *prefix_buffer,
|
||||
size_t prefix_len) {
|
||||
if ((len + prefix_len) > maxBufferSize()) {
|
||||
// currently not guaranteed to work if more than 32 bytes!
|
||||
// we will need to find out if some platforms have larger
|
||||
// I2C buffer sizes :/
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice could not write such a large buffer"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
_wire->beginTransmission(_addr);
|
||||
|
||||
// Write the prefix data (usually an address)
|
||||
if ((prefix_len != 0) && (prefix_buffer != nullptr)) {
|
||||
if (_wire->write(prefix_buffer, prefix_len) != prefix_len) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the data itself
|
||||
if (_wire->write(buffer, len) != len) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println(F("\tI2CDevice failed to write"));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
|
||||
DEBUG_SERIAL.print(F("\tI2CWRITE @ 0x"));
|
||||
DEBUG_SERIAL.print(_addr, HEX);
|
||||
DEBUG_SERIAL.print(F(" :: "));
|
||||
if ((prefix_len != 0) && (prefix_buffer != nullptr)) {
|
||||
for (uint16_t i = 0; i < prefix_len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(prefix_buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
}
|
||||
}
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (i % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
|
||||
if (stop) {
|
||||
DEBUG_SERIAL.print("\tSTOP");
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_wire->endTransmission(stop) == 0) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println();
|
||||
// DEBUG_SERIAL.println("Sent!");
|
||||
#endif
|
||||
return true;
|
||||
} else {
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.println("\tFailed to send!");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Read from I2C into a buffer from the I2C device.
|
||||
* Cannot be more than maxBufferSize() bytes.
|
||||
* @param buffer Pointer to buffer of data to read into
|
||||
* @param len Number of bytes from buffer to read.
|
||||
* @param stop Whether to send an I2C STOP signal on read
|
||||
* @return True if read was successful, otherwise false.
|
||||
*/
|
||||
bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {
|
||||
size_t pos = 0;
|
||||
while (pos < len) {
|
||||
size_t read_len =
|
||||
((len - pos) > maxBufferSize()) ? maxBufferSize() : (len - pos);
|
||||
bool read_stop = (pos < (len - read_len)) ? false : stop;
|
||||
if (!_read(buffer + pos, read_len, read_stop))
|
||||
return false;
|
||||
pos += read_len;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Adafruit_I2CDevice::_read(uint8_t *buffer, size_t len, bool stop) {
|
||||
#if defined(TinyWireM_h)
|
||||
size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len);
|
||||
#elif defined(ARDUINO_ARCH_MEGAAVR)
|
||||
size_t recv = _wire->requestFrom(_addr, len, stop);
|
||||
#else
|
||||
size_t recv = _wire->requestFrom((uint8_t)_addr, (uint8_t)len, (uint8_t)stop);
|
||||
#endif
|
||||
|
||||
if (recv != len) {
|
||||
// Not enough data available to fulfill our obligation!
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tI2CDevice did not receive enough data: "));
|
||||
DEBUG_SERIAL.println(recv);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
buffer[i] = _wire->read();
|
||||
}
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tI2CREAD @ 0x"));
|
||||
DEBUG_SERIAL.print(_addr, HEX);
|
||||
DEBUG_SERIAL.print(F(" :: "));
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (len % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
DEBUG_SERIAL.println();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write some data, then read some data from I2C into another buffer.
|
||||
* Cannot be more than maxBufferSize() bytes. The buffers can point to
|
||||
* same/overlapping locations.
|
||||
* @param write_buffer Pointer to buffer of data to write from
|
||||
* @param write_len Number of bytes from buffer to write.
|
||||
* @param read_buffer Pointer to buffer of data to read into.
|
||||
* @param read_len Number of bytes from buffer to read.
|
||||
* @param stop Whether to send an I2C STOP signal between the write and read
|
||||
* @return True if write & read was successful, otherwise false.
|
||||
*/
|
||||
bool Adafruit_I2CDevice::write_then_read(const uint8_t *write_buffer,
|
||||
size_t write_len, uint8_t *read_buffer,
|
||||
size_t read_len, bool stop) {
|
||||
if (!write(write_buffer, write_len, stop)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return read(read_buffer, read_len);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Returns the 7-bit address of this device
|
||||
* @return The 7-bit address of this device
|
||||
*/
|
||||
uint8_t Adafruit_I2CDevice::address(void) { return _addr; }
|
||||
|
||||
/*!
|
||||
* @brief Change the I2C clock speed to desired (relies on
|
||||
* underlying Wire support!
|
||||
* @param desiredclk The desired I2C SCL frequency
|
||||
* @return True if this platform supports changing I2C speed.
|
||||
* Not necessarily that the speed was achieved!
|
||||
*/
|
||||
bool Adafruit_I2CDevice::setSpeed(uint32_t desiredclk) {
|
||||
#if defined(__AVR_ATmega328__) || \
|
||||
defined(__AVR_ATmega328P__) // fix arduino core set clock
|
||||
// calculate TWBR correctly
|
||||
|
||||
if ((F_CPU / 18) < desiredclk) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
Serial.println(F("I2C.setSpeed too high."));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
uint32_t atwbr = ((F_CPU / desiredclk) - 16) / 2;
|
||||
if (atwbr > 16320) {
|
||||
#ifdef DEBUG_SERIAL
|
||||
Serial.println(F("I2C.setSpeed too low."));
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
if (atwbr <= 255) {
|
||||
atwbr /= 1;
|
||||
TWSR = 0x0;
|
||||
} else if (atwbr <= 1020) {
|
||||
atwbr /= 4;
|
||||
TWSR = 0x1;
|
||||
} else if (atwbr <= 4080) {
|
||||
atwbr /= 16;
|
||||
TWSR = 0x2;
|
||||
} else { // if (atwbr <= 16320)
|
||||
atwbr /= 64;
|
||||
TWSR = 0x3;
|
||||
}
|
||||
TWBR = atwbr;
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
Serial.print(F("TWSR prescaler = "));
|
||||
Serial.println(pow(4, TWSR));
|
||||
Serial.print(F("TWBR = "));
|
||||
Serial.println(atwbr);
|
||||
#endif
|
||||
return true;
|
||||
#elif (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) && \
|
||||
!defined(TinyWireM_h)
|
||||
_wire->setClock(desiredclk);
|
||||
return true;
|
||||
|
||||
#else
|
||||
(void)desiredclk;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef Adafruit_I2CDevice_h
|
||||
#define Adafruit_I2CDevice_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
///< The class which defines how we will talk to this device over I2C
|
||||
class Adafruit_I2CDevice {
|
||||
public:
|
||||
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire = &Wire);
|
||||
uint8_t address(void);
|
||||
bool begin(bool addr_detect = true);
|
||||
void end(void);
|
||||
bool detected(void);
|
||||
|
||||
bool read(uint8_t *buffer, size_t len, bool stop = true);
|
||||
bool write(const uint8_t *buffer, size_t len, bool stop = true,
|
||||
const uint8_t *prefix_buffer = nullptr, size_t prefix_len = 0);
|
||||
bool write_then_read(const uint8_t *write_buffer, size_t write_len,
|
||||
uint8_t *read_buffer, size_t read_len,
|
||||
bool stop = false);
|
||||
bool setSpeed(uint32_t desiredclk);
|
||||
|
||||
/*! @brief How many bytes we can read in a transaction
|
||||
* @return The size of the Wire receive/transmit buffer */
|
||||
size_t maxBufferSize() { return _maxBufferSize; }
|
||||
|
||||
private:
|
||||
uint8_t _addr;
|
||||
TwoWire *_wire;
|
||||
bool _begun;
|
||||
size_t _maxBufferSize;
|
||||
bool _read(uint8_t *buffer, size_t len, bool stop);
|
||||
};
|
||||
|
||||
#endif // Adafruit_I2CDevice_h
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef _ADAFRUIT_I2C_REGISTER_H_
|
||||
#define _ADAFRUIT_I2C_REGISTER_H_
|
||||
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
typedef Adafruit_BusIO_Register Adafruit_I2CRegister;
|
||||
typedef Adafruit_BusIO_RegisterBits Adafruit_I2CRegisterBits;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,508 @@
|
||||
#include "Adafruit_SPIDevice.h"
|
||||
|
||||
//#define DEBUG_SERIAL Serial
|
||||
|
||||
/*!
|
||||
* @brief Create an SPI device with the given CS pin and settings
|
||||
* @param cspin The arduino pin number to use for chip select
|
||||
* @param freq The SPI clock frequency to use, defaults to 1MHz
|
||||
* @param dataOrder The SPI data order to use for bits within each byte,
|
||||
* defaults to SPI_BITORDER_MSBFIRST
|
||||
* @param dataMode The SPI mode to use, defaults to SPI_MODE0
|
||||
* @param theSPI The SPI bus to use, defaults to &theSPI
|
||||
*/
|
||||
Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq,
|
||||
BusIOBitOrder dataOrder,
|
||||
uint8_t dataMode, SPIClass *theSPI) {
|
||||
#ifdef BUSIO_HAS_HW_SPI
|
||||
_cs = cspin;
|
||||
_sck = _mosi = _miso = -1;
|
||||
_spi = theSPI;
|
||||
_begun = false;
|
||||
_spiSetting = new SPISettings(freq, dataOrder, dataMode);
|
||||
_freq = freq;
|
||||
_dataOrder = dataOrder;
|
||||
_dataMode = dataMode;
|
||||
#else
|
||||
// unused, but needed to suppress compiler warns
|
||||
(void)cspin;
|
||||
(void)freq;
|
||||
(void)dataOrder;
|
||||
(void)dataMode;
|
||||
(void)theSPI;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Create an SPI device with the given CS pin and settings
|
||||
* @param cspin The arduino pin number to use for chip select
|
||||
* @param sckpin The arduino pin number to use for SCK
|
||||
* @param misopin The arduino pin number to use for MISO, set to -1 if not
|
||||
* used
|
||||
* @param mosipin The arduino pin number to use for MOSI, set to -1 if not
|
||||
* used
|
||||
* @param freq The SPI clock frequency to use, defaults to 1MHz
|
||||
* @param dataOrder The SPI data order to use for bits within each byte,
|
||||
* defaults to SPI_BITORDER_MSBFIRST
|
||||
* @param dataMode The SPI mode to use, defaults to SPI_MODE0
|
||||
*/
|
||||
Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, int8_t sckpin,
|
||||
int8_t misopin, int8_t mosipin,
|
||||
uint32_t freq, BusIOBitOrder dataOrder,
|
||||
uint8_t dataMode) {
|
||||
_cs = cspin;
|
||||
_sck = sckpin;
|
||||
_miso = misopin;
|
||||
_mosi = mosipin;
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
csPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(cspin));
|
||||
csPinMask = digitalPinToBitMask(cspin);
|
||||
if (mosipin != -1) {
|
||||
mosiPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(mosipin));
|
||||
mosiPinMask = digitalPinToBitMask(mosipin);
|
||||
}
|
||||
if (misopin != -1) {
|
||||
misoPort = (BusIO_PortReg *)portInputRegister(digitalPinToPort(misopin));
|
||||
misoPinMask = digitalPinToBitMask(misopin);
|
||||
}
|
||||
clkPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(sckpin));
|
||||
clkPinMask = digitalPinToBitMask(sckpin);
|
||||
#endif
|
||||
|
||||
_freq = freq;
|
||||
_dataOrder = dataOrder;
|
||||
_dataMode = dataMode;
|
||||
_begun = false;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Release memory allocated in constructors
|
||||
*/
|
||||
Adafruit_SPIDevice::~Adafruit_SPIDevice() {
|
||||
if (_spiSetting)
|
||||
delete _spiSetting;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initializes SPI bus and sets CS pin high
|
||||
* @return Always returns true because there's no way to test success of SPI
|
||||
* init
|
||||
*/
|
||||
bool Adafruit_SPIDevice::begin(void) {
|
||||
if (_cs != -1) {
|
||||
pinMode(_cs, OUTPUT);
|
||||
digitalWrite(_cs, HIGH);
|
||||
}
|
||||
|
||||
if (_spi) { // hardware SPI
|
||||
#ifdef BUSIO_HAS_HW_SPI
|
||||
_spi->begin();
|
||||
#endif
|
||||
} else {
|
||||
pinMode(_sck, OUTPUT);
|
||||
|
||||
if ((_dataMode == SPI_MODE0) || (_dataMode == SPI_MODE1)) {
|
||||
// idle low on mode 0 and 1
|
||||
digitalWrite(_sck, LOW);
|
||||
} else {
|
||||
// idle high on mode 2 or 3
|
||||
digitalWrite(_sck, HIGH);
|
||||
}
|
||||
if (_mosi != -1) {
|
||||
pinMode(_mosi, OUTPUT);
|
||||
digitalWrite(_mosi, HIGH);
|
||||
}
|
||||
if (_miso != -1) {
|
||||
pinMode(_miso, INPUT);
|
||||
}
|
||||
}
|
||||
|
||||
_begun = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Transfer (send/receive) a buffer over hard/soft SPI, without
|
||||
* transaction management
|
||||
* @param buffer The buffer to send and receive at the same time
|
||||
* @param len The number of bytes to transfer
|
||||
*/
|
||||
void Adafruit_SPIDevice::transfer(uint8_t *buffer, size_t len) {
|
||||
//
|
||||
// HARDWARE SPI
|
||||
//
|
||||
if (_spi) {
|
||||
#ifdef BUSIO_HAS_HW_SPI
|
||||
#if defined(SPARK)
|
||||
_spi->transfer(buffer, buffer, len, nullptr);
|
||||
#elif defined(STM32)
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
_spi->transfer(buffer[i]);
|
||||
}
|
||||
#else
|
||||
_spi->transfer(buffer, len);
|
||||
#endif
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
//
|
||||
// SOFTWARE SPI
|
||||
//
|
||||
uint8_t startbit;
|
||||
if (_dataOrder == SPI_BITORDER_LSBFIRST) {
|
||||
startbit = 0x1;
|
||||
} else {
|
||||
startbit = 0x80;
|
||||
}
|
||||
|
||||
bool towrite, lastmosi = !(buffer[0] & startbit);
|
||||
uint8_t bitdelay_us = (1000000 / _freq) / 2;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
uint8_t reply = 0;
|
||||
uint8_t send = buffer[i];
|
||||
|
||||
/*
|
||||
Serial.print("\tSending software SPI byte 0x");
|
||||
Serial.print(send, HEX);
|
||||
Serial.print(" -> 0x");
|
||||
*/
|
||||
|
||||
// Serial.print(send, HEX);
|
||||
for (uint8_t b = startbit; b != 0;
|
||||
b = (_dataOrder == SPI_BITORDER_LSBFIRST) ? b << 1 : b >> 1) {
|
||||
|
||||
if (bitdelay_us) {
|
||||
delayMicroseconds(bitdelay_us);
|
||||
}
|
||||
|
||||
if (_dataMode == SPI_MODE0 || _dataMode == SPI_MODE2) {
|
||||
towrite = send & b;
|
||||
if ((_mosi != -1) && (lastmosi != towrite)) {
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
if (towrite)
|
||||
*mosiPort |= mosiPinMask;
|
||||
else
|
||||
*mosiPort &= ~mosiPinMask;
|
||||
#else
|
||||
digitalWrite(_mosi, towrite);
|
||||
#endif
|
||||
lastmosi = towrite;
|
||||
}
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
*clkPort |= clkPinMask; // Clock high
|
||||
#else
|
||||
digitalWrite(_sck, HIGH);
|
||||
#endif
|
||||
|
||||
if (bitdelay_us) {
|
||||
delayMicroseconds(bitdelay_us);
|
||||
}
|
||||
|
||||
if (_miso != -1) {
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
if (*misoPort & misoPinMask) {
|
||||
#else
|
||||
if (digitalRead(_miso)) {
|
||||
#endif
|
||||
reply |= b;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
*clkPort &= ~clkPinMask; // Clock low
|
||||
#else
|
||||
digitalWrite(_sck, LOW);
|
||||
#endif
|
||||
} else { // if (_dataMode == SPI_MODE1 || _dataMode == SPI_MODE3)
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
*clkPort |= clkPinMask; // Clock high
|
||||
#else
|
||||
digitalWrite(_sck, HIGH);
|
||||
#endif
|
||||
|
||||
if (bitdelay_us) {
|
||||
delayMicroseconds(bitdelay_us);
|
||||
}
|
||||
|
||||
if (_mosi != -1) {
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
if (send & b)
|
||||
*mosiPort |= mosiPinMask;
|
||||
else
|
||||
*mosiPort &= ~mosiPinMask;
|
||||
#else
|
||||
digitalWrite(_mosi, send & b);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
*clkPort &= ~clkPinMask; // Clock low
|
||||
#else
|
||||
digitalWrite(_sck, LOW);
|
||||
#endif
|
||||
|
||||
if (_miso != -1) {
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
if (*misoPort & misoPinMask) {
|
||||
#else
|
||||
if (digitalRead(_miso)) {
|
||||
#endif
|
||||
reply |= b;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_miso != -1) {
|
||||
buffer[i] = reply;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Transfer (send/receive) one byte over hard/soft SPI, without
|
||||
* transaction management
|
||||
* @param send The byte to send
|
||||
* @return The byte received while transmitting
|
||||
*/
|
||||
uint8_t Adafruit_SPIDevice::transfer(uint8_t send) {
|
||||
uint8_t data = send;
|
||||
transfer(&data, 1);
|
||||
return data;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Manually begin a transaction (calls beginTransaction if hardware
|
||||
* SPI)
|
||||
*/
|
||||
void Adafruit_SPIDevice::beginTransaction(void) {
|
||||
if (_spi) {
|
||||
#ifdef BUSIO_HAS_HW_SPI
|
||||
_spi->beginTransaction(*_spiSetting);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Manually end a transaction (calls endTransaction if hardware SPI)
|
||||
*/
|
||||
void Adafruit_SPIDevice::endTransaction(void) {
|
||||
if (_spi) {
|
||||
#ifdef BUSIO_HAS_HW_SPI
|
||||
_spi->endTransaction();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Assert/Deassert the CS pin if it is defined
|
||||
* @param value The state the CS is set to
|
||||
*/
|
||||
void Adafruit_SPIDevice::setChipSelect(int value) {
|
||||
if (_cs != -1) {
|
||||
digitalWrite(_cs, value);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write a buffer or two to the SPI device, with transaction
|
||||
* management.
|
||||
* @brief Manually begin a transaction (calls beginTransaction if hardware
|
||||
* SPI) with asserting the CS pin
|
||||
*/
|
||||
void Adafruit_SPIDevice::beginTransactionWithAssertingCS() {
|
||||
beginTransaction();
|
||||
setChipSelect(LOW);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Manually end a transaction (calls endTransaction if hardware SPI)
|
||||
* with deasserting the CS pin
|
||||
*/
|
||||
void Adafruit_SPIDevice::endTransactionWithDeassertingCS() {
|
||||
setChipSelect(HIGH);
|
||||
endTransaction();
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write a buffer or two to the SPI device, with transaction
|
||||
* management.
|
||||
* @param buffer Pointer to buffer of data to write
|
||||
* @param len Number of bytes from buffer to write
|
||||
* @param prefix_buffer Pointer to optional array of data to write before
|
||||
* buffer.
|
||||
* @param prefix_len Number of bytes from prefix buffer to write
|
||||
* @return Always returns true because there's no way to test success of SPI
|
||||
* writes
|
||||
*/
|
||||
bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
|
||||
const uint8_t *prefix_buffer,
|
||||
size_t prefix_len) {
|
||||
beginTransactionWithAssertingCS();
|
||||
|
||||
// do the writing
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
if (_spi) {
|
||||
if (prefix_len > 0) {
|
||||
_spi->transferBytes((uint8_t *)prefix_buffer, nullptr, prefix_len);
|
||||
}
|
||||
if (len > 0) {
|
||||
_spi->transferBytes((uint8_t *)buffer, nullptr, len);
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
for (size_t i = 0; i < prefix_len; i++) {
|
||||
transfer(prefix_buffer[i]);
|
||||
}
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
transfer(buffer[i]);
|
||||
}
|
||||
}
|
||||
endTransactionWithDeassertingCS();
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tSPIDevice Wrote: "));
|
||||
if ((prefix_len != 0) && (prefix_buffer != nullptr)) {
|
||||
for (uint16_t i = 0; i < prefix_len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(prefix_buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
}
|
||||
}
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (i % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
DEBUG_SERIAL.println();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Read from SPI into a buffer from the SPI device, with transaction
|
||||
* management.
|
||||
* @param buffer Pointer to buffer of data to read into
|
||||
* @param len Number of bytes from buffer to read.
|
||||
* @param sendvalue The 8-bits of data to write when doing the data read,
|
||||
* defaults to 0xFF
|
||||
* @return Always returns true because there's no way to test success of SPI
|
||||
* writes
|
||||
*/
|
||||
bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
|
||||
memset(buffer, sendvalue, len); // clear out existing buffer
|
||||
|
||||
beginTransactionWithAssertingCS();
|
||||
transfer(buffer, len);
|
||||
endTransactionWithDeassertingCS();
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tSPIDevice Read: "));
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (len % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
DEBUG_SERIAL.println();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write some data, then read some data from SPI into another buffer,
|
||||
* with transaction management. The buffers can point to same/overlapping
|
||||
* locations. This does not transmit-receive at the same time!
|
||||
* @param write_buffer Pointer to buffer of data to write from
|
||||
* @param write_len Number of bytes from buffer to write.
|
||||
* @param read_buffer Pointer to buffer of data to read into.
|
||||
* @param read_len Number of bytes from buffer to read.
|
||||
* @param sendvalue The 8-bits of data to write when doing the data read,
|
||||
* defaults to 0xFF
|
||||
* @return Always returns true because there's no way to test success of SPI
|
||||
* writes
|
||||
*/
|
||||
bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
|
||||
size_t write_len, uint8_t *read_buffer,
|
||||
size_t read_len, uint8_t sendvalue) {
|
||||
beginTransactionWithAssertingCS();
|
||||
// do the writing
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
if (_spi) {
|
||||
if (write_len > 0) {
|
||||
_spi->transferBytes((uint8_t *)write_buffer, nullptr, write_len);
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
for (size_t i = 0; i < write_len; i++) {
|
||||
transfer(write_buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tSPIDevice Wrote: "));
|
||||
for (uint16_t i = 0; i < write_len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(write_buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (write_len % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
DEBUG_SERIAL.println();
|
||||
#endif
|
||||
|
||||
// do the reading
|
||||
for (size_t i = 0; i < read_len; i++) {
|
||||
read_buffer[i] = transfer(sendvalue);
|
||||
}
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("\tSPIDevice Read: "));
|
||||
for (uint16_t i = 0; i < read_len; i++) {
|
||||
DEBUG_SERIAL.print(F("0x"));
|
||||
DEBUG_SERIAL.print(read_buffer[i], HEX);
|
||||
DEBUG_SERIAL.print(F(", "));
|
||||
if (read_len % 32 == 31) {
|
||||
DEBUG_SERIAL.println();
|
||||
}
|
||||
}
|
||||
DEBUG_SERIAL.println();
|
||||
#endif
|
||||
|
||||
endTransactionWithDeassertingCS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Write some data and read some data at the same time from SPI
|
||||
* into the same buffer, with transaction management. This is basicaly a wrapper
|
||||
* for transfer() with CS-pin and transaction management. This /does/
|
||||
* transmit-receive at the same time!
|
||||
* @param buffer Pointer to buffer of data to write/read to/from
|
||||
* @param len Number of bytes from buffer to write/read.
|
||||
* @return Always returns true because there's no way to test success of SPI
|
||||
* writes
|
||||
*/
|
||||
bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {
|
||||
beginTransactionWithAssertingCS();
|
||||
transfer(buffer, len);
|
||||
endTransactionWithDeassertingCS();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
#ifndef Adafruit_SPIDevice_h
|
||||
#define Adafruit_SPIDevice_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if !defined(SPI_INTERFACES_COUNT) || \
|
||||
(defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
|
||||
// HW SPI available
|
||||
#include <SPI.h>
|
||||
#define BUSIO_HAS_HW_SPI
|
||||
#else
|
||||
// SW SPI ONLY
|
||||
enum { SPI_MODE0, SPI_MODE1, SPI_MODE2, _SPI_MODE4 };
|
||||
typedef uint8_t SPIClass;
|
||||
#endif
|
||||
|
||||
// some modern SPI definitions don't have BitOrder enum
|
||||
#if (defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)) || \
|
||||
defined(ESP8266) || defined(TEENSYDUINO) || defined(SPARK) || \
|
||||
defined(ARDUINO_ARCH_SPRESENSE) || defined(MEGATINYCORE) || \
|
||||
defined(DXCORE) || defined(ARDUINO_AVR_ATmega4809) || \
|
||||
defined(ARDUINO_AVR_ATmega4808) || defined(ARDUINO_AVR_ATmega3209) || \
|
||||
defined(ARDUINO_AVR_ATmega3208) || defined(ARDUINO_AVR_ATmega1609) || \
|
||||
defined(ARDUINO_AVR_ATmega1608) || defined(ARDUINO_AVR_ATmega809) || \
|
||||
defined(ARDUINO_AVR_ATmega808) || defined(ARDUINO_ARCH_ARC32)
|
||||
|
||||
typedef enum _BitOrder {
|
||||
SPI_BITORDER_MSBFIRST = MSBFIRST,
|
||||
SPI_BITORDER_LSBFIRST = LSBFIRST,
|
||||
} BusIOBitOrder;
|
||||
|
||||
#elif defined(ESP32) || defined(__ASR6501__) || defined(__ASR6502__)
|
||||
|
||||
// some modern SPI definitions don't have BitOrder enum and have different SPI
|
||||
// mode defines
|
||||
typedef enum _BitOrder {
|
||||
SPI_BITORDER_MSBFIRST = SPI_MSBFIRST,
|
||||
SPI_BITORDER_LSBFIRST = SPI_LSBFIRST,
|
||||
} BusIOBitOrder;
|
||||
|
||||
#else
|
||||
// Some platforms have a BitOrder enum but its named MSBFIRST/LSBFIRST
|
||||
#define SPI_BITORDER_MSBFIRST MSBFIRST
|
||||
#define SPI_BITORDER_LSBFIRST LSBFIRST
|
||||
typedef BitOrder BusIOBitOrder;
|
||||
#endif
|
||||
|
||||
#if defined(__IMXRT1062__) // Teensy 4.x
|
||||
// *Warning* I disabled the usage of FAST_PINIO as the set/clear operations
|
||||
// used in the cpp file are not atomic and can effect multiple IO pins
|
||||
// and if an interrupt happens in between the time the code reads the register
|
||||
// and writes out the updated value, that changes one or more other IO pins
|
||||
// on that same IO port, those change will be clobbered when the updated
|
||||
// values are written back. A fast version can be implemented that uses the
|
||||
// ports set and clear registers which are atomic.
|
||||
// typedef volatile uint32_t BusIO_PortReg;
|
||||
// typedef uint32_t BusIO_PortMask;
|
||||
//#define BUSIO_USE_FAST_PINIO
|
||||
|
||||
#elif defined(__AVR__) || defined(TEENSYDUINO)
|
||||
typedef volatile uint8_t BusIO_PortReg;
|
||||
typedef uint8_t BusIO_PortMask;
|
||||
#define BUSIO_USE_FAST_PINIO
|
||||
|
||||
#elif defined(ESP8266) || defined(ESP32) || defined(__SAM3X8E__) || \
|
||||
defined(ARDUINO_ARCH_SAMD)
|
||||
typedef volatile uint32_t BusIO_PortReg;
|
||||
typedef uint32_t BusIO_PortMask;
|
||||
#define BUSIO_USE_FAST_PINIO
|
||||
|
||||
#elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) && \
|
||||
!defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040)
|
||||
typedef volatile uint32_t BusIO_PortReg;
|
||||
typedef uint32_t BusIO_PortMask;
|
||||
#if !defined(__ASR6501__) && !defined(__ASR6502__)
|
||||
#define BUSIO_USE_FAST_PINIO
|
||||
#endif
|
||||
|
||||
#else
|
||||
#undef BUSIO_USE_FAST_PINIO
|
||||
#endif
|
||||
|
||||
/**! The class which defines how we will talk to this device over SPI **/
|
||||
class Adafruit_SPIDevice {
|
||||
public:
|
||||
#ifdef BUSIO_HAS_HW_SPI
|
||||
Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,
|
||||
BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
|
||||
uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = &SPI);
|
||||
#else
|
||||
Adafruit_SPIDevice(int8_t cspin, uint32_t freq = 1000000,
|
||||
BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
|
||||
uint8_t dataMode = SPI_MODE0, SPIClass *theSPI = nullptr);
|
||||
#endif
|
||||
Adafruit_SPIDevice(int8_t cspin, int8_t sck, int8_t miso, int8_t mosi,
|
||||
uint32_t freq = 1000000,
|
||||
BusIOBitOrder dataOrder = SPI_BITORDER_MSBFIRST,
|
||||
uint8_t dataMode = SPI_MODE0);
|
||||
~Adafruit_SPIDevice();
|
||||
|
||||
bool begin(void);
|
||||
bool read(uint8_t *buffer, size_t len, uint8_t sendvalue = 0xFF);
|
||||
bool write(const uint8_t *buffer, size_t len,
|
||||
const uint8_t *prefix_buffer = nullptr, size_t prefix_len = 0);
|
||||
bool write_then_read(const uint8_t *write_buffer, size_t write_len,
|
||||
uint8_t *read_buffer, size_t read_len,
|
||||
uint8_t sendvalue = 0xFF);
|
||||
bool write_and_read(uint8_t *buffer, size_t len);
|
||||
|
||||
uint8_t transfer(uint8_t send);
|
||||
void transfer(uint8_t *buffer, size_t len);
|
||||
void beginTransaction(void);
|
||||
void endTransaction(void);
|
||||
void beginTransactionWithAssertingCS();
|
||||
void endTransactionWithDeassertingCS();
|
||||
|
||||
private:
|
||||
#ifdef BUSIO_HAS_HW_SPI
|
||||
SPIClass *_spi = nullptr;
|
||||
SPISettings *_spiSetting = nullptr;
|
||||
#else
|
||||
uint8_t *_spi = nullptr;
|
||||
uint8_t *_spiSetting = nullptr;
|
||||
#endif
|
||||
uint32_t _freq;
|
||||
BusIOBitOrder _dataOrder;
|
||||
uint8_t _dataMode;
|
||||
void setChipSelect(int value);
|
||||
|
||||
int8_t _cs, _sck, _mosi, _miso;
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
BusIO_PortReg *mosiPort, *clkPort, *misoPort, *csPort;
|
||||
BusIO_PortMask mosiPinMask, misoPinMask, clkPinMask, csPinMask;
|
||||
#endif
|
||||
bool _begun;
|
||||
};
|
||||
|
||||
#endif // Adafruit_SPIDevice_h
|
||||
@@ -0,0 +1,11 @@
|
||||
# Adafruit Bus IO Library
|
||||
# https://github.com/adafruit/Adafruit_BusIO
|
||||
# MIT License
|
||||
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
idf_component_register(SRCS "Adafruit_I2CDevice.cpp" "Adafruit_BusIO_Register.cpp" "Adafruit_SPIDevice.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES arduino)
|
||||
|
||||
project(Adafruit_BusIO)
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Adafruit Industries
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,8 @@
|
||||
# Adafruit Bus IO Library [](https://github.com/adafruit/Adafruit_BusIO/actions)
|
||||
|
||||
|
||||
This is a helper library to abstract away I2C & SPI transactions and registers
|
||||
|
||||
Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
|
||||
|
||||
MIT license, all text above must be included in any redistribution
|
||||
@@ -0,0 +1 @@
|
||||
COMPONENT_ADD_INCLUDEDIRS = .
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
|
||||
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(0x10);
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("I2C address detection test");
|
||||
|
||||
if (!i2c_dev.begin()) {
|
||||
Serial.print("Did not find device at 0x");
|
||||
Serial.println(i2c_dev.address(), HEX);
|
||||
while (1);
|
||||
}
|
||||
Serial.print("Device found on address 0x");
|
||||
Serial.println(i2c_dev.address(), HEX);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
|
||||
#define I2C_ADDRESS 0x60
|
||||
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS);
|
||||
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("I2C device read and write test");
|
||||
|
||||
if (!i2c_dev.begin()) {
|
||||
Serial.print("Did not find device at 0x");
|
||||
Serial.println(i2c_dev.address(), HEX);
|
||||
while (1);
|
||||
}
|
||||
Serial.print("Device found on address 0x");
|
||||
Serial.println(i2c_dev.address(), HEX);
|
||||
|
||||
uint8_t buffer[32];
|
||||
// Try to read 32 bytes
|
||||
i2c_dev.read(buffer, 32);
|
||||
Serial.print("Read: ");
|
||||
for (uint8_t i=0; i<32; i++) {
|
||||
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// read a register by writing first, then reading
|
||||
buffer[0] = 0x0C; // we'll reuse the same buffer
|
||||
i2c_dev.write_then_read(buffer, 1, buffer, 2, false);
|
||||
Serial.print("Write then Read: ");
|
||||
for (uint8_t i=0; i<2; i++) {
|
||||
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
|
||||
#define I2C_ADDRESS 0x60
|
||||
Adafruit_I2CDevice i2c_dev = Adafruit_I2CDevice(I2C_ADDRESS);
|
||||
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("I2C device register test");
|
||||
|
||||
if (!i2c_dev.begin()) {
|
||||
Serial.print("Did not find device at 0x");
|
||||
Serial.println(i2c_dev.address(), HEX);
|
||||
while (1);
|
||||
}
|
||||
Serial.print("Device found on address 0x");
|
||||
Serial.println(i2c_dev.address(), HEX);
|
||||
|
||||
Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register(&i2c_dev, 0x0C, 2, LSBFIRST);
|
||||
uint16_t id;
|
||||
id_reg.read(&id);
|
||||
Serial.print("ID register = 0x"); Serial.println(id, HEX);
|
||||
|
||||
Adafruit_BusIO_Register thresh_reg = Adafruit_BusIO_Register(&i2c_dev, 0x01, 2, LSBFIRST);
|
||||
uint16_t thresh;
|
||||
thresh_reg.read(&thresh);
|
||||
Serial.print("Initial threshold register = 0x"); Serial.println(thresh, HEX);
|
||||
|
||||
thresh_reg.write(~thresh);
|
||||
|
||||
Serial.print("Post threshold register = 0x"); Serial.println(thresh_reg.read(), HEX);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
|
||||
// Define which interface to use by setting the unused interface to NULL!
|
||||
|
||||
#define SPIDEVICE_CS 10
|
||||
Adafruit_SPIDevice *spi_dev = NULL; // new Adafruit_SPIDevice(SPIDEVICE_CS);
|
||||
|
||||
#define I2C_ADDRESS 0x5D
|
||||
Adafruit_I2CDevice *i2c_dev = new Adafruit_I2CDevice(I2C_ADDRESS);
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("I2C or SPI device register test");
|
||||
|
||||
if (spi_dev && !spi_dev->begin()) {
|
||||
Serial.println("Could not initialize SPI device");
|
||||
}
|
||||
|
||||
if (i2c_dev) {
|
||||
if (i2c_dev->begin()) {
|
||||
Serial.print("Device found on I2C address 0x");
|
||||
Serial.println(i2c_dev->address(), HEX);
|
||||
} else {
|
||||
Serial.print("Did not find I2C device at 0x");
|
||||
Serial.println(i2c_dev->address(), HEX);
|
||||
}
|
||||
}
|
||||
|
||||
Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register(i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, 0x0F);
|
||||
uint8_t id=0;
|
||||
id_reg.read(&id);
|
||||
Serial.print("ID register = 0x"); Serial.println(id, HEX);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
|
||||
#define SPIDEVICE_CS 10
|
||||
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1);
|
||||
//Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS, 13, 12, 11, 100000, SPI_BITORDER_MSBFIRST, SPI_MODE1);
|
||||
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("SPI device mode test");
|
||||
|
||||
if (!spi_dev.begin()) {
|
||||
Serial.println("Could not initialize SPI device");
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("\n\nTransfer test");
|
||||
for (uint16_t x=0; x<=0xFF; x++) {
|
||||
uint8_t i = x;
|
||||
Serial.print("0x"); Serial.print(i, HEX);
|
||||
spi_dev.read(&i, 1, i);
|
||||
Serial.print("/"); Serial.print(i, HEX);
|
||||
Serial.print(", ");
|
||||
delay(25);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
|
||||
#define SPIDEVICE_CS 10
|
||||
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS);
|
||||
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("SPI device read and write test");
|
||||
|
||||
if (!spi_dev.begin()) {
|
||||
Serial.println("Could not initialize SPI device");
|
||||
while (1);
|
||||
}
|
||||
|
||||
uint8_t buffer[32];
|
||||
|
||||
// Try to read 32 bytes
|
||||
spi_dev.read(buffer, 32);
|
||||
Serial.print("Read: ");
|
||||
for (uint8_t i=0; i<32; i++) {
|
||||
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// read a register by writing first, then reading
|
||||
buffer[0] = 0x8F; // we'll reuse the same buffer
|
||||
spi_dev.write_then_read(buffer, 1, buffer, 2, false);
|
||||
Serial.print("Write then Read: ");
|
||||
for (uint8_t i=0; i<2; i++) {
|
||||
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
/***************************************************
|
||||
|
||||
This is an example for how to use Adafruit_BusIO_RegisterBits from Adafruit_BusIO library.
|
||||
|
||||
Designed specifically to work with the Adafruit RTD Sensor
|
||||
----> https://www.adafruit.com/products/3328
|
||||
uisng a MAX31865 RTD-to-Digital Converter
|
||||
----> https://datasheets.maximintegrated.com/en/ds/MAX31865.pdf
|
||||
|
||||
This sensor uses SPI to communicate, 4 pins are required to
|
||||
interface.
|
||||
A fifth pin helps to detect when a new conversion is ready.
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Example written (2020/3) by Andreas Hardtung/AnHard.
|
||||
BSD license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
|
||||
#define MAX31865_SPI_SPEED (5000000)
|
||||
#define MAX31865_SPI_BITORDER (SPI_BITORDER_MSBFIRST)
|
||||
#define MAX31865_SPI_MODE (SPI_MODE1)
|
||||
|
||||
#define MAX31865_SPI_CS (10)
|
||||
#define MAX31865_READY_PIN (2)
|
||||
|
||||
|
||||
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice( MAX31865_SPI_CS, MAX31865_SPI_SPEED, MAX31865_SPI_BITORDER, MAX31865_SPI_MODE, &SPI); // Hardware SPI
|
||||
// Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice( MAX31865_SPI_CS, 13, 12, 11, MAX31865_SPI_SPEED, MAX31865_SPI_BITORDER, MAX31865_SPI_MODE); // Software SPI
|
||||
|
||||
// MAX31865 chip related *********************************************************************************************
|
||||
Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(&spi_dev, 0x00, ADDRBIT8_HIGH_TOWRITE, 1, MSBFIRST);
|
||||
Adafruit_BusIO_RegisterBits bias_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 7);
|
||||
Adafruit_BusIO_RegisterBits auto_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 6);
|
||||
Adafruit_BusIO_RegisterBits oneS_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 5);
|
||||
Adafruit_BusIO_RegisterBits wire_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 4);
|
||||
Adafruit_BusIO_RegisterBits faultT_bits = Adafruit_BusIO_RegisterBits(&config_reg, 2, 2);
|
||||
Adafruit_BusIO_RegisterBits faultR_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 1);
|
||||
Adafruit_BusIO_RegisterBits fi50hz_bit = Adafruit_BusIO_RegisterBits(&config_reg, 1, 0);
|
||||
|
||||
Adafruit_BusIO_Register rRatio_reg = Adafruit_BusIO_Register(&spi_dev, 0x01, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST);
|
||||
Adafruit_BusIO_RegisterBits rRatio_bits = Adafruit_BusIO_RegisterBits(&rRatio_reg, 15, 1);
|
||||
Adafruit_BusIO_RegisterBits fault_bit = Adafruit_BusIO_RegisterBits(&rRatio_reg, 1, 0);
|
||||
|
||||
Adafruit_BusIO_Register maxRratio_reg = Adafruit_BusIO_Register(&spi_dev, 0x03, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST);
|
||||
Adafruit_BusIO_RegisterBits maxRratio_bits = Adafruit_BusIO_RegisterBits(&maxRratio_reg, 15, 1);
|
||||
|
||||
Adafruit_BusIO_Register minRratio_reg = Adafruit_BusIO_Register(&spi_dev, 0x05, ADDRBIT8_HIGH_TOWRITE, 2, MSBFIRST);
|
||||
Adafruit_BusIO_RegisterBits minRratio_bits = Adafruit_BusIO_RegisterBits(&minRratio_reg, 15, 1);
|
||||
|
||||
Adafruit_BusIO_Register fault_reg = Adafruit_BusIO_Register(&spi_dev, 0x07, ADDRBIT8_HIGH_TOWRITE, 1, MSBFIRST);
|
||||
Adafruit_BusIO_RegisterBits range_high_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 7);
|
||||
Adafruit_BusIO_RegisterBits range_low_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 6);
|
||||
Adafruit_BusIO_RegisterBits refin_high_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 5);
|
||||
Adafruit_BusIO_RegisterBits refin_low_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 4);
|
||||
Adafruit_BusIO_RegisterBits rtdin_low_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 3);
|
||||
Adafruit_BusIO_RegisterBits voltage_fault_bit = Adafruit_BusIO_RegisterBits(&fault_reg, 1, 2);
|
||||
|
||||
// Print the details of the configuration register.
|
||||
void printConfig( void ) {
|
||||
Serial.print("BIAS: "); if (bias_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
|
||||
Serial.print(", AUTO: "); if (auto_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
|
||||
Serial.print(", ONES: "); if (oneS_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
|
||||
Serial.print(", WIRE: "); if (wire_bit.read() ) Serial.print("3"); else Serial.print("2/4");
|
||||
Serial.print(", FAULTCLEAR: "); if (faultR_bit.read() ) Serial.print("ON"); else Serial.print("OFF");
|
||||
Serial.print(", "); if (fi50hz_bit.read() ) Serial.print("50HZ"); else Serial.print("60HZ");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// Check and print faults. Then clear them.
|
||||
void checkFaults( void ) {
|
||||
if (fault_bit.read()) {
|
||||
Serial.print("MAX: "); Serial.println(maxRratio_bits.read());
|
||||
Serial.print("VAL: "); Serial.println( rRatio_bits.read());
|
||||
Serial.print("MIN: "); Serial.println(minRratio_bits.read());
|
||||
|
||||
if (range_high_fault_bit.read() ) Serial.println("Range high fault");
|
||||
if ( range_low_fault_bit.read() ) Serial.println("Range low fault");
|
||||
if (refin_high_fault_bit.read() ) Serial.println("REFIN high fault");
|
||||
if ( refin_low_fault_bit.read() ) Serial.println("REFIN low fault");
|
||||
if ( rtdin_low_fault_bit.read() ) Serial.println("RTDIN low fault");
|
||||
if ( voltage_fault_bit.read() ) Serial.println("Voltage fault");
|
||||
|
||||
faultR_bit.write(1); // clear fault
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
#if (MAX31865_1_READY_PIN != -1)
|
||||
pinMode(MAX31865_READY_PIN ,INPUT_PULLUP);
|
||||
#endif
|
||||
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("SPI Adafruit_BusIO_RegisterBits test on MAX31865");
|
||||
|
||||
if (!spi_dev.begin()) {
|
||||
Serial.println("Could not initialize SPI device");
|
||||
while (1);
|
||||
}
|
||||
|
||||
// Set up for automode 50Hz. We don't care about selfheating. We want the highest possible sampling rate.
|
||||
auto_bit.write(0); // Don't switch filtermode while auto_mode is on.
|
||||
fi50hz_bit.write(1); // Set filter to 50Hz mode.
|
||||
faultR_bit.write(1); // Clear faults.
|
||||
bias_bit.write(1); // In automode we want to have the bias current always on.
|
||||
delay(5); // Wait until bias current settles down.
|
||||
// 10.5 time constants of the input RC network is required.
|
||||
// 10ms worst case for 10kω reference resistor and a 0.1µF capacitor across the RTD inputs.
|
||||
// Adafruit Module has 0.1µF and only 430/4300ω So here 0.43/4.3ms
|
||||
auto_bit.write(1); // Now we can set automode. Automatically starting first conversion.
|
||||
|
||||
// Test the READY_PIN
|
||||
#if (defined( MAX31865_READY_PIN ) && (MAX31865_READY_PIN != -1))
|
||||
int i = 0;
|
||||
while (digitalRead(MAX31865_READY_PIN) && i++ <= 100) { delay(1); }
|
||||
if (i >= 100) {
|
||||
Serial.print("ERROR: Max31865 Pin detection does not work. PIN:");
|
||||
Serial.println(MAX31865_READY_PIN);
|
||||
}
|
||||
#else
|
||||
delay(100);
|
||||
#endif
|
||||
|
||||
// Set ratio range.
|
||||
// Setting the temperatures would need some more calculation - not related to Adafruit_BusIO_RegisterBits.
|
||||
uint16_t ratio = rRatio_bits.read();
|
||||
maxRratio_bits.write( (ratio < 0x8fffu-1000u) ? ratio + 1000u : 0x8fffu );
|
||||
minRratio_bits.write( (ratio > 1000u) ? ratio - 1000u : 0u );
|
||||
|
||||
printConfig();
|
||||
checkFaults();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
#if (defined( MAX31865_READY_PIN ) && (MAX31865_1_READY_PIN != -1))
|
||||
// Is conversion ready?
|
||||
if (!digitalRead(MAX31865_READY_PIN))
|
||||
#else
|
||||
// Warant conversion is ready.
|
||||
delay(21); // 21ms for 50Hz-mode. 19ms in 60Hz-mode.
|
||||
#endif
|
||||
{
|
||||
// Read ratio, calculate temperature, scale, filter and print.
|
||||
Serial.println( rRatio2C( rRatio_bits.read() ) * 100.0f, 0); // Temperature scaled by 100
|
||||
// Check, print, clear faults.
|
||||
checkFaults();
|
||||
}
|
||||
|
||||
// Do something else.
|
||||
//delay(15000);
|
||||
}
|
||||
|
||||
|
||||
// Module/Sensor related. Here Adafruit PT100 module with a 2_Wire PT100 Class C *****************************
|
||||
float rRatio2C(uint16_t ratio) {
|
||||
// A simple linear conversion.
|
||||
const float R0 = 100.0f;
|
||||
const float Rref = 430.0f;
|
||||
const float alphaPT = 0.003850f;
|
||||
const float ADCmax = (1u << 15) - 1.0f;
|
||||
const float rscale = Rref / ADCmax;
|
||||
// Measured temperature in boiling water 101.08°C with factor a = 1 and b = 0. Rref and MAX at about 22±2°C.
|
||||
// Measured temperature in ice/water bath 0.76°C with factor a = 1 and b = 0. Rref and MAX at about 22±2°C.
|
||||
//const float a = 1.0f / (alphaPT * R0);
|
||||
const float a = (100.0f/101.08f) / (alphaPT * R0);
|
||||
//const float b = 0.0f; // 101.08
|
||||
const float b = -0.76f; // 100.32 > 101.08
|
||||
|
||||
return filterRing( ((ratio * rscale) - R0) * a + b );
|
||||
}
|
||||
|
||||
// General purpose *********************************************************************************************
|
||||
#define RINGLENGTH 250
|
||||
float filterRing( float newVal ) {
|
||||
static float ring[RINGLENGTH] = { 0.0 };
|
||||
static uint8_t ringIndex = 0;
|
||||
static bool ringFull = false;
|
||||
|
||||
if ( ringIndex == RINGLENGTH ) { ringFull = true; ringIndex = 0; }
|
||||
ring[ringIndex] = newVal;
|
||||
uint8_t loopEnd = (ringFull) ? RINGLENGTH : ringIndex + 1;
|
||||
float ringSum = 0.0f;
|
||||
for (uint8_t i = 0; i < loopEnd; i++) ringSum += ring[i];
|
||||
ringIndex++;
|
||||
return ringSum / loopEnd;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
|
||||
#define SPIDEVICE_CS 10
|
||||
Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS);
|
||||
|
||||
void setup() {
|
||||
while (!Serial) { delay(10); }
|
||||
Serial.begin(115200);
|
||||
Serial.println("SPI device register test");
|
||||
|
||||
if (!spi_dev.begin()) {
|
||||
Serial.println("Could not initialize SPI device");
|
||||
while (1);
|
||||
}
|
||||
|
||||
Adafruit_BusIO_Register id_reg = Adafruit_BusIO_Register(&spi_dev, 0x0F, ADDRBIT8_HIGH_TOREAD);
|
||||
uint8_t id = 0;
|
||||
id_reg.read(&id);
|
||||
Serial.print("ID register = 0x"); Serial.println(id, HEX);
|
||||
|
||||
Adafruit_BusIO_Register thresh_reg = Adafruit_BusIO_Register(&spi_dev, 0x0C, ADDRBIT8_HIGH_TOREAD, 2, LSBFIRST);
|
||||
uint16_t thresh = 0;
|
||||
thresh_reg.read(&thresh);
|
||||
Serial.print("Initial threshold register = 0x"); Serial.println(thresh, HEX);
|
||||
|
||||
thresh_reg.write(~thresh);
|
||||
|
||||
Serial.print("Post threshold register = 0x"); Serial.println(thresh_reg.read(), HEX);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
name=Adafruit BusIO
|
||||
version=1.14.3
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=This is a library for abstracting away UART, I2C and SPI interfacing
|
||||
paragraph=This is a library for abstracting away UART, I2C and SPI interfacing
|
||||
category=Signal Input/Output
|
||||
url=https://github.com/adafruit/Adafruit_BusIO
|
||||
architectures=*
|
||||
@@ -0,0 +1,108 @@
|
||||
#
|
||||
# NOTE! Don't add files that are generated in specific
|
||||
# subdirectories here. Add them in the ".gitignore" file
|
||||
# in that subdirectory instead.
|
||||
#
|
||||
# NOTE! Please use 'git ls-files -i --exclude-standard'
|
||||
# command after changing this file, to see if there are
|
||||
# any tracked files which get ignored after the change.
|
||||
#
|
||||
# Normal rules
|
||||
#
|
||||
.*
|
||||
*.o
|
||||
*.o.*
|
||||
*.a
|
||||
*.s
|
||||
*.ko
|
||||
*.so
|
||||
*.so.dbg
|
||||
*.mod.c
|
||||
*.i
|
||||
*.lst
|
||||
*.symtypes
|
||||
*.order
|
||||
modules.builtin
|
||||
*.elf
|
||||
*.bin
|
||||
*.gz
|
||||
*.bz2
|
||||
*.lzma
|
||||
*.patch
|
||||
*.gcno
|
||||
|
||||
#
|
||||
# Top-level generic files
|
||||
#
|
||||
/tags
|
||||
/TAGS
|
||||
/linux
|
||||
/vmlinux
|
||||
/vmlinuz
|
||||
/System.map
|
||||
/Module.markers
|
||||
/Module.symvers
|
||||
|
||||
#
|
||||
# git files that we don't want to ignore even it they are dot-files
|
||||
#
|
||||
!.gitignore
|
||||
!.mailmap
|
||||
|
||||
#
|
||||
# Generated include files
|
||||
#
|
||||
include/config
|
||||
include/linux/version.h
|
||||
include/generated
|
||||
|
||||
# stgit generated dirs
|
||||
patches-*
|
||||
|
||||
# quilt's files
|
||||
patches
|
||||
series
|
||||
|
||||
# cscope files
|
||||
cscope.*
|
||||
ncscope.*
|
||||
|
||||
# gnu global files
|
||||
GPATH
|
||||
GRTAGS
|
||||
GSYMS
|
||||
GTAGS
|
||||
|
||||
# QT-Creator files
|
||||
Makefile.am.user
|
||||
*.config
|
||||
*.creator
|
||||
*.creator.user
|
||||
*.files
|
||||
*.includes
|
||||
|
||||
*.orig
|
||||
*~
|
||||
\#*#
|
||||
*.lo
|
||||
*.la
|
||||
Makefile
|
||||
Makefile.in
|
||||
aclocal.m4
|
||||
autoconfig.h
|
||||
autoconfig.h.in
|
||||
autom4te.cache/
|
||||
build-aux/
|
||||
config.log
|
||||
config.status
|
||||
configure
|
||||
libtool
|
||||
libupnp.pc
|
||||
m4/libtool.m4
|
||||
m4/ltoptions.m4
|
||||
m4/ltsugar.m4
|
||||
m4/ltversion.m4
|
||||
m4/lt~obsolete.m4
|
||||
stamp-h1
|
||||
docs/doxygen
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
#include "Adafruit_Sensor.h"
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Prints sensor information to serial console
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_Sensor::printSensorDetails(void) {
|
||||
sensor_t sensor;
|
||||
getSensor(&sensor);
|
||||
Serial.println(F("------------------------------------"));
|
||||
Serial.print(F("Sensor: "));
|
||||
Serial.println(sensor.name);
|
||||
Serial.print(F("Type: "));
|
||||
switch ((sensors_type_t)sensor.type) {
|
||||
case SENSOR_TYPE_ACCELEROMETER:
|
||||
Serial.print(F("Acceleration (m/s2)"));
|
||||
break;
|
||||
case SENSOR_TYPE_MAGNETIC_FIELD:
|
||||
Serial.print(F("Magnetic (uT)"));
|
||||
break;
|
||||
case SENSOR_TYPE_ORIENTATION:
|
||||
Serial.print(F("Orientation (degrees)"));
|
||||
break;
|
||||
case SENSOR_TYPE_GYROSCOPE:
|
||||
Serial.print(F("Gyroscopic (rad/s)"));
|
||||
break;
|
||||
case SENSOR_TYPE_LIGHT:
|
||||
Serial.print(F("Light (lux)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PRESSURE:
|
||||
Serial.print(F("Pressure (hPa)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PROXIMITY:
|
||||
Serial.print(F("Distance (cm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_GRAVITY:
|
||||
Serial.print(F("Gravity (m/s2)"));
|
||||
break;
|
||||
case SENSOR_TYPE_LINEAR_ACCELERATION:
|
||||
Serial.print(F("Linear Acceleration (m/s2)"));
|
||||
break;
|
||||
case SENSOR_TYPE_ROTATION_VECTOR:
|
||||
Serial.print(F("Rotation vector"));
|
||||
break;
|
||||
case SENSOR_TYPE_RELATIVE_HUMIDITY:
|
||||
Serial.print(F("Relative Humidity (%)"));
|
||||
break;
|
||||
case SENSOR_TYPE_AMBIENT_TEMPERATURE:
|
||||
Serial.print(F("Ambient Temp (C)"));
|
||||
break;
|
||||
case SENSOR_TYPE_OBJECT_TEMPERATURE:
|
||||
Serial.print(F("Object Temp (C)"));
|
||||
break;
|
||||
case SENSOR_TYPE_VOLTAGE:
|
||||
Serial.print(F("Voltage (V)"));
|
||||
break;
|
||||
case SENSOR_TYPE_CURRENT:
|
||||
Serial.print(F("Current (mA)"));
|
||||
break;
|
||||
case SENSOR_TYPE_COLOR:
|
||||
Serial.print(F("Color (RGBA)"));
|
||||
break;
|
||||
case SENSOR_TYPE_TVOC:
|
||||
Serial.print(F("Total Volatile Organic Compounds (ppb)"));
|
||||
break;
|
||||
case SENSOR_TYPE_VOC_INDEX:
|
||||
Serial.print(F("Volatile Organic Compounds (Index)"));
|
||||
break;
|
||||
case SENSOR_TYPE_NOX_INDEX:
|
||||
Serial.print(F("Nitrogen Oxides (Index)"));
|
||||
break;
|
||||
case SENSOR_TYPE_CO2:
|
||||
Serial.print(F("Carbon Dioxide (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_ECO2:
|
||||
Serial.print(F("Equivalent/estimated CO2 (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PM10_STD:
|
||||
Serial.print(F("Standard Particulate Matter 1.0 (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PM25_STD:
|
||||
Serial.print(F("Standard Particulate Matter 2.5 (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PM100_STD:
|
||||
Serial.print(F("Standard Particulate Matter 10.0 (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PM10_ENV:
|
||||
Serial.print(F("Environmental Particulate Matter 1.0 (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PM25_ENV:
|
||||
Serial.print(F("Environmental Particulate Matter 2.5 (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_PM100_ENV:
|
||||
Serial.print(F("Environmental Particulate Matter 10.0 (ppm)"));
|
||||
break;
|
||||
case SENSOR_TYPE_GAS_RESISTANCE:
|
||||
Serial.print(F("Gas Resistance (ohms)"));
|
||||
break;
|
||||
case SENSOR_TYPE_UNITLESS_PERCENT:
|
||||
Serial.print(F("Unitless Percent (%)"));
|
||||
break;
|
||||
case SENSOR_TYPE_ALTITUDE:
|
||||
Serial.print(F("Altitude (m)"));
|
||||
break;
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.print(F("Driver Ver: "));
|
||||
Serial.println(sensor.version);
|
||||
Serial.print(F("Unique ID: "));
|
||||
Serial.println(sensor.sensor_id);
|
||||
Serial.print(F("Min Value: "));
|
||||
Serial.println(sensor.min_value);
|
||||
Serial.print(F("Max Value: "));
|
||||
Serial.println(sensor.max_value);
|
||||
Serial.print(F("Resolution: "));
|
||||
Serial.println(sensor.resolution);
|
||||
Serial.println(F("------------------------------------\n"));
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software< /span>
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* Update by K. Townsend (Adafruit Industries) for lighter typedefs, and
|
||||
* extended sensor support to include color, voltage and current */
|
||||
|
||||
#ifndef _ADAFRUIT_SENSOR_H
|
||||
#define _ADAFRUIT_SENSOR_H
|
||||
|
||||
#ifndef ARDUINO
|
||||
#include <stdint.h>
|
||||
#elif ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#include "Print.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
/* Constants */
|
||||
#define SENSORS_GRAVITY_EARTH (9.80665F) /**< Earth's gravity in m/s^2 */
|
||||
#define SENSORS_GRAVITY_MOON (1.6F) /**< The moon's gravity in m/s^2 */
|
||||
#define SENSORS_GRAVITY_SUN (275.0F) /**< The sun's gravity in m/s^2 */
|
||||
#define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH)
|
||||
#define SENSORS_MAGFIELD_EARTH_MAX \
|
||||
(60.0F) /**< Maximum magnetic field on Earth's surface */
|
||||
#define SENSORS_MAGFIELD_EARTH_MIN \
|
||||
(30.0F) /**< Minimum magnetic field on Earth's surface */
|
||||
#define SENSORS_PRESSURE_SEALEVELHPA \
|
||||
(1013.25F) /**< Average sea level pressure is 1013.25 hPa */
|
||||
#define SENSORS_DPS_TO_RADS \
|
||||
(0.017453293F) /**< Degrees/s to rad/s multiplier \
|
||||
*/
|
||||
#define SENSORS_RADS_TO_DPS \
|
||||
(57.29577793F) /**< Rad/s to degrees/s multiplier */
|
||||
#define SENSORS_GAUSS_TO_MICROTESLA \
|
||||
(100) /**< Gauss to micro-Tesla multiplier */
|
||||
|
||||
/** Sensor types */
|
||||
typedef enum {
|
||||
SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */
|
||||
SENSOR_TYPE_MAGNETIC_FIELD = (2),
|
||||
SENSOR_TYPE_ORIENTATION = (3),
|
||||
SENSOR_TYPE_GYROSCOPE = (4),
|
||||
SENSOR_TYPE_LIGHT = (5),
|
||||
SENSOR_TYPE_PRESSURE = (6),
|
||||
SENSOR_TYPE_PROXIMITY = (8),
|
||||
SENSOR_TYPE_GRAVITY = (9),
|
||||
SENSOR_TYPE_LINEAR_ACCELERATION =
|
||||
(10), /**< Acceleration not including gravity */
|
||||
SENSOR_TYPE_ROTATION_VECTOR = (11),
|
||||
SENSOR_TYPE_RELATIVE_HUMIDITY = (12),
|
||||
SENSOR_TYPE_AMBIENT_TEMPERATURE = (13),
|
||||
SENSOR_TYPE_OBJECT_TEMPERATURE = (14),
|
||||
SENSOR_TYPE_VOLTAGE = (15),
|
||||
SENSOR_TYPE_CURRENT = (16),
|
||||
SENSOR_TYPE_COLOR = (17),
|
||||
SENSOR_TYPE_TVOC = (18),
|
||||
SENSOR_TYPE_VOC_INDEX = (19),
|
||||
SENSOR_TYPE_NOX_INDEX = (20),
|
||||
SENSOR_TYPE_CO2 = (21),
|
||||
SENSOR_TYPE_ECO2 = (22),
|
||||
SENSOR_TYPE_PM10_STD = (23),
|
||||
SENSOR_TYPE_PM25_STD = (24),
|
||||
SENSOR_TYPE_PM100_STD = (25),
|
||||
SENSOR_TYPE_PM10_ENV = (26),
|
||||
SENSOR_TYPE_PM25_ENV = (27),
|
||||
SENSOR_TYPE_PM100_ENV = (28),
|
||||
SENSOR_TYPE_GAS_RESISTANCE = (29),
|
||||
SENSOR_TYPE_UNITLESS_PERCENT = (30),
|
||||
SENSOR_TYPE_ALTITUDE = (31)
|
||||
} sensors_type_t;
|
||||
|
||||
/** struct sensors_vec_s is used to return a vector in a common format. */
|
||||
typedef struct {
|
||||
union {
|
||||
float v[3]; ///< 3D vector elements
|
||||
struct {
|
||||
float x; ///< X component of vector
|
||||
float y; ///< Y component of vector
|
||||
float z; ///< Z component of vector
|
||||
}; ///< Struct for holding XYZ component
|
||||
/* Orientation sensors */
|
||||
struct {
|
||||
float roll; /**< Rotation around the longitudinal axis (the plane body, 'X
|
||||
axis'). Roll is positive and increasing when moving
|
||||
downward. -90 degrees <= roll <= 90 degrees */
|
||||
float pitch; /**< Rotation around the lateral axis (the wing span, 'Y
|
||||
axis'). Pitch is positive and increasing when moving
|
||||
upwards. -180 degrees <= pitch <= 180 degrees) */
|
||||
float heading; /**< Angle between the longitudinal axis (the plane body)
|
||||
and magnetic north, measured clockwise when viewing from
|
||||
the top of the device. 0-359 degrees */
|
||||
}; ///< Struct for holding roll/pitch/heading
|
||||
}; ///< Union that can hold 3D vector array, XYZ components or
|
||||
///< roll/pitch/heading
|
||||
int8_t status; ///< Status byte
|
||||
uint8_t reserved[3]; ///< Reserved
|
||||
} sensors_vec_t;
|
||||
|
||||
/** struct sensors_color_s is used to return color data in a common format. */
|
||||
typedef struct {
|
||||
union {
|
||||
float c[3]; ///< Raw 3-element data
|
||||
/* RGB color space */
|
||||
struct {
|
||||
float r; /**< Red component */
|
||||
float g; /**< Green component */
|
||||
float b; /**< Blue component */
|
||||
}; ///< RGB data in floating point notation
|
||||
}; ///< Union of various ways to describe RGB colorspace
|
||||
uint32_t rgba; /**< 24-bit RGBA value */
|
||||
} sensors_color_t;
|
||||
|
||||
/* Sensor event (36 bytes) */
|
||||
/** struct sensor_event_s is used to provide a single sensor event in a common
|
||||
* format. */
|
||||
typedef struct {
|
||||
int32_t version; /**< must be sizeof(struct sensors_event_t) */
|
||||
int32_t sensor_id; /**< unique sensor identifier */
|
||||
int32_t type; /**< sensor type */
|
||||
int32_t reserved0; /**< reserved */
|
||||
int32_t timestamp; /**< time is in milliseconds */
|
||||
union {
|
||||
float data[4]; ///< Raw data */
|
||||
sensors_vec_t acceleration; /**< acceleration values are in meter per second
|
||||
per second (m/s^2) */
|
||||
sensors_vec_t
|
||||
magnetic; /**< magnetic vector values are in micro-Tesla (uT) */
|
||||
sensors_vec_t orientation; /**< orientation values are in degrees */
|
||||
sensors_vec_t gyro; /**< gyroscope values are in rad/s */
|
||||
float temperature; /**< temperature is in degrees centigrade (Celsius) */
|
||||
float distance; /**< distance in centimeters */
|
||||
float light; /**< light in SI lux units */
|
||||
float pressure; /**< pressure in hectopascal (hPa) */
|
||||
float relative_humidity; /**< relative humidity in percent */
|
||||
float current; /**< current in milliamps (mA) */
|
||||
float voltage; /**< voltage in volts (V) */
|
||||
float tvoc; /**< Total Volatile Organic Compounds, in ppb */
|
||||
float voc_index; /**< VOC (Volatile Organic Compound) index where 100 is
|
||||
normal (unitless) */
|
||||
float nox_index; /**< NOx (Nitrogen Oxides) index where 100 is normal
|
||||
(unitless) */
|
||||
float CO2; /**< Measured CO2 in parts per million (ppm) */
|
||||
float eCO2; /**< equivalent/estimated CO2 in parts per million (ppm
|
||||
estimated from some other measurement) */
|
||||
float pm10_std; /**< Standard Particulate Matter <=1.0 in parts per million
|
||||
(ppm) */
|
||||
float pm25_std; /**< Standard Particulate Matter <=2.5 in parts per million
|
||||
(ppm) */
|
||||
float pm100_std; /**< Standard Particulate Matter <=10.0 in parts per
|
||||
million (ppm) */
|
||||
float pm10_env; /**< Environmental Particulate Matter <=1.0 in parts per
|
||||
million (ppm) */
|
||||
float pm25_env; /**< Environmental Particulate Matter <=2.5 in parts per
|
||||
million (ppm) */
|
||||
float pm100_env; /**< Environmental Particulate Matter <=10.0 in parts per
|
||||
million (ppm) */
|
||||
float gas_resistance; /**< Proportional to the amount of VOC particles in
|
||||
the air (Ohms) */
|
||||
float unitless_percent; /**<Percentage, unit-less (%) */
|
||||
sensors_color_t color; /**< color in RGB component values */
|
||||
float altitude; /**< Distance between a reference datum and a point or
|
||||
object, in meters. */
|
||||
}; ///< Union for the wide ranges of data we can carry
|
||||
} sensors_event_t;
|
||||
|
||||
/* Sensor details (40 bytes) */
|
||||
/** struct sensor_s is used to describe basic information about a specific
|
||||
* sensor. */
|
||||
typedef struct {
|
||||
char name[12]; /**< sensor name */
|
||||
int32_t version; /**< version of the hardware + driver */
|
||||
int32_t sensor_id; /**< unique sensor identifier */
|
||||
int32_t type; /**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */
|
||||
float max_value; /**< maximum value of this sensor's value in SI units */
|
||||
float min_value; /**< minimum value of this sensor's value in SI units */
|
||||
float resolution; /**< smallest difference between two values reported by this
|
||||
sensor */
|
||||
int32_t min_delay; /**< min delay in microseconds between events. zero = not a
|
||||
constant rate */
|
||||
} sensor_t;
|
||||
|
||||
/** @brief Common sensor interface to unify various sensors.
|
||||
* Intentionally modeled after sensors.h in the Android API:
|
||||
* https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h
|
||||
*/
|
||||
class Adafruit_Sensor {
|
||||
public:
|
||||
// Constructor(s)
|
||||
Adafruit_Sensor() {}
|
||||
virtual ~Adafruit_Sensor() {}
|
||||
|
||||
// These must be defined by the subclass
|
||||
|
||||
/*! @brief Whether we should automatically change the range (if possible) for
|
||||
higher precision
|
||||
@param enabled True if we will try to autorange */
|
||||
virtual void enableAutoRange(bool enabled) {
|
||||
(void)enabled; /* suppress unused warning */
|
||||
};
|
||||
|
||||
/*! @brief Get the latest sensor event
|
||||
@returns True if able to fetch an event */
|
||||
virtual bool getEvent(sensors_event_t *) = 0;
|
||||
/*! @brief Get info about the sensor itself */
|
||||
virtual void getSensor(sensor_t *) = 0;
|
||||
|
||||
void printSensorDetails(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,271 @@
|
||||
# Adafruit Unified Sensor Driver #
|
||||
|
||||
Many small embedded systems exist to collect data from sensors, analyse the data, and either take an appropriate action or send that sensor data to another system for processing.
|
||||
|
||||
One of the many challenges of embedded systems design is the fact that parts you used today may be out of production tomorrow, or system requirements may change and you may need to choose a different sensor down the road.
|
||||
|
||||
Creating new drivers is a relatively easy task, but integrating them into existing systems is both error prone and time consuming since sensors rarely use the exact same units of measurement.
|
||||
|
||||
By reducing all data to a single **sensors\_event\_t** 'type' and settling on specific, **standardised SI units** for each sensor family the same sensor types return values that are comparable with any other similar sensor. This enables you to switch sensor models with very little impact on the rest of the system, which can help mitigate some of the risks and problems of sensor availability and code reuse.
|
||||
|
||||
The unified sensor abstraction layer is also useful for data-logging and data-transmission since you only have one well-known type to log or transmit over the air or wire.
|
||||
|
||||
## Unified Sensor Drivers ##
|
||||
|
||||
The following drivers are based on the Adafruit Unified Sensor Driver:
|
||||
|
||||
**Accelerometers**
|
||||
- [Adafruit\_ADXL345](https://github.com/adafruit/Adafruit_ADXL345)
|
||||
- [Adafruit\_LSM303DLHC](https://github.com/adafruit/Adafruit_LSM303DLHC)
|
||||
- [Adafruit\_MMA8451\_Library](https://github.com/adafruit/Adafruit_MMA8451_Library)
|
||||
|
||||
**Gyroscope**
|
||||
- [Adafruit\_L3GD20\_U](https://github.com/adafruit/Adafruit_L3GD20_U)
|
||||
|
||||
**Light**
|
||||
- [Adafruit\_TSL2561](https://github.com/adafruit/Adafruit_TSL2561)
|
||||
- [Adafruit\_TSL2591\_Library](https://github.com/adafruit/Adafruit_TSL2591_Library)
|
||||
|
||||
**Magnetometers**
|
||||
- [Adafruit\_LSM303DLHC](https://github.com/adafruit/Adafruit_LSM303DLHC)
|
||||
- [Adafruit\_HMC5883\_Unified](https://github.com/adafruit/Adafruit_HMC5883_Unified)
|
||||
|
||||
**Barometric Pressure**
|
||||
- [Adafruit\_BMP085\_Unified](https://github.com/adafruit/Adafruit_BMP085_Unified)
|
||||
- [Adafruit\_BMP183\_Unified\_Library](https://github.com/adafruit/Adafruit_BMP183_Unified_Library)
|
||||
|
||||
**Humidity & Temperature**
|
||||
- [DHT-sensor-library](https://github.com/adafruit/DHT-sensor-library)
|
||||
|
||||
**Humidity, Temperature, & Barometric Pressure**
|
||||
- [Adafruit_BME280_Library](https://github.com/adafruit/Adafruit_BME280_Library/)
|
||||
|
||||
**Orientation**
|
||||
- [Adafruit_BNO055](https://github.com/adafruit/Adafruit_BNO055)
|
||||
|
||||
**All in one device**
|
||||
- [Adafruit_LSM9DS0](https://github.com/adafruit/Adafruit_LSM9DS0_Library) (accelerometer, gyroscope, magnetometer)
|
||||
- [Adafruit_LSM9DS1](https://github.com/adafruit/Adafruit_LSM9DS1/) (accelerometer, gyroscope, magnetometer)
|
||||
|
||||
|
||||
## How Does it Work? ##
|
||||
|
||||
Any driver that supports the Adafruit unified sensor abstraction layer will implement the Adafruit\_Sensor base class. There are two main typedefs and one enum defined in Adafruit_Sensor.h that are used to 'abstract' away the sensor details and values:
|
||||
|
||||
## Sensor Types (`sensors_type_t`)
|
||||
|
||||
These pre-defined sensor types are used to properly handle the two related typedefs below, and allows us determine what types of units the sensor uses, etc.
|
||||
|
||||
```c++
|
||||
/** Sensor types */
|
||||
typedef enum
|
||||
{
|
||||
SENSOR_TYPE_ACCELEROMETER = (1),
|
||||
SENSOR_TYPE_MAGNETIC_FIELD = (2),
|
||||
SENSOR_TYPE_ORIENTATION = (3),
|
||||
SENSOR_TYPE_GYROSCOPE = (4),
|
||||
SENSOR_TYPE_LIGHT = (5),
|
||||
SENSOR_TYPE_PRESSURE = (6),
|
||||
SENSOR_TYPE_PROXIMITY = (8),
|
||||
SENSOR_TYPE_GRAVITY = (9),
|
||||
SENSOR_TYPE_LINEAR_ACCELERATION = (10),
|
||||
SENSOR_TYPE_ROTATION_VECTOR = (11),
|
||||
SENSOR_TYPE_RELATIVE_HUMIDITY = (12),
|
||||
SENSOR_TYPE_AMBIENT_TEMPERATURE = (13),
|
||||
SENSOR_TYPE_VOLTAGE = (15),
|
||||
SENSOR_TYPE_CURRENT = (16),
|
||||
SENSOR_TYPE_COLOR = (17),
|
||||
SENSOR_TYPE_TVOC = (18),
|
||||
SENSOR_TYPE_VOC_INDEX = (19),
|
||||
SENSOR_TYPE_NOX_INDEX = (20),
|
||||
SENSOR_TYPE_CO2 = (21),
|
||||
SENSOR_TYPE_ECO2 = (22),
|
||||
SENSOR_TYPE_PM10_STD = (23),
|
||||
SENSOR_TYPE_PM25_STD = (24),
|
||||
SENSOR_TYPE_PM100_STD = (25),
|
||||
SENSOR_TYPE_PM10_ENV = (26),
|
||||
SENSOR_TYPE_PM25_ENV = (27),
|
||||
SENSOR_TYPE_PM100_ENV = (28),
|
||||
SENSOR_TYPE_GAS_RESISTANCE = (29),
|
||||
SENSOR_TYPE_UNITLESS_PERCENT = (30),
|
||||
SENSOR_TYPE_ALTITUDE = (31),
|
||||
} sensors_type_t;
|
||||
```
|
||||
|
||||
## Sensor Details (`sensor_t`)
|
||||
|
||||
This typedef describes the specific capabilities of this sensor, and allows us to know what sensor we are using beneath the abstraction layer.
|
||||
|
||||
```c++
|
||||
/* Sensor details (40 bytes) */
|
||||
/** struct sensor_s is used to describe basic information about a specific sensor. */
|
||||
typedef struct
|
||||
{
|
||||
char name[12];
|
||||
int32_t version;
|
||||
int32_t sensor_id;
|
||||
int32_t type;
|
||||
float max_value;
|
||||
float min_value;
|
||||
float resolution;
|
||||
int32_t min_delay;
|
||||
} sensor_t;
|
||||
```
|
||||
|
||||
The individual fields are intended to be used as follows:
|
||||
|
||||
- **name**: The sensor name or ID, up to a maximum of twelve characters (ex. "MPL115A2")
|
||||
- **version**: The version of the sensor HW and the driver to allow us to differentiate versions of the board or driver
|
||||
- **sensor\_id**: A unique sensor identifier that is used to differentiate this specific sensor instance from any others that are present on the system or in the sensor network
|
||||
- **type**: The sensor type, based on **sensors\_type\_t** in sensors.h
|
||||
- **max\_value**: The maximum value that this sensor can return (in the appropriate SI unit)
|
||||
- **min\_value**: The minimum value that this sensor can return (in the appropriate SI unit)
|
||||
- **resolution**: The smallest difference between two values that this sensor can report (in the appropriate SI unit)
|
||||
- **min\_delay**: The minimum delay in microseconds between two sensor events, or '0' if there is no constant sensor rate
|
||||
|
||||
## Sensor Data/Events (`sensors_event_t`)
|
||||
|
||||
This typedef is used to return sensor data from any sensor supported by the abstraction layer, using standard SI units and scales.
|
||||
|
||||
```c++
|
||||
/* Sensor event (36 bytes) */
|
||||
/** struct sensor_event_s is used to provide a single sensor event in a common format. */
|
||||
typedef struct
|
||||
{
|
||||
int32_t version;
|
||||
int32_t sensor_id;
|
||||
int32_t type;
|
||||
int32_t reserved0;
|
||||
int32_t timestamp;
|
||||
union
|
||||
{
|
||||
float data[4];
|
||||
sensors_vec_t acceleration;
|
||||
sensors_vec_t magnetic;
|
||||
sensors_vec_t orientation;
|
||||
sensors_vec_t gyro;
|
||||
float temperature;
|
||||
float distance;
|
||||
float light;
|
||||
float pressure;
|
||||
float relative_humidity;
|
||||
float current;
|
||||
float voltage;
|
||||
float tvoc;
|
||||
float voc_index;
|
||||
float nox_index;
|
||||
float CO2,
|
||||
float eCO2,
|
||||
float pm10_std,
|
||||
float pm25_std,
|
||||
float pm100_std,
|
||||
float pm10_env,
|
||||
float pm25_env,
|
||||
float pm100_env,
|
||||
float gas_resistance,
|
||||
float unitless_percent,
|
||||
float altitude,
|
||||
sensors_color_t color;
|
||||
};
|
||||
} sensors_event_t;
|
||||
```
|
||||
It includes the following fields:
|
||||
|
||||
- **version**: Contain 'sizeof(sensors\_event\_t)' to identify which version of the API we're using in case this changes in the future
|
||||
- **sensor\_id**: A unique sensor identifier that is used to differentiate this specific sensor instance from any others that are present on the system or in the sensor network (must match the sensor\_id value in the corresponding sensor\_t enum above!)
|
||||
- **type**: the sensor type, based on **sensors\_type\_t** in sensors.h
|
||||
- **timestamp**: time in milliseconds when the sensor value was read
|
||||
- **data[4]**: An array of four 32-bit values that allows us to encapsulate any type of sensor data via a simple union (further described below)
|
||||
|
||||
## Required Functions
|
||||
|
||||
In addition to the two standard types and the sensor type enum, all drivers based on Adafruit_Sensor must also implement the following two functions:
|
||||
|
||||
```c++
|
||||
bool getEvent(sensors_event_t*);
|
||||
```
|
||||
Calling this function will populate the supplied sensors\_event\_t reference with the latest available sensor data. You should call this function as often as you want to update your data.
|
||||
|
||||
```c++
|
||||
void getSensor(sensor_t*);
|
||||
```
|
||||
Calling this function will provide some basic information about the sensor (the sensor name, driver version, min and max values, etc.
|
||||
|
||||
## Standardised SI values for `sensors_event_t`
|
||||
|
||||
A key part of the abstraction layer is the standardization of values on SI units of a particular scale, which is accomplished via the data[4] union in sensors\_event\_t above. This 16 byte union includes fields for each main sensor type, and uses the following SI units and scales:
|
||||
|
||||
- **acceleration**: values are in **meter per second per second** (m/s^2)
|
||||
- **magnetic**: values are in **micro-Tesla** (uT)
|
||||
- **orientation**: values are in **degrees**
|
||||
- **gyro**: values are in **rad/s**
|
||||
- **temperature**: values in **degrees centigrade** (Celsius)
|
||||
- **distance**: values are in **centimeters**
|
||||
- **light**: values are in **SI lux** units
|
||||
- **pressure**: values are in **hectopascal** (hPa)
|
||||
- **relative\_humidity**: values are in **percent**
|
||||
- **current**: values are in **milliamps** (mA)
|
||||
- **voltage**: values are in **volts** (V)
|
||||
- **color**: values are in 0..1.0 RGB channel luminosity and 32-bit RGBA format
|
||||
- **tvoc**: values are in **parts per billion** (ppb)
|
||||
- **voc_index**: values are an **index** from 1-500 with 100 being normal
|
||||
- **nox_index**: values are an **index** from 1-500 with 100 being normal
|
||||
- **CO2**: values are in **parts per million** (ppm)
|
||||
- **eCO2**: values are in **parts per million** (ppm)
|
||||
- **pm10_std**: values are in **parts per million** (ppm)
|
||||
- **pm25_std**: values are in **parts per million** (ppm)
|
||||
- **pm100_std**: values are in **parts per million** (ppm)
|
||||
- **pm10_env**: values are in **parts per million** (ppm)
|
||||
- **pm25_env**: values are in **parts per million** (ppm)
|
||||
- **pm100_env**: values are in **parts per million** (ppm)
|
||||
- **gas_resistance**: values are in **ohms**
|
||||
- **unitless_percent**: values are in **%**
|
||||
- **altitude**: values are in **meters** (m)
|
||||
|
||||
## The Unified Driver Abstraction Layer in Practice ##
|
||||
|
||||
Using the unified sensor abstraction layer is relatively easy once a compliant driver has been created.
|
||||
|
||||
Every compliant sensor can now be read using a single, well-known 'type' (sensors\_event\_t), and there is a standardized way of interrogating a sensor about its specific capabilities (via sensor\_t).
|
||||
|
||||
An example of reading the [TSL2561](https://github.com/adafruit/Adafruit_TSL2561) light sensor can be seen below:
|
||||
|
||||
```c++
|
||||
Adafruit_TSL2561 tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);
|
||||
...
|
||||
/* 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");
|
||||
}
|
||||
```
|
||||
|
||||
Similarly, we can get the basic technical capabilities of this sensor with the following code:
|
||||
|
||||
```c++
|
||||
sensor_t sensor;
|
||||
|
||||
sensor_t sensor;
|
||||
tsl.getSensor(&sensor);
|
||||
|
||||
/* Display the sensor details */
|
||||
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("");
|
||||
```
|
||||
@@ -0,0 +1,153 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_ADXL343.h>
|
||||
|
||||
/* Assign a unique ID to this sensor at the same time */
|
||||
/* Uncomment following line for default Wire bus */
|
||||
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
|
||||
|
||||
/* NeoTrellis M4, etc. */
|
||||
/* Uncomment following line for Wire1 bus */
|
||||
//Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &Wire1);
|
||||
|
||||
void displaySensorDetails(void)
|
||||
{
|
||||
sensor_t sensor;
|
||||
accel.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(" m/s^2");
|
||||
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
|
||||
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" m/s^2");
|
||||
Serial.println("------------------------------------");
|
||||
Serial.println("");
|
||||
delay(500);
|
||||
}
|
||||
|
||||
void displayDataRate(void)
|
||||
{
|
||||
Serial.print ("Data Rate: ");
|
||||
|
||||
switch(accel.getDataRate())
|
||||
{
|
||||
case ADXL343_DATARATE_3200_HZ:
|
||||
Serial.print ("3200 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_1600_HZ:
|
||||
Serial.print ("1600 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_800_HZ:
|
||||
Serial.print ("800 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_400_HZ:
|
||||
Serial.print ("400 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_200_HZ:
|
||||
Serial.print ("200 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_100_HZ:
|
||||
Serial.print ("100 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_50_HZ:
|
||||
Serial.print ("50 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_25_HZ:
|
||||
Serial.print ("25 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_12_5_HZ:
|
||||
Serial.print ("12.5 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_6_25HZ:
|
||||
Serial.print ("6.25 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_3_13_HZ:
|
||||
Serial.print ("3.13 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_1_56_HZ:
|
||||
Serial.print ("1.56 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_78_HZ:
|
||||
Serial.print ("0.78 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_39_HZ:
|
||||
Serial.print ("0.39 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_20_HZ:
|
||||
Serial.print ("0.20 ");
|
||||
break;
|
||||
case ADXL343_DATARATE_0_10_HZ:
|
||||
Serial.print ("0.10 ");
|
||||
break;
|
||||
default:
|
||||
Serial.print ("???? ");
|
||||
break;
|
||||
}
|
||||
Serial.println(" Hz");
|
||||
}
|
||||
|
||||
void displayRange(void)
|
||||
{
|
||||
Serial.print ("Range: +/- ");
|
||||
|
||||
switch(accel.getRange())
|
||||
{
|
||||
case ADXL343_RANGE_16_G:
|
||||
Serial.print ("16 ");
|
||||
break;
|
||||
case ADXL343_RANGE_8_G:
|
||||
Serial.print ("8 ");
|
||||
break;
|
||||
case ADXL343_RANGE_4_G:
|
||||
Serial.print ("4 ");
|
||||
break;
|
||||
case ADXL343_RANGE_2_G:
|
||||
Serial.print ("2 ");
|
||||
break;
|
||||
default:
|
||||
Serial.print ("?? ");
|
||||
break;
|
||||
}
|
||||
Serial.println(" g");
|
||||
}
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
Serial.println("Accelerometer Test"); Serial.println("");
|
||||
|
||||
/* Initialise the sensor */
|
||||
if(!accel.begin())
|
||||
{
|
||||
/* There was a problem detecting the ADXL343 ... check your connections */
|
||||
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Set the range to whatever is appropriate for your project */
|
||||
accel.setRange(ADXL343_RANGE_16_G);
|
||||
// accel.setRange(ADXL343_RANGE_8_G);
|
||||
// accel.setRange(ADXL343_RANGE_4_G);
|
||||
// accel.setRange(ADXL343_RANGE_2_G);
|
||||
|
||||
/* Display some basic information on this sensor */
|
||||
displaySensorDetails();
|
||||
displayDataRate();
|
||||
displayRange();
|
||||
Serial.println("");
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
/* Get a new sensor event */
|
||||
sensors_event_t event;
|
||||
accel.getEvent(&event);
|
||||
|
||||
/* Display the results (acceleration is measured in m/s^2) */
|
||||
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
|
||||
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
|
||||
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");Serial.println("m/s^2 ");
|
||||
delay(500);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
name=Adafruit Unified Sensor
|
||||
version=1.1.13
|
||||
author=Adafruit <info@adafruit.com>
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Required for all Adafruit Unified Sensor based libraries.
|
||||
paragraph=A unified sensor abstraction layer used by many Adafruit sensor libraries.
|
||||
category=Sensors
|
||||
url=https://github.com/adafruit/Adafruit_Sensor
|
||||
architectures=*
|
||||
includes=Adafruit_Sensor.h
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# See: https://github.com/codespell-project/codespell#using-a-config-file
|
||||
[codespell]
|
||||
# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here:
|
||||
ignore-words-list = ,
|
||||
check-filenames =
|
||||
check-hidden =
|
||||
skip = ./.git
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
# See: https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates#about-the-dependabotyml-file
|
||||
version: 2
|
||||
|
||||
updates:
|
||||
# Configure check for outdated GitHub Actions actions in workflows.
|
||||
# See: https://docs.github.com/en/github/administering-a-repository/keeping-your-actions-up-to-date-with-dependabot
|
||||
- package-ecosystem: github-actions
|
||||
directory: / # Check the repository's workflows under /.github/workflows/
|
||||
schedule:
|
||||
interval: daily
|
||||
labels:
|
||||
- "topic: infrastructure"
|
||||
@@ -0,0 +1,28 @@
|
||||
name: Check Arduino
|
||||
|
||||
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
schedule:
|
||||
# Run every Tuesday at 8 AM UTC to catch breakage caused by new rules added to Arduino Lint.
|
||||
- cron: "0 8 * * TUE"
|
||||
workflow_dispatch:
|
||||
repository_dispatch:
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Arduino Lint
|
||||
uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
compliance: specification
|
||||
library-manager: update
|
||||
# Always use this setting for official repositories. Remove for 3rd party projects.
|
||||
official: true
|
||||
project-type: library
|
||||
@@ -0,0 +1,62 @@
|
||||
name: Compile Examples
|
||||
|
||||
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- ".github/workflows/compile-examples.yml"
|
||||
- "examples/**"
|
||||
- "src/**"
|
||||
pull_request:
|
||||
paths:
|
||||
- ".github/workflows/compile-examples.yml"
|
||||
- "examples/**"
|
||||
- "src/**"
|
||||
schedule:
|
||||
# Run every Tuesday at 8 AM UTC to catch breakage caused by changes to external resources (libraries, platforms).
|
||||
- cron: "0 8 * * TUE"
|
||||
workflow_dispatch:
|
||||
repository_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: ${{ matrix.board.fqbn }}
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
env:
|
||||
SKETCHES_REPORTS_PATH: sketches-reports
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
|
||||
matrix:
|
||||
board:
|
||||
- fqbn: arduino:samd:mkr1000
|
||||
platforms: |
|
||||
- name: arduino:samd
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Compile examples
|
||||
uses: arduino/compile-sketches@v1
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
fqbn: ${{ matrix.board.fqbn }}
|
||||
platforms: ${{ matrix.board.platforms }}
|
||||
libraries: |
|
||||
# Install the library from the local path.
|
||||
- source-path: ./
|
||||
- name: WiFi101
|
||||
sketch-paths: |
|
||||
- examples
|
||||
enable-deltas-report: true
|
||||
sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }}
|
||||
|
||||
- name: Save sketches report as workflow artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
if-no-files-found: error
|
||||
path: ${{ env.SKETCHES_REPORTS_PATH }}
|
||||
name: ${{ env.SKETCHES_REPORTS_PATH }}
|
||||
@@ -0,0 +1,24 @@
|
||||
name: Report Size Deltas
|
||||
|
||||
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- ".github/workflows/report-size-deltas.yml"
|
||||
schedule:
|
||||
# Run at the minimum interval allowed by GitHub Actions.
|
||||
# Note: GitHub Actions periodically has outages which result in workflow failures.
|
||||
# In this event, the workflows will start passing again once the service recovers.
|
||||
- cron: "*/5 * * * *"
|
||||
workflow_dispatch:
|
||||
repository_dispatch:
|
||||
|
||||
jobs:
|
||||
report:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Comment size deltas reports to PRs
|
||||
uses: arduino/report-size-deltas@v1
|
||||
with:
|
||||
# The name of the workflow artifact created by the sketch compilation workflow
|
||||
sketches-reports-source: sketches-reports
|
||||
@@ -0,0 +1,22 @@
|
||||
name: Spell Check
|
||||
|
||||
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
schedule:
|
||||
# Run every Tuesday at 8 AM UTC to catch new misspelling detections resulting from dictionary updates.
|
||||
- cron: "0 8 * * TUE"
|
||||
workflow_dispatch:
|
||||
repository_dispatch:
|
||||
|
||||
jobs:
|
||||
spellcheck:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Spell check
|
||||
uses: codespell-project/actions-codespell@master
|
||||
@@ -0,0 +1,138 @@
|
||||
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/sync-labels.md
|
||||
name: Sync Labels
|
||||
|
||||
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- ".github/workflows/sync-labels.ya?ml"
|
||||
- ".github/label-configuration-files/*.ya?ml"
|
||||
pull_request:
|
||||
paths:
|
||||
- ".github/workflows/sync-labels.ya?ml"
|
||||
- ".github/label-configuration-files/*.ya?ml"
|
||||
schedule:
|
||||
# Run daily at 8 AM UTC to sync with changes to shared label configurations.
|
||||
- cron: "0 8 * * *"
|
||||
workflow_dispatch:
|
||||
repository_dispatch:
|
||||
|
||||
env:
|
||||
CONFIGURATIONS_FOLDER: .github/label-configuration-files
|
||||
CONFIGURATIONS_ARTIFACT: label-configuration-files
|
||||
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Download JSON schema for labels configuration file
|
||||
id: download-schema
|
||||
uses: carlosperate/download-file-action@v2
|
||||
with:
|
||||
file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json
|
||||
location: ${{ runner.temp }}/label-configuration-schema
|
||||
|
||||
- name: Install JSON schema validator
|
||||
run: |
|
||||
sudo npm install \
|
||||
--global \
|
||||
ajv-cli \
|
||||
ajv-formats
|
||||
|
||||
- name: Validate local labels configuration
|
||||
run: |
|
||||
# See: https://github.com/ajv-validator/ajv-cli#readme
|
||||
ajv validate \
|
||||
--all-errors \
|
||||
-c ajv-formats \
|
||||
-s "${{ steps.download-schema.outputs.file-path }}" \
|
||||
-d "${{ env.CONFIGURATIONS_FOLDER }}/*.{yml,yaml}"
|
||||
|
||||
download:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
filename:
|
||||
# Filenames of the shared configurations to apply to the repository in addition to the local configuration.
|
||||
# https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/sync-labels
|
||||
- universal.yml
|
||||
|
||||
steps:
|
||||
- name: Download
|
||||
uses: carlosperate/download-file-action@v2
|
||||
with:
|
||||
file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/${{ matrix.filename }}
|
||||
|
||||
- name: Pass configuration files to next job via workflow artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
path: |
|
||||
*.yaml
|
||||
*.yml
|
||||
if-no-files-found: error
|
||||
name: ${{ env.CONFIGURATIONS_ARTIFACT }}
|
||||
|
||||
sync:
|
||||
needs: download
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Set environment variables
|
||||
run: |
|
||||
# See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
|
||||
echo "MERGED_CONFIGURATION_PATH=${{ runner.temp }}/labels.yml" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Determine whether to dry run
|
||||
id: dry-run
|
||||
if: >
|
||||
github.event_name == 'pull_request' ||
|
||||
(
|
||||
(
|
||||
github.event_name == 'push' ||
|
||||
github.event_name == 'workflow_dispatch'
|
||||
) &&
|
||||
github.ref != format('refs/heads/{0}', github.event.repository.default_branch)
|
||||
)
|
||||
run: |
|
||||
# Use of this flag in the github-label-sync command will cause it to only check the validity of the
|
||||
# configuration.
|
||||
echo "::set-output name=flag::--dry-run"
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Download configuration files artifact
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: ${{ env.CONFIGURATIONS_ARTIFACT }}
|
||||
path: ${{ env.CONFIGURATIONS_FOLDER }}
|
||||
|
||||
- name: Remove unneeded artifact
|
||||
uses: geekyeggo/delete-artifact@v2
|
||||
with:
|
||||
name: ${{ env.CONFIGURATIONS_ARTIFACT }}
|
||||
|
||||
- name: Merge label configuration files
|
||||
run: |
|
||||
# Merge all configuration files
|
||||
shopt -s extglob
|
||||
cat "${{ env.CONFIGURATIONS_FOLDER }}"/*.@(yml|yaml) > "${{ env.MERGED_CONFIGURATION_PATH }}"
|
||||
|
||||
- name: Install github-label-sync
|
||||
run: sudo npm install --global github-label-sync
|
||||
|
||||
- name: Sync labels
|
||||
env:
|
||||
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
# See: https://github.com/Financial-Times/github-label-sync
|
||||
github-label-sync \
|
||||
--labels "${{ env.MERGED_CONFIGURATION_PATH }}" \
|
||||
${{ steps.dry-run.outputs.flag }} \
|
||||
${{ github.repository }}
|
||||
@@ -0,0 +1 @@
|
||||
{"type": "library", "name": "ArduinoHttpClient", "version": "0.5.0", "spec": {"owner": "arduino-libraries", "id": 798, "name": "ArduinoHttpClient", "requirements": null, "uri": null}}
|
||||
@@ -0,0 +1,33 @@
|
||||
## ArduinoHttpClient 0.4.0 - 2019.04.09
|
||||
|
||||
* Added URLEncoder helper
|
||||
|
||||
## ArduinoHttpClient 0.3.2 - 2019.02.04
|
||||
|
||||
* Changed Flush return value resulting in compilation error. Thanks @forGGe
|
||||
|
||||
## ArduinoHttpClient 0.3.1 - 2017.09.25
|
||||
|
||||
* Changed examples to support Arduino Create secret tabs
|
||||
* Increase WebSocket secret-key length to 24 characters
|
||||
|
||||
## ArduinoHttpClient 0.3.0 - 2017.04.20
|
||||
|
||||
* Added support for PATCH operations
|
||||
* Added support for chunked response bodies
|
||||
* Added new beginBody API
|
||||
|
||||
## ArduinoHttpClient 0.2.0 - 2017.01.12
|
||||
|
||||
* Added PATCH method
|
||||
* Added basic auth example
|
||||
* Added custom header example
|
||||
|
||||
## ArduinoHttpClient 0.1.1 - 2016.12.16
|
||||
|
||||
* More robust response parser
|
||||
|
||||
## ArduinoHttpClient 0.1.0 - 2016.07.05
|
||||
|
||||
* Initial release
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# ArduinoHttpClient
|
||||
|
||||
[](https://github.com/arduino-libraries/ArduinoHttpClient/actions/workflows/check-arduino.yml)
|
||||
[](https://github.com/arduino-libraries/ArduinoHttpClient/actions/workflows/compile-examples.yml)
|
||||
[](https://github.com/arduino-libraries/ArduinoHttpClient/actions/workflows/spell-check.yml)
|
||||
|
||||
ArduinoHttpClient is a library to make it easier to interact with web servers from Arduino.
|
||||
|
||||
Derived from [Adrian McEwen's HttpClient library](https://github.com/amcewen/HttpClient)
|
||||
|
||||
## Dependencies
|
||||
|
||||
- Requires a networking hardware and a library that provides transport specific `Client` instance, such as:
|
||||
- [WiFiNINA](https://github.com/arduino-libraries/WiFiNINA)
|
||||
- [WiFi101](https://github.com/arduino-libraries/WiFi101)
|
||||
- [Ethernet](https://github.com/arduino-libraries/Ethernet)
|
||||
- [MKRGSM](https://github.com/arduino-libraries/MKRGSM)
|
||||
- [MKRNB](https://github.com/arduino-libraries/MKRNB)
|
||||
- [WiFi](https://github.com/arduino-libraries/WiFi)
|
||||
- [GSM](https://github.com/arduino-libraries/GSM)
|
||||
|
||||
## Usage
|
||||
|
||||
In normal usage, handles the outgoing request and Host header. The returned status code is parsed for you, as is the Content-Length header (if present).
|
||||
|
||||
Because it expects an object of type Client, you can use it with any of the networking classes that derive from that. Which means it will work with WiFiClient, EthernetClient and GSMClient.
|
||||
|
||||
See the examples for more detail on how the library is used.
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
GET client with HTTP basic authentication for ArduinoHttpClient library
|
||||
Connects to server once every five seconds, sends a GET request
|
||||
|
||||
created 14 Feb 2016
|
||||
by Tom Igoe
|
||||
modified 3 Jan 2017 to add HTTP basic authentication
|
||||
by Sandeep Mistry
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
char serverAddress[] = "192.168.0.3"; // server address
|
||||
int port = 8080;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("making GET request with HTTP basic authentication");
|
||||
client.beginRequest();
|
||||
client.get("/secure");
|
||||
client.sendBasicAuth("username", "password"); // send the username and password for authentication
|
||||
client.endRequest();
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
Custom request header example for the ArduinoHttpClient
|
||||
library. This example sends a GET and a POST request with a custom header every 5 seconds.
|
||||
|
||||
based on SimpleGet example by Tom Igoe
|
||||
header modifications by Todd Treece
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
char serverAddress[] = "192.168.0.3"; // server address
|
||||
int port = 8080;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("making GET request");
|
||||
client.beginRequest();
|
||||
client.get("/");
|
||||
client.sendHeader("X-CUSTOM-HEADER", "custom_value");
|
||||
client.endRequest();
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("GET Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("GET Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
|
||||
Serial.println("making POST request");
|
||||
String postData = "name=Alice&age=12";
|
||||
client.beginRequest();
|
||||
client.post("/");
|
||||
client.sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
|
||||
client.sendHeader(HTTP_HEADER_CONTENT_LENGTH, postData.length());
|
||||
client.sendHeader("X-CUSTOM-HEADER", "custom_value");
|
||||
client.endRequest();
|
||||
client.write((const byte*)postData.c_str(), postData.length());
|
||||
// note: the above line can also be achieved with the simpler line below:
|
||||
//client.print(postData);
|
||||
|
||||
// read the status code and body of the response
|
||||
statusCode = client.responseStatusCode();
|
||||
response = client.responseBody();
|
||||
|
||||
Serial.print("POST Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("POST Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
Dweet.io GET client for ArduinoHttpClient library
|
||||
Connects to dweet.io once every ten seconds,
|
||||
sends a GET request and a request body. Uses SSL
|
||||
|
||||
Shows how to use Strings to assemble path and parse content
|
||||
from response. dweet.io expects:
|
||||
https://dweet.io/get/latest/dweet/for/thingName
|
||||
|
||||
For more on dweet.io, see https://dweet.io/play/
|
||||
|
||||
created 15 Feb 2016
|
||||
updated 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
const char serverAddress[] = "dweet.io"; // server address
|
||||
int port = 80;
|
||||
String dweetName = "scandalous-cheese-hoarder"; // use your own thing name here
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// assemble the path for the GET message:
|
||||
String path = "/get/latest/dweet/for/" + dweetName;
|
||||
|
||||
// send the GET request
|
||||
Serial.println("making GET request");
|
||||
client.get(path);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
/*
|
||||
Typical response is:
|
||||
{"this":"succeeded",
|
||||
"by":"getting",
|
||||
"the":"dweets",
|
||||
"with":[{"thing":"my-thing-name",
|
||||
"created":"2016-02-16T05:10:36.589Z",
|
||||
"content":{"sensorValue":456}}]}
|
||||
|
||||
You want "content": numberValue
|
||||
*/
|
||||
// now parse the response looking for "content":
|
||||
int labelStart = response.indexOf("content\":");
|
||||
// find the first { after "content":
|
||||
int contentStart = response.indexOf("{", labelStart);
|
||||
// find the following } and get what's between the braces:
|
||||
int contentEnd = response.indexOf("}", labelStart);
|
||||
String content = response.substring(contentStart + 1, contentEnd);
|
||||
Serial.println(content);
|
||||
|
||||
// now get the value after the colon, and convert to an int:
|
||||
int valueStart = content.indexOf(":");
|
||||
String valueString = content.substring(valueStart + 1);
|
||||
int number = valueString.toInt();
|
||||
Serial.print("Value string: ");
|
||||
Serial.println(valueString);
|
||||
Serial.print("Actual value: ");
|
||||
Serial.println(number);
|
||||
|
||||
Serial.println("Wait ten seconds\n");
|
||||
delay(10000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
Dweet.io POST client for ArduinoHttpClient library
|
||||
Connects to dweet.io once every ten seconds,
|
||||
sends a POST request and a request body.
|
||||
|
||||
Shows how to use Strings to assemble path and body
|
||||
|
||||
created 15 Feb 2016
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
const char serverAddress[] = "dweet.io"; // server address
|
||||
int port = 80;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while(!Serial);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// assemble the path for the POST message:
|
||||
String dweetName = "scandalous-cheese-hoarder";
|
||||
String path = "/dweet/for/" + dweetName;
|
||||
String contentType = "application/json";
|
||||
|
||||
// assemble the body of the POST message:
|
||||
int sensorValue = analogRead(A0);
|
||||
String postData = "{\"sensorValue\":\"";
|
||||
postData += sensorValue;
|
||||
postData += "\"}";
|
||||
|
||||
Serial.println("making POST request");
|
||||
|
||||
// send the POST request
|
||||
client.post(path, contentType, postData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait ten seconds\n");
|
||||
delay(10000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/* HueBlink example for ArduinoHttpClient library
|
||||
|
||||
Uses ArduinoHttpClient library to control Philips Hue
|
||||
For more on Hue developer API see http://developer.meethue.com
|
||||
|
||||
To control a light, the Hue expects a HTTP PUT request to:
|
||||
|
||||
http://hue.hub.address/api/hueUserName/lights/lightNumber/state
|
||||
|
||||
The body of the PUT request looks like this:
|
||||
{"on": true} or {"on":false}
|
||||
|
||||
This example shows how to concatenate Strings to assemble the
|
||||
PUT request and the body of the request.
|
||||
|
||||
modified 15 Feb 2016
|
||||
by Tom Igoe (tigoe) to match new API
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <WiFi101.h>
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
int status = WL_IDLE_STATUS; // the WiFi radio's status
|
||||
|
||||
char hueHubIP[] = "192.168.0.3"; // IP address of the HUE bridge
|
||||
String hueUserName = "huebridgeusername"; // hue bridge username
|
||||
|
||||
// make a WiFiClient instance and a HttpClient instance:
|
||||
WiFiClient wifi;
|
||||
HttpClient httpClient = HttpClient(wifi, hueHubIP);
|
||||
|
||||
|
||||
void setup() {
|
||||
//Initialize serial and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial); // wait for serial port to connect.
|
||||
|
||||
// attempt to connect to WiFi network:
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to WPA SSID: ");
|
||||
Serial.println(ssid);
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// you're connected now, so print out the data:
|
||||
Serial.print("You're connected to the network IP = ");
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sendRequest(3, "on", "true"); // turn light on
|
||||
delay(2000); // wait 2 seconds
|
||||
sendRequest(3, "on", "false"); // turn light off
|
||||
delay(2000); // wait 2 seconds
|
||||
}
|
||||
|
||||
void sendRequest(int light, String cmd, String value) {
|
||||
// make a String for the HTTP request path:
|
||||
String request = "/api/" + hueUserName;
|
||||
request += "/lights/";
|
||||
request += light;
|
||||
request += "/state/";
|
||||
|
||||
String contentType = "application/json";
|
||||
|
||||
// make a string for the JSON command:
|
||||
String hueCmd = "{\"" + cmd;
|
||||
hueCmd += "\":";
|
||||
hueCmd += value;
|
||||
hueCmd += "}";
|
||||
// see what you assembled to send:
|
||||
Serial.print("PUT request to server: ");
|
||||
Serial.println(request);
|
||||
Serial.print("JSON command to server: ");
|
||||
|
||||
// make the PUT request to the hub:
|
||||
httpClient.put(request, contentType, hueCmd);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = httpClient.responseStatusCode();
|
||||
String response = httpClient.responseBody();
|
||||
|
||||
Serial.println(hueCmd);
|
||||
Serial.print("Status code from server: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Server response: ");
|
||||
Serial.println(response);
|
||||
Serial.println();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
POST with headers client for ArduinoHttpClient library
|
||||
Connects to server once every five seconds, sends a POST request
|
||||
with custom headers and a request body
|
||||
|
||||
created 14 Feb 2016
|
||||
by Tom Igoe
|
||||
modified 18 Mar 2017
|
||||
by Sandeep Mistry
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
|
||||
char serverAddress[] = "192.168.0.3"; // server address
|
||||
int port = 8080;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("making POST request");
|
||||
String postData = "name=Alice&age=12";
|
||||
|
||||
client.beginRequest();
|
||||
client.post("/");
|
||||
client.sendHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
client.sendHeader("Content-Length", postData.length());
|
||||
client.sendHeader("X-Custom-Header", "custom-header-value");
|
||||
client.beginBody();
|
||||
client.print(postData);
|
||||
client.endRequest();
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
Simple DELETE client for ArduinoHttpClient library
|
||||
Connects to server once every five seconds, sends a DELETE request
|
||||
and a request body
|
||||
|
||||
created 14 Feb 2016
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
|
||||
char serverAddress[] = "192.168.0.3"; // server address
|
||||
int port = 8080;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("making DELETE request");
|
||||
String contentType = "application/x-www-form-urlencoded";
|
||||
String delData = "name=light&age=46";
|
||||
|
||||
client.del("/", contentType, delData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
Simple GET client for ArduinoHttpClient library
|
||||
Connects to server once every five seconds, sends a GET request
|
||||
|
||||
created 14 Feb 2016
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
char serverAddress[] = "192.168.0.3"; // server address
|
||||
int port = 8080;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("making GET request");
|
||||
client.get("/");
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
// (c) Copyright 2010-2012 MCQN Ltd.
|
||||
// Released under Apache License, version 2.0
|
||||
//
|
||||
// Simple example to show how to use the HttpClient library
|
||||
// Gets the web page given at http://<kHostname><kPath> and
|
||||
// outputs the content to the serial port
|
||||
|
||||
#include <SPI.h>
|
||||
#include <WiFi101.h>
|
||||
#include <ArduinoHttpClient.h>
|
||||
|
||||
// This example downloads the URL "http://arduino.cc/"
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
|
||||
|
||||
// Name of the server we want to connect to
|
||||
const char kHostname[] = "arduino.cc";
|
||||
// Path to download (this is the bit after the hostname in the URL
|
||||
// that you want to download
|
||||
const char kPath[] = "/";
|
||||
|
||||
// Number of milliseconds to wait without receiving any data before we give up
|
||||
const int kNetworkTimeout = 30*1000;
|
||||
// Number of milliseconds to wait if no data is available before trying again
|
||||
const int kNetworkDelay = 1000;
|
||||
|
||||
WiFiClient c;
|
||||
HttpClient http(c, kHostname);
|
||||
|
||||
void setup()
|
||||
{
|
||||
//Initialize serial and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
// attempt to connect to WiFi network:
|
||||
Serial.print("Attempting to connect to WPA SSID: ");
|
||||
Serial.println(ssid);
|
||||
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
|
||||
// unsuccessful, retry in 4 seconds
|
||||
Serial.print("failed ... ");
|
||||
delay(4000);
|
||||
Serial.print("retrying ... ");
|
||||
}
|
||||
|
||||
Serial.println("connected");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int err =0;
|
||||
|
||||
err = http.get(kPath);
|
||||
if (err == 0)
|
||||
{
|
||||
Serial.println("startedRequest ok");
|
||||
|
||||
err = http.responseStatusCode();
|
||||
if (err >= 0)
|
||||
{
|
||||
Serial.print("Got status code: ");
|
||||
Serial.println(err);
|
||||
|
||||
// Usually you'd check that the response code is 200 or a
|
||||
// similar "success" code (200-299) before carrying on,
|
||||
// but we'll print out whatever response we get
|
||||
|
||||
// If you are interesting in the response headers, you
|
||||
// can read them here:
|
||||
//while(http.headerAvailable())
|
||||
//{
|
||||
// String headerName = http.readHeaderName();
|
||||
// String headerValue = http.readHeaderValue();
|
||||
//}
|
||||
|
||||
int bodyLen = http.contentLength();
|
||||
Serial.print("Content length is: ");
|
||||
Serial.println(bodyLen);
|
||||
Serial.println();
|
||||
Serial.println("Body returned follows:");
|
||||
|
||||
// Now we've got to the body, so we can print it out
|
||||
unsigned long timeoutStart = millis();
|
||||
char c;
|
||||
// Whilst we haven't timed out & haven't reached the end of the body
|
||||
while ( (http.connected() || http.available()) &&
|
||||
(!http.endOfBodyReached()) &&
|
||||
((millis() - timeoutStart) < kNetworkTimeout) )
|
||||
{
|
||||
if (http.available())
|
||||
{
|
||||
c = http.read();
|
||||
// Print out this character
|
||||
Serial.print(c);
|
||||
|
||||
// We read something, reset the timeout counter
|
||||
timeoutStart = millis();
|
||||
}
|
||||
else
|
||||
{
|
||||
// We haven't got any data, so let's pause to allow some to
|
||||
// arrive
|
||||
delay(kNetworkDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Getting response failed: ");
|
||||
Serial.println(err);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Connect failed: ");
|
||||
Serial.println(err);
|
||||
}
|
||||
http.stop();
|
||||
|
||||
// And just stop, now that we've tried a download
|
||||
while(1);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Simple POST client for ArduinoHttpClient library
|
||||
Connects to server once every five seconds, sends a POST request
|
||||
and a request body
|
||||
|
||||
created 14 Feb 2016
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
char serverAddress[] = "192.168.0.3"; // server address
|
||||
int port = 8080;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("making POST request");
|
||||
String contentType = "application/x-www-form-urlencoded";
|
||||
String postData = "name=Alice&age=12";
|
||||
|
||||
client.post("/", contentType, postData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Simple PUT client for ArduinoHttpClient library
|
||||
Connects to server once every five seconds, sends a PUT request
|
||||
and a request body
|
||||
|
||||
created 14 Feb 2016
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
char serverAddress[] = "192.168.0.3"; // server address
|
||||
int port = 8080;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("making PUT request");
|
||||
String contentType = "application/x-www-form-urlencoded";
|
||||
String putData = "name=light&age=46";
|
||||
|
||||
client.put("/", contentType, putData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
Simple WebSocket client for ArduinoHttpClient library
|
||||
Connects to the WebSocket server, and sends a hello
|
||||
message every 5 seconds
|
||||
|
||||
created 28 Jun 2016
|
||||
by Sandeep Mistry
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
char serverAddress[] = "echo.websocket.org"; // server address
|
||||
int port = 80;
|
||||
|
||||
WiFiClient wifi;
|
||||
WebSocketClient client = WebSocketClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
int count = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to Network named: ");
|
||||
Serial.println(ssid); // print the network name (SSID);
|
||||
|
||||
// Connect to WPA/WPA2 network:
|
||||
status = WiFi.begin(ssid, pass);
|
||||
}
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("starting WebSocket client");
|
||||
client.begin();
|
||||
|
||||
while (client.connected()) {
|
||||
Serial.print("Sending hello ");
|
||||
Serial.println(count);
|
||||
|
||||
// send a hello #
|
||||
client.beginMessage(TYPE_TEXT);
|
||||
client.print("hello ");
|
||||
client.print(count);
|
||||
client.endMessage();
|
||||
|
||||
// increment count for next message
|
||||
count++;
|
||||
|
||||
// check if a message is available to be received
|
||||
int messageSize = client.parseMessage();
|
||||
|
||||
if (messageSize > 0) {
|
||||
Serial.println("Received a message:");
|
||||
Serial.println(client.readString());
|
||||
}
|
||||
|
||||
// wait 5 seconds
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
Serial.println("disconnected");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "node_test_server",
|
||||
"version": "0.0.1",
|
||||
"author": {
|
||||
"name": "Tom Igoe"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": ">=1.11.0",
|
||||
"express": ">=4.0.0",
|
||||
"multer": "*",
|
||||
"ws": "^1.1.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For ArduinoHttpClient
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
ArduinoHttpClient KEYWORD1
|
||||
HttpClient KEYWORD1
|
||||
WebSocketClient KEYWORD1
|
||||
URLEncoder KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
get KEYWORD2
|
||||
post KEYWORD2
|
||||
put KEYWORD2
|
||||
patch KEYWORD2
|
||||
startRequest KEYWORD2
|
||||
beginRequest KEYWORD2
|
||||
beginBody KEYWORD2
|
||||
sendHeader KEYWORD2
|
||||
sendBasicAuth KEYWORD2
|
||||
endRequest KEYWORD2
|
||||
responseStatusCode KEYWORD2
|
||||
readHeader KEYWORD2
|
||||
skipResponseHeaders KEYWORD2
|
||||
endOfHeadersReached KEYWORD2
|
||||
endOfBodyReached KEYWORD2
|
||||
completed KEYWORD2
|
||||
contentLength KEYWORD2
|
||||
isResponseChunked KEYWORD2
|
||||
connectionKeepAlive KEYWORD2
|
||||
noDefaultRequestHeaders KEYWORD2
|
||||
headerAvailable KEYWORD2
|
||||
readHeaderName KEYWORD2
|
||||
readHeaderValue KEYWORD2
|
||||
responseBody KEYWORD2
|
||||
|
||||
beginMessage KEYWORD2
|
||||
endMessage KEYWORD2
|
||||
parseMessage KEYWORD2
|
||||
messageType KEYWORD2
|
||||
isFinal KEYWORD2
|
||||
readString KEYWORD2
|
||||
ping KEYWORD2
|
||||
|
||||
encode KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
HTTP_SUCCESS LITERAL1
|
||||
HTTP_ERROR_CONNECTION_FAILED LITERAL1
|
||||
HTTP_ERROR_API LITERAL1
|
||||
HTTP_ERROR_TIMED_OUT LITERAL1
|
||||
HTTP_ERROR_INVALID_RESPONSE LITERAL1
|
||||
|
||||
TYPE_CONTINUATION LITERAL1
|
||||
TYPE_TEXT LITERAL1
|
||||
TYPE_BINARY LITERAL1
|
||||
TYPE_CONNECTION_CLOSE LITERAL1
|
||||
TYPE_PING LITERAL1
|
||||
TYPE_PONG LITERAL1
|
||||
@@ -0,0 +1,10 @@
|
||||
name=ArduinoHttpClient
|
||||
version=0.5.0
|
||||
author=Arduino
|
||||
maintainer=Arduino <info@arduino.cc>
|
||||
sentence=[EXPERIMENTAL] Easily interact with web servers from Arduino, using HTTP and WebSockets.
|
||||
paragraph=This library can be used for HTTP (GET, POST, PUT, DELETE) requests to a web server. It also supports exchanging messages with WebSocket servers. Based on Adrian McEwen's HttpClient library.
|
||||
category=Communication
|
||||
url=https://github.com/arduino-libraries/ArduinoHttpClient
|
||||
architectures=*
|
||||
includes=ArduinoHttpClient.h
|
||||
@@ -0,0 +1 @@
|
||||
{"type": "library", "name": "CayenneMQTT", "version": "1.3.0", "spec": {"owner": "mydevicesiot", "id": 1449, "name": "CayenneMQTT", "requirements": null, "uri": null}}
|
||||
@@ -0,0 +1,60 @@
|
||||
# Cayenne MQTT Arduino Library
|
||||
The Cayenne MQTT Arduino Library provides functions to easily connect to the [Cayenne IoT project builder](https://www.cayenne-mydevices.com). This library is designed to work with Arduino, ESP8266 and ESP32 devices. With it you can send data to and receive data from Cayenne.
|
||||
|
||||

|
||||
|
||||
[Arduino Uno image](https://commons.wikimedia.org/wiki/File%3AArduino_Uno_-_R3.jpg) by [SparkFun](https://www.sparkfun.com) is licensed under [CC BY 2.0](https://creativecommons.org/licenses/by/2.0/)
|
||||
|
||||

|
||||
|
||||
[Adafruit HUZZAH ESP8266 image](https://learn.adafruit.com/assets/24792) by [lady ada](https://learn.adafruit.com/users/adafruit2) is licensed under [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/)
|
||||
|
||||

|
||||
|
||||
[SparkFun ESP32 Thing image](https://www.sparkfun.com/products/13907) by [SparkFun](https://www.sparkfun.com) is licensed under [CC BY 2.0](https://creativecommons.org/licenses/by/2.0/)
|
||||
|
||||
## Requirements
|
||||
### Hardware
|
||||
* An Arduino, ESP8266 or ESP32 module, e.g. the [Arduino Uno](https://store.arduino.cc/usa/arduino-uno-rev3), [Adafruit HUZZAH ESP8266](https://www.adafruit.com/product/2471) or [SparkFun ESP32 Thing](https://www.sparkfun.com/products/13907).
|
||||
* A USB cable, or if using certain ESP devices, a USB to Serial FTDI cable.
|
||||
|
||||
### Software
|
||||
* [Arduino IDE](https://www.arduino.cc/en/Main/Software) for Windows, Linux or Mac OS.
|
||||
* [This library](https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/archive/master.zip).
|
||||
|
||||
## Getting Started
|
||||
### Environment Setup
|
||||
1. Download and install the [Arduino IDE](https://www.arduino.cc/en/Main/Software).
|
||||
2. Install the **CayenneMQTT** library from **Sketch -> Include Library -> Manage Libraries**.
|
||||
3. If using an ESP module, install the Board Package.
|
||||
1. For **ESP8266**:
|
||||
1. Under **File -> Preferences** add `http://arduino.esp8266.com/stable/package_esp8266com_index.json` to the **Additional Boards Manager URLs** field.
|
||||
2. Install the **esp8266** platform from **Tools -> Board -> Boards Manager**.
|
||||
2. For **ESP32**, manually install the board package by following the instructions here: https://github.com/espressif/arduino-esp32/blob/master/README.md#installation-instructions.
|
||||
4. Connect your device to your computer using the appropriate USB cable or USB to Serial FTDI cable.
|
||||
5. Select your Arduino board or ESP module and and the correct port from the **Tools** menu.
|
||||
|
||||
### Cayenne Setup
|
||||
1. Create your Cayenne account at https://www.cayenne-mydevices.com.
|
||||
2. Add a new device using the **Add new...** page **MicroControllers** or **Bring Your Own Thing** options.
|
||||
|
||||
### Building Examples
|
||||
1. Open an included example connection sketch from **File -> Examples -> CayenneMQTT -> Connections**.
|
||||
2. Modify the included sketch with the Cayenne authentication info you received when adding your device.
|
||||
3. If using an ESP module or WiFi shield modify the sketch with your network info.
|
||||
3. Compile and upload the sketch to your device. For ESP devices uploading may require setting the device into bootload mode. For the Adafruit HUZZAH ESP8266 module this is done with the folowing steps:
|
||||
1. Hold down the **GPIO0** button.
|
||||
2. Click the **Reset** button.
|
||||
3. Let go of the **GPIO0** button.
|
||||
4. The red LED should light up, indicating the device is ready for uploading.
|
||||
5. Upload your sketch.
|
||||
4. Check the Cayenne dashboard to ensure it is receiving sample data from your device.
|
||||
|
||||
## Compatibility
|
||||
This library was tested with various Arduino boards including the Uno, Mega, Due, etc. as well as the Adafruit HUZZAH ESP8266 breakout module and an Espressif ESP32 development board. It should also work with other standard ESP8266 and ESP32 modules that are compatible with the Arduino IDE. Boards with very constrained memory like the Arduino Leonardo or Micro may have issues fitting this library alongside other libraries. For those boards you might try using a manually specified IP as shown in the [ManualConnection example sketch](https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/ManualConnection/ManualConnection.ino) in order to save space.
|
||||
|
||||
## Contributions
|
||||
* This library includes the [Eclipse Paho MQTT C/C++ client](https://github.com/eclipse/paho.mqtt.embedded-c).
|
||||
|
||||
## Cayenne MQTT Libraries
|
||||
Additional libraries are available for connecting to Cayenne on other platforms and devices. These can be found at https://github.com/myDevicesIoT.
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Cayenne Generic Digital Output Example
|
||||
|
||||
This sketch shows how to set up a Generic Digital Output with Cayenne.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. In the Cayenne Dashboard add a new Generic Digital Output widget.
|
||||
2. Select a virtual channel number for the widget.
|
||||
3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
|
||||
4. Attach an output actuator (e.g. an LED) to a digital pin on your Arduino.
|
||||
5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your output actuator.
|
||||
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
7. Compile and upload this sketch.
|
||||
8. Once the Arduino connects to the Dashboard you can use the widget button to send digital values.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(ACTUATOR_PIN, OUTPUT);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called when data is sent from Cayenne.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
int value = getValue.asInt();
|
||||
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
|
||||
// Write the value received to the digital pin.
|
||||
digitalWrite(ACTUATOR_PIN, value);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
Cayenne Generic PWM Output Example
|
||||
|
||||
This sketch shows how to set up a Generic PWM Output with Cayenne.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. In the Cayenne Dashboard add a new Generic PWM Output widget.
|
||||
2. Select a virtual channel number.
|
||||
3. Select a slider widget.
|
||||
4. Set the slider widget min value to 0 and max value to 255.
|
||||
5. Set the VIRTUAL_CHANNEL value below to the virtual channel number you selected.
|
||||
6. Attach an output device to a digital PWM pin (3, 5, 6, 9, 10, and 11 on most Arduino boards).
|
||||
7. Set the ACTUATOR_PIN value below to the digital PWM pin number you selected.
|
||||
8. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
9. Compile and upload this sketch.
|
||||
10. Once the Arduino connects to the Dashboard you can use the slider to change PWM values.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 3
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called when data is sent from Cayenne.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
int value = getValue.asInt(); // 0 to 255
|
||||
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
|
||||
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
|
||||
analogWrite(ACTUATOR_PIN, value);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Cayenne Light Switch Example
|
||||
|
||||
This sketch shows how to set up a Light Switch with Cayenne.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. In the Cayenne Dashboard add a new Light Switch Widget.
|
||||
2. Select a virtual channel number for the widget.
|
||||
3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
|
||||
4. Attach the negative leg of an LED to ground and the other leg to the selected digital pin.
|
||||
Schematic:
|
||||
[Ground] -- [LED] -- [Resistor] -- [Digital Pin]
|
||||
5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your LED.
|
||||
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
7. Compile and upload this sketch.
|
||||
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the LED on and off.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(ACTUATOR_PIN, OUTPUT);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called when data is sent from Cayenne.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
int value = getValue.asInt();
|
||||
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
|
||||
// Write the value received to the digital pin.
|
||||
digitalWrite(ACTUATOR_PIN, value);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Cayenne Luminosity Example
|
||||
|
||||
This sketch sample file shows how to change the brightness on an LED using Cayenne Dashboard.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. In the Cayenne Dashboard add a new Luminosity widget.
|
||||
2. Select a virtual channel number.
|
||||
3. Select a slider widget.
|
||||
4. Set the slider widget min value to 0 and max value to 255.
|
||||
5. Set the VIRTUAL_CHANNEL value below to the virtual channel number you selected.
|
||||
6. Connect the LED's legs to GND and to a PWM pin (3, 5, 6, 9, 10, and 11 on most Arduino boards).
|
||||
Use a 1k resistor if possible.
|
||||
Schematic:
|
||||
[Ground] -- [LED] -- [1k-resistor] -- [PWM Pin]
|
||||
7. Set the ACTUATOR_PIN value below to the digital PWM pin number you selected.
|
||||
8. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
9. Compile and upload this sketch.
|
||||
10. Once the Arduino connects to the Dashboard you can use the slider to change LED brightness.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 3
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called when data is sent from Cayenne.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
int value = getValue.asInt(); // 0 to 255
|
||||
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
|
||||
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
|
||||
analogWrite(ACTUATOR_PIN, value);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Cayenne Motor Switch Actuator Example
|
||||
|
||||
This sketch shows how to set up a Motor Switch with Cayenne. The Arduino cannot
|
||||
drive a motor because it does not output the needed current. As a result, in order
|
||||
to make this example work, various electronic components are necessary to drive
|
||||
the DC motor. To keep it simple, you will need an external power source, transistor (eg. PN2222),
|
||||
diode (eg. 1N4001), and a 270 ohm resistor.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. In the Cayenne Dashboard add a new Motor Switch widget.
|
||||
2. Select a virtual channel number for the widget.
|
||||
3. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
|
||||
4. Set up your motor schematic and attach it to a PWM pin (3, 5, 6, 9, 10, and 11 on most Arduino boards).
|
||||
5. Set the ACTUATOR_PIN value below to the PWM pin number you selected.
|
||||
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
7. Compile and upload this sketch.
|
||||
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the motor on and off.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 3
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Enable or disable the motor based on value received on virtual channel.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
int speed = 155;
|
||||
// Read the integer value which should be 0 or 1.
|
||||
int enabled = getValue.asInt();
|
||||
if (enabled == 1) {
|
||||
// Turn on the motor at the specified speed.
|
||||
analogWrite(ACTUATOR_PIN, speed);
|
||||
}
|
||||
else {
|
||||
// Turn off the motor.
|
||||
analogWrite(ACTUATOR_PIN, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
Cayenne Relay Switch Example
|
||||
|
||||
This sketch sample file shows how to set up a Relay Switch with Cayenne.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. In the Cayenne Dashboard add a new Relay Switch widget.
|
||||
2. Select a virtual channel number for the widget.
|
||||
3. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
|
||||
4. Connect your relay switch to a digital pin.
|
||||
5. Set the ACTUATOR_PIN value below to the digital pin number you selected.
|
||||
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
7. Compile and upload this sketch.
|
||||
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the relay switch on and off.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(ACTUATOR_PIN, OUTPUT);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called when data is sent from Cayenne.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
|
||||
if (getValue.asInt() == 0) {
|
||||
digitalWrite(ACTUATOR_PIN, HIGH);
|
||||
}
|
||||
else {
|
||||
digitalWrite(ACTUATOR_PIN, LOW);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Cayenne Servo Motor Example
|
||||
|
||||
This sketch sample file shows how to connect a Servo Motor with CayenneMQTT Library.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Install the Servo library from the Arduino Library Manager.
|
||||
2. In the Cayenne Dashboard add a new Servo Motor widget.
|
||||
3. Select a virtual channel number for the widget.
|
||||
4. Set min value to 0 and max value to 1.
|
||||
5. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
|
||||
6. Connect the Servo's legs to GND, VCC (5.0), and a digital pin.
|
||||
7. Set the ACTUATOR_PIN value below to the digital pin number you selected.
|
||||
8. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
9. Compile and upload this sketch.
|
||||
10. Once the Arduino connects to the Dashboard you can use the slider to change the servo position.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
#include <Servo.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 4
|
||||
|
||||
Servo s1;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
s1.attach(ACTUATOR_PIN);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called when data is sent from Cayenne.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
// Determine angle to set the servo.
|
||||
int position = getValue.asDouble() * 180;
|
||||
// Move the servo to the specified position.
|
||||
s1.write(position);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Cayenne Valve Switch Example
|
||||
|
||||
This sketch shows how to set up a Valve Switch with Cayenne. The Arduino cannot
|
||||
drive a valve because it does not output the needed current. As a result, in order
|
||||
to make this example work, various electronic components are necessary to connect
|
||||
the valve. To keep it simple, you will need an external power source, transistor (eg. TIP120),
|
||||
diode (eg. 1N4001), and a 1k ohm resistor. Alternatively you could also use a relay switch to
|
||||
connect the valve.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. In the Cayenne Dashboard add a new Valve Switch widget.
|
||||
2. Select a virtual channel number for the widget.
|
||||
3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
|
||||
4. Set up your valve schematic and attach it to a digital pin.
|
||||
5. Set the ACTUATOR_PIN value below to the digital pin number you selected.
|
||||
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
7. Compile and upload this sketch.
|
||||
8. Once the Arduino connects to the Dashboard you can use the widget button to open and close the valve.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
#define VIRTUAL_CHANNEL 1
|
||||
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(ACTUATOR_PIN, OUTPUT);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called when data is sent from Cayenne.
|
||||
CAYENNE_IN(VIRTUAL_CHANNEL)
|
||||
{
|
||||
int value = getValue.asInt();
|
||||
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
|
||||
// Write the value received to the digital pin.
|
||||
digitalWrite(ACTUATOR_PIN, value);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
This example sketch shows how a value can be sent from the Cayenne Dashboard to the Arduino on a Virtual Channel.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Create a Generic Digital Output Actuator attached to Virtual Channel 4 in the Cayenne Dashboard.
|
||||
2. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
3. Compile and upload the sketch.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h> // Change this to use a different communication device. See Communications examples.
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function will be called every time a Dashboard widget writes a value to Virtual Channel 4.
|
||||
CAYENNE_IN(4)
|
||||
{
|
||||
CAYENNE_LOG("Got a value: %s", getValue.asStr());
|
||||
// You can also use:
|
||||
// int i = getValue.asInt() or
|
||||
// double d = getValue.asDouble()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
This example sketch shows how a value can be sent from the Arduino to the Cayenne Dashboard at automatic intervals.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
2. Compile and upload the sketch.
|
||||
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h> // Change this to use a different communication device. See Communications examples.
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
// Use Virtual Channel 5 for uptime display.
|
||||
#define VIRTUAL_CHANNEL 5
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// This function is called at intervals to send data to Cayenne.
|
||||
CAYENNE_OUT(VIRTUAL_CHANNEL)
|
||||
{
|
||||
CAYENNE_LOG("Send data for Virtual Channel %d", VIRTUAL_CHANNEL);
|
||||
// This command writes the device's uptime in seconds to the Virtual Channel.
|
||||
Cayenne.virtualWrite(VIRTUAL_CHANNEL, millis() / 1000);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
This example sketch shows how a value can be sent from the Arduino to the Cayenne Dashboard at specified intervals.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
2. Compile and upload the sketch.
|
||||
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
|
||||
NOTE:
|
||||
For this example you'll need SimpleTimer library:
|
||||
https://github.com/jfturcot/SimpleTimer
|
||||
Visit this page for more information:
|
||||
http://playground.arduino.cc/Code/SimpleTimer
|
||||
*/
|
||||
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h> // Change this to use a different communication device. See Communications examples.
|
||||
#include <SimpleTimer.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
SimpleTimer timer;
|
||||
|
||||
// This function sends the device's uptime to Cayenne on Virtual Channel 5.
|
||||
void sendUptime()
|
||||
{
|
||||
// Send values using the virtualWrite function. Cayenne currently accepts int and float values.
|
||||
// Please don't send more that 10 values per second.
|
||||
Cayenne.virtualWrite(5, millis() / 1000);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600); // See the connection status in Serial Monitor
|
||||
Cayenne.begin(username, password, clientID);
|
||||
|
||||
// Set up a function to be called every 5 seconds
|
||||
timer.setInterval(5000L, sendUptime);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop(); // Runs main loop
|
||||
timer.run(); // Initiates SimpleTimer
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// This example shows how to connect to Cayenne using an Arduino Yun and send/receive sample data.
|
||||
|
||||
//#define CAYENNE_DEBUG
|
||||
#define CAYENNE_PRINT Serial
|
||||
#include <CayenneMQTTYun.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
unsigned long lastMillis = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
|
||||
//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
|
||||
if (millis() - lastMillis > 10000) {
|
||||
lastMillis = millis();
|
||||
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
|
||||
Cayenne.virtualWrite(0, lastMillis);
|
||||
//Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
}
|
||||
|
||||
//Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using an ESP32 and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. If you have not already installed the ESP32 Board Package install it using the instructions here: https://github.com/espressif/arduino-esp32/blob/master/README.md#installation-instructions.
|
||||
2. Select your ESP32 board from the Tools menu.
|
||||
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
4. Set the network name and password.
|
||||
5. Compile and upload the sketch.
|
||||
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG
|
||||
#define CAYENNE_PRINT Serial
|
||||
#include <CayenneMQTTESP32.h>
|
||||
|
||||
// WiFi network info.
|
||||
char ssid[] = "ssid";
|
||||
char wifiPassword[] = "wifiPassword";
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
|
||||
2. Select your ESP8266 board from the Tools menu.
|
||||
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
4. Set the network name and password.
|
||||
5. Compile and upload the sketch.
|
||||
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG
|
||||
#define CAYENNE_PRINT Serial
|
||||
#include <CayenneMQTTESP8266.h>
|
||||
|
||||
// WiFi network info.
|
||||
char ssid[] = "ssid";
|
||||
char wifiPassword[] = "wifiPassword";
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
This sketch connects to the Cayenne server using an ESP8266 WiFi module as a shield connected via a hardware serial to an Arduino.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Install the ESP8266SerialLibrary.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library) from the Cayenne extras/libraries
|
||||
folder (e.g. My Documents\Arduino\libraries\CayenneMQTT\extras\libraries) to compile this example.
|
||||
2. Connect the ESP8266 as a shield to your Arduino. This example uses the Serial1 hardware serial pins available on the Mega. You can also try
|
||||
using a software serial, though it may be less stable.
|
||||
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
4. Set the network name and password.
|
||||
5. Compile and upload the sketch.
|
||||
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
|
||||
NOTE: This code requires ESP8266 firmware version 1.0.0 (AT v0.22) or later.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG // Uncomment to show debug messages
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTESP8266Shield.h>
|
||||
|
||||
// WiFi network info.
|
||||
char ssid[] = "ssid";
|
||||
char wifiPassword[] = "wifiPassword";
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
|
||||
#define EspSerial Serial1
|
||||
|
||||
ESP8266 wifi(&EspSerial);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
delay(10);
|
||||
|
||||
// Set ESP8266 baud rate
|
||||
EspSerial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
2. Compile and upload the sketch.
|
||||
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG // Uncomment to show debug messages
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using an Ethernet W5200 shield and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Download the Ethernet_Shield_W5200 library (https://github.com/Seeed-Studio/Ethernet_Shield_W5200) as a zip file.
|
||||
2. From the Arduino IDE Include Library menu select Add .ZIP Library and add the downloaded Ethernet_Shield_W5200 zip library.
|
||||
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
4. Compile and upload the sketch.
|
||||
5. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG // Uncomment to show debug messages
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernetW5200.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using an Ethernet W5500 shield and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Download the Ethernet2 library (https://github.com/adafruit/Ethernet2) as a zip file.
|
||||
2. From the Arduino IDE Include Library menu select Add .ZIP Library and add the downloaded Ethernet2 zip library.
|
||||
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
4. Compile and upload the sketch.
|
||||
5. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG // Uncomment to show debug messages
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernetW5500.h>
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using a GSM device and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Install the TinyGSM library (https://github.com/vshymanskyy/TinyGSM) from the Arduino Library Manager.
|
||||
2. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
3. Uncomment the correct GSM modem type and set the GSM connection info, if needed.
|
||||
4. Compile and upload the sketch.
|
||||
5. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG // Uncomment to show debug messages
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
|
||||
// Uncomment your modem type:
|
||||
#define TINY_GSM_MODEM_SIM800
|
||||
// #define TINY_GSM_MODEM_SIM808
|
||||
// #define TINY_GSM_MODEM_SIM900
|
||||
// #define TINY_GSM_MODEM_UBLOX
|
||||
// #define TINY_GSM_MODEM_BG96
|
||||
// #define TINY_GSM_MODEM_A6
|
||||
// #define TINY_GSM_MODEM_A7
|
||||
// #define TINY_GSM_MODEM_M590
|
||||
// #define TINY_GSM_MODEM_ESP8266
|
||||
// #define TINY_GSM_MODEM_XBEE
|
||||
|
||||
#include <CayenneMQTTGSM.h>
|
||||
|
||||
// This sketch uses a software serial connection.
|
||||
#include <SoftwareSerial.h>
|
||||
SoftwareSerial gsmSerial(2, 3); // RX, TX
|
||||
// If you are using a device that supports a hardware serial (Mega, Leonardo, etc.) and prefer to use
|
||||
// that you can comment out the above lines and uncomment the one below.
|
||||
//#define gsmSerial Serial1
|
||||
|
||||
// GSM connection info.
|
||||
char apn[] = ""; // Access point name. Leave empty if it is not needed.
|
||||
char gprsLogin[] = ""; // GPRS username. Leave empty if it is not needed.
|
||||
char gprsPassword[] = ""; // GPRS password. Leave empty if it is not needed.
|
||||
char pin[] = ""; // SIM pin number. Leave empty if it is not needed.
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// Auto-detect the GSM serial baud rate. You can manually set it instead if you want to save a bit of space.
|
||||
TinyGsmAutoBaud(gsmSerial);
|
||||
Cayenne.begin(username, password, clientID, gsmSerial, apn, gprsLogin, gprsPassword, pin);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using an Arduino/Genuino MKR1000 and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Install the Arduino SAMD Boards from the Arduino IDE Boards Manager if you have not already done so.
|
||||
2. Select the MKR1000 board from the Arduino IDE Tools menu.
|
||||
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
4. Set the network name and password.
|
||||
5. Compile and upload the sketch.
|
||||
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG // Uncomment to show debug messages
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTMKR1000.h>
|
||||
|
||||
// WiFi network info.
|
||||
char ssid[] = "ssid";
|
||||
char wifiPassword[] = "wifiPassword";
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
This example shows how to connect to Cayenne using a manually specified Ethernet connection and send/receive sample data.
|
||||
|
||||
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
||||
|
||||
Steps:
|
||||
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
||||
2. Compile and upload the sketch.
|
||||
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
||||
*/
|
||||
|
||||
//#define CAYENNE_DEBUG // Uncomment to show debug messages
|
||||
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
|
||||
#include <CayenneMQTTEthernet.h>
|
||||
//#include <CayenneMQTTEthernetW5500.h> // Uncomment this and comment out CayenneMQTTEthernet.h to use an Ethernet 2 shield or other Ethernet W5500 shield.
|
||||
// You will need the Ethernet2 library installed. See the ArduinoEthernetW5500 example sketch for more info.
|
||||
//#include <CayenneMQTTEthernetW5200.h> // Uncomment this and comment out CayenneMQTTEthernet.h to use an Ethernet W5200 shield.
|
||||
// You will need the EthernetW5200 library installed. See the ArduinoEthernetW5200 example sketch for more info.
|
||||
|
||||
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
||||
char username[] = "MQTT_USERNAME";
|
||||
char password[] = "MQTT_PASSWORD";
|
||||
char clientID[] = "CLIENT_ID";
|
||||
|
||||
// Mac address should be different for each device in your LAN
|
||||
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||
IPAddress arduino_ip(10, 0, 0, 20);
|
||||
IPAddress dns_ip(8, 8, 8, 8);
|
||||
IPAddress gateway_ip(10, 0, 0, 1);
|
||||
IPAddress subnet_mask(255, 255, 255, 0);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Cayenne.begin(username, password, clientID, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Cayenne.loop();
|
||||
}
|
||||
|
||||
// Default function for sending sensor data at intervals to Cayenne.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
|
||||
CAYENNE_OUT_DEFAULT()
|
||||
{
|
||||
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
|
||||
Cayenne.virtualWrite(0, millis());
|
||||
// Some examples of other functions you can use to send data.
|
||||
//Cayenne.celsiusWrite(1, 22.0);
|
||||
//Cayenne.luxWrite(2, 700);
|
||||
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
|
||||
}
|
||||
|
||||
// Default function for processing actuator commands from the Cayenne Dashboard.
|
||||
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
|
||||
CAYENNE_IN_DEFAULT()
|
||||
{
|
||||
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
|
||||
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user