main
This commit is contained in:
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
.pio/build
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
/test/
|
||||
/.vscode/
|
1
.pio/libdeps/esp32-s3-devkitc-1/AHT10/.piopm
Normal file
1
.pio/libdeps/esp32-s3-devkitc-1/AHT10/.piopm
Normal file
@ -0,0 +1 @@
|
||||
{"type": "library", "name": "AHT10", "version": "1.1.0", "spec": {"owner": "enjoyneering", "id": 7484, "name": "AHT10", "requirements": null, "uri": null}}
|
44
.pio/libdeps/esp32-s3-devkitc-1/AHT10/README.md
Normal file
44
.pio/libdeps/esp32-s3-devkitc-1/AHT10/README.md
Normal file
@ -0,0 +1,44 @@
|
||||
[![license-badge][]][license] ![version] [![stars][]][stargazers] [![hit-count][]][count] [![github-issues][]][issues]
|
||||
|
||||
# Aosong ASAIR AHT10, AHT15 & AHT20
|
||||
|
||||
This is an Arduino library for Aosong ASAIR AHT10, AHT15 & AHT20 Digital Humidity & Temperature Sensor
|
||||
|
||||
- Supply voltage: 1.8v - 3.6v for AHT10, AHT15 & 2.0v - 5.5v for AHT20
|
||||
- Temperature range: -40°C..+85°C
|
||||
- Temperature resolution: 0.01°C
|
||||
- Temperature accuracy: ±0.3°C
|
||||
- Relative humidity range: 0%..100%
|
||||
- Relative humidity resolution: 0.024%
|
||||
- Relative humidity accuracy: ±2%**
|
||||
- I²C bus speed: 0Hz - 400KHz
|
||||
- Recomended polling frequency: 8sec - 30sec***
|
||||
|
||||
|
||||
Supports all sensors features:
|
||||
|
||||
- read humidity****
|
||||
- read temperature****
|
||||
- soft reset with sensor initialization
|
||||
|
||||
Tested on:
|
||||
- Arduino AVR
|
||||
- Arduino ESP8266
|
||||
- Arduino ESP32
|
||||
- Arduino STM32
|
||||
|
||||
**Prolonged exposure for 60 hours at humidity > 80% can lead to a temporary drift of the signal +3%. Sensor slowly returns to the calibrated state at normal operating conditions.
|
||||
|
||||
***If sampling rate of the measurement is too high, the sensor overheats. To prevent the temperature of the sensor from rising > 0.1°C, read sensor once every 2 seconds.
|
||||
|
||||
****The library returns 255 if a communication error occurs or if the calibration coefficient is off.
|
||||
|
||||
[license-badge]: https://img.shields.io/badge/License-GPLv3-blue.svg
|
||||
[license]: https://choosealicense.com/licenses/gpl-3.0/
|
||||
[version]: https://img.shields.io/badge/Version-1.1.0-green.svg
|
||||
[stars]: https://img.shields.io/github/stars/enjoyneering/AHT10.svg
|
||||
[hit-count]: http://hits.dwyl.io/enjoyneering/AHT10/badges.svg
|
||||
[count]: http://hits.dwyl.io/enjoyneering/AHT10/badges
|
||||
[stargazers]: https://github.com/enjoyneering/AHT10/stargazers
|
||||
[github-issues]: https://img.shields.io/github/issues/enjoyneering/AHT10.svg
|
||||
[issues]: https://github.com/enjoyneering/AHT10/issues/
|
@ -0,0 +1,128 @@
|
||||
/***************************************************************************************************/
|
||||
/*
|
||||
This is an Arduino library for Aosong ASAIR AHT10, AHT15 Digital Humidity & Temperature Sensor
|
||||
|
||||
written by : enjoyneering79
|
||||
sourse code: https://github.com/enjoyneering/
|
||||
|
||||
This chip uses I2C bus to communicate, specials pins are required to interface
|
||||
Board: SDA SCL Level
|
||||
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
||||
Mega2560................................. 20 21 5v
|
||||
Due, SAM3X8E............................. 20 21 3.3v
|
||||
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
||||
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
||||
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
||||
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
||||
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
||||
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
||||
|
||||
Frameworks & Libraries:
|
||||
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
|
||||
ESP32 Core - https://github.com/espressif/arduino-esp32
|
||||
ESP8266 Core - https://github.com/esp8266/Arduino
|
||||
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
|
||||
- https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
|
||||
|
||||
GNU GPL license, all text above must be included in any redistribution,
|
||||
see link for details - https://www.gnu.org/licenses/licenses.html
|
||||
*/
|
||||
/***************************************************************************************************/
|
||||
#include <Wire.h>
|
||||
#include <AHT10.h>
|
||||
#include <LiquidCrystal_I2C.h> //https://github.com/enjoyneering/LiquidCrystal_I2C
|
||||
|
||||
#define LCD_ROWS 4 //qnt. of lcd rows
|
||||
#define LCD_COLUMNS 20 //qnt. of lcd columns
|
||||
#define DEGREE_SYMBOL 0xDF //degree symbol from LCD ROM
|
||||
#define LED 1 //connect led to ATtiny85 pin physical pin no.6 in series with 470 Ohm resistor
|
||||
|
||||
float valueTH = 0;
|
||||
|
||||
const uint8_t temperature_icon[8] PROGMEM = {0x04, 0x0A, 0x0A, 0x0E, 0x0E, 0x1F, 0x1F, 0x0E}; //PROGMEM saves variable to flash & keeps dynamic memory free
|
||||
const uint8_t humidity_icon[8] PROGMEM = {0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x1F, 0x0E, 0x00};
|
||||
const uint8_t plus_minus_icon[8] PROGMEM = {0x00, 0x04, 0x0E, 0x04, 0x00, 0x0E, 0x00, 0x00};
|
||||
|
||||
|
||||
AHT10 myAHT10(AHT10_ADDRESS_0X38);
|
||||
LiquidCrystal_I2C lcd(PCF8574_ADDR_A21_A11_A01, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(LED, OUTPUT);
|
||||
|
||||
/* LCD connection check */
|
||||
while (lcd.begin(LCD_COLUMNS, LCD_ROWS) != true) //20 colums, 4 rows
|
||||
{
|
||||
digitalWrite(LED, HIGH);
|
||||
delay(500);
|
||||
digitalWrite(LED, LOW);
|
||||
delay(500);
|
||||
}
|
||||
|
||||
/* AHT10 connection check */
|
||||
while (myAHT10.begin() != true)
|
||||
{
|
||||
lcd.print(F("AHT10 error")); //(F()) saves string to flash & keeps dynamic memory free
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
lcd.clear();
|
||||
|
||||
//Wire.setClock(400000); //experimental I2C speed! 400KHz, default 100KHz
|
||||
|
||||
lcd.print(F("AHT10 OK"));
|
||||
delay(2000);
|
||||
|
||||
lcd.clear();
|
||||
|
||||
/* load custom symbol to CGRAM */
|
||||
lcd.createChar(0, temperature_icon);
|
||||
lcd.createChar(1, humidity_icon);
|
||||
lcd.createChar(2, plus_minus_icon);
|
||||
|
||||
/* prints static text */
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.write(0); //print custom tempereture symbol from CGRAM
|
||||
|
||||
lcd.setCursor(0, 1); //set 1-st colum & 2-nd row, first colum & row started at zero
|
||||
lcd.write(1); //print custom humidity symbol from CGRAM
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
/* prints dynamic temperature data */
|
||||
lcd.setCursor(1, 0);
|
||||
valueTH = myAHT10.readTemperature(); //read 6 bytes over I2C
|
||||
if (valueTH != AHT10_ERROR)
|
||||
{
|
||||
lcd.print(myAHT10.readTemperature());
|
||||
lcd.write(2); //print custom plus/minus symbol
|
||||
lcd.print(F("0.3"));
|
||||
lcd.write(DEGREE_SYMBOL); //print degree symbol from the LCD ROM
|
||||
lcd.print(F("C "));
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd.print(F("i2c error "));
|
||||
}
|
||||
|
||||
/* prints dynamic humidity data */
|
||||
lcd.setCursor(1, 1);
|
||||
valueTH = myAHT10.readHumidity(); //read 6 bytes over I2C
|
||||
if (valueTH != AHT10_ERROR)
|
||||
{
|
||||
lcd.print(valueTH);
|
||||
lcd.write(2);
|
||||
lcd.print(F("2% "));
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd.print(F("i2c error "));
|
||||
}
|
||||
|
||||
delay(10000); //recomended polling frequency 8sec..30sec
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
/***************************************************************************************************/
|
||||
/*
|
||||
This is an Arduino library for Aosong ASAIR AHT10, AHT15 Digital Humidity & Temperature Sensor
|
||||
|
||||
written by : enjoyneering79
|
||||
sourse code: https://github.com/enjoyneering/
|
||||
|
||||
This chip uses I2C bus to communicate, specials pins are required to interface
|
||||
Board: SDA SCL Level
|
||||
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
||||
Mega2560................................. 20 21 5v
|
||||
Due, SAM3X8E............................. 20 21 3.3v
|
||||
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
||||
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
||||
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
||||
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
||||
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
||||
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
||||
|
||||
Frameworks & Libraries:
|
||||
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
|
||||
ESP32 Core - https://github.com/espressif/arduino-esp32
|
||||
ESP8266 Core - https://github.com/esp8266/Arduino
|
||||
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
|
||||
- https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
|
||||
|
||||
GNU GPL license, all text above must be included in any redistribution,
|
||||
see link for details - https://www.gnu.org/licenses/licenses.html
|
||||
*/
|
||||
/***************************************************************************************************/
|
||||
#include <Wire.h>
|
||||
#include <AHT10.h>
|
||||
#include <LiquidCrystal_I2C.h> //https://github.com/enjoyneering/LiquidCrystal_I2C
|
||||
|
||||
#define LCD_ROWS 4 //qnt. of lcd rows
|
||||
#define LCD_COLUMNS 20 //qnt. of lcd columns
|
||||
#define DEGREE_SYMBOL 0xDF //degree symbol from LCD ROM
|
||||
|
||||
uint8_t readStatus = 0;
|
||||
|
||||
const uint8_t temperature_icon[8] PROGMEM = {0x04, 0x0A, 0x0A, 0x0E, 0x0E, 0x1F, 0x1F, 0x0E}; //PROGMEM saves variable to flash & keeps dynamic memory free
|
||||
const uint8_t humidity_icon[8] PROGMEM = {0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x1F, 0x0E, 0x00};
|
||||
const uint8_t plus_minus_icon[8] PROGMEM = {0x00, 0x04, 0x0E, 0x04, 0x00, 0x0E, 0x00, 0x00};
|
||||
|
||||
|
||||
AHT10 myAHT10(AHT10_ADDRESS_0X38);
|
||||
LiquidCrystal_I2C lcd(PCF8574_ADDR_A21_A11_A01, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
/* LCD connection check */
|
||||
while (lcd.begin(LCD_COLUMNS, LCD_ROWS) != true) //20 colums, 4 rows
|
||||
{
|
||||
Serial.println(F("PCF8574 is not connected or lcd pins declaration is wrong. Only pins numbers: 4,5,6,16,11,12,13,14 are legal."));
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
/* AHT10 connection check */
|
||||
while (myAHT10.begin() != true)
|
||||
{
|
||||
lcd.print(F("AHT10 Error")); //(F()) saves string to flash & keeps dynamic memory free
|
||||
Serial.println(F("AHT10 not connected or fail to load calibration coefficient"));
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
//Wire.setClock(400000); //experimental I2C speed! 400KHz, default 100KHz
|
||||
|
||||
lcd.clear();
|
||||
|
||||
lcd.print(F("AHT10 OK"));
|
||||
delay(2000);
|
||||
|
||||
lcd.clear();
|
||||
|
||||
/* load custom symbol to CGRAM */
|
||||
lcd.createChar(0, temperature_icon);
|
||||
lcd.createChar(1, humidity_icon);
|
||||
lcd.createChar(2, plus_minus_icon);
|
||||
|
||||
/* prints static text */
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.write(0); //print custom tempereture symbol from CGRAM
|
||||
|
||||
lcd.setCursor(0, 1); //set 1-st colum & 2-nd row, first colum & row started at zero
|
||||
lcd.write(1); //print custom humidity symbol from CGRAM
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
readStatus = myAHT10.readRawData(); //read 6 bytes from AHT10 over I2C
|
||||
|
||||
/* prints dynamic temperature data */
|
||||
lcd.setCursor(1, 0);
|
||||
|
||||
if (readStatus != AHT10_ERROR)
|
||||
{
|
||||
lcd.print(myAHT10.readTemperature(AHT10_USE_READ_DATA)); //use previously read 6 bytes
|
||||
lcd.write(2); //print custom plus/minus symbol
|
||||
lcd.print(F("0.3"));
|
||||
lcd.write(DEGREE_SYMBOL); //print degree symbol from the LCD ROM
|
||||
lcd.print(F("C "));
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd.print(F("i2c error "));
|
||||
}
|
||||
|
||||
/* prints dynamic humidity data */
|
||||
lcd.setCursor(1, 1);
|
||||
|
||||
if (readStatus != AHT10_ERROR)
|
||||
{
|
||||
lcd.print(myAHT10.readHumidity(AHT10_USE_READ_DATA)); //use previously read 6 bytes
|
||||
lcd.write(2);
|
||||
lcd.print(F("2% "));
|
||||
}
|
||||
else
|
||||
{
|
||||
lcd.print(F("i2c error "));
|
||||
}
|
||||
|
||||
delay(10000); //recomended polling frequency 8sec..30sec
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/***************************************************************************************************/
|
||||
/*
|
||||
This is an Arduino library for Aosong ASAIR AHT10, AHT15 Digital Humidity & Temperature Sensor
|
||||
|
||||
written by : enjoyneering79
|
||||
sourse code: https://github.com/enjoyneering/
|
||||
|
||||
This chip uses I2C bus to communicate, specials pins are required to interface
|
||||
Board: SDA SCL Level
|
||||
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
||||
Mega2560................................. 20 21 5v
|
||||
Due, SAM3X8E............................. 20 21 3.3v
|
||||
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
||||
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
||||
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
||||
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
||||
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
||||
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
||||
|
||||
Frameworks & Libraries:
|
||||
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
|
||||
ESP32 Core - https://github.com/espressif/arduino-esp32
|
||||
ESP8266 Core - https://github.com/esp8266/Arduino
|
||||
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
|
||||
- https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
|
||||
|
||||
GNU GPL license, all text above must be included in any redistribution,
|
||||
see link for details - https://www.gnu.org/licenses/licenses.html
|
||||
*/
|
||||
/***************************************************************************************************/
|
||||
#include <AHT10.h>
|
||||
#include <Wire.h>
|
||||
|
||||
uint8_t readStatus = 0;
|
||||
|
||||
AHT10 myAHT10(AHT10_ADDRESS_0X38);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
while (myAHT10.begin() != true)
|
||||
{
|
||||
Serial.println(F("AHT10 not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
|
||||
delay(5000);
|
||||
}
|
||||
Serial.println(F("AHT10 OK"));
|
||||
|
||||
//Wire.setClock(400000); //experimental I2C speed! 400KHz, default 100KHz
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
/* DEMO - 1, every temperature or humidity call will read 6 bytes over I2C, total 12 bytes */
|
||||
Serial.println(F("DEMO 1: read 12-bytes, show 255 if communication error is occurred"));
|
||||
Serial.print(F("Temperature: ")); Serial.print(myAHT10.readTemperature()); Serial.println(F(" +-0.3C")); //by default "AHT10_FORCE_READ_DATA"
|
||||
Serial.print(F("Humidity...: ")); Serial.print(myAHT10.readHumidity()); Serial.println(F(" +-2%")); //by default "AHT10_FORCE_READ_DATA"
|
||||
|
||||
|
||||
/* DEMO - 2, temperature call will read 6 bytes via I2C, humidity will use same 6 bytes */
|
||||
Serial.println(F("DEMO 2: read 6 byte, show 255 if communication error is occurred"));
|
||||
Serial.print(F("Temperature: ")); Serial.print(myAHT10.readTemperature(AHT10_FORCE_READ_DATA)); Serial.println(F(" +-0.3C"));
|
||||
Serial.print(F("Humidity...: ")); Serial.print(myAHT10.readHumidity(AHT10_USE_READ_DATA)); Serial.println(F(" +-2%"));
|
||||
|
||||
|
||||
/* DEMO - 3, same as demo2 but different call procedure */
|
||||
Serial.println(F("DEMO 3: read 6-bytes, show 255 if communication error is occurred"));
|
||||
|
||||
readStatus = myAHT10.readRawData(); //read 6 bytes from AHT10 over I2C
|
||||
|
||||
if (readStatus != AHT10_ERROR)
|
||||
{
|
||||
Serial.print(F("Temperature: ")); Serial.print(myAHT10.readTemperature(AHT10_USE_READ_DATA)); Serial.println(F(" +-0.3C"));
|
||||
Serial.print(F("Humidity...: ")); Serial.print(myAHT10.readHumidity(AHT10_USE_READ_DATA)); Serial.println(F(" +-2%"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Failed to read - reset: "));
|
||||
Serial.println(myAHT10.softReset()); //reset 1-success, 0-failed
|
||||
}
|
||||
|
||||
delay(10000); //recomended polling frequency 8sec..30sec
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
/***************************************************************************************************/
|
||||
/*
|
||||
This is an Arduino library for Aosong ASAIR AHT10, AHT15 Digital Humidity & Temperature Sensor
|
||||
|
||||
written by : enjoyneering79
|
||||
sourse code: https://github.com/enjoyneering/
|
||||
|
||||
This chip uses I2C bus to communicate, specials pins are required to interface
|
||||
Board: SDA SCL Level
|
||||
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
||||
Mega2560................................. 20 21 5v
|
||||
Due, SAM3X8E............................. 20 21 3.3v
|
||||
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
||||
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
||||
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
||||
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
||||
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
||||
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
||||
|
||||
Frameworks & Libraries:
|
||||
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
|
||||
ESP32 Core - https://github.com/espressif/arduino-esp32
|
||||
ESP8266 Core - https://github.com/esp8266/Arduino
|
||||
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
|
||||
- https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
|
||||
|
||||
GNU GPL license, all text above must be included in any redistribution,
|
||||
see link for details - https://www.gnu.org/licenses/licenses.html
|
||||
*/
|
||||
/***************************************************************************************************/
|
||||
#include <Wire.h> //use bug free i2c driver https://github.com/enjoyneering/ESP8266-I2C-Driver
|
||||
#include <AHT10.h>
|
||||
#include <LiquidCrystal_I2C.h> //https://github.com/enjoyneering/LiquidCrystal_I2C
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
|
||||
#define LCD_ROWS 4 //qnt. of LCD rows
|
||||
#define LCD_COLUMNS 20 //qnt. of LCD columns
|
||||
#define DEGREE_SYMBOL 0xDF //degree symbol from LCD ROM
|
||||
|
||||
|
||||
float valueTH = 0;
|
||||
|
||||
const uint8_t temperature_icon[8] PROGMEM = {0x04, 0x0A, 0x0A, 0x0A, 0x0A, 0x1F, 0x1F, 0x0E}; //PROGMEM saves variable to flash & keeps dynamic memory free
|
||||
const uint8_t humidity_icon[8] PROGMEM = {0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x1F, 0x0E, 0x00};
|
||||
const uint8_t plus_minus_icon[8] PROGMEM = {0x00, 0x04, 0x0E, 0x04, 0x00, 0x0E, 0x00, 0x00};
|
||||
|
||||
AHT10 myAHT10(AHT10_ADDRESS_0X38);
|
||||
LiquidCrystal_I2C lcd(PCF8574_ADDR_A21_A11_A01, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
WiFi.persistent(false); //disable saving wifi config into SDK flash area
|
||||
WiFi.forceSleepBegin(); //disable AP & station by calling "WiFi.mode(WIFI_OFF)" & put modem to sleep
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
/* LCD connection check */
|
||||
while (lcd.begin(LCD_COLUMNS, LCD_ROWS, LCD_5x8DOTS, SDA, SCL) != true) //20 colums, 4 rows, 5x8 pixels char size
|
||||
{
|
||||
Serial.println(F("PCF8574 is not connected or lcd pins declaration is wrong. Only pins numbers: 4,5,6,16,11,12,13,14 are legal."));
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
lcd.print(F("PCF8574 is OK")); //(F()) saves string to flash & keeps dynamic memory free
|
||||
delay(1000);
|
||||
|
||||
lcd.clear();
|
||||
|
||||
/* AHT10 connection check */
|
||||
while (myAHT10.begin(SDA, SCL) != true)
|
||||
{
|
||||
lcd.print(F("AHT10 Error"));
|
||||
Serial.println(F("AHT10 not connected or fail to load calibration coefficient"));
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
//Wire.setClock(400000); //experimental I2C speed! 400KHz, default 100KHz
|
||||
//Wire.setClockStretchLimit(10000); //experimental I2C clock stretch intervals!, default 230usec
|
||||
|
||||
lcd.clear();
|
||||
|
||||
lcd.print(F("AHT10 OK"));
|
||||
delay(2000);
|
||||
|
||||
lcd.clear();
|
||||
|
||||
/* load custom symbol to CGRAM */
|
||||
lcd.createChar(0, temperature_icon);
|
||||
lcd.createChar(1, humidity_icon);
|
||||
lcd.createChar(2, plus_minus_icon);
|
||||
|
||||
/* prints static text */
|
||||
lcd.setCursor(0, 0); //set 1-st colum & 1-st row
|
||||
lcd.write(0); //print custom tempereture symbol from CGRAM
|
||||
|
||||
lcd.setCursor(0, 1);
|
||||
lcd.write(1);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
/* prints dynamic temperature data */
|
||||
lcd.setCursor(1, 0);
|
||||
|
||||
valueTH = myAHT10.readTemperature(AHT10_FORCE_READ_DATA); //read 6-bytes over I2C
|
||||
|
||||
if (valueTH != AHT10_ERROR) lcd.print(valueTH);
|
||||
else lcd.print(F("xxx"));
|
||||
|
||||
lcd.write(2); //print custom plus/minus symbol
|
||||
lcd.print(F("0.3"));
|
||||
lcd.write(DEGREE_SYMBOL); //print degree symbol from the LCD ROM
|
||||
lcd.print(F("C "));
|
||||
|
||||
/* prints dynamic humidity data */
|
||||
lcd.setCursor(1, 1);
|
||||
|
||||
valueTH = myAHT10.readHumidity(AHT10_USE_READ_DATA); //use same 6-bytes
|
||||
|
||||
if (valueTH != AHT10_ERROR) lcd.print(valueTH);
|
||||
else lcd.print(F("xxx"));
|
||||
|
||||
lcd.write(2);
|
||||
lcd.print(F("2% "));
|
||||
|
||||
delay(10000); //recomended polling frequency 8sec..30sec
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/***************************************************************************************************/
|
||||
/*
|
||||
This is an Arduino library for Aosong ASAIR AHT20 Digital Humidity & Temperature Sensor
|
||||
|
||||
written by : enjoyneering79
|
||||
sourse code: https://github.com/enjoyneering/
|
||||
|
||||
This chip uses I2C bus to communicate, specials pins are required to interface
|
||||
Board: SDA SCL Level
|
||||
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
||||
Mega2560................................. 20 21 5v
|
||||
Due, SAM3X8E............................. 20 21 3.3v
|
||||
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
||||
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
||||
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
||||
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
||||
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
||||
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
||||
|
||||
Frameworks & Libraries:
|
||||
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
|
||||
ESP32 Core - https://github.com/espressif/arduino-esp32
|
||||
ESP8266 Core - https://github.com/esp8266/Arduino
|
||||
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
|
||||
- https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
|
||||
|
||||
GNU GPL license, all text above must be included in any redistribution,
|
||||
see link for details - https://www.gnu.org/licenses/licenses.html
|
||||
*/
|
||||
/***************************************************************************************************/
|
||||
#include <AHT10.h>
|
||||
#include <Wire.h>
|
||||
|
||||
uint8_t readStatus = 0;
|
||||
|
||||
AHT10 myAHT20(AHT10_ADDRESS_0X38, AHT20_SENSOR);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
while (myAHT20.begin() != true)
|
||||
{
|
||||
Serial.println(F("AHT20 not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
|
||||
delay(5000);
|
||||
}
|
||||
Serial.println(F("AHT20 OK"));
|
||||
|
||||
//Wire.setClock(400000); //experimental I2C speed! 400KHz, default 100KHz
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
/* DEMO - 1, every temperature or humidity call will read 6 bytes over I2C, total 12 bytes */
|
||||
Serial.println(F("DEMO 1: read 12-bytes, show 255 if communication error is occurred"));
|
||||
Serial.print(F("Temperature: ")); Serial.print(myAHT20.readTemperature()); Serial.println(F(" +-0.3C")); //by default "AHT10_FORCE_READ_DATA"
|
||||
Serial.print(F("Humidity...: ")); Serial.print(myAHT20.readHumidity()); Serial.println(F(" +-2%")); //by default "AHT10_FORCE_READ_DATA"
|
||||
|
||||
|
||||
/* DEMO - 2, temperature call will read 6 bytes via I2C, humidity will use same 6 bytes */
|
||||
Serial.println(F("DEMO 2: read 6 byte, show 255 if communication error is occurred"));
|
||||
Serial.print(F("Temperature: ")); Serial.print(myAHT20.readTemperature(AHT10_FORCE_READ_DATA)); Serial.println(F(" +-0.3C"));
|
||||
Serial.print(F("Humidity...: ")); Serial.print(myAHT20.readHumidity(AHT10_USE_READ_DATA)); Serial.println(F(" +-2%"));
|
||||
|
||||
|
||||
/* DEMO - 3, same as demo2 but different call procedure */
|
||||
Serial.println(F("DEMO 3: read 6-bytes, show 255 if communication error is occurred"));
|
||||
|
||||
readStatus = myAHT20.readRawData(); //read 6 bytes from AHT10 over I2C
|
||||
|
||||
if (readStatus != AHT10_ERROR)
|
||||
{
|
||||
Serial.print(F("Temperature: ")); Serial.print(myAHT20.readTemperature(AHT10_USE_READ_DATA)); Serial.println(F(" +-0.3C"));
|
||||
Serial.print(F("Humidity...: ")); Serial.print(myAHT20.readHumidity(AHT10_USE_READ_DATA)); Serial.println(F(" +-2%"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Failed to read - reset: "));
|
||||
Serial.println(myAHT20.softReset()); //reset 1-success, 0-failed
|
||||
}
|
||||
|
||||
delay(10000); //recomended polling frequency 8sec..30sec
|
||||
}
|
38
.pio/libdeps/esp32-s3-devkitc-1/AHT10/keywords.txt
Normal file
38
.pio/libdeps/esp32-s3-devkitc-1/AHT10/keywords.txt
Normal file
@ -0,0 +1,38 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map AHT10
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
readRawData KEYWORD2
|
||||
readTemperature KEYWORD2
|
||||
readHumidity KEYWORD2
|
||||
softReset KEYWORD2
|
||||
setNormalMode KEYWORD2
|
||||
setCycleMode KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
AHT10 KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
AHT10_SENSOR LITERAL1
|
||||
AHT15_SENSOR LITERAL1
|
||||
AHT20_SENSOR LITERAL1
|
||||
|
||||
AHT10_FORCE_READ_DATA LITERAL1
|
||||
AHT10_USE_READ_DATA LITERAL1
|
||||
|
||||
AHT10_ERROR LITERAL1
|
18
.pio/libdeps/esp32-s3-devkitc-1/AHT10/library.json
Normal file
18
.pio/libdeps/esp32-s3-devkitc-1/AHT10/library.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "AHT10",
|
||||
"keywords": "humidity, temperature, aht10, aht15, aht20, aosong, asair, i2c",
|
||||
"description": "AHT10, AHT15 & AHT20 Digital Humidity & Temperature Sensor",
|
||||
"authors":
|
||||
{
|
||||
"name": "ejoyneering",
|
||||
"email": "enjoyneering@protonmail.com"
|
||||
},
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/enjoyneering/AHT10.git"
|
||||
},
|
||||
"version": "1.1.0",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
9
.pio/libdeps/esp32-s3-devkitc-1/AHT10/library.properties
Normal file
9
.pio/libdeps/esp32-s3-devkitc-1/AHT10/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name = AHT10, AHT15 & AHT20
|
||||
version = 1.1.0
|
||||
author = Enjoyneering <enjoyneering@protonmail.com>
|
||||
maintainer = Enjoyneering <enjoyneering@protonmail.com>
|
||||
sentence = Aosong ASAIR AHT10, AHT15 & AHT20, I²C Temperature & Humidity Sensor
|
||||
paragraph = Aosong ASAIR AHT10, AHT15 & AHT20, I²C Temperature & Humidity Sensor
|
||||
category = Sensors
|
||||
url = https://github.com/enjoyneering/AHT10
|
||||
architectures = *
|
389
.pio/libdeps/esp32-s3-devkitc-1/AHT10/src/AHT10.cpp
Normal file
389
.pio/libdeps/esp32-s3-devkitc-1/AHT10/src/AHT10.cpp
Normal file
@ -0,0 +1,389 @@
|
||||
/***************************************************************************************************/
|
||||
/*
|
||||
This is an Arduino library for Aosong ASAIR AHT10, AHT15 Digital Humidity & Temperature Sensor
|
||||
|
||||
written by : enjoyneering79
|
||||
sourse code: https://github.com/enjoyneering/
|
||||
|
||||
|
||||
This chip uses I2C bus to communicate, specials pins are required to interface
|
||||
Board: SDA SCL Level
|
||||
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
||||
Mega2560................................. 20 21 5v
|
||||
Due, SAM3X8E............................. 20 21 3.3v
|
||||
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
||||
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
||||
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
||||
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
||||
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
||||
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
||||
|
||||
Frameworks & Libraries:
|
||||
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
|
||||
ESP32 Core - https://github.com/espressif/arduino-esp32
|
||||
ESP8266 Core - https://github.com/esp8266/Arduino
|
||||
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
|
||||
- https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
|
||||
GNU GPL license, all text above must be included in any redistribution,
|
||||
see link for details - https://www.gnu.org/licenses/licenses.html
|
||||
*/
|
||||
/***************************************************************************************************/
|
||||
|
||||
#include "AHT10.h"
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
Constructor
|
||||
*/
|
||||
/**************************************************************************/
|
||||
AHT10::AHT10(uint8_t address, ASAIR_I2C_SENSOR sensorName)
|
||||
{
|
||||
_address = address;
|
||||
_sensorName = sensorName;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
begin()
|
||||
|
||||
Initialize I2C & configure the sensor, call this function before
|
||||
doing anything else
|
||||
|
||||
NOTE:
|
||||
- Wire.endTransmission() returned value:
|
||||
- 0 success
|
||||
- 1 data too long to fit in transmit data16
|
||||
- 2 received NACK on transmit of address
|
||||
- 3 received NACK on transmit of data
|
||||
- 4 other error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
#if defined(ESP8266)
|
||||
bool AHT10::begin(uint8_t sda, uint8_t scl)
|
||||
{
|
||||
Wire.begin(sda, scl);
|
||||
Wire.setClock(100000); //experimental! ESP8266 I2C bus speed: 50kHz..400kHz/50000..400000, default 100000
|
||||
Wire.setClockStretchLimit(230); //experimental! default 230usec
|
||||
#else
|
||||
bool AHT10::begin(void)
|
||||
{
|
||||
Wire.begin();
|
||||
Wire.setClock(100000); //experimental! AVR I2C bus speed: 31kHz..400kHz/31000..400000, default 100000
|
||||
#endif
|
||||
|
||||
delay(AHT10_POWER_ON_DELAY); //wait for sensor to initialize
|
||||
|
||||
setNormalMode(); //one measurement+sleep mode
|
||||
|
||||
return enableFactoryCalCoeff(); //load factory calibration coeff
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
readRawData()
|
||||
|
||||
Read raw measurment data from sensor over I2C
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t AHT10::readRawData()
|
||||
{
|
||||
/* send measurment command */
|
||||
Wire.beginTransmission(_address);
|
||||
#if (ARDUINO) >= 100
|
||||
Wire.write(AHT10_START_MEASURMENT_CMD); //send measurment command
|
||||
Wire.write(AHT10_DATA_MEASURMENT_CMD); //send measurment parameter
|
||||
Wire.write(AHT10_DATA_NOP); //send measurment parameter
|
||||
#else
|
||||
Wire.send(AHT10_START_MEASURMENT_CMD);
|
||||
Wire.send(AHT10_DATA_MEASURMENT_CMD);
|
||||
Wire.send(AHT10_DATA_NOP);
|
||||
#endif
|
||||
if (Wire.endTransmission(true) != 0) return AHT10_ERROR; //error handler, collision on I2C bus
|
||||
|
||||
if (getCalibrationBit() != 0x01) return AHT10_ERROR; //error handler, calibration coefficient turned off
|
||||
if (getBusyBit(AHT10_USE_READ_DATA) != 0x00) delay(AHT10_MEASURMENT_DELAY); //measurement delay
|
||||
|
||||
/* read 6-bytes from sensor */
|
||||
#if defined(_VARIANT_ARDUINO_STM32_)
|
||||
Wire.requestFrom(_address, 6);
|
||||
#else
|
||||
Wire.requestFrom(_address, 6, true); //true - send stop after transmission & release I2C bus
|
||||
#endif
|
||||
if (Wire.available() != 6)
|
||||
{
|
||||
_rawDataBuffer[0] = AHT10_ERROR; //for condition when AHT10_USE_READ_DATA is used
|
||||
return AHT10_ERROR; //check rxBuffer & error handler, collision on the i2c bus
|
||||
}
|
||||
|
||||
/* read 6 bytes from "wire.h" rxBuffer */
|
||||
#if (ARDUINO) >= 100
|
||||
for (uint8_t i = 0; i < 6 ; i++)
|
||||
{
|
||||
_rawDataBuffer[i] = Wire.read();
|
||||
}
|
||||
#else
|
||||
for (uint8_t i = 0; i < 6 ; i++)
|
||||
{
|
||||
_rawDataBuffer[i] = Wire.receive();
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
readTemperature()
|
||||
|
||||
Read temperature, °C
|
||||
|
||||
NOTE:
|
||||
- temperature range -40°C..+80°C
|
||||
- temperature resolution 0.01°C
|
||||
- temperature accuracy ±0.3°C
|
||||
*/
|
||||
/**************************************************************************/
|
||||
float AHT10::readTemperature(bool readI2C)
|
||||
{
|
||||
if (readI2C == AHT10_FORCE_READ_DATA)
|
||||
{
|
||||
if (readRawData() == AHT10_ERROR) return AHT10_ERROR; //force to read data to _rawDataBuffer & error handler
|
||||
}
|
||||
|
||||
if (_rawDataBuffer[0] == AHT10_ERROR) return AHT10_ERROR; //error handler, collision on I2C bus
|
||||
|
||||
uint32_t temperature = ((uint32_t)(_rawDataBuffer[3] & 0x0F) << 16) | ((uint16_t)_rawDataBuffer[4] << 8) | _rawDataBuffer[5]; //20-bit raw temperature data
|
||||
|
||||
return (float)temperature * 0.000191 - 50;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
readHumidity()
|
||||
|
||||
Read relative humidity, %
|
||||
|
||||
NOTE:
|
||||
- prolonged exposure for 60 hours at humidity > 80% can lead to a
|
||||
temporary drift of the signal +3%. Sensor slowly returns to the
|
||||
calibrated state at normal operating conditions.
|
||||
- relative humidity range 0%..100%
|
||||
- relative humidity resolution 0.024%
|
||||
- relative humidity accuracy ±2%
|
||||
*/
|
||||
/**************************************************************************/
|
||||
float AHT10::readHumidity(bool readI2C)
|
||||
{
|
||||
if (readI2C == AHT10_FORCE_READ_DATA)
|
||||
{
|
||||
if (readRawData() == AHT10_ERROR) return AHT10_ERROR; //force to read data to _rawDataBuffer & error handler
|
||||
}
|
||||
|
||||
if (_rawDataBuffer[0] == AHT10_ERROR) return AHT10_ERROR; //error handler, collision on I2C bus
|
||||
|
||||
uint32_t rawData = (((uint32_t)_rawDataBuffer[1] << 16) | ((uint16_t)_rawDataBuffer[2] << 8) | (_rawDataBuffer[3])) >> 4; //20-bit raw humidity data
|
||||
|
||||
float humidity = (float)rawData * 0.000095;
|
||||
|
||||
if (humidity < 0) return 0;
|
||||
if (humidity > 100) return 100;
|
||||
return humidity;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
softReset()
|
||||
|
||||
Restart sensor, without power off
|
||||
|
||||
NOTE:
|
||||
- takes ~20ms
|
||||
- all registers restores to default
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool AHT10::softReset(void)
|
||||
{
|
||||
Wire.beginTransmission(_address);
|
||||
|
||||
#if (ARDUINO) >= 100
|
||||
Wire.write(AHT10_SOFT_RESET_CMD);
|
||||
#else
|
||||
Wire.send(AHT10_SOFT_RESET_CMD);
|
||||
#endif
|
||||
|
||||
if (Wire.endTransmission(true) != 0) return false; //safety check, make sure sensor reset
|
||||
|
||||
delay(AHT10_SOFT_RESET_DELAY);
|
||||
|
||||
setNormalMode(); //reinitialize sensor registers after reset
|
||||
|
||||
return enableFactoryCalCoeff(); //reinitialize sensor registers after reset
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
setNormalMode()
|
||||
|
||||
Set normal measurment mode
|
||||
|
||||
NOTE:
|
||||
- one measurement & power down??? no info in datasheet!!!
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool AHT10::setNormalMode(void)
|
||||
{
|
||||
Wire.beginTransmission(_address);
|
||||
|
||||
#if (ARDUINO) >= 100
|
||||
Wire.write(AHT10_NORMAL_CMD);
|
||||
Wire.write(AHT10_DATA_NOP);
|
||||
Wire.write(AHT10_DATA_NOP);
|
||||
#else
|
||||
Wire.send(AHT10_NORMAL_CMD);
|
||||
Wire.send(AHT10_DATA_NOP);
|
||||
Wire.send(AHT10_DATA_NOP);
|
||||
#endif
|
||||
|
||||
if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete
|
||||
|
||||
delay(AHT10_CMD_DELAY);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
setCycleMode()
|
||||
|
||||
Set cycle measurment mode
|
||||
|
||||
NOTE:
|
||||
- continuous measurement
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool AHT10::setCycleMode(void)
|
||||
{
|
||||
Wire.beginTransmission(_address);
|
||||
|
||||
#if (ARDUINO) >= 100
|
||||
if (_sensorName != AHT20_SENSOR) Wire.write(AHT10_INIT_CMD); //set command mode
|
||||
else Wire.write(AHT20_INIT_CMD);
|
||||
Wire.write(AHT10_INIT_CYCLE_MODE | AHT10_INIT_CAL_ENABLE); //0,[0,1],0,[1],0,0,0
|
||||
Wire.write(AHT10_DATA_NOP);
|
||||
#else
|
||||
if (_sensorName != AHT20_SENSOR) Wire.send(AHT10_INIT_CMD);
|
||||
else Wire.send(AHT20_INIT_CMD);
|
||||
Wire.send(AHT10_INIT_CYCLE_MODE | AHT10_INIT_CAL_ENABLE);
|
||||
Wire.send(AHT10_DATA_NOP);
|
||||
#endif
|
||||
|
||||
if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
readStatusByte()
|
||||
|
||||
Read status byte from sensor over I2C
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t AHT10::readStatusByte()
|
||||
{
|
||||
#if defined(_VARIANT_ARDUINO_STM32_)
|
||||
Wire.requestFrom(_address, 1);
|
||||
#else
|
||||
Wire.requestFrom(_address, 1, true); //true - send stop after transmission & release I2C bus
|
||||
#endif
|
||||
if (Wire.available() != 1) return AHT10_ERROR; //check rxBuffer & error handler, collision on I2C bus
|
||||
|
||||
/* read byte from "wire.h" rxBuffer */
|
||||
#if (ARDUINO) >= 100
|
||||
return Wire.read();
|
||||
#else
|
||||
return Wire.receive();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
getCalibrationBit()
|
||||
|
||||
Read Calibration bit from status byte
|
||||
|
||||
NOTE:
|
||||
- 0, factory calibration coeff disabled
|
||||
- 1, factory calibration coeff loaded
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t AHT10::getCalibrationBit(bool readI2C)
|
||||
{
|
||||
if (readI2C == AHT10_FORCE_READ_DATA) _rawDataBuffer[0] = readStatusByte(); //force to read status byte
|
||||
|
||||
if (_rawDataBuffer[0] != AHT10_ERROR) return bitRead(_rawDataBuffer[0], 3); //get 3-rd bit
|
||||
return AHT10_ERROR;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
enableFactoryCalCoeff()
|
||||
|
||||
Load factory calibration coefficients
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool AHT10::enableFactoryCalCoeff()
|
||||
{
|
||||
/* load factory calibration coeff */
|
||||
Wire.beginTransmission(_address);
|
||||
|
||||
#if (ARDUINO) >= 100
|
||||
if (_sensorName != AHT20_SENSOR) Wire.write(AHT10_INIT_CMD); //set command mode
|
||||
else Wire.write(AHT20_INIT_CMD);
|
||||
Wire.write(AHT10_INIT_CAL_ENABLE); //0,0,0,0,[1],0,0,0
|
||||
Wire.write(AHT10_DATA_NOP); //0,0,0,0,0,0,0,0
|
||||
#else
|
||||
if (_sensorName != AHT20_SENSOR) Wire.send(AHT10_INIT_CMD);
|
||||
else Wire.send(AHT20_INIT_CMD);
|
||||
Wire.send(AHT10_INIT_CAL_ENABLE);
|
||||
Wire.send(AHT10_DATA_NOP);
|
||||
#endif
|
||||
|
||||
if (Wire.endTransmission(true) != 0) return false; //safety check, make sure transmission complete
|
||||
|
||||
delay(AHT10_CMD_DELAY);
|
||||
|
||||
/*check calibration enable */
|
||||
if (getCalibrationBit() == 0x01) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
getBusyBit()
|
||||
|
||||
Read busy bit from status byte
|
||||
|
||||
NOTE:
|
||||
- 0, sensor idle & sleeping
|
||||
- 1, sensor busy & in measurement state
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t AHT10::getBusyBit(bool readI2C)
|
||||
{
|
||||
if (readI2C == AHT10_FORCE_READ_DATA) _rawDataBuffer[0] = readStatusByte(); //force to read status byte
|
||||
|
||||
if (_rawDataBuffer[0] != AHT10_ERROR) return bitRead(_rawDataBuffer[0], 7); //get 7-th bit
|
||||
return AHT10_ERROR;
|
||||
}
|
121
.pio/libdeps/esp32-s3-devkitc-1/AHT10/src/AHT10.h
Normal file
121
.pio/libdeps/esp32-s3-devkitc-1/AHT10/src/AHT10.h
Normal file
@ -0,0 +1,121 @@
|
||||
/***************************************************************************************************/
|
||||
/*
|
||||
This is an Arduino library for Aosong ASAIR AHT10, AHT15 Digital Humidity & Temperature Sensor
|
||||
|
||||
written by : enjoyneering79
|
||||
sourse code: https://github.com/enjoyneering/
|
||||
|
||||
|
||||
This chip uses I2C bus to communicate, specials pins are required to interface
|
||||
Board: SDA SCL Level
|
||||
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
||||
Mega2560................................. 20 21 5v
|
||||
Due, SAM3X8E............................. 20 21 3.3v
|
||||
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
||||
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
||||
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
||||
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
||||
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
||||
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
||||
|
||||
Frameworks & Libraries:
|
||||
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
|
||||
ESP32 Core - https://github.com/espressif/arduino-esp32
|
||||
ESP8266 Core - https://github.com/esp8266/Arduino
|
||||
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
|
||||
- https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
|
||||
GNU GPL license, all text above must be included in any redistribution,
|
||||
see link for details - https://www.gnu.org/licenses/licenses.html
|
||||
*/
|
||||
/***************************************************************************************************/
|
||||
|
||||
#ifndef AHT10_h
|
||||
#define AHT10_h
|
||||
|
||||
#if defined(ARDUINO) && ((ARDUINO) >= 100) //arduino core v1.0 or later
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#endif
|
||||
|
||||
#if defined(__AVR__)
|
||||
#include <avr/pgmspace.h> //for Arduino AVR PROGMEM support
|
||||
#elif defined(ESP8266)
|
||||
#include <pgmspace.h> //for Arduino ESP8266 PROGMEM support
|
||||
#elif defined(_VARIANT_ARDUINO_STM32_)
|
||||
#include <avr/pgmspace.h> //for Arduino STM32 PROGMEM support
|
||||
#endif
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
|
||||
|
||||
#define AHT10_ADDRESS_0X38 0x38 //chip I2C address no.1 for AHT10/AHT15/AHT20, address pin connected to GND
|
||||
#define AHT10_ADDRESS_0X39 0x39 //chip I2C address no.2 for AHT10 only, address pin connected to Vcc
|
||||
|
||||
#define AHT10_INIT_CMD 0xE1 //initialization command for AHT10/AHT15
|
||||
#define AHT20_INIT_CMD 0xBE //initialization command for AHT20
|
||||
#define AHT10_START_MEASURMENT_CMD 0xAC //start measurment command
|
||||
#define AHT10_NORMAL_CMD 0xA8 //normal cycle mode command, no info in datasheet!!!
|
||||
#define AHT10_SOFT_RESET_CMD 0xBA //soft reset command
|
||||
|
||||
#define AHT10_INIT_NORMAL_MODE 0x00 //enable normal mode
|
||||
#define AHT10_INIT_CYCLE_MODE 0x20 //enable cycle mode
|
||||
#define AHT10_INIT_CMD_MODE 0x40 //enable command mode
|
||||
#define AHT10_INIT_CAL_ENABLE 0x08 //load factory calibration coeff
|
||||
|
||||
|
||||
#define AHT10_DATA_MEASURMENT_CMD 0x33 //no info in datasheet!!! my guess it is DAC resolution, saw someone send 0x00 instead
|
||||
#define AHT10_DATA_NOP 0x00 //no info in datasheet!!!
|
||||
|
||||
|
||||
#define AHT10_MEASURMENT_DELAY 80 //at least 75 milliseconds
|
||||
#define AHT10_POWER_ON_DELAY 40 //at least 20..40 milliseconds
|
||||
#define AHT10_CMD_DELAY 350 //at least 300 milliseconds, no info in datasheet!!!
|
||||
#define AHT10_SOFT_RESET_DELAY 20 //less than 20 milliseconds
|
||||
|
||||
#define AHT10_FORCE_READ_DATA true //force to read data
|
||||
#define AHT10_USE_READ_DATA false //force to use data from previous read
|
||||
#define AHT10_ERROR 0xFF //returns 255, if communication error is occurred
|
||||
|
||||
|
||||
typedef enum : uint8_t
|
||||
{
|
||||
AHT10_SENSOR = 0x00,
|
||||
AHT15_SENSOR = 0x01,
|
||||
AHT20_SENSOR = 0x02
|
||||
}
|
||||
ASAIR_I2C_SENSOR;
|
||||
|
||||
|
||||
class AHT10
|
||||
{
|
||||
public:
|
||||
|
||||
AHT10(uint8_t address = AHT10_ADDRESS_0X38, ASAIR_I2C_SENSOR = AHT10_SENSOR);
|
||||
|
||||
#if defined(ESP8266)
|
||||
bool begin(uint8_t sda = SDA, uint8_t scl = SCL);
|
||||
#else
|
||||
bool begin();
|
||||
#endif
|
||||
uint8_t readRawData();
|
||||
float readTemperature(bool readI2C = AHT10_FORCE_READ_DATA);
|
||||
float readHumidity(bool readI2C = AHT10_FORCE_READ_DATA);
|
||||
bool softReset();
|
||||
bool setNormalMode();
|
||||
bool setCycleMode();
|
||||
|
||||
private:
|
||||
uint8_t _address;
|
||||
ASAIR_I2C_SENSOR _sensorName;
|
||||
uint8_t _rawDataBuffer[6] = {AHT10_ERROR, 0, 0, 0, 0, 0};
|
||||
|
||||
uint8_t readStatusByte();
|
||||
uint8_t getCalibrationBit(bool readI2C = AHT10_FORCE_READ_DATA);
|
||||
bool enableFactoryCalCoeff();
|
||||
uint8_t getBusyBit(bool readI2C = AHT10_FORCE_READ_DATA);
|
||||
};
|
||||
|
||||
#endif
|
46
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.github/ISSUE_TEMPLATE.md
vendored
Normal file
46
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.github/ISSUE_TEMPLATE.md
vendored
Normal file
@ -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**
|
26
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
26
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@ -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.
|
33
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.github/workflows/githubci.yml
vendored
Normal file
33
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.github/workflows/githubci.yml
vendored
Normal file
@ -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
|
||||
|
1
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.piopm
Normal file
1
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/.piopm
Normal file
@ -0,0 +1 @@
|
||||
{"type": "library", "name": "Adafruit BusIO", "version": "1.17.1", "spec": {"owner": "adafruit", "id": 6214, "name": "Adafruit BusIO", "requirements": null, "uri": null}}
|
@ -0,0 +1,384 @@
|
||||
#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 Create a register we access over a GenericDevice
|
||||
* @param genericdevice Generic device to use
|
||||
* @param reg_addr Register address we will read/write
|
||||
* @param width Width of the register in bytes (1-4)
|
||||
* @param byteorder Byte order of register data (LSBFIRST or MSBFIRST)
|
||||
* @param address_width Width of the register address in bytes (1 or 2)
|
||||
*/
|
||||
Adafruit_BusIO_Register::Adafruit_BusIO_Register(
|
||||
Adafruit_GenericDevice *genericdevice, uint16_t reg_addr, uint8_t width,
|
||||
uint8_t byteorder, uint8_t address_width) {
|
||||
_i2cdevice = nullptr;
|
||||
_spidevice = nullptr;
|
||||
_genericdevice = genericdevice;
|
||||
_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);
|
||||
}
|
||||
if (_genericdevice) {
|
||||
return _genericdevice->writeRegister(addrbuffer, _addrwidth, buffer, len);
|
||||
}
|
||||
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 number of bytes from a register into a buffer
|
||||
@param buffer Buffer to read data into
|
||||
@param len Number of bytes to read into the buffer
|
||||
@return true on successful read, otherwise false
|
||||
*/
|
||||
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);
|
||||
}
|
||||
if (_genericdevice) {
|
||||
return _genericdevice->readRegister(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,112 @@
|
||||
#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_GenericDevice.h>
|
||||
#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);
|
||||
|
||||
Adafruit_BusIO_Register(Adafruit_GenericDevice *genericdevice,
|
||||
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_GenericDevice *_genericdevice;
|
||||
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,82 @@
|
||||
/*
|
||||
Written with help by Claude!
|
||||
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
|
||||
chats are not shareable :(
|
||||
*/
|
||||
|
||||
#include "Adafruit_GenericDevice.h"
|
||||
|
||||
/*!
|
||||
* @brief Create a Generic device with the provided read/write functions
|
||||
* @param obj Pointer to object instance
|
||||
* @param read_func Function pointer for reading raw data
|
||||
* @param write_func Function pointer for writing raw data
|
||||
* @param readreg_func Function pointer for reading registers (optional)
|
||||
* @param writereg_func Function pointer for writing registers (optional) */
|
||||
Adafruit_GenericDevice::Adafruit_GenericDevice(
|
||||
void *obj, busio_genericdevice_read_t read_func,
|
||||
busio_genericdevice_write_t write_func,
|
||||
busio_genericdevice_readreg_t readreg_func,
|
||||
busio_genericdevice_writereg_t writereg_func) {
|
||||
_obj = obj;
|
||||
_read_func = read_func;
|
||||
_write_func = write_func;
|
||||
_readreg_func = readreg_func;
|
||||
_writereg_func = writereg_func;
|
||||
_begun = false;
|
||||
}
|
||||
|
||||
/*! @brief Simple begin function (doesn't do much at this time)
|
||||
@return true always
|
||||
*/
|
||||
bool Adafruit_GenericDevice::begin(void) {
|
||||
_begun = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*! @brief Write a buffer of data
|
||||
@param buffer Pointer to buffer of data to write
|
||||
@param len Number of bytes to write
|
||||
@return true if write was successful, otherwise false */
|
||||
bool Adafruit_GenericDevice::write(const uint8_t *buffer, size_t len) {
|
||||
if (!_begun)
|
||||
return false;
|
||||
return _write_func(_obj, buffer, len);
|
||||
}
|
||||
|
||||
/*! @brief Read data into a buffer
|
||||
@param buffer Pointer to buffer to read data into
|
||||
@param len Number of bytes to read
|
||||
@return true if read was successful, otherwise false */
|
||||
bool Adafruit_GenericDevice::read(uint8_t *buffer, size_t len) {
|
||||
if (!_begun)
|
||||
return false;
|
||||
return _read_func(_obj, buffer, len);
|
||||
}
|
||||
|
||||
/*! @brief Read from a register location
|
||||
@param addr_buf Buffer containing register address
|
||||
@param addrsiz Size of register address in bytes
|
||||
@param buf Buffer to store read data
|
||||
@param bufsiz Size of data to read in bytes
|
||||
@return true if read was successful, otherwise false */
|
||||
bool Adafruit_GenericDevice::readRegister(uint8_t *addr_buf, uint8_t addrsiz,
|
||||
uint8_t *buf, uint16_t bufsiz) {
|
||||
if (!_begun || !_readreg_func)
|
||||
return false;
|
||||
return _readreg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
|
||||
}
|
||||
|
||||
/*! @brief Write to a register location
|
||||
@param addr_buf Buffer containing register address
|
||||
@param addrsiz Size of register address in bytes
|
||||
@param buf Buffer containing data to write
|
||||
@param bufsiz Size of data to write in bytes
|
||||
@return true if write was successful, otherwise false */
|
||||
bool Adafruit_GenericDevice::writeRegister(uint8_t *addr_buf, uint8_t addrsiz,
|
||||
const uint8_t *buf,
|
||||
uint16_t bufsiz) {
|
||||
if (!_begun || !_writereg_func)
|
||||
return false;
|
||||
return _writereg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
#ifndef ADAFRUIT_GENERICDEVICE_H
|
||||
#define ADAFRUIT_GENERICDEVICE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
typedef bool (*busio_genericdevice_read_t)(void *obj, uint8_t *buffer,
|
||||
size_t len);
|
||||
typedef bool (*busio_genericdevice_write_t)(void *obj, const uint8_t *buffer,
|
||||
size_t len);
|
||||
typedef bool (*busio_genericdevice_readreg_t)(void *obj, uint8_t *addr_buf,
|
||||
uint8_t addrsiz, uint8_t *data,
|
||||
uint16_t datalen);
|
||||
typedef bool (*busio_genericdevice_writereg_t)(void *obj, uint8_t *addr_buf,
|
||||
uint8_t addrsiz,
|
||||
const uint8_t *data,
|
||||
uint16_t datalen);
|
||||
|
||||
/*!
|
||||
* @brief Class for communicating with a device via generic read/write functions
|
||||
*/
|
||||
class Adafruit_GenericDevice {
|
||||
public:
|
||||
Adafruit_GenericDevice(
|
||||
void *obj, busio_genericdevice_read_t read_func,
|
||||
busio_genericdevice_write_t write_func,
|
||||
busio_genericdevice_readreg_t readreg_func = nullptr,
|
||||
busio_genericdevice_writereg_t writereg_func = nullptr);
|
||||
|
||||
bool begin(void);
|
||||
|
||||
bool read(uint8_t *buffer, size_t len);
|
||||
bool write(const uint8_t *buffer, size_t len);
|
||||
bool readRegister(uint8_t *addr_buf, uint8_t addrsiz, uint8_t *buf,
|
||||
uint16_t bufsiz);
|
||||
bool writeRegister(uint8_t *addr_buf, uint8_t addrsiz, const uint8_t *buf,
|
||||
uint16_t bufsiz);
|
||||
|
||||
protected:
|
||||
/*! @brief Function pointer for reading raw data from the device */
|
||||
busio_genericdevice_read_t _read_func;
|
||||
/*! @brief Function pointer for writing raw data to the device */
|
||||
busio_genericdevice_write_t _write_func;
|
||||
/*! @brief Function pointer for reading a 'register' from the device */
|
||||
busio_genericdevice_readreg_t _readreg_func;
|
||||
/*! @brief Function pointer for writing a 'register' to the device */
|
||||
busio_genericdevice_writereg_t _writereg_func;
|
||||
|
||||
bool _begun; ///< whether we have initialized yet (in case the function needs
|
||||
///< to do something)
|
||||
|
||||
private:
|
||||
void *_obj; ///< Pointer to object instance
|
||||
};
|
||||
|
||||
#endif // ADAFRUIT_GENERICDEVICE_H
|
@ -0,0 +1,320 @@
|
||||
#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
|
||||
* don't respond well to 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);
|
||||
#ifdef DEBUG_SERIAL
|
||||
DEBUG_SERIAL.print(F("Address 0x"));
|
||||
DEBUG_SERIAL.print(_addr, HEX);
|
||||
#endif
|
||||
#ifdef ARDUINO_ARCH_MBED
|
||||
_wire->write(0); // forces a write request instead of a read
|
||||
#endif
|
||||
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 = *mosiPort | mosiPinMask;
|
||||
else
|
||||
*mosiPort = *mosiPort & ~mosiPinMask;
|
||||
#else
|
||||
digitalWrite(_mosi, towrite);
|
||||
#endif
|
||||
lastmosi = towrite;
|
||||
}
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
*clkPort = *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 = *clkPort & ~clkPinMask; // Clock low
|
||||
#else
|
||||
digitalWrite(_sck, LOW);
|
||||
#endif
|
||||
} else { // if (_dataMode == SPI_MODE1 || _dataMode == SPI_MODE3)
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
*clkPort = *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 = *mosiPort | mosiPinMask;
|
||||
else
|
||||
*mosiPort = *mosiPort & ~mosiPinMask;
|
||||
#else
|
||||
digitalWrite(_mosi, send & b);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef BUSIO_USE_FAST_PINIO
|
||||
*clkPort = *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,149 @@
|
||||
#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) || \
|
||||
defined(ARDUINO_ARCH_XMC)
|
||||
|
||||
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(__MBED__) || defined(__ZEPHYR__)
|
||||
// Boards based on RTOS cores like mbed or Zephyr are not going to expose the
|
||||
// low level registers needed for fast pin manipulation
|
||||
#undef BUSIO_USE_FAST_PINIO
|
||||
|
||||
#elif defined(ARDUINO_ARCH_XMC)
|
||||
#undef 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_RP2040) && !defined(ARDUINO_SILABS) && \
|
||||
!defined(ARDUINO_UNOR4_MINIMA) && !defined(ARDUINO_UNOR4_WIFI) && \
|
||||
!defined(PORTDUINO)
|
||||
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" "Adafruit_GenericDevice.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES arduino-esp32)
|
||||
|
||||
project(Adafruit_BusIO)
|
21
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/LICENSE
Normal file
21
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/LICENSE
Normal file
@ -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.
|
8
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/README.md
Normal file
8
.pio/libdeps/esp32-s3-devkitc-1/Adafruit BusIO/README.md
Normal file
@ -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, and 'generic transport' (e.g. UART) 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,219 @@
|
||||
/*
|
||||
Advanced example of using bstracted transport for reading and writing
|
||||
register data from a UART-based device such as a TMC2209
|
||||
|
||||
Written with help by Claude!
|
||||
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
|
||||
chats are not shareable :(
|
||||
*/
|
||||
|
||||
#include "Adafruit_BusIO_Register.h"
|
||||
#include "Adafruit_GenericDevice.h"
|
||||
|
||||
// Debugging macros
|
||||
#define DEBUG_SERIAL Serial
|
||||
|
||||
#ifdef DEBUG_SERIAL
|
||||
#define DEBUG_PRINT(x) DEBUG_SERIAL.print(x)
|
||||
#define DEBUG_PRINTLN(x) DEBUG_SERIAL.println(x)
|
||||
#define DEBUG_PRINT_HEX(x) \
|
||||
do { \
|
||||
if (x < 0x10) \
|
||||
DEBUG_SERIAL.print('0'); \
|
||||
DEBUG_SERIAL.print(x, HEX); \
|
||||
DEBUG_SERIAL.print(' '); \
|
||||
} while (0)
|
||||
#else
|
||||
#define DEBUG_PRINT(x)
|
||||
#define DEBUG_PRINTLN(x)
|
||||
#define DEBUG_PRINT_HEX(x)
|
||||
#endif
|
||||
|
||||
#define TMC2209_IOIN 0x06
|
||||
|
||||
class TMC2209_UART {
|
||||
private:
|
||||
Stream *_uart_stream;
|
||||
uint8_t _addr;
|
||||
|
||||
static bool uart_read(void *thiz, uint8_t *buffer, size_t len) {
|
||||
TMC2209_UART *dev = (TMC2209_UART *)thiz;
|
||||
uint16_t timeout = 100;
|
||||
while (dev->_uart_stream->available() < len && timeout--) {
|
||||
delay(1);
|
||||
}
|
||||
if (timeout == 0) {
|
||||
DEBUG_PRINTLN("Read timeout!");
|
||||
return false;
|
||||
}
|
||||
|
||||
DEBUG_PRINT("Reading: ");
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
buffer[i] = dev->_uart_stream->read();
|
||||
DEBUG_PRINT_HEX(buffer[i]);
|
||||
}
|
||||
DEBUG_PRINTLN("");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool uart_write(void *thiz, const uint8_t *buffer, size_t len) {
|
||||
TMC2209_UART *dev = (TMC2209_UART *)thiz;
|
||||
DEBUG_PRINT("Writing: ");
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
DEBUG_PRINT_HEX(buffer[i]);
|
||||
}
|
||||
DEBUG_PRINTLN("");
|
||||
|
||||
dev->_uart_stream->write(buffer, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool uart_readreg(void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
|
||||
uint8_t *data, uint16_t datalen) {
|
||||
TMC2209_UART *dev = (TMC2209_UART *)thiz;
|
||||
while (dev->_uart_stream->available())
|
||||
dev->_uart_stream->read();
|
||||
|
||||
uint8_t packet[4] = {0x05, uint8_t(dev->_addr << 1), addr_buf[0], 0x00};
|
||||
|
||||
packet[3] = calcCRC(packet, 3);
|
||||
if (!uart_write(thiz, packet, 4))
|
||||
return false;
|
||||
|
||||
// Read back echo
|
||||
uint8_t echo[4];
|
||||
if (!uart_read(thiz, echo, 4))
|
||||
return false;
|
||||
|
||||
// Verify echo
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
if (echo[i] != packet[i]) {
|
||||
DEBUG_PRINTLN("Echo mismatch");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t response[8]; // sync + 0xFF + reg + 4 data bytes + CRC
|
||||
if (!uart_read(thiz, response, 8))
|
||||
return false;
|
||||
|
||||
// Verify response
|
||||
if (response[0] != 0x05) {
|
||||
DEBUG_PRINTLN("Invalid sync byte");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response[1] != 0xFF) {
|
||||
DEBUG_PRINTLN("Invalid reply address");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response[2] != addr_buf[0]) {
|
||||
DEBUG_PRINTLN("Register mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t crc = calcCRC(response, 7);
|
||||
if (crc != response[7]) {
|
||||
DEBUG_PRINTLN("CRC mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(data, &response[3], 4);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool uart_writereg(void *thiz, uint8_t *addr_buf, uint8_t addrsiz,
|
||||
const uint8_t *data, uint16_t datalen) {
|
||||
TMC2209_UART *dev = (TMC2209_UART *)thiz;
|
||||
while (dev->_uart_stream->available())
|
||||
dev->_uart_stream->read();
|
||||
|
||||
uint8_t packet[8] = {0x05,
|
||||
uint8_t(dev->_addr << 1),
|
||||
uint8_t(addr_buf[0] | 0x80),
|
||||
data[0],
|
||||
data[1],
|
||||
data[2],
|
||||
data[3],
|
||||
0x00};
|
||||
|
||||
packet[7] = calcCRC(packet, 7);
|
||||
if (!uart_write(thiz, packet, 8))
|
||||
return false;
|
||||
|
||||
uint8_t echo[8];
|
||||
if (!uart_read(thiz, echo, 8))
|
||||
return false;
|
||||
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (echo[i] != packet[i]) {
|
||||
DEBUG_PRINTLN("Write echo mismatch");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint8_t calcCRC(uint8_t *data, uint8_t length) {
|
||||
uint8_t crc = 0;
|
||||
for (uint8_t i = 0; i < length; i++) {
|
||||
uint8_t currentByte = data[i];
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
if ((crc >> 7) ^ (currentByte & 0x01)) {
|
||||
crc = (crc << 1) ^ 0x07;
|
||||
} else {
|
||||
crc = crc << 1;
|
||||
}
|
||||
currentByte = currentByte >> 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
public:
|
||||
TMC2209_UART(Stream *serial, uint8_t addr)
|
||||
: _uart_stream(serial), _addr(addr) {}
|
||||
|
||||
Adafruit_GenericDevice *createDevice() {
|
||||
return new Adafruit_GenericDevice(this, uart_read, uart_write, uart_readreg,
|
||||
uart_writereg);
|
||||
}
|
||||
};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial)
|
||||
;
|
||||
delay(100);
|
||||
Serial.println("TMC2209 Generic Device register read/write test!");
|
||||
|
||||
Serial1.begin(115200);
|
||||
|
||||
TMC2209_UART uart(&Serial1, 0);
|
||||
Adafruit_GenericDevice *device = uart.createDevice();
|
||||
device->begin();
|
||||
|
||||
// Create register object for IOIN
|
||||
Adafruit_BusIO_Register ioin_reg(device,
|
||||
TMC2209_IOIN, // device and register address
|
||||
4, // width = 4 bytes
|
||||
MSBFIRST, // byte order
|
||||
1); // address width = 1 byte
|
||||
Serial.print("IOIN = 0x");
|
||||
Serial.println(ioin_reg.read(), HEX);
|
||||
|
||||
// Create RegisterBits for VERSION field (bits 31:24)
|
||||
Adafruit_BusIO_RegisterBits version_bits(
|
||||
&ioin_reg, 8, 24); // 8 bits wide, starting at bit 24
|
||||
|
||||
Serial.println("Reading VERSION...");
|
||||
uint8_t version = version_bits.read();
|
||||
|
||||
Serial.print("VERSION = 0x");
|
||||
Serial.println(version, HEX);
|
||||
}
|
||||
|
||||
void loop() { delay(1000); }
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
Abstracted transport for reading and writing data from a UART-based
|
||||
device such as a TMC2209
|
||||
|
||||
Written with help by Claude!
|
||||
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
|
||||
chats are not shareable :(
|
||||
*/
|
||||
|
||||
#include "Adafruit_GenericDevice.h"
|
||||
|
||||
/**
|
||||
* Basic UART device class that demonstrates using GenericDevice with a Stream
|
||||
* interface. This example shows how to wrap a Stream (like HardwareSerial or
|
||||
* SoftwareSerial) with read/write callbacks that can be used by BusIO's
|
||||
* register functions.
|
||||
*/
|
||||
class UARTDevice {
|
||||
public:
|
||||
UARTDevice(Stream *serial) : _serial(serial) {}
|
||||
|
||||
// Static callback for writing data to UART
|
||||
// Called by GenericDevice when data needs to be sent
|
||||
static bool uart_write(void *thiz, const uint8_t *buffer, size_t len) {
|
||||
UARTDevice *dev = (UARTDevice *)thiz;
|
||||
dev->_serial->write(buffer, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Static callback for reading data from UART
|
||||
// Includes timeout and will return false if not enough data available
|
||||
static bool uart_read(void *thiz, uint8_t *buffer, size_t len) {
|
||||
UARTDevice *dev = (UARTDevice *)thiz;
|
||||
uint16_t timeout = 100;
|
||||
while (dev->_serial->available() < len && timeout--) {
|
||||
delay(1);
|
||||
}
|
||||
if (timeout == 0) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
buffer[i] = dev->_serial->read();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Create a GenericDevice instance using our callbacks
|
||||
Adafruit_GenericDevice *createDevice() {
|
||||
return new Adafruit_GenericDevice(this, uart_read, uart_write);
|
||||
}
|
||||
|
||||
private:
|
||||
Stream *_serial; // Underlying Stream instance (HardwareSerial, etc)
|
||||
};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial)
|
||||
;
|
||||
delay(100);
|
||||
|
||||
Serial.println("Generic Device test!");
|
||||
|
||||
// Initialize UART for device communication
|
||||
Serial1.begin(115200);
|
||||
|
||||
// Create UART wrapper and BusIO device
|
||||
UARTDevice uart(&Serial1);
|
||||
Adafruit_GenericDevice *device = uart.createDevice();
|
||||
device->begin();
|
||||
|
||||
// Test write/read cycle
|
||||
uint8_t write_buf[4] = {0x5, 0x0, 0x0, 0x48};
|
||||
uint8_t read_buf[8];
|
||||
|
||||
Serial.println("Writing data...");
|
||||
if (!device->write(write_buf, 4)) {
|
||||
Serial.println("Write failed!");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Reading response...");
|
||||
if (!device->read(read_buf, 8)) {
|
||||
Serial.println("Read failed!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Print response bytes
|
||||
Serial.print("Got response: ");
|
||||
for (int i = 0; i < 8; i++) {
|
||||
Serial.print("0x");
|
||||
Serial.print(read_buf[i], HEX);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() { delay(1000); }
|
@ -0,0 +1,22 @@
|
||||
#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,45 @@
|
||||
#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,43 @@
|
||||
#include <Adafruit_BusIO_Register.h>
|
||||
#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 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,40 @@
|
||||
#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,35 @@
|
||||
#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,43 @@
|
||||
#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,268 @@
|
||||
/***************************************************
|
||||
|
||||
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,40 @@
|
||||
#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.17.1
|
||||
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=*
|
5
.pio/libdeps/esp32-s3-devkitc-1/ArduinoHttpClient/.gitignore
vendored
Normal file
5
.pio/libdeps/esp32-s3-devkitc-1/ArduinoHttpClient/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.development
|
||||
examples/node_test_server/node_modules/
|
||||
*.DS_Store
|
||||
*/.DS_Store
|
||||
examples/.DS_Store
|
1
.pio/libdeps/esp32-s3-devkitc-1/ArduinoHttpClient/.piopm
Normal file
1
.pio/libdeps/esp32-s3-devkitc-1/ArduinoHttpClient/.piopm
Normal file
@ -0,0 +1 @@
|
||||
{"type": "library", "name": "ArduinoHttpClient", "version": "0.4.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 secrect-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
|
||||
|
25
.pio/libdeps/esp32-s3-devkitc-1/ArduinoHttpClient/README.md
Normal file
25
.pio/libdeps/esp32-s3-devkitc-1/ArduinoHttpClient/README.md
Normal file
@ -0,0 +1,25 @@
|
||||
# ArduinoHttpClient
|
||||
|
||||
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 wifi 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 custome 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,133 @@
|
||||
// (c) Copyright 2010-2012 MCQN Ltd.
|
||||
// Released under Apache License, version 2.0
|
||||
//
|
||||
// Simple example to show how to use the HttpClient library
|
||||
// Get's 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 HttpClient
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# 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,12 @@
|
||||
{
|
||||
"name": "ArduinoHttpClient",
|
||||
"keywords": "http, web, client, ethernet, wifi, GSM",
|
||||
"description": "Easily interact with web servers from Arduino, using HTTP and WebSocket's.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/arduino-libraries/ArduinoHttpClient.git"
|
||||
},
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"version": "0.4.0"
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
name=ArduinoHttpClient
|
||||
version=0.4.0
|
||||
author=Arduino
|
||||
maintainer=Arduino <info@arduino.cc>
|
||||
sentence=[EXPERIMENTAL] Easily interact with web servers from Arduino, using HTTP and WebSocket's.
|
||||
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,12 @@
|
||||
// Library to simplify HTTP fetching on Arduino
|
||||
// (c) Copyright Arduino. 2016
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#ifndef ArduinoHttpClient_h
|
||||
#define ArduinoHttpClient_h
|
||||
|
||||
#include "HttpClient.h"
|
||||
#include "WebSocketClient.h"
|
||||
#include "URLEncoder.h"
|
||||
|
||||
#endif
|
@ -0,0 +1,862 @@
|
||||
// Class to simplify HTTP fetching on Arduino
|
||||
// (c) Copyright 2010-2011 MCQN Ltd
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#include "HttpClient.h"
|
||||
#include "b64.h"
|
||||
|
||||
// Initialize constants
|
||||
const char* HttpClient::kUserAgent = "Arduino/2.2.0";
|
||||
const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
|
||||
const char* HttpClient::kTransferEncodingChunked = HTTP_HEADER_TRANSFER_ENCODING ": " HTTP_HEADER_VALUE_CHUNKED;
|
||||
|
||||
HttpClient::HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
|
||||
: iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort),
|
||||
iConnectionClose(true), iSendDefaultRequestHeaders(true)
|
||||
{
|
||||
resetState();
|
||||
}
|
||||
|
||||
HttpClient::HttpClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
|
||||
: HttpClient(aClient, aServerName.c_str(), aServerPort)
|
||||
{
|
||||
}
|
||||
|
||||
HttpClient::HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
|
||||
: iClient(&aClient), iServerName(NULL), iServerAddress(aServerAddress), iServerPort(aServerPort),
|
||||
iConnectionClose(true), iSendDefaultRequestHeaders(true)
|
||||
{
|
||||
resetState();
|
||||
}
|
||||
|
||||
void HttpClient::resetState()
|
||||
{
|
||||
iState = eIdle;
|
||||
iStatusCode = 0;
|
||||
iContentLength = kNoContentLengthHeader;
|
||||
iBodyLengthConsumed = 0;
|
||||
iContentLengthPtr = kContentLengthPrefix;
|
||||
iTransferEncodingChunkedPtr = kTransferEncodingChunked;
|
||||
iIsChunked = false;
|
||||
iChunkLength = 0;
|
||||
iHttpResponseTimeout = kHttpResponseTimeout;
|
||||
}
|
||||
|
||||
void HttpClient::stop()
|
||||
{
|
||||
iClient->stop();
|
||||
resetState();
|
||||
}
|
||||
|
||||
void HttpClient::connectionKeepAlive()
|
||||
{
|
||||
iConnectionClose = false;
|
||||
}
|
||||
|
||||
void HttpClient::noDefaultRequestHeaders()
|
||||
{
|
||||
iSendDefaultRequestHeaders = false;
|
||||
}
|
||||
|
||||
void HttpClient::beginRequest()
|
||||
{
|
||||
iState = eRequestStarted;
|
||||
}
|
||||
|
||||
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod,
|
||||
const char* aContentType, int aContentLength, const byte aBody[])
|
||||
{
|
||||
if (iState == eReadingBody || iState == eReadingChunkLength || iState == eReadingBodyChunk)
|
||||
{
|
||||
flushClientRx();
|
||||
|
||||
resetState();
|
||||
}
|
||||
|
||||
tHttpState initialState = iState;
|
||||
|
||||
if ((eIdle != iState) && (eRequestStarted != iState))
|
||||
{
|
||||
return HTTP_ERROR_API;
|
||||
}
|
||||
|
||||
if (iConnectionClose || !iClient->connected())
|
||||
{
|
||||
if (iServerName)
|
||||
{
|
||||
if (!iClient->connect(iServerName, iServerPort) > 0)
|
||||
{
|
||||
#ifdef LOGGING
|
||||
Serial.println("Connection failed");
|
||||
#endif
|
||||
return HTTP_ERROR_CONNECTION_FAILED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!iClient->connect(iServerAddress, iServerPort) > 0)
|
||||
{
|
||||
#ifdef LOGGING
|
||||
Serial.println("Connection failed");
|
||||
#endif
|
||||
return HTTP_ERROR_CONNECTION_FAILED;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef LOGGING
|
||||
Serial.println("Connection already open");
|
||||
#endif
|
||||
}
|
||||
|
||||
// Now we're connected, send the first part of the request
|
||||
int ret = sendInitialHeaders(aURLPath, aHttpMethod);
|
||||
|
||||
if (HTTP_SUCCESS == ret)
|
||||
{
|
||||
if (aContentType)
|
||||
{
|
||||
sendHeader(HTTP_HEADER_CONTENT_TYPE, aContentType);
|
||||
}
|
||||
|
||||
if (aContentLength > 0)
|
||||
{
|
||||
sendHeader(HTTP_HEADER_CONTENT_LENGTH, aContentLength);
|
||||
}
|
||||
|
||||
bool hasBody = (aBody && aContentLength > 0);
|
||||
|
||||
if (initialState == eIdle || hasBody)
|
||||
{
|
||||
// This was a simple version of the API, so terminate the headers now
|
||||
finishHeaders();
|
||||
}
|
||||
// else we'll call it in endRequest or in the first call to print, etc.
|
||||
|
||||
if (hasBody)
|
||||
{
|
||||
write(aBody, aContentLength);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod)
|
||||
{
|
||||
#ifdef LOGGING
|
||||
Serial.println("Connected");
|
||||
#endif
|
||||
// Send the HTTP command, i.e. "GET /somepath/ HTTP/1.0"
|
||||
iClient->print(aHttpMethod);
|
||||
iClient->print(" ");
|
||||
|
||||
iClient->print(aURLPath);
|
||||
iClient->println(" HTTP/1.1");
|
||||
if (iSendDefaultRequestHeaders)
|
||||
{
|
||||
// The host header, if required
|
||||
if (iServerName)
|
||||
{
|
||||
iClient->print("Host: ");
|
||||
iClient->print(iServerName);
|
||||
if (iServerPort != kHttpPort)
|
||||
{
|
||||
iClient->print(":");
|
||||
iClient->print(iServerPort);
|
||||
}
|
||||
iClient->println();
|
||||
}
|
||||
// And user-agent string
|
||||
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
|
||||
}
|
||||
|
||||
if (iConnectionClose)
|
||||
{
|
||||
// Tell the server to
|
||||
// close this connection after we're done
|
||||
sendHeader(HTTP_HEADER_CONNECTION, "close");
|
||||
}
|
||||
|
||||
// Everything has gone well
|
||||
iState = eRequestStarted;
|
||||
return HTTP_SUCCESS;
|
||||
}
|
||||
|
||||
void HttpClient::sendHeader(const char* aHeader)
|
||||
{
|
||||
iClient->println(aHeader);
|
||||
}
|
||||
|
||||
void HttpClient::sendHeader(const char* aHeaderName, const char* aHeaderValue)
|
||||
{
|
||||
iClient->print(aHeaderName);
|
||||
iClient->print(": ");
|
||||
iClient->println(aHeaderValue);
|
||||
}
|
||||
|
||||
void HttpClient::sendHeader(const char* aHeaderName, const int aHeaderValue)
|
||||
{
|
||||
iClient->print(aHeaderName);
|
||||
iClient->print(": ");
|
||||
iClient->println(aHeaderValue);
|
||||
}
|
||||
|
||||
void HttpClient::sendBasicAuth(const char* aUser, const char* aPassword)
|
||||
{
|
||||
// Send the initial part of this header line
|
||||
iClient->print("Authorization: Basic ");
|
||||
// Now Base64 encode "aUser:aPassword" and send that
|
||||
// This seems trickier than it should be but it's mostly to avoid either
|
||||
// (a) some arbitrarily sized buffer which hopes to be big enough, or
|
||||
// (b) allocating and freeing memory
|
||||
// ...so we'll loop through 3 bytes at a time, outputting the results as we
|
||||
// go.
|
||||
// In Base64, each 3 bytes of unencoded data become 4 bytes of encoded data
|
||||
unsigned char input[3];
|
||||
unsigned char output[5]; // Leave space for a '\0' terminator so we can easily print
|
||||
int userLen = strlen(aUser);
|
||||
int passwordLen = strlen(aPassword);
|
||||
int inputOffset = 0;
|
||||
for (int i = 0; i < (userLen+1+passwordLen); i++)
|
||||
{
|
||||
// Copy the relevant input byte into the input
|
||||
if (i < userLen)
|
||||
{
|
||||
input[inputOffset++] = aUser[i];
|
||||
}
|
||||
else if (i == userLen)
|
||||
{
|
||||
input[inputOffset++] = ':';
|
||||
}
|
||||
else
|
||||
{
|
||||
input[inputOffset++] = aPassword[i-(userLen+1)];
|
||||
}
|
||||
// See if we've got a chunk to encode
|
||||
if ( (inputOffset == 3) || (i == userLen+passwordLen) )
|
||||
{
|
||||
// We've either got to a 3-byte boundary, or we've reached then end
|
||||
b64_encode(input, inputOffset, output, 4);
|
||||
// NUL-terminate the output string
|
||||
output[4] = '\0';
|
||||
// And write it out
|
||||
iClient->print((char*)output);
|
||||
// FIXME We might want to fill output with '=' characters if b64_encode doesn't
|
||||
// FIXME do it for us when we're encoding the final chunk
|
||||
inputOffset = 0;
|
||||
}
|
||||
}
|
||||
// And end the header we've sent
|
||||
iClient->println();
|
||||
}
|
||||
|
||||
void HttpClient::finishHeaders()
|
||||
{
|
||||
iClient->println();
|
||||
iState = eRequestSent;
|
||||
}
|
||||
|
||||
void HttpClient::flushClientRx()
|
||||
{
|
||||
while (iClient->available())
|
||||
{
|
||||
iClient->read();
|
||||
}
|
||||
}
|
||||
|
||||
void HttpClient::endRequest()
|
||||
{
|
||||
beginBody();
|
||||
}
|
||||
|
||||
void HttpClient::beginBody()
|
||||
{
|
||||
if (iState < eRequestSent)
|
||||
{
|
||||
// We still need to finish off the headers
|
||||
finishHeaders();
|
||||
}
|
||||
// else the end of headers has already been sent, so nothing to do here
|
||||
}
|
||||
|
||||
int HttpClient::get(const char* aURLPath)
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_GET);
|
||||
}
|
||||
|
||||
int HttpClient::get(const String& aURLPath)
|
||||
{
|
||||
return get(aURLPath.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::post(const char* aURLPath)
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_POST);
|
||||
}
|
||||
|
||||
int HttpClient::post(const String& aURLPath)
|
||||
{
|
||||
return post(aURLPath.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::post(const char* aURLPath, const char* aContentType, const char* aBody)
|
||||
{
|
||||
return post(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
|
||||
}
|
||||
|
||||
int HttpClient::post(const String& aURLPath, const String& aContentType, const String& aBody)
|
||||
{
|
||||
return post(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_POST, aContentType, aContentLength, aBody);
|
||||
}
|
||||
|
||||
int HttpClient::put(const char* aURLPath)
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_PUT);
|
||||
}
|
||||
|
||||
int HttpClient::put(const String& aURLPath)
|
||||
{
|
||||
return put(aURLPath.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::put(const char* aURLPath, const char* aContentType, const char* aBody)
|
||||
{
|
||||
return put(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
|
||||
}
|
||||
|
||||
int HttpClient::put(const String& aURLPath, const String& aContentType, const String& aBody)
|
||||
{
|
||||
return put(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_PUT, aContentType, aContentLength, aBody);
|
||||
}
|
||||
|
||||
int HttpClient::patch(const char* aURLPath)
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_PATCH);
|
||||
}
|
||||
|
||||
int HttpClient::patch(const String& aURLPath)
|
||||
{
|
||||
return patch(aURLPath.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::patch(const char* aURLPath, const char* aContentType, const char* aBody)
|
||||
{
|
||||
return patch(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
|
||||
}
|
||||
|
||||
int HttpClient::patch(const String& aURLPath, const String& aContentType, const String& aBody)
|
||||
{
|
||||
return patch(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::patch(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_PATCH, aContentType, aContentLength, aBody);
|
||||
}
|
||||
|
||||
int HttpClient::del(const char* aURLPath)
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_DELETE);
|
||||
}
|
||||
|
||||
int HttpClient::del(const String& aURLPath)
|
||||
{
|
||||
return del(aURLPath.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::del(const char* aURLPath, const char* aContentType, const char* aBody)
|
||||
{
|
||||
return del(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
|
||||
}
|
||||
|
||||
int HttpClient::del(const String& aURLPath, const String& aContentType, const String& aBody)
|
||||
{
|
||||
return del(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
|
||||
}
|
||||
|
||||
int HttpClient::del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
|
||||
{
|
||||
return startRequest(aURLPath, HTTP_METHOD_DELETE, aContentType, aContentLength, aBody);
|
||||
}
|
||||
|
||||
int HttpClient::responseStatusCode()
|
||||
{
|
||||
if (iState < eRequestSent)
|
||||
{
|
||||
return HTTP_ERROR_API;
|
||||
}
|
||||
// The first line will be of the form Status-Line:
|
||||
// HTTP-Version SP Status-Code SP Reason-Phrase CRLF
|
||||
// Where HTTP-Version is of the form:
|
||||
// HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
|
||||
|
||||
int c = '\0';
|
||||
do
|
||||
{
|
||||
// Make sure the status code is reset, and likewise the state. This
|
||||
// lets us easily cope with 1xx informational responses by just
|
||||
// ignoring them really, and reading the next line for a proper response
|
||||
iStatusCode = 0;
|
||||
iState = eRequestSent;
|
||||
|
||||
unsigned long timeoutStart = millis();
|
||||
// Psuedo-regexp we're expecting before the status-code
|
||||
const char* statusPrefix = "HTTP/*.* ";
|
||||
const char* statusPtr = statusPrefix;
|
||||
// Whilst we haven't timed out & haven't reached the end of the headers
|
||||
while ((c != '\n') &&
|
||||
( (millis() - timeoutStart) < iHttpResponseTimeout ))
|
||||
{
|
||||
if (available())
|
||||
{
|
||||
c = read();
|
||||
if (c != -1)
|
||||
{
|
||||
switch(iState)
|
||||
{
|
||||
case eRequestSent:
|
||||
// We haven't reached the status code yet
|
||||
if ( (*statusPtr == '*') || (*statusPtr == c) )
|
||||
{
|
||||
// This character matches, just move along
|
||||
statusPtr++;
|
||||
if (*statusPtr == '\0')
|
||||
{
|
||||
// We've reached the end of the prefix
|
||||
iState = eReadingStatusCode;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return HTTP_ERROR_INVALID_RESPONSE;
|
||||
}
|
||||
break;
|
||||
case eReadingStatusCode:
|
||||
if (isdigit(c))
|
||||
{
|
||||
// This assumes we won't get more than the 3 digits we
|
||||
// want
|
||||
iStatusCode = iStatusCode*10 + (c - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
// We've reached the end of the status code
|
||||
// We could sanity check it here or double-check for ' '
|
||||
// rather than anything else, but let's be lenient
|
||||
iState = eStatusCodeRead;
|
||||
}
|
||||
break;
|
||||
case eStatusCodeRead:
|
||||
// We're just waiting for the end of the line now
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
// 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(kHttpWaitForDataDelay);
|
||||
}
|
||||
}
|
||||
if ( (c == '\n') && (iStatusCode < 200 && iStatusCode != 101) )
|
||||
{
|
||||
// We've reached the end of an informational status line
|
||||
c = '\0'; // Clear c so we'll go back into the data reading loop
|
||||
}
|
||||
}
|
||||
// If we've read a status code successfully but it's informational (1xx)
|
||||
// loop back to the start
|
||||
while ( (iState == eStatusCodeRead) && (iStatusCode < 200 && iStatusCode != 101) );
|
||||
|
||||
if ( (c == '\n') && (iState == eStatusCodeRead) )
|
||||
{
|
||||
// We've read the status-line successfully
|
||||
return iStatusCode;
|
||||
}
|
||||
else if (c != '\n')
|
||||
{
|
||||
// We must've timed out before we reached the end of the line
|
||||
return HTTP_ERROR_TIMED_OUT;
|
||||
}
|
||||
else
|
||||
{
|
||||
// This wasn't a properly formed status line, or at least not one we
|
||||
// could understand
|
||||
return HTTP_ERROR_INVALID_RESPONSE;
|
||||
}
|
||||
}
|
||||
|
||||
int HttpClient::skipResponseHeaders()
|
||||
{
|
||||
// Just keep reading until we finish reading the headers or time out
|
||||
unsigned long timeoutStart = millis();
|
||||
// Whilst we haven't timed out & haven't reached the end of the headers
|
||||
while ((!endOfHeadersReached()) &&
|
||||
( (millis() - timeoutStart) < iHttpResponseTimeout ))
|
||||
{
|
||||
if (available())
|
||||
{
|
||||
(void)readHeader();
|
||||
// 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(kHttpWaitForDataDelay);
|
||||
}
|
||||
}
|
||||
if (endOfHeadersReached())
|
||||
{
|
||||
// Success
|
||||
return HTTP_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We must've timed out
|
||||
return HTTP_ERROR_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
bool HttpClient::endOfHeadersReached()
|
||||
{
|
||||
return (iState == eReadingBody || iState == eReadingChunkLength || iState == eReadingBodyChunk);
|
||||
};
|
||||
|
||||
int HttpClient::contentLength()
|
||||
{
|
||||
// skip the response headers, if they haven't been read already
|
||||
if (!endOfHeadersReached())
|
||||
{
|
||||
skipResponseHeaders();
|
||||
}
|
||||
|
||||
return iContentLength;
|
||||
}
|
||||
|
||||
String HttpClient::responseBody()
|
||||
{
|
||||
int bodyLength = contentLength();
|
||||
String response;
|
||||
|
||||
if (bodyLength > 0)
|
||||
{
|
||||
// try to reserve bodyLength bytes
|
||||
if (response.reserve(bodyLength) == 0) {
|
||||
// String reserve failed
|
||||
return String((const char*)NULL);
|
||||
}
|
||||
}
|
||||
|
||||
// keep on timedRead'ing, until:
|
||||
// - we have a content length: body length equals consumed or no bytes
|
||||
// available
|
||||
// - no content length: no bytes are available
|
||||
while (iBodyLengthConsumed != bodyLength)
|
||||
{
|
||||
int c = timedRead();
|
||||
|
||||
if (c == -1) {
|
||||
// read timed out, done
|
||||
break;
|
||||
}
|
||||
|
||||
if (!response.concat((char)c)) {
|
||||
// adding char failed
|
||||
return String((const char*)NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if (bodyLength > 0 && (unsigned int)bodyLength != response.length()) {
|
||||
// failure, we did not read in reponse content length bytes
|
||||
return String((const char*)NULL);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
bool HttpClient::endOfBodyReached()
|
||||
{
|
||||
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))
|
||||
{
|
||||
// We've got to the body and we know how long it will be
|
||||
return (iBodyLengthConsumed >= contentLength());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int HttpClient::available()
|
||||
{
|
||||
if (iState == eReadingChunkLength)
|
||||
{
|
||||
while (iClient->available())
|
||||
{
|
||||
char c = iClient->read();
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
iState = eReadingBodyChunk;
|
||||
break;
|
||||
}
|
||||
else if (c == '\r')
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
else if (isHexadecimalDigit(c))
|
||||
{
|
||||
char digit[2] = {c, '\0'};
|
||||
|
||||
iChunkLength = (iChunkLength * 16) + strtol(digit, NULL, 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (iState == eReadingBodyChunk && iChunkLength == 0)
|
||||
{
|
||||
iState = eReadingChunkLength;
|
||||
}
|
||||
|
||||
if (iState == eReadingChunkLength)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int clientAvailable = iClient->available();
|
||||
|
||||
if (iState == eReadingBodyChunk)
|
||||
{
|
||||
return min(clientAvailable, iChunkLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return clientAvailable;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int HttpClient::read()
|
||||
{
|
||||
if (iIsChunked && !available())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = iClient->read();
|
||||
if (ret >= 0)
|
||||
{
|
||||
if (endOfHeadersReached() && iContentLength > 0)
|
||||
{
|
||||
// We're outputting the body now and we've seen a Content-Length header
|
||||
// So keep track of how many bytes are left
|
||||
iBodyLengthConsumed++;
|
||||
}
|
||||
|
||||
if (iState == eReadingBodyChunk)
|
||||
{
|
||||
iChunkLength--;
|
||||
|
||||
if (iChunkLength == 0)
|
||||
{
|
||||
iState = eReadingChunkLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool HttpClient::headerAvailable()
|
||||
{
|
||||
// clear the currently store header line
|
||||
iHeaderLine = "";
|
||||
|
||||
while (!endOfHeadersReached())
|
||||
{
|
||||
// read a byte from the header
|
||||
int c = readHeader();
|
||||
|
||||
if (c == '\r' || c == '\n')
|
||||
{
|
||||
if (iHeaderLine.length())
|
||||
{
|
||||
// end of the line, all done
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// ignore any CR or LF characters
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// append byte to header line
|
||||
iHeaderLine += (char)c;
|
||||
}
|
||||
|
||||
return (iHeaderLine.length() > 0);
|
||||
}
|
||||
|
||||
String HttpClient::readHeaderName()
|
||||
{
|
||||
int colonIndex = iHeaderLine.indexOf(':');
|
||||
|
||||
if (colonIndex == -1)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return iHeaderLine.substring(0, colonIndex);
|
||||
}
|
||||
|
||||
String HttpClient::readHeaderValue()
|
||||
{
|
||||
int colonIndex = iHeaderLine.indexOf(':');
|
||||
int startIndex = colonIndex + 1;
|
||||
|
||||
if (colonIndex == -1)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
// trim any leading whitespace
|
||||
while (startIndex < (int)iHeaderLine.length() && isSpace(iHeaderLine[startIndex]))
|
||||
{
|
||||
startIndex++;
|
||||
}
|
||||
|
||||
return iHeaderLine.substring(startIndex);
|
||||
}
|
||||
|
||||
int HttpClient::read(uint8_t *buf, size_t size)
|
||||
{
|
||||
int ret =iClient->read(buf, size);
|
||||
if (endOfHeadersReached() && iContentLength > 0)
|
||||
{
|
||||
// We're outputting the body now and we've seen a Content-Length header
|
||||
// So keep track of how many bytes are left
|
||||
if (ret >= 0)
|
||||
{
|
||||
iBodyLengthConsumed += ret;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HttpClient::readHeader()
|
||||
{
|
||||
char c = read();
|
||||
|
||||
if (endOfHeadersReached())
|
||||
{
|
||||
// We've passed the headers, but rather than return an error, we'll just
|
||||
// act as a slightly less efficient version of read()
|
||||
return c;
|
||||
}
|
||||
|
||||
// Whilst reading out the headers to whoever wants them, we'll keep an
|
||||
// eye out for the "Content-Length" header
|
||||
switch(iState)
|
||||
{
|
||||
case eStatusCodeRead:
|
||||
// We're at the start of a line, or somewhere in the middle of reading
|
||||
// the Content-Length prefix
|
||||
if (*iContentLengthPtr == c)
|
||||
{
|
||||
// This character matches, just move along
|
||||
iContentLengthPtr++;
|
||||
if (*iContentLengthPtr == '\0')
|
||||
{
|
||||
// We've reached the end of the prefix
|
||||
iState = eReadingContentLength;
|
||||
// Just in case we get multiple Content-Length headers, this
|
||||
// will ensure we just get the value of the last one
|
||||
iContentLength = 0;
|
||||
iBodyLengthConsumed = 0;
|
||||
}
|
||||
}
|
||||
else if (*iTransferEncodingChunkedPtr == c)
|
||||
{
|
||||
// This character matches, just move along
|
||||
iTransferEncodingChunkedPtr++;
|
||||
if (*iTransferEncodingChunkedPtr == '\0')
|
||||
{
|
||||
// We've reached the end of the Transfer Encoding: chunked header
|
||||
iIsChunked = true;
|
||||
iState = eSkipToEndOfHeader;
|
||||
}
|
||||
}
|
||||
else if (((iContentLengthPtr == kContentLengthPrefix) && (iTransferEncodingChunkedPtr == kTransferEncodingChunked)) && (c == '\r'))
|
||||
{
|
||||
// We've found a '\r' at the start of a line, so this is probably
|
||||
// the end of the headers
|
||||
iState = eLineStartingCRFound;
|
||||
}
|
||||
else
|
||||
{
|
||||
// This isn't the Content-Length or Transfer Encoding chunked header, skip to the end of the line
|
||||
iState = eSkipToEndOfHeader;
|
||||
}
|
||||
break;
|
||||
case eReadingContentLength:
|
||||
if (isdigit(c))
|
||||
{
|
||||
iContentLength = iContentLength*10 + (c - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
// We've reached the end of the content length
|
||||
// We could sanity check it here or double-check for "\r\n"
|
||||
// rather than anything else, but let's be lenient
|
||||
iState = eSkipToEndOfHeader;
|
||||
}
|
||||
break;
|
||||
case eLineStartingCRFound:
|
||||
if (c == '\n')
|
||||
{
|
||||
if (iIsChunked)
|
||||
{
|
||||
iState = eReadingChunkLength;
|
||||
iChunkLength = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
iState = eReadingBody;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// We're just waiting for the end of the line now
|
||||
break;
|
||||
};
|
||||
|
||||
if ( (c == '\n') && !endOfHeadersReached() )
|
||||
{
|
||||
// We've got to the end of this line, start processing again
|
||||
iState = eStatusCodeRead;
|
||||
iContentLengthPtr = kContentLengthPrefix;
|
||||
iTransferEncodingChunkedPtr = kTransferEncodingChunked;
|
||||
}
|
||||
// And return the character read to whoever wants it
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,392 @@
|
||||
// Class to simplify HTTP fetching on Arduino
|
||||
// (c) Copyright MCQN Ltd. 2010-2012
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#ifndef HttpClient_h
|
||||
#define HttpClient_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <IPAddress.h>
|
||||
#include "Client.h"
|
||||
|
||||
static const int HTTP_SUCCESS =0;
|
||||
// The end of the headers has been reached. This consumes the '\n'
|
||||
// Could not connect to the server
|
||||
static const int HTTP_ERROR_CONNECTION_FAILED =-1;
|
||||
// This call was made when the HttpClient class wasn't expecting it
|
||||
// to be called. Usually indicates your code is using the class
|
||||
// incorrectly
|
||||
static const int HTTP_ERROR_API =-2;
|
||||
// Spent too long waiting for a reply
|
||||
static const int HTTP_ERROR_TIMED_OUT =-3;
|
||||
// The response from the server is invalid, is it definitely an HTTP
|
||||
// server?
|
||||
static const int HTTP_ERROR_INVALID_RESPONSE =-4;
|
||||
|
||||
// Define some of the common methods and headers here
|
||||
// That lets other code reuse them without having to declare another copy
|
||||
// of them, so saves code space and RAM
|
||||
#define HTTP_METHOD_GET "GET"
|
||||
#define HTTP_METHOD_POST "POST"
|
||||
#define HTTP_METHOD_PUT "PUT"
|
||||
#define HTTP_METHOD_PATCH "PATCH"
|
||||
#define HTTP_METHOD_DELETE "DELETE"
|
||||
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
|
||||
#define HTTP_HEADER_CONTENT_TYPE "Content-Type"
|
||||
#define HTTP_HEADER_CONNECTION "Connection"
|
||||
#define HTTP_HEADER_TRANSFER_ENCODING "Transfer-Encoding"
|
||||
#define HTTP_HEADER_USER_AGENT "User-Agent"
|
||||
#define HTTP_HEADER_VALUE_CHUNKED "chunked"
|
||||
|
||||
class HttpClient : public Client
|
||||
{
|
||||
public:
|
||||
static const int kNoContentLengthHeader =-1;
|
||||
static const int kHttpPort =80;
|
||||
static const char* kUserAgent;
|
||||
|
||||
// FIXME Write longer API request, using port and user-agent, example
|
||||
// FIXME Update tempToPachube example to calculate Content-Length correctly
|
||||
|
||||
HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort = kHttpPort);
|
||||
HttpClient(Client& aClient, const String& aServerName, uint16_t aServerPort = kHttpPort);
|
||||
HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort = kHttpPort);
|
||||
|
||||
/** Start a more complex request.
|
||||
Use this when you need to send additional headers in the request,
|
||||
but you will also need to call endRequest() when you are finished.
|
||||
*/
|
||||
void beginRequest();
|
||||
|
||||
/** End a more complex request.
|
||||
Use this when you need to have sent additional headers in the request,
|
||||
but you will also need to call beginRequest() at the start.
|
||||
*/
|
||||
void endRequest();
|
||||
|
||||
/** Start the body of a more complex request.
|
||||
Use this when you need to send the body after additional headers
|
||||
in the request, but can optionally call endRequest() when
|
||||
you are finished.
|
||||
*/
|
||||
void beginBody();
|
||||
|
||||
/** Connect to the server and start to send a GET request.
|
||||
@param aURLPath Url to request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int get(const char* aURLPath);
|
||||
int get(const String& aURLPath);
|
||||
|
||||
/** Connect to the server and start to send a POST request.
|
||||
@param aURLPath Url to request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int post(const char* aURLPath);
|
||||
int post(const String& aURLPath);
|
||||
|
||||
/** Connect to the server and send a POST request
|
||||
with body and content type
|
||||
@param aURLPath Url to request
|
||||
@param aContentType Content type of request body
|
||||
@param aBody Body of the request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int post(const char* aURLPath, const char* aContentType, const char* aBody);
|
||||
int post(const String& aURLPath, const String& aContentType, const String& aBody);
|
||||
int post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
|
||||
|
||||
/** Connect to the server and start to send a PUT request.
|
||||
@param aURLPath Url to request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int put(const char* aURLPath);
|
||||
int put(const String& aURLPath);
|
||||
|
||||
/** Connect to the server and send a PUT request
|
||||
with body and content type
|
||||
@param aURLPath Url to request
|
||||
@param aContentType Content type of request body
|
||||
@param aBody Body of the request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int put(const char* aURLPath, const char* aContentType, const char* aBody);
|
||||
int put(const String& aURLPath, const String& aContentType, const String& aBody);
|
||||
int put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
|
||||
|
||||
/** Connect to the server and start to send a PATCH request.
|
||||
@param aURLPath Url to request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int patch(const char* aURLPath);
|
||||
int patch(const String& aURLPath);
|
||||
|
||||
/** Connect to the server and send a PATCH request
|
||||
with body and content type
|
||||
@param aURLPath Url to request
|
||||
@param aContentType Content type of request body
|
||||
@param aBody Body of the request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int patch(const char* aURLPath, const char* aContentType, const char* aBody);
|
||||
int patch(const String& aURLPath, const String& aContentType, const String& aBody);
|
||||
int patch(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
|
||||
|
||||
/** Connect to the server and start to send a DELETE request.
|
||||
@param aURLPath Url to request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int del(const char* aURLPath);
|
||||
int del(const String& aURLPath);
|
||||
|
||||
/** Connect to the server and send a DELETE request
|
||||
with body and content type
|
||||
@param aURLPath Url to request
|
||||
@param aContentType Content type of request body
|
||||
@param aBody Body of the request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int del(const char* aURLPath, const char* aContentType, const char* aBody);
|
||||
int del(const String& aURLPath, const String& aContentType, const String& aBody);
|
||||
int del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
|
||||
|
||||
/** Connect to the server and start to send the request.
|
||||
If a body is provided, the entire request (including headers and body) will be sent
|
||||
@param aURLPath Url to request
|
||||
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
|
||||
@param aContentType Content type of request body (optional)
|
||||
@param aContentLength Length of request body (optional)
|
||||
@param aBody Body of request (optional)
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int startRequest(const char* aURLPath,
|
||||
const char* aHttpMethod,
|
||||
const char* aContentType = NULL,
|
||||
int aContentLength = -1,
|
||||
const byte aBody[] = NULL);
|
||||
|
||||
/** Send an additional header line. This can only be called in between the
|
||||
calls to beginRequest and endRequest.
|
||||
@param aHeader Header line to send, in its entirety (but without the
|
||||
trailing CRLF. E.g. "Authorization: Basic YQDDCAIGES"
|
||||
*/
|
||||
void sendHeader(const char* aHeader);
|
||||
|
||||
void sendHeader(const String& aHeader)
|
||||
{ sendHeader(aHeader.c_str()); }
|
||||
|
||||
/** Send an additional header line. This is an alternate form of
|
||||
sendHeader() which takes the header name and content as separate strings.
|
||||
The call will add the ": " to separate the header, so for example, to
|
||||
send a XXXXXX header call sendHeader("XXXXX", "Something")
|
||||
@param aHeaderName Type of header being sent
|
||||
@param aHeaderValue Value for that header
|
||||
*/
|
||||
void sendHeader(const char* aHeaderName, const char* aHeaderValue);
|
||||
|
||||
void sendHeader(const String& aHeaderName, const String& aHeaderValue)
|
||||
{ sendHeader(aHeaderName.c_str(), aHeaderValue.c_str()); }
|
||||
|
||||
/** Send an additional header line. This is an alternate form of
|
||||
sendHeader() which takes the header name and content separately but where
|
||||
the value is provided as an integer.
|
||||
The call will add the ": " to separate the header, so for example, to
|
||||
send a XXXXXX header call sendHeader("XXXXX", 123)
|
||||
@param aHeaderName Type of header being sent
|
||||
@param aHeaderValue Value for that header
|
||||
*/
|
||||
void sendHeader(const char* aHeaderName, const int aHeaderValue);
|
||||
|
||||
void sendHeader(const String& aHeaderName, const int aHeaderValue)
|
||||
{ sendHeader(aHeaderName.c_str(), aHeaderValue); }
|
||||
|
||||
/** Send a basic authentication header. This will encode the given username
|
||||
and password, and send them in suitable header line for doing Basic
|
||||
Authentication.
|
||||
@param aUser Username for the authorization
|
||||
@param aPassword Password for the user aUser
|
||||
*/
|
||||
void sendBasicAuth(const char* aUser, const char* aPassword);
|
||||
|
||||
void sendBasicAuth(const String& aUser, const String& aPassword)
|
||||
{ sendBasicAuth(aUser.c_str(), aPassword.c_str()); }
|
||||
|
||||
/** Get the HTTP status code contained in the response.
|
||||
For example, 200 for successful request, 404 for file not found, etc.
|
||||
*/
|
||||
int responseStatusCode();
|
||||
|
||||
/** Check if a header is available to be read.
|
||||
Use readHeaderName() to read header name, and readHeaderValue() to
|
||||
read the header value
|
||||
MUST be called after responseStatusCode() and before contentLength()
|
||||
*/
|
||||
bool headerAvailable();
|
||||
|
||||
/** Read the name of the current response header.
|
||||
Returns empty string if a header is not available.
|
||||
*/
|
||||
String readHeaderName();
|
||||
|
||||
/** Read the vallue of the current response header.
|
||||
Returns empty string if a header is not available.
|
||||
*/
|
||||
String readHeaderValue();
|
||||
|
||||
/** Read the next character of the response headers.
|
||||
This functions in the same way as read() but to be used when reading
|
||||
through the headers. Check whether or not the end of the headers has
|
||||
been reached by calling endOfHeadersReached(), although after that point
|
||||
this will still return data as read() would, but slightly less efficiently
|
||||
MUST be called after responseStatusCode() and before contentLength()
|
||||
@return The next character of the response headers
|
||||
*/
|
||||
int readHeader();
|
||||
|
||||
/** Skip any response headers to get to the body.
|
||||
Use this if you don't want to do any special processing of the headers
|
||||
returned in the response. You can also use it after you've found all of
|
||||
the headers you're interested in, and just want to get on with processing
|
||||
the body.
|
||||
MUST be called after responseStatusCode()
|
||||
@return HTTP_SUCCESS if successful, else an error code
|
||||
*/
|
||||
int skipResponseHeaders();
|
||||
|
||||
/** Test whether all of the response headers have been consumed.
|
||||
@return true if we are now processing the response body, else false
|
||||
*/
|
||||
bool endOfHeadersReached();
|
||||
|
||||
/** Test whether the end of the body has been reached.
|
||||
Only works if the Content-Length header was returned by the server
|
||||
@return true if we are now at the end of the body, else false
|
||||
*/
|
||||
bool endOfBodyReached();
|
||||
virtual bool endOfStream() { return endOfBodyReached(); };
|
||||
virtual bool completed() { return endOfBodyReached(); };
|
||||
|
||||
/** Return the length of the body.
|
||||
Also skips response headers if they have not been read already
|
||||
MUST be called after responseStatusCode()
|
||||
@return Length of the body, in bytes, or kNoContentLengthHeader if no
|
||||
Content-Length header was returned by the server
|
||||
*/
|
||||
int contentLength();
|
||||
|
||||
/** Returns if the response body is chunked
|
||||
@return true if response body is chunked, false otherwise
|
||||
*/
|
||||
int isResponseChunked() { return iIsChunked; }
|
||||
|
||||
/** Return the response body as a String
|
||||
Also skips response headers if they have not been read already
|
||||
MUST be called after responseStatusCode()
|
||||
@return response body of request as a String
|
||||
*/
|
||||
String responseBody();
|
||||
|
||||
/** Enables connection keep-alive mode
|
||||
*/
|
||||
void connectionKeepAlive();
|
||||
|
||||
/** Disables sending the default request headers (Host and User Agent)
|
||||
*/
|
||||
void noDefaultRequestHeaders();
|
||||
|
||||
// Inherited from Print
|
||||
// Note: 1st call to these indicates the user is sending the body, so if need
|
||||
// Note: be we should finish the header first
|
||||
virtual size_t write(uint8_t aByte) { if (iState < eRequestSent) { finishHeaders(); }; return iClient-> write(aByte); };
|
||||
virtual size_t write(const uint8_t *aBuffer, size_t aSize) { if (iState < eRequestSent) { finishHeaders(); }; return iClient->write(aBuffer, aSize); };
|
||||
// Inherited from Stream
|
||||
virtual int available();
|
||||
/** Read the next byte from the server.
|
||||
@return Byte read or -1 if there are no bytes available.
|
||||
*/
|
||||
virtual int read();
|
||||
virtual int read(uint8_t *buf, size_t size);
|
||||
virtual int peek() { return iClient->peek(); };
|
||||
virtual void flush() { iClient->flush(); };
|
||||
|
||||
// Inherited from Client
|
||||
virtual int connect(IPAddress ip, uint16_t port) { return iClient->connect(ip, port); };
|
||||
virtual int connect(const char *host, uint16_t port) { return iClient->connect(host, port); };
|
||||
virtual void stop();
|
||||
virtual uint8_t connected() { return iClient->connected(); };
|
||||
virtual operator bool() { return bool(iClient); };
|
||||
virtual uint32_t httpResponseTimeout() { return iHttpResponseTimeout; };
|
||||
virtual void setHttpResponseTimeout(uint32_t timeout) { iHttpResponseTimeout = timeout; };
|
||||
protected:
|
||||
/** Reset internal state data back to the "just initialised" state
|
||||
*/
|
||||
void resetState();
|
||||
|
||||
/** Send the first part of the request and the initial headers.
|
||||
@param aURLPath Url to request
|
||||
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int sendInitialHeaders(const char* aURLPath,
|
||||
const char* aHttpMethod);
|
||||
|
||||
/* Let the server know that we've reached the end of the headers
|
||||
*/
|
||||
void finishHeaders();
|
||||
|
||||
/** Reading any pending data from the client (used in connection keep alive mode)
|
||||
*/
|
||||
void flushClientRx();
|
||||
|
||||
// Number of milliseconds that we wait each time there isn't any data
|
||||
// available to be read (during status code and header processing)
|
||||
static const int kHttpWaitForDataDelay = 1000;
|
||||
// Number of milliseconds that we'll wait in total without receiveing any
|
||||
// data before returning HTTP_ERROR_TIMED_OUT (during status code and header
|
||||
// processing)
|
||||
static const int kHttpResponseTimeout = 30*1000;
|
||||
static const char* kContentLengthPrefix;
|
||||
static const char* kTransferEncodingChunked;
|
||||
typedef enum {
|
||||
eIdle,
|
||||
eRequestStarted,
|
||||
eRequestSent,
|
||||
eReadingStatusCode,
|
||||
eStatusCodeRead,
|
||||
eReadingContentLength,
|
||||
eSkipToEndOfHeader,
|
||||
eLineStartingCRFound,
|
||||
eReadingBody,
|
||||
eReadingChunkLength,
|
||||
eReadingBodyChunk
|
||||
} tHttpState;
|
||||
// Client we're using
|
||||
Client* iClient;
|
||||
// Server we are connecting to
|
||||
const char* iServerName;
|
||||
IPAddress iServerAddress;
|
||||
// Port of server we are connecting to
|
||||
uint16_t iServerPort;
|
||||
// Current state of the finite-state-machine
|
||||
tHttpState iState;
|
||||
// Stores the status code for the response, once known
|
||||
int iStatusCode;
|
||||
// Stores the value of the Content-Length header, if present
|
||||
int iContentLength;
|
||||
// How many bytes of the response body have been read by the user
|
||||
int iBodyLengthConsumed;
|
||||
// How far through a Content-Length header prefix we are
|
||||
const char* iContentLengthPtr;
|
||||
// How far through a Transfer-Encoding chunked header we are
|
||||
const char* iTransferEncodingChunkedPtr;
|
||||
// Stores if the response body is chunked
|
||||
bool iIsChunked;
|
||||
// Stores the value of the current chunk length, if present
|
||||
int iChunkLength;
|
||||
uint32_t iHttpResponseTimeout;
|
||||
bool iConnectionClose;
|
||||
bool iSendDefaultRequestHeaders;
|
||||
String iHeaderLine;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,53 @@
|
||||
// Library to simplify HTTP fetching on Arduino
|
||||
// (c) Copyright Arduino. 2019
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#include "URLEncoder.h"
|
||||
|
||||
URLEncoderClass::URLEncoderClass()
|
||||
{
|
||||
}
|
||||
|
||||
URLEncoderClass::~URLEncoderClass()
|
||||
{
|
||||
}
|
||||
|
||||
String URLEncoderClass::encode(const char* str)
|
||||
{
|
||||
return encode(str, strlen(str));
|
||||
}
|
||||
|
||||
String URLEncoderClass::encode(const String& str)
|
||||
{
|
||||
return encode(str.c_str(), str.length());
|
||||
}
|
||||
|
||||
String URLEncoderClass::encode(const char* str, int length)
|
||||
{
|
||||
String encoded;
|
||||
|
||||
encoded.reserve(length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
char c = str[i];
|
||||
|
||||
const char HEX_DIGIT_MAPPER[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
|
||||
if (isAlphaNumeric(c) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) {
|
||||
encoded += c;
|
||||
} else {
|
||||
char s[4];
|
||||
|
||||
s[0] = '%';
|
||||
s[1] = HEX_DIGIT_MAPPER[(c >> 4) & 0xf];
|
||||
s[2] = HEX_DIGIT_MAPPER[(c & 0x0f)];
|
||||
s[3] = 0;
|
||||
|
||||
encoded += s;
|
||||
}
|
||||
}
|
||||
|
||||
return encoded;
|
||||
}
|
||||
|
||||
URLEncoderClass URLEncoder;
|
@ -0,0 +1,25 @@
|
||||
// Library to simplify HTTP fetching on Arduino
|
||||
// (c) Copyright Arduino. 2019
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#ifndef URL_ENCODER_H
|
||||
#define URL_ENCODER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class URLEncoderClass
|
||||
{
|
||||
public:
|
||||
URLEncoderClass();
|
||||
virtual ~URLEncoderClass();
|
||||
|
||||
static String encode(const char* str);
|
||||
static String encode(const String& str);
|
||||
|
||||
private:
|
||||
static String encode(const char* str, int length);
|
||||
};
|
||||
|
||||
extern URLEncoderClass URLEncoder;
|
||||
|
||||
#endif
|
@ -0,0 +1,372 @@
|
||||
// (c) Copyright Arduino. 2016
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#include "b64.h"
|
||||
|
||||
#include "WebSocketClient.h"
|
||||
|
||||
WebSocketClient::WebSocketClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
|
||||
: HttpClient(aClient, aServerName, aServerPort),
|
||||
iTxStarted(false),
|
||||
iRxSize(0)
|
||||
{
|
||||
}
|
||||
|
||||
WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
|
||||
: HttpClient(aClient, aServerName, aServerPort),
|
||||
iTxStarted(false),
|
||||
iRxSize(0)
|
||||
{
|
||||
}
|
||||
|
||||
WebSocketClient::WebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
|
||||
: HttpClient(aClient, aServerAddress, aServerPort),
|
||||
iTxStarted(false),
|
||||
iRxSize(0)
|
||||
{
|
||||
}
|
||||
|
||||
int WebSocketClient::begin(const char* aPath)
|
||||
{
|
||||
// start the GET request
|
||||
beginRequest();
|
||||
connectionKeepAlive();
|
||||
int status = get(aPath);
|
||||
|
||||
if (status == 0)
|
||||
{
|
||||
uint8_t randomKey[16];
|
||||
char base64RandomKey[25];
|
||||
|
||||
// create a random key for the connection upgrade
|
||||
for (int i = 0; i < (int)sizeof(randomKey); i++)
|
||||
{
|
||||
randomKey[i] = random(0x01, 0xff);
|
||||
}
|
||||
memset(base64RandomKey, 0x00, sizeof(base64RandomKey));
|
||||
b64_encode(randomKey, sizeof(randomKey), (unsigned char*)base64RandomKey, sizeof(base64RandomKey));
|
||||
|
||||
// start the connection upgrade sequence
|
||||
sendHeader("Upgrade", "websocket");
|
||||
sendHeader("Connection", "Upgrade");
|
||||
sendHeader("Sec-WebSocket-Key", base64RandomKey);
|
||||
sendHeader("Sec-WebSocket-Version", "13");
|
||||
endRequest();
|
||||
|
||||
status = responseStatusCode();
|
||||
|
||||
if (status > 0)
|
||||
{
|
||||
skipResponseHeaders();
|
||||
}
|
||||
}
|
||||
|
||||
iRxSize = 0;
|
||||
|
||||
// status code of 101 means success
|
||||
return (status == 101) ? 0 : status;
|
||||
}
|
||||
|
||||
int WebSocketClient::begin(const String& aPath)
|
||||
{
|
||||
return begin(aPath.c_str());
|
||||
}
|
||||
|
||||
int WebSocketClient::beginMessage(int aType)
|
||||
{
|
||||
if (iTxStarted)
|
||||
{
|
||||
// fail TX already started
|
||||
return 1;
|
||||
}
|
||||
|
||||
iTxStarted = true;
|
||||
iTxMessageType = (aType & 0xf);
|
||||
iTxSize = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WebSocketClient::endMessage()
|
||||
{
|
||||
if (!iTxStarted)
|
||||
{
|
||||
// fail TX not started
|
||||
return 1;
|
||||
}
|
||||
|
||||
// send FIN + the message type (opcode)
|
||||
HttpClient::write(0x80 | iTxMessageType);
|
||||
|
||||
// the message is masked (0x80)
|
||||
// send the length
|
||||
if (iTxSize < 126)
|
||||
{
|
||||
HttpClient::write(0x80 | (uint8_t)iTxSize);
|
||||
}
|
||||
else if (iTxSize < 0xffff)
|
||||
{
|
||||
HttpClient::write(0x80 | 126);
|
||||
HttpClient::write((iTxSize >> 8) & 0xff);
|
||||
HttpClient::write((iTxSize >> 0) & 0xff);
|
||||
}
|
||||
else
|
||||
{
|
||||
HttpClient::write(0x80 | 127);
|
||||
HttpClient::write((iTxSize >> 56) & 0xff);
|
||||
HttpClient::write((iTxSize >> 48) & 0xff);
|
||||
HttpClient::write((iTxSize >> 40) & 0xff);
|
||||
HttpClient::write((iTxSize >> 32) & 0xff);
|
||||
HttpClient::write((iTxSize >> 24) & 0xff);
|
||||
HttpClient::write((iTxSize >> 16) & 0xff);
|
||||
HttpClient::write((iTxSize >> 8) & 0xff);
|
||||
HttpClient::write((iTxSize >> 0) & 0xff);
|
||||
}
|
||||
|
||||
uint8_t maskKey[4];
|
||||
|
||||
// create a random mask for the data and send
|
||||
for (int i = 0; i < (int)sizeof(maskKey); i++)
|
||||
{
|
||||
maskKey[i] = random(0xff);
|
||||
}
|
||||
HttpClient::write(maskKey, sizeof(maskKey));
|
||||
|
||||
// mask the data and send
|
||||
for (int i = 0; i < (int)iTxSize; i++) {
|
||||
iTxBuffer[i] ^= maskKey[i % sizeof(maskKey)];
|
||||
}
|
||||
|
||||
size_t txSize = iTxSize;
|
||||
|
||||
iTxStarted = false;
|
||||
iTxSize = 0;
|
||||
|
||||
return (HttpClient::write(iTxBuffer, txSize) == txSize) ? 0 : 1;
|
||||
}
|
||||
|
||||
size_t WebSocketClient::write(uint8_t aByte)
|
||||
{
|
||||
return write(&aByte, sizeof(aByte));
|
||||
}
|
||||
|
||||
size_t WebSocketClient::write(const uint8_t *aBuffer, size_t aSize)
|
||||
{
|
||||
if (iState < eReadingBody)
|
||||
{
|
||||
// have not upgraded the connection yet
|
||||
return HttpClient::write(aBuffer, aSize);
|
||||
}
|
||||
|
||||
if (!iTxStarted)
|
||||
{
|
||||
// fail TX not started
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check if the write size, fits in the buffer
|
||||
if ((iTxSize + aSize) > sizeof(iTxBuffer))
|
||||
{
|
||||
aSize = sizeof(iTxSize) - iTxSize;
|
||||
}
|
||||
|
||||
// copy data into the buffer
|
||||
memcpy(iTxBuffer + iTxSize, aBuffer, aSize);
|
||||
|
||||
iTxSize += aSize;
|
||||
|
||||
return aSize;
|
||||
}
|
||||
|
||||
int WebSocketClient::parseMessage()
|
||||
{
|
||||
flushRx();
|
||||
|
||||
// make sure 2 bytes (opcode + length)
|
||||
// are available
|
||||
if (HttpClient::available() < 2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// read open code and length
|
||||
uint8_t opcode = HttpClient::read();
|
||||
int length = HttpClient::read();
|
||||
|
||||
if ((opcode & 0x0f) == 0)
|
||||
{
|
||||
// continuation, use previous opcode and update flags
|
||||
iRxOpCode |= opcode;
|
||||
}
|
||||
else
|
||||
{
|
||||
iRxOpCode = opcode;
|
||||
}
|
||||
|
||||
iRxMasked = (length & 0x80);
|
||||
length &= 0x7f;
|
||||
|
||||
// read the RX size
|
||||
if (length < 126)
|
||||
{
|
||||
iRxSize = length;
|
||||
}
|
||||
else if (length == 126)
|
||||
{
|
||||
iRxSize = (HttpClient::read() << 8) | HttpClient::read();
|
||||
}
|
||||
else
|
||||
{
|
||||
iRxSize = ((uint64_t)HttpClient::read() << 56) |
|
||||
((uint64_t)HttpClient::read() << 48) |
|
||||
((uint64_t)HttpClient::read() << 40) |
|
||||
((uint64_t)HttpClient::read() << 32) |
|
||||
((uint64_t)HttpClient::read() << 24) |
|
||||
((uint64_t)HttpClient::read() << 16) |
|
||||
((uint64_t)HttpClient::read() << 8) |
|
||||
(uint64_t)HttpClient::read();
|
||||
}
|
||||
|
||||
// read in the mask, if present
|
||||
if (iRxMasked)
|
||||
{
|
||||
for (int i = 0; i < (int)sizeof(iRxMaskKey); i++)
|
||||
{
|
||||
iRxMaskKey[i] = HttpClient::read();
|
||||
}
|
||||
}
|
||||
|
||||
iRxMaskIndex = 0;
|
||||
|
||||
if (TYPE_CONNECTION_CLOSE == messageType())
|
||||
{
|
||||
flushRx();
|
||||
stop();
|
||||
iRxSize = 0;
|
||||
}
|
||||
else if (TYPE_PING == messageType())
|
||||
{
|
||||
beginMessage(TYPE_PONG);
|
||||
while(available())
|
||||
{
|
||||
write(read());
|
||||
}
|
||||
endMessage();
|
||||
|
||||
iRxSize = 0;
|
||||
}
|
||||
else if (TYPE_PONG == messageType())
|
||||
{
|
||||
flushRx();
|
||||
iRxSize = 0;
|
||||
}
|
||||
|
||||
return iRxSize;
|
||||
}
|
||||
|
||||
int WebSocketClient::messageType()
|
||||
{
|
||||
return (iRxOpCode & 0x0f);
|
||||
}
|
||||
|
||||
bool WebSocketClient::isFinal()
|
||||
{
|
||||
return ((iRxOpCode & 0x80) != 0);
|
||||
}
|
||||
|
||||
String WebSocketClient::readString()
|
||||
{
|
||||
int avail = available();
|
||||
String s;
|
||||
|
||||
if (avail > 0)
|
||||
{
|
||||
s.reserve(avail);
|
||||
|
||||
for (int i = 0; i < avail; i++)
|
||||
{
|
||||
s += (char)read();
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int WebSocketClient::ping()
|
||||
{
|
||||
uint8_t pingData[16];
|
||||
|
||||
// create random data for the ping
|
||||
for (int i = 0; i < (int)sizeof(pingData); i++)
|
||||
{
|
||||
pingData[i] = random(0xff);
|
||||
}
|
||||
|
||||
beginMessage(TYPE_PING);
|
||||
write(pingData, sizeof(pingData));
|
||||
return endMessage();
|
||||
}
|
||||
|
||||
int WebSocketClient::available()
|
||||
{
|
||||
if (iState < eReadingBody)
|
||||
{
|
||||
return HttpClient::available();
|
||||
}
|
||||
|
||||
return iRxSize;
|
||||
}
|
||||
|
||||
int WebSocketClient::read()
|
||||
{
|
||||
byte b;
|
||||
|
||||
if (read(&b, sizeof(b)))
|
||||
{
|
||||
return b;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int WebSocketClient::read(uint8_t *aBuffer, size_t aSize)
|
||||
{
|
||||
int readCount = HttpClient::read(aBuffer, aSize);
|
||||
|
||||
if (readCount > 0)
|
||||
{
|
||||
iRxSize -= readCount;
|
||||
|
||||
// unmask the RX data if needed
|
||||
if (iRxMasked)
|
||||
{
|
||||
for (int i = 0; i < (int)aSize; i++, iRxMaskIndex++)
|
||||
{
|
||||
aBuffer[i] ^= iRxMaskKey[iRxMaskIndex % sizeof(iRxMaskKey)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return readCount;
|
||||
}
|
||||
|
||||
int WebSocketClient::peek()
|
||||
{
|
||||
int p = HttpClient::peek();
|
||||
|
||||
if (p != -1 && iRxMasked)
|
||||
{
|
||||
// unmask the RX data if needed
|
||||
p = (uint8_t)p ^ iRxMaskKey[iRxMaskIndex % sizeof(iRxMaskKey)];
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void WebSocketClient::flushRx()
|
||||
{
|
||||
while(available())
|
||||
{
|
||||
read();
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
// (c) Copyright Arduino. 2016
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#ifndef WebSocketClient_h
|
||||
#define WebSocketClient_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "HttpClient.h"
|
||||
|
||||
static const int TYPE_CONTINUATION = 0x0;
|
||||
static const int TYPE_TEXT = 0x1;
|
||||
static const int TYPE_BINARY = 0x2;
|
||||
static const int TYPE_CONNECTION_CLOSE = 0x8;
|
||||
static const int TYPE_PING = 0x9;
|
||||
static const int TYPE_PONG = 0xa;
|
||||
|
||||
class WebSocketClient : public HttpClient
|
||||
{
|
||||
public:
|
||||
WebSocketClient(Client& aClient, const char* aServerName, uint16_t aServerPort = HttpClient::kHttpPort);
|
||||
WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort = HttpClient::kHttpPort);
|
||||
WebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort = HttpClient::kHttpPort);
|
||||
|
||||
/** Start the Web Socket connection to the specified path
|
||||
@param aURLPath Path to use in request (optional, "/" is used by default)
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int begin(const char* aPath = "/");
|
||||
int begin(const String& aPath);
|
||||
|
||||
/** Begin to send a message of type (TYPE_TEXT or TYPE_BINARY)
|
||||
Use the write or Stream API's to set message content, followed by endMessage
|
||||
to complete the message.
|
||||
@param aURLPath Path to use in request
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int beginMessage(int aType);
|
||||
|
||||
/** Completes sending of a message started by beginMessage
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int endMessage();
|
||||
|
||||
/** Try to parse an incoming messages
|
||||
@return 0 if no message available, else size of parsed message
|
||||
*/
|
||||
int parseMessage();
|
||||
|
||||
/** Returns type of current parsed message
|
||||
@return type of current parsedMessage (TYPE_TEXT or TYPE_BINARY)
|
||||
*/
|
||||
int messageType();
|
||||
|
||||
/** Returns if the current message is the final chunk of a split
|
||||
message
|
||||
@return true for final message, false otherwise
|
||||
*/
|
||||
bool isFinal();
|
||||
|
||||
/** Read the current messages as a string
|
||||
@return current message as a string
|
||||
*/
|
||||
String readString();
|
||||
|
||||
/** Send a ping
|
||||
@return 0 if successful, else error
|
||||
*/
|
||||
int ping();
|
||||
|
||||
// Inherited from Print
|
||||
virtual size_t write(uint8_t aByte);
|
||||
virtual size_t write(const uint8_t *aBuffer, size_t aSize);
|
||||
// Inherited from Stream
|
||||
virtual int available();
|
||||
/** Read the next byte from the server.
|
||||
@return Byte read or -1 if there are no bytes available.
|
||||
*/
|
||||
virtual int read();
|
||||
virtual int read(uint8_t *buf, size_t size);
|
||||
virtual int peek();
|
||||
|
||||
private:
|
||||
void flushRx();
|
||||
|
||||
private:
|
||||
bool iTxStarted;
|
||||
uint8_t iTxMessageType;
|
||||
uint8_t iTxBuffer[128];
|
||||
uint64_t iTxSize;
|
||||
|
||||
uint8_t iRxOpCode;
|
||||
uint64_t iRxSize;
|
||||
bool iRxMasked;
|
||||
int iRxMaskIndex;
|
||||
uint8_t iRxMaskKey[4];
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,72 @@
|
||||
// Simple Base64 code
|
||||
// (c) Copyright 2010 MCQN Ltd.
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#include "b64.h"
|
||||
|
||||
/* Simple test program
|
||||
#include <stdio.h>
|
||||
void main()
|
||||
{
|
||||
char* in = "amcewen";
|
||||
char out[22];
|
||||
|
||||
b64_encode(in, 15, out, 22);
|
||||
out[21] = '\0';
|
||||
|
||||
printf(out);
|
||||
}
|
||||
*/
|
||||
|
||||
int b64_encode(const unsigned char* aInput, int aInputLen, unsigned char* aOutput, int aOutputLen)
|
||||
{
|
||||
// Work out if we've got enough space to encode the input
|
||||
// Every 6 bits of input becomes a byte of output
|
||||
if (aOutputLen < (aInputLen*8)/6)
|
||||
{
|
||||
// FIXME Should we return an error here, or just the length
|
||||
return (aInputLen*8)/6;
|
||||
}
|
||||
|
||||
// If we get here we've got enough space to do the encoding
|
||||
|
||||
const char* b64_dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
if (aInputLen == 3)
|
||||
{
|
||||
aOutput[0] = b64_dictionary[aInput[0] >> 2];
|
||||
aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)];
|
||||
aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2|(aInput[2]>>6)];
|
||||
aOutput[3] = b64_dictionary[aInput[2]&0x3F];
|
||||
}
|
||||
else if (aInputLen == 2)
|
||||
{
|
||||
aOutput[0] = b64_dictionary[aInput[0] >> 2];
|
||||
aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)];
|
||||
aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2];
|
||||
aOutput[3] = '=';
|
||||
}
|
||||
else if (aInputLen == 1)
|
||||
{
|
||||
aOutput[0] = b64_dictionary[aInput[0] >> 2];
|
||||
aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4];
|
||||
aOutput[2] = '=';
|
||||
aOutput[3] = '=';
|
||||
}
|
||||
else
|
||||
{
|
||||
// Break the input into 3-byte chunks and process each of them
|
||||
int i;
|
||||
for (i = 0; i < aInputLen/3; i++)
|
||||
{
|
||||
b64_encode(&aInput[i*3], 3, &aOutput[i*4], 4);
|
||||
}
|
||||
if (aInputLen % 3 > 0)
|
||||
{
|
||||
// It doesn't fit neatly into a 3-byte chunk, so process what's left
|
||||
b64_encode(&aInput[i*3], aInputLen % 3, &aOutput[i*4], aOutputLen - (i*4));
|
||||
}
|
||||
}
|
||||
|
||||
return ((aInputLen+2)/3)*4;
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
#ifndef b64_h
|
||||
#define b64_h
|
||||
|
||||
int b64_encode(const unsigned char* aInput, int aInputLen, unsigned char* aOutput, int aOutputLen);
|
||||
|
||||
#endif
|
1
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/.piopm
Normal file
1
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/.piopm
Normal file
@ -0,0 +1 @@
|
||||
{"type": "library", "name": "ArduinoJson", "version": "6.21.5", "spec": {"owner": "bblanchon", "id": 64, "name": "ArduinoJson", "requirements": null, "uri": null}}
|
@ -0,0 +1,5 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include "src/ArduinoJson.h"
|
10
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/LICENSE.txt
Normal file
10
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/LICENSE.txt
Normal file
@ -0,0 +1,10 @@
|
||||
The MIT License (MIT)
|
||||
---------------------
|
||||
|
||||
Copyright © 2014-2023, Benoit BLANCHON
|
||||
|
||||
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.
|
161
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/README.md
Normal file
161
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/README.md
Normal file
@ -0,0 +1,161 @@
|
||||
<p align="center">
|
||||
<a href="https://arduinojson.org/"><img alt="ArduinoJson" src="https://arduinojson.org/images/logo.svg" width="200" /></a>
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
[](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22+branch%3A6.x)
|
||||
[](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/6.x)
|
||||
[](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:arduinojson)
|
||||
[](https://coveralls.io/github/bblanchon/ArduinoJson?branch=6.x)
|
||||
[](https://www.ardu-badge.com/ArduinoJson/6.21.5)
|
||||
[](https://registry.platformio.org/packages/libraries/bblanchon/ArduinoJson?version=6.21.5)
|
||||
[](https://components.espressif.com/components/bblanchon/arduinojson)
|
||||
[](https://github.com/bblanchon/ArduinoJson/stargazers)
|
||||
[](https://github.com/sponsors/bblanchon)
|
||||
|
||||
ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
|
||||
|
||||
## Features
|
||||
|
||||
* [JSON deserialization](https://arduinojson.org/v6/api/json/deserializejson/)
|
||||
* [Optionally decodes UTF-16 escape sequences to UTF-8](https://arduinojson.org/v6/api/config/decode_unicode/)
|
||||
* [Optionally stores links to the input buffer (zero-copy)](https://arduinojson.org/v6/api/json/deserializejson/)
|
||||
* [Optionally supports comments in the input](https://arduinojson.org/v6/api/config/enable_comments/)
|
||||
* [Optionally filters the input to keep only desired values](https://arduinojson.org/v6/api/json/deserializejson/#filtering)
|
||||
* Supports single quotes as a string delimiter
|
||||
* Compatible with [NDJSON](http://ndjson.org/) and [JSON Lines](https://jsonlines.org/)
|
||||
* [JSON serialization](https://arduinojson.org/v6/api/json/serializejson/)
|
||||
* [Can write to a buffer or a stream](https://arduinojson.org/v6/api/json/serializejson/)
|
||||
* [Optionally indents the document (prettified JSON)](https://arduinojson.org/v6/api/json/serializejsonpretty/)
|
||||
* [MessagePack serialization](https://arduinojson.org/v6/api/msgpack/serializemsgpack/)
|
||||
* [MessagePack deserialization](https://arduinojson.org/v6/api/msgpack/deserializemsgpack/)
|
||||
* Efficient
|
||||
* [Twice smaller than the "official" Arduino_JSON library](https://arduinojson.org/2019/11/19/arduinojson-vs-arduino_json/)
|
||||
* [Almost 10% faster than the "official" Arduino_JSON library](https://arduinojson.org/2019/11/19/arduinojson-vs-arduino_json/)
|
||||
* [Consumes roughly 10% less RAM than the "official" Arduino_JSON library](https://arduinojson.org/2019/11/19/arduinojson-vs-arduino_json/)
|
||||
* [Fixed memory allocation, no heap fragmentation](https://arduinojson.org/v6/api/jsondocument/)
|
||||
* [Optionally works without heap memory (zero malloc)](https://arduinojson.org/v6/api/staticjsondocument/)
|
||||
* [Deduplicates strings](https://arduinojson.org/news/2020/08/01/version-6-16-0/)
|
||||
* Versatile
|
||||
* Supports [custom allocators (to use external RAM chip, for example)](https://arduinojson.org/v6/how-to/use-external-ram-on-esp32/)
|
||||
* Supports [`String`](https://arduinojson.org/v6/api/config/enable_arduino_string/), [`std::string`](https://arduinojson.org/v6/api/config/enable_std_string/), and [`std::string_view`](https://arduinojson.org/v6/api/config/enable_string_view/)
|
||||
* Supports [`Stream`](https://arduinojson.org/v6/api/config/enable_arduino_stream/) and [`std::istream`/`std::ostream`](https://arduinojson.org/v6/api/config/enable_std_stream/)
|
||||
* Supports [Flash strings](https://arduinojson.org/v6/api/config/enable_progmem/)
|
||||
* Supports [custom readers](https://arduinojson.org/v6/api/json/deserializejson/#custom-reader) and [custom writers](https://arduinojson.org/v6/api/json/serializejson/#custom-writer)
|
||||
* Supports [custom converters](https://arduinojson.org/news/2021/05/04/version-6-18-0/)
|
||||
* Portable
|
||||
* Usable on any C++ project (not limited to Arduino)
|
||||
* Compatible with C++11, C++14 and C++17
|
||||
* Support for C++98/C++03 available on [ArduinoJson 6.20.x](https://github.com/bblanchon/ArduinoJson/tree/6.20.x)
|
||||
* Zero warnings with `-Wall -Wextra -pedantic` and `/W4`
|
||||
* [Header-only library](https://en.wikipedia.org/wiki/Header-only)
|
||||
* Works with virtually any board
|
||||
* Arduino boards: [Uno](https://amzn.to/38aL2ik), [Due](https://amzn.to/36YkWi2), [Micro](https://amzn.to/35WkdwG), [Nano](https://amzn.to/2QTvwRX), [Mega](https://amzn.to/36XWhuf), [Yun](https://amzn.to/30odURc), [Leonardo](https://amzn.to/36XWjlR)...
|
||||
* Espressif chips: [ESP8266](https://amzn.to/36YluV8), [ESP32](https://amzn.to/2G4pRCB)
|
||||
* Lolin (WeMos) boards: [D1 mini](https://amzn.to/2QUpz7q), [D1 Mini Pro](https://amzn.to/36UsGSs)...
|
||||
* Teensy boards: [4.0](https://amzn.to/30ljXGq), [3.2](https://amzn.to/2FT0EuC), [2.0](https://amzn.to/2QXUMXj)
|
||||
* Particle boards: [Argon](https://amzn.to/2FQHa9X), [Boron](https://amzn.to/36WgLUd), [Electron](https://amzn.to/30vEc4k), [Photon](https://amzn.to/387F9Cd)...
|
||||
* Texas Instruments boards: [MSP430](https://amzn.to/30nJWgg)...
|
||||
* Soft cores: [Nios II](https://en.wikipedia.org/wiki/Nios_II)...
|
||||
* Tested on all major development environments
|
||||
* [Arduino IDE](https://www.arduino.cc/en/Main/Software)
|
||||
* [Atmel Studio](http://www.atmel.com/microsite/atmel-studio/)
|
||||
* [Atollic TrueSTUDIO](https://atollic.com/truestudio/)
|
||||
* [Energia](http://energia.nu/)
|
||||
* [IAR Embedded Workbench](https://www.iar.com/iar-embedded-workbench/)
|
||||
* [Keil uVision](http://www.keil.com/)
|
||||
* [MPLAB X IDE](http://www.microchip.com/mplab/mplab-x-ide)
|
||||
* [Particle](https://www.particle.io/)
|
||||
* [PlatformIO](http://platformio.org/)
|
||||
* [Sloeber plugin for Eclipse](https://eclipse.baeyens.it/)
|
||||
* [Visual Micro](http://www.visualmicro.com/)
|
||||
* [Visual Studio](https://www.visualstudio.com/)
|
||||
* [Even works with online compilers like wandbox.org](https://wandbox.org/permlink/RlZSKy17DjJ6HcdN)
|
||||
* [CMake friendly](https://arduinojson.org/v6/how-to/use-arduinojson-with-cmake/)
|
||||
* Well designed
|
||||
* [Elegant API](http://arduinojson.org/v6/example/)
|
||||
* [Thread-safe](https://en.wikipedia.org/wiki/Thread_safety)
|
||||
* Self-contained (no external dependency)
|
||||
* `const` friendly
|
||||
* [`for` friendly](https://arduinojson.org/v6/api/jsonobject/begin_end/)
|
||||
* [TMP friendly](https://en.wikipedia.org/wiki/Template_metaprogramming)
|
||||
* Handles [integer overflows](https://arduinojson.org/v6/api/jsonvariant/as/#integer-overflows)
|
||||
* Well tested
|
||||
* [Unit test coverage close to 100%](https://coveralls.io/github/bblanchon/ArduinoJson?branch=6.x)
|
||||
* Continuously tested on
|
||||
* [Visual Studio 2017, 2019, 2022](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/6.x)
|
||||
* [GCC 5, 6, 7, 8, 9, 10, 11](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
|
||||
* [Clang 3.8, 3.9, 4.0, 5.0, 6.0, 7, 8, 9, 10](https://github.com/bblanchon/ArduinoJson/actions?query=workflow%3A%22Continuous+Integration%22)
|
||||
* [Continuously fuzzed with Google OSS Fuzz](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:arduinojson)
|
||||
* Passes all default checks of [clang-tidy](https://releases.llvm.org/10.0.0/tools/clang/tools/extra/docs/clang-tidy/)
|
||||
* Well documented
|
||||
* [Tutorials](https://arduinojson.org/v6/doc/deserialization/)
|
||||
* [Examples](https://arduinojson.org/v6/example/)
|
||||
* [How-tos](https://arduinojson.org/v6/example/)
|
||||
* [FAQ](https://arduinojson.org/v6/faq/)
|
||||
* [Troubleshooter](https://arduinojson.org/v6/troubleshooter/)
|
||||
* [Book](https://arduinojson.org/book/)
|
||||
* [Changelog](CHANGELOG.md)
|
||||
* Vibrant user community
|
||||
* Most popular of all Arduino libraries on [GitHub](https://github.com/search?o=desc&q=arduino+library&s=stars&type=Repositories)
|
||||
* [Used in hundreds of projects](https://www.hackster.io/search?i=projects&q=arduinojson)
|
||||
* [Responsive support](https://github.com/bblanchon/ArduinoJson/issues?q=is%3Aissue+is%3Aclosed)
|
||||
|
||||
## Quickstart
|
||||
|
||||
### Deserialization
|
||||
|
||||
Here is a program that parses a JSON document with ArduinoJson.
|
||||
|
||||
```c++
|
||||
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
|
||||
DynamicJsonDocument doc(1024);
|
||||
deserializeJson(doc, json);
|
||||
|
||||
const char* sensor = doc["sensor"];
|
||||
long time = doc["time"];
|
||||
double latitude = doc["data"][0];
|
||||
double longitude = doc["data"][1];
|
||||
```
|
||||
|
||||
See the [tutorial on arduinojson.org](https://arduinojson.org/v6/doc/deserialization/)
|
||||
|
||||
### Serialization
|
||||
|
||||
Here is a program that generates a JSON document with ArduinoJson:
|
||||
|
||||
```c++
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
doc["sensor"] = "gps";
|
||||
doc["time"] = 1351824120;
|
||||
doc["data"][0] = 48.756080;
|
||||
doc["data"][1] = 2.302038;
|
||||
|
||||
serializeJson(doc, Serial);
|
||||
// This prints:
|
||||
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||
```
|
||||
|
||||
See the [tutorial on arduinojson.org](https://arduinojson.org/v6/doc/serialization/)
|
||||
|
||||
## Sponsors
|
||||
|
||||
ArduinoJson is thankful to its sponsors. Please give them a visit; they deserve it!
|
||||
|
||||
<p>
|
||||
<a href="https://www.programmingelectronics.com/" rel="sponsored">
|
||||
<img src="https://arduinojson.org/images/2021/10/programmingeleactronicsacademy.png" alt="Programming Electronics Academy" width="200">
|
||||
</a>
|
||||
</p>
|
||||
<p>
|
||||
<a href="https://github.com/1technophile" rel="sponsored">
|
||||
<img alt="1technophile" src="https://avatars.githubusercontent.com/u/12672732?s=40&v=4">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
If you run a commercial project that embeds ArduinoJson, think about [sponsoring the library's development](https://github.com/sponsors/bblanchon): it ensures the code that your products rely on stays actively maintained. It can also give your project some exposure to the makers' community.
|
||||
|
||||
If you are an individual user and want to support the development (or give a sign of appreciation), consider purchasing the book [Mastering ArduinoJson](https://arduinojson.org/book/) ❤, or simply [cast a star](https://github.com/bblanchon/ArduinoJson/stargazers) ⭐.
|
@ -0,0 +1,160 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to store your project configuration in a file.
|
||||
// It uses the SD library but can be easily modified for any other file-system.
|
||||
//
|
||||
// The file contains a JSON document with the following content:
|
||||
// {
|
||||
// "hostname": "examples.com",
|
||||
// "port": 2731
|
||||
// }
|
||||
//
|
||||
// To run this program, you need an SD card connected to the SPI bus as follows:
|
||||
// * MOSI <-> pin 11
|
||||
// * MISO <-> pin 12
|
||||
// * CLK <-> pin 13
|
||||
// * CS <-> pin 4
|
||||
//
|
||||
// https://arduinojson.org/v6/example/config/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <SD.h>
|
||||
#include <SPI.h>
|
||||
|
||||
// Our configuration structure.
|
||||
//
|
||||
// Never use a JsonDocument to store the configuration!
|
||||
// A JsonDocument is *not* a permanent storage; it's only a temporary storage
|
||||
// used during the serialization phase. See:
|
||||
// https://arduinojson.org/v6/faq/why-must-i-create-a-separate-config-object/
|
||||
struct Config {
|
||||
char hostname[64];
|
||||
int port;
|
||||
};
|
||||
|
||||
const char *filename = "/config.txt"; // <- SD library uses 8.3 filenames
|
||||
Config config; // <- global configuration object
|
||||
|
||||
// Loads the configuration from a file
|
||||
void loadConfiguration(const char *filename, Config &config) {
|
||||
// Open file for reading
|
||||
File file = SD.open(filename);
|
||||
|
||||
// Allocate a temporary JsonDocument
|
||||
// Don't forget to change the capacity to match your requirements.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<512> doc;
|
||||
|
||||
// Deserialize the JSON document
|
||||
DeserializationError error = deserializeJson(doc, file);
|
||||
if (error)
|
||||
Serial.println(F("Failed to read file, using default configuration"));
|
||||
|
||||
// Copy values from the JsonDocument to the Config
|
||||
config.port = doc["port"] | 2731;
|
||||
strlcpy(config.hostname, // <- destination
|
||||
doc["hostname"] | "example.com", // <- source
|
||||
sizeof(config.hostname)); // <- destination's capacity
|
||||
|
||||
// Close the file (Curiously, File's destructor doesn't close the file)
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Saves the configuration to a file
|
||||
void saveConfiguration(const char *filename, const Config &config) {
|
||||
// Delete existing file, otherwise the configuration is appended to the file
|
||||
SD.remove(filename);
|
||||
|
||||
// Open file for writing
|
||||
File file = SD.open(filename, FILE_WRITE);
|
||||
if (!file) {
|
||||
Serial.println(F("Failed to create file"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Allocate a temporary JsonDocument
|
||||
// Don't forget to change the capacity to match your requirements.
|
||||
// Use https://arduinojson.org/assistant to compute the capacity.
|
||||
StaticJsonDocument<256> doc;
|
||||
|
||||
// Set the values in the document
|
||||
doc["hostname"] = config.hostname;
|
||||
doc["port"] = config.port;
|
||||
|
||||
// Serialize JSON to file
|
||||
if (serializeJson(doc, file) == 0) {
|
||||
Serial.println(F("Failed to write to file"));
|
||||
}
|
||||
|
||||
// Close the file
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Prints the content of a file to the Serial
|
||||
void printFile(const char *filename) {
|
||||
// Open file for reading
|
||||
File file = SD.open(filename);
|
||||
if (!file) {
|
||||
Serial.println(F("Failed to read file"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract each characters by one by one
|
||||
while (file.available()) {
|
||||
Serial.print((char)file.read());
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Close the file
|
||||
file.close();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// Initialize SD library
|
||||
const int chipSelect = 4;
|
||||
while (!SD.begin(chipSelect)) {
|
||||
Serial.println(F("Failed to initialize SD library"));
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
// Should load default config if run for the first time
|
||||
Serial.println(F("Loading configuration..."));
|
||||
loadConfiguration(filename, config);
|
||||
|
||||
// Create configuration file
|
||||
Serial.println(F("Saving configuration..."));
|
||||
saveConfiguration(filename, config);
|
||||
|
||||
// Dump config file
|
||||
Serial.println(F("Print config file..."));
|
||||
printFile(filename);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Performance issue?
|
||||
// ------------------
|
||||
//
|
||||
// File is an unbuffered stream, which is not optimal for ArduinoJson.
|
||||
// See: https://arduinojson.org/v6/how-to/improve-speed/
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization or deserialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a case study of a project that has
|
||||
// a complex configuration with nested members.
|
||||
// Contrary to this example, the project in the book uses the SPIFFS filesystem.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,63 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to use DeserializationOption::Filter
|
||||
//
|
||||
// https://arduinojson.org/v6/example/filter/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// The huge input: an extract from OpenWeatherMap response
|
||||
auto input_json = F(
|
||||
"{\"cod\":\"200\",\"message\":0,\"list\":[{\"dt\":1581498000,\"main\":{"
|
||||
"\"temp\":3.23,\"feels_like\":-3.63,\"temp_min\":3.23,\"temp_max\":4.62,"
|
||||
"\"pressure\":1014,\"sea_level\":1014,\"grnd_level\":1010,\"humidity\":"
|
||||
"58,\"temp_kf\":-1.39},\"weather\":[{\"id\":800,\"main\":\"Clear\","
|
||||
"\"description\":\"clear "
|
||||
"sky\",\"icon\":\"01d\"}],\"clouds\":{\"all\":0},\"wind\":{\"speed\":6."
|
||||
"19,\"deg\":266},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2020-02-12 "
|
||||
"09:00:00\"},{\"dt\":1581508800,\"main\":{\"temp\":6.09,\"feels_like\":-"
|
||||
"1.07,\"temp_min\":6.09,\"temp_max\":7.13,\"pressure\":1015,\"sea_"
|
||||
"level\":1015,\"grnd_level\":1011,\"humidity\":48,\"temp_kf\":-1.04},"
|
||||
"\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear "
|
||||
"sky\",\"icon\":\"01d\"}],\"clouds\":{\"all\":9},\"wind\":{\"speed\":6."
|
||||
"64,\"deg\":268},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2020-02-12 "
|
||||
"12:00:00\"}],\"city\":{\"id\":2643743,\"name\":\"London\",\"coord\":{"
|
||||
"\"lat\":51.5085,\"lon\":-0.1257},\"country\":\"GB\",\"population\":"
|
||||
"1000000,\"timezone\":0,\"sunrise\":1581492085,\"sunset\":1581527294}}");
|
||||
|
||||
// The filter: it contains "true" for each value we want to keep
|
||||
StaticJsonDocument<200> filter;
|
||||
filter["list"][0]["dt"] = true;
|
||||
filter["list"][0]["main"]["temp"] = true;
|
||||
|
||||
// Deserialize the document
|
||||
StaticJsonDocument<400> doc;
|
||||
deserializeJson(doc, input_json, DeserializationOption::Filter(filter));
|
||||
|
||||
// Print the result
|
||||
serializeJsonPretty(doc, Serial);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// deserialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like deserializing directly from a file or an HTTP request.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,77 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to generate a JSON document with ArduinoJson.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/generator/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
// Initialize Serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the RAM allocated to this document.
|
||||
// Don't forget to change this value to match your requirement.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
|
||||
// Add values in the document
|
||||
//
|
||||
doc["sensor"] = "gps";
|
||||
doc["time"] = 1351824120;
|
||||
|
||||
// Add an array.
|
||||
//
|
||||
JsonArray data = doc.createNestedArray("data");
|
||||
data.add(48.756080);
|
||||
data.add(2.302038);
|
||||
|
||||
// Generate the minified JSON and send it to the Serial port.
|
||||
//
|
||||
serializeJson(doc, Serial);
|
||||
// The above line prints:
|
||||
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||
|
||||
// Start a new line
|
||||
Serial.println();
|
||||
|
||||
// Generate the prettified JSON and send it to the Serial port.
|
||||
//
|
||||
serializeJsonPretty(doc, Serial);
|
||||
// The above line prints:
|
||||
// {
|
||||
// "sensor": "gps",
|
||||
// "time": 1351824120,
|
||||
// "data": [
|
||||
// 48.756080,
|
||||
// 2.302038
|
||||
// ]
|
||||
// }
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like serializing directly to a file or an HTTP request.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,126 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to parse a JSON document in an HTTP response.
|
||||
// It uses the Ethernet library, but can be easily adapted for Wifi.
|
||||
//
|
||||
// It performs a GET resquest on https://arduinojson.org/example.json
|
||||
// Here is the expected response:
|
||||
// {
|
||||
// "sensor": "gps",
|
||||
// "time": 1351824120,
|
||||
// "data": [
|
||||
// 48.756080,
|
||||
// 2.302038
|
||||
// ]
|
||||
// }
|
||||
//
|
||||
// https://arduinojson.org/v6/example/http-client/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
|
||||
void setup() {
|
||||
// Initialize Serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// Initialize Ethernet library
|
||||
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
||||
if (!Ethernet.begin(mac)) {
|
||||
Serial.println(F("Failed to configure Ethernet"));
|
||||
return;
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
Serial.println(F("Connecting..."));
|
||||
|
||||
// Connect to HTTP server
|
||||
EthernetClient client;
|
||||
client.setTimeout(10000);
|
||||
if (!client.connect("arduinojson.org", 80)) {
|
||||
Serial.println(F("Connection failed"));
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println(F("Connected!"));
|
||||
|
||||
// Send HTTP request
|
||||
client.println(F("GET /example.json HTTP/1.0"));
|
||||
client.println(F("Host: arduinojson.org"));
|
||||
client.println(F("Connection: close"));
|
||||
if (client.println() == 0) {
|
||||
Serial.println(F("Failed to send request"));
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check HTTP status
|
||||
char status[32] = {0};
|
||||
client.readBytesUntil('\r', status, sizeof(status));
|
||||
// It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
|
||||
if (strcmp(status + 9, "200 OK") != 0) {
|
||||
Serial.print(F("Unexpected response: "));
|
||||
Serial.println(status);
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip HTTP headers
|
||||
char endOfHeaders[] = "\r\n\r\n";
|
||||
if (!client.find(endOfHeaders)) {
|
||||
Serial.println(F("Invalid response"));
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Allocate the JSON document
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
|
||||
DynamicJsonDocument doc(capacity);
|
||||
|
||||
// Parse JSON object
|
||||
DeserializationError error = deserializeJson(doc, client);
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract values
|
||||
Serial.println(F("Response:"));
|
||||
Serial.println(doc["sensor"].as<const char*>());
|
||||
Serial.println(doc["time"].as<long>());
|
||||
Serial.println(doc["data"][0].as<float>(), 6);
|
||||
Serial.println(doc["data"][1].as<float>(), 6);
|
||||
|
||||
// Disconnect
|
||||
client.stop();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Performance issue?
|
||||
// ------------------
|
||||
//
|
||||
// EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
|
||||
// See: https://arduinojson.org/v6/how-to/improve-speed/
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization
|
||||
// showing how to parse the response from GitHub's API. In the last chapter,
|
||||
// it shows how to parse the huge documents from OpenWeatherMap
|
||||
// and Reddit.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,80 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to deserialize a JSON document with ArduinoJson.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/parser/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
|
||||
// Don't forget to change this value to match your JSON document.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonDocument<N> allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
|
||||
// JSON input string.
|
||||
//
|
||||
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
|
||||
// the minimal amount of memory because the JsonDocument stores pointers to
|
||||
// the input buffer.
|
||||
// If you use another type of input, ArduinoJson must copy the strings from
|
||||
// the input to the JsonDocument, so you need to increase the capacity of the
|
||||
// JsonDocument.
|
||||
char json[] =
|
||||
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
|
||||
// Deserialize the JSON document
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
|
||||
// Test if parsing succeeds.
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch values.
|
||||
//
|
||||
// Most of the time, you can rely on the implicit casts.
|
||||
// In other case, you can do doc["time"].as<long>();
|
||||
const char* sensor = doc["sensor"];
|
||||
long time = doc["time"];
|
||||
double latitude = doc["data"][0];
|
||||
double longitude = doc["data"][1];
|
||||
|
||||
// Print values.
|
||||
Serial.println(sensor);
|
||||
Serial.println(time);
|
||||
Serial.println(latitude, 6);
|
||||
Serial.println(longitude, 6);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// deserialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like deserializing directly from a file or an HTTP request.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,117 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to implement an HTTP server that sends a JSON document
|
||||
// in the response.
|
||||
// It uses the Ethernet library but can be easily adapted for Wifi.
|
||||
//
|
||||
// The JSON document contains the values of the analog and digital pins.
|
||||
// It looks like that:
|
||||
// {
|
||||
// "analog": [0, 76, 123, 158, 192, 205],
|
||||
// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
|
||||
// }
|
||||
//
|
||||
// https://arduinojson.org/v6/example/http-server/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
|
||||
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
||||
EthernetServer server(80);
|
||||
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// Initialize Ethernet libary
|
||||
if (!Ethernet.begin(mac)) {
|
||||
Serial.println(F("Failed to initialize Ethernet library"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Start to listen
|
||||
server.begin();
|
||||
|
||||
Serial.println(F("Server is ready."));
|
||||
Serial.print(F("Please connect to http://"));
|
||||
Serial.println(Ethernet.localIP());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Wait for an incomming connection
|
||||
EthernetClient client = server.available();
|
||||
|
||||
// Do we have a client?
|
||||
if (!client)
|
||||
return;
|
||||
|
||||
Serial.println(F("New client"));
|
||||
|
||||
// Read the request (we ignore the content in this example)
|
||||
while (client.available()) client.read();
|
||||
|
||||
// Allocate a temporary JsonDocument
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<500> doc;
|
||||
|
||||
// Create the "analog" array
|
||||
JsonArray analogValues = doc.createNestedArray("analog");
|
||||
for (int pin = 0; pin < 6; pin++) {
|
||||
// Read the analog input
|
||||
int value = analogRead(pin);
|
||||
|
||||
// Add the value at the end of the array
|
||||
analogValues.add(value);
|
||||
}
|
||||
|
||||
// Create the "digital" array
|
||||
JsonArray digitalValues = doc.createNestedArray("digital");
|
||||
for (int pin = 0; pin < 14; pin++) {
|
||||
// Read the digital input
|
||||
int value = digitalRead(pin);
|
||||
|
||||
// Add the value at the end of the array
|
||||
digitalValues.add(value);
|
||||
}
|
||||
|
||||
Serial.print(F("Sending: "));
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
|
||||
// Write response headers
|
||||
client.println(F("HTTP/1.0 200 OK"));
|
||||
client.println(F("Content-Type: application/json"));
|
||||
client.println(F("Connection: close"));
|
||||
client.print(F("Content-Length: "));
|
||||
client.println(measureJsonPretty(doc));
|
||||
client.println();
|
||||
|
||||
// Write JSON document
|
||||
serializeJsonPretty(doc, client);
|
||||
|
||||
// Disconnect
|
||||
client.stop();
|
||||
}
|
||||
|
||||
// Performance issue?
|
||||
// ------------------
|
||||
//
|
||||
// EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
|
||||
// See: https://arduinojson.org/v6/how-to/improve-speed/
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, then adds more features like serializing
|
||||
// directly to a file or an HTTP client.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,106 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to send a JSON document to a UDP socket.
|
||||
// At regular interval, it sends a UDP packet that contains the status of
|
||||
// analog and digital pins.
|
||||
// It looks like that:
|
||||
// {
|
||||
// "analog": [0, 76, 123, 158, 192, 205],
|
||||
// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
|
||||
// }
|
||||
//
|
||||
// If you want to test this program, you need to be able to receive the UDP
|
||||
// packets.
|
||||
// For example, you can run netcat on your computer
|
||||
// $ ncat -ulp 8888
|
||||
// See https://nmap.org/ncat/
|
||||
//
|
||||
// https://arduinojson.org/v6/example/udp-beacon/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
|
||||
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
||||
IPAddress remoteIp(192, 168, 0, 108); // <- EDIT!!!!
|
||||
unsigned short remotePort = 8888;
|
||||
unsigned short localPort = 8888;
|
||||
EthernetUDP udp;
|
||||
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// Initialize Ethernet libary
|
||||
if (!Ethernet.begin(mac)) {
|
||||
Serial.println(F("Failed to initialize Ethernet library"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Enable UDP
|
||||
udp.begin(localPort);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Allocate a temporary JsonDocument
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<500> doc;
|
||||
|
||||
// Create the "analog" array
|
||||
JsonArray analogValues = doc.createNestedArray("analog");
|
||||
for (int pin = 0; pin < 6; pin++) {
|
||||
// Read the analog input
|
||||
int value = analogRead(pin);
|
||||
|
||||
// Add the value at the end of the array
|
||||
analogValues.add(value);
|
||||
}
|
||||
|
||||
// Create the "digital" array
|
||||
JsonArray digitalValues = doc.createNestedArray("digital");
|
||||
for (int pin = 0; pin < 14; pin++) {
|
||||
// Read the digital input
|
||||
int value = digitalRead(pin);
|
||||
|
||||
// Add the value at the end of the array
|
||||
digitalValues.add(value);
|
||||
}
|
||||
|
||||
// Log
|
||||
Serial.print(F("Sending to "));
|
||||
Serial.print(remoteIp);
|
||||
Serial.print(F(" on port "));
|
||||
Serial.println(remotePort);
|
||||
serializeJson(doc, Serial);
|
||||
|
||||
// Send UDP packet
|
||||
udp.beginPacket(remoteIp, remotePort);
|
||||
serializeJson(doc, udp);
|
||||
udp.println();
|
||||
udp.endPacket();
|
||||
|
||||
// Wait
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
// Performance issue?
|
||||
// ------------------
|
||||
//
|
||||
// EthernetUDP is an unbuffered stream, which is not optimal for ArduinoJson.
|
||||
// See: https://arduinojson.org/v6/how-to/improve-speed/
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, then adds more features like serializing
|
||||
// directly to a file or any stream.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,75 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to deserialize a MessagePack document with
|
||||
// ArduinoJson.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/msgpack-parser/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
// Initialize serial port
|
||||
Serial.begin(9600);
|
||||
while (!Serial) continue;
|
||||
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
|
||||
// Don't forget to change this value to match your JSON document.
|
||||
// Use https://arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonObject which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonObject doc(200);
|
||||
|
||||
// MessagePack input string.
|
||||
//
|
||||
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
|
||||
// the minimal amount of memory because the JsonDocument stores pointers to
|
||||
// the input buffer.
|
||||
// If you use another type of input, ArduinoJson must copy the strings from
|
||||
// the input to the JsonDocument, so you need to increase the capacity of the
|
||||
// JsonDocument.
|
||||
uint8_t input[] = {131, 166, 115, 101, 110, 115, 111, 114, 163, 103, 112, 115,
|
||||
164, 116, 105, 109, 101, 206, 80, 147, 50, 248, 164, 100,
|
||||
97, 116, 97, 146, 203, 64, 72, 96, 199, 58, 188, 148,
|
||||
112, 203, 64, 2, 106, 146, 230, 33, 49, 169};
|
||||
// This MessagePack document contains:
|
||||
// {
|
||||
// "sensor": "gps",
|
||||
// "time": 1351824120,
|
||||
// "data": [48.75608, 2.302038]
|
||||
// }
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
|
||||
// Test if parsing succeeded.
|
||||
if (error) {
|
||||
Serial.print("deserializeMsgPack() failed: ");
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch values.
|
||||
//
|
||||
// Most of the time, you can rely on the implicit casts.
|
||||
// In other case, you can do doc["time"].as<long>();
|
||||
const char* sensor = doc["sensor"];
|
||||
long time = doc["time"];
|
||||
double latitude = doc["data"][0];
|
||||
double longitude = doc["data"][1];
|
||||
|
||||
// Print values.
|
||||
Serial.println(sensor);
|
||||
Serial.println(time);
|
||||
Serial.println(latitude, 6);
|
||||
Serial.println(longitude, 6);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows the different ways you can use Flash strings with
|
||||
// ArduinoJson.
|
||||
//
|
||||
// Use Flash strings sparingly, because ArduinoJson duplicates them in the
|
||||
// JsonDocument. Prefer plain old char*, as they are more efficient in term of
|
||||
// code size, speed, and memory usage.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/progmem/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
// You can use a Flash String as your JSON input.
|
||||
// WARNING: the strings in the input will be duplicated in the JsonDocument.
|
||||
deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
|
||||
"\"data\":[48.756080,2.302038]}"));
|
||||
|
||||
// You can use a Flash String as a key to get a member from JsonDocument
|
||||
// No duplication is done.
|
||||
long time = doc[F("time")];
|
||||
|
||||
// You can use a Flash String as a key to set a member of a JsonDocument
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonDocument.
|
||||
doc[F("time")] = time;
|
||||
|
||||
// You can set a Flash String as the content of a JsonVariant
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonDocument.
|
||||
doc["sensor"] = F("gps");
|
||||
|
||||
// It works with serialized() too:
|
||||
doc["sensor"] = serialized(F("\"gps\""));
|
||||
doc["sensor"] = serialized(F("\xA3gps"), 3);
|
||||
|
||||
// You can compare the content of a JsonVariant to a Flash String
|
||||
if (doc["sensor"] == F("gps")) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any memory
|
||||
// problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
||||
// how your microcontroller stores strings in memory. It also tells why you
|
||||
// should not abuse Flash strings with ArduinoJson.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
@ -0,0 +1,77 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
//
|
||||
// This example shows the different ways you can use String with ArduinoJson.
|
||||
//
|
||||
// Use String objects sparingly, because ArduinoJson duplicates them in the
|
||||
// JsonDocument. Prefer plain old char[], as they are more efficient in term of
|
||||
// code size, speed, and memory usage.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/string/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
void setup() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
// You can use a String as your JSON input.
|
||||
// WARNING: the string in the input will be duplicated in the JsonDocument.
|
||||
String input =
|
||||
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
deserializeJson(doc, input);
|
||||
|
||||
// You can use a String as a key to get a member from JsonDocument
|
||||
// No duplication is done.
|
||||
long time = doc[String("time")];
|
||||
|
||||
// You can use a String as a key to set a member of a JsonDocument
|
||||
// WARNING: the content of the String will be duplicated in the JsonDocument.
|
||||
doc[String("time")] = time;
|
||||
|
||||
// You can get the content of a JsonVariant as a String
|
||||
// No duplication is done, at least not in the JsonDocument.
|
||||
String sensor = doc["sensor"];
|
||||
|
||||
// Unfortunately, the following doesn't work (issue #118):
|
||||
// sensor = doc["sensor"]; // <- error "ambiguous overload for 'operator='"
|
||||
// As a workaround, you need to replace by:
|
||||
sensor = doc["sensor"].as<String>();
|
||||
|
||||
// You can set a String as the content of a JsonVariant
|
||||
// WARNING: the content of the String will be duplicated in the JsonDocument.
|
||||
doc["sensor"] = sensor;
|
||||
|
||||
// It works with serialized() too:
|
||||
doc["sensor"] = serialized(sensor);
|
||||
|
||||
// You can also concatenate strings
|
||||
// WARNING: the content of the String will be duplicated in the JsonDocument.
|
||||
doc[String("sen") + "sor"] = String("gp") + "s";
|
||||
|
||||
// You can compare the content of a JsonObject with a String
|
||||
if (doc["sensor"] == sensor) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// Lastly, you can print the resulting JSON to a String
|
||||
// WARNING: it doesn't replace the content but appends to it
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
||||
// how your microcontroller stores strings in memory. On several occasions, it
|
||||
// shows how you can avoid String in your program.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
23
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/library.json
Normal file
23
.pio/libdeps/esp32-s3-devkitc-1/ArduinoJson/library.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "ArduinoJson",
|
||||
"keywords": "json, rest, http, web",
|
||||
"description": "A simple and efficient JSON library for embedded C++. ArduinoJson supports ✔ serialization, ✔ deserialization, ✔ MessagePack, ✔ fixed allocation, ✔ zero-copy, ✔ streams, ✔ filtering, and more. It is the most popular Arduino library on GitHub ❤❤❤❤❤. Check out arduinojson.org for a comprehensive documentation.",
|
||||
"homepage": "https://arduinojson.org/?utm_source=meta&utm_medium=library.json",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||
},
|
||||
"version": "6.21.5",
|
||||
"authors": {
|
||||
"name": "Benoit Blanchon",
|
||||
"url": "https://blog.benoitblanchon.fr"
|
||||
},
|
||||
"export": {
|
||||
"include": ["src", "examples", "LICENSE.txt", "ArduinoJson.h"]
|
||||
},
|
||||
"frameworks": "*",
|
||||
"platforms": "*",
|
||||
"build": {
|
||||
"libArchive": false
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
name=ArduinoJson
|
||||
version=6.21.5
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=A simple and efficient JSON library for embedded C++.
|
||||
paragraph=ArduinoJson supports ✔ serialization, ✔ deserialization, ✔ MessagePack, ✔ fixed allocation, ✔ zero-copy, ✔ streams, ✔ filtering, and more. It is the most popular Arduino library on GitHub ❤❤❤❤❤. Check out arduinojson.org for a comprehensive documentation.
|
||||
category=Data Processing
|
||||
url=https://arduinojson.org/?utm_source=meta&utm_medium=library.properties
|
||||
architectures=*
|
||||
repository=https://github.com/bblanchon/ArduinoJson.git
|
||||
license=MIT
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user