IP Configuration

This commit is contained in:
Gašper Dobrovoljc
2023-03-11 15:11:03 +01:00
commit ec125f27db
662 changed files with 103738 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
//
// FILE: HX711.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.1
// PURPOSE: Library for Loadcells for UNO
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2019-09-04 initial release
// 0.1.1 2019-09-09 change long to float (reduce footprint)
// 0.2.0 2020-06-15 refactor; add price functions;
// 0.2.1 2020-12-28 add arduino-ci + unit test
#include "HX711.h"
HX711::HX711()
{
reset();
}
HX711::~HX711() {}
void HX711::begin(uint8_t dataPin, uint8_t clockPin)
{
_dataPin = dataPin;
_clockPin = clockPin;
pinMode(_dataPin, INPUT);
pinMode(_clockPin, OUTPUT);
digitalWrite(_clockPin, LOW);
reset();
}
void HX711::reset()
{
_offset = 0;
_scale = 1;
_gain = 128;
}
bool HX711::is_ready()
{
return digitalRead(_dataPin) == LOW;
}
float HX711::read()
{
// this waiting takes most time...
while (digitalRead(_dataPin) == HIGH) yield();
union
{
long value = 0;
uint8_t data[4];
} v;
noInterrupts();
// Pulse the clock pin 24 times to read the data.
v.data[2] = shiftIn(_dataPin, _clockPin, MSBFIRST);
v.data[1] = shiftIn(_dataPin, _clockPin, MSBFIRST);
v.data[0] = shiftIn(_dataPin, _clockPin, MSBFIRST);
// TABLE 3 page 4 datasheet
// only default verified, so other values not supported yet
uint8_t m = 1; // default gain == 128
if (_gain == 64) m = 3;
if (_gain == 32) m = 2;
while (m > 0)
{
digitalWrite(_clockPin, HIGH);
digitalWrite(_clockPin, LOW);
m--;
}
interrupts();
// SIGN extend
if (v.data[2] & 0x80) v.data[3] = 0xFF;
_lastRead = millis();
return 1.0 * v.value;
}
// assumes tare() has been set.
void HX711::callibrate_scale(uint16_t weight, uint8_t times)
{
_scale = (1.0 * weight) / (read_average(times) - _offset);
}
void HX711::wait_ready(uint32_t ms)
{
while (!is_ready())
{
delay(ms);
}
}
bool HX711::wait_ready_retry(uint8_t retries, uint32_t ms)
{
while (retries--)
{
if (is_ready()) return true;
delay(ms);
}
return false;
}
bool HX711::wait_ready_timeout(uint32_t timeout, uint32_t ms)
{
uint32_t start = millis();
while (millis() - start < timeout)
{
if (is_ready()) return true;
delay(ms);
}
return false;
}
float HX711::read_average(uint8_t times)
{
float sum = 0;
for (uint8_t i = 0; i < times; i++)
{
sum += read();
yield();
}
return sum / times;
}
float HX711::get_units(uint8_t times)
{
float units = get_value(times) * _scale;
return units;
};
void HX711::power_down()
{
digitalWrite(_clockPin, LOW);
digitalWrite(_clockPin, HIGH);
}
void HX711::power_up()
{
digitalWrite(_clockPin, LOW);
}
// -- END OF FILE --

View File

@@ -0,0 +1,99 @@
#pragma once
//
// FILE: HX711.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.1
// PURPOSE: Library for Loadcells for UNO
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY: see HX711.cpp
//
// NOTES
// Superset of interface of HX711 class of Bogde
// float iso long as float has 23 bits mantisse.
#include "Arduino.h"
#define HX711_LIB_VERSION (F("0.2.1"))
class HX711
{
public:
HX711();
~HX711();
// fixed gain 128 for now
void begin(uint8_t dataPin, uint8_t clockPin);
void reset();
// checks if loadcell is ready to read.
bool is_ready();
// wait until ready,
// check every ms
void wait_ready(uint32_t ms = 0);
// max # retries
bool wait_ready_retry(uint8_t retries = 3, uint32_t ms = 0);
// max timeout
bool wait_ready_timeout(uint32_t timeout = 1000, uint32_t ms = 0);
// raw read
float read();
// multiple raw reads
float read_average(uint8_t times = 10);
// corrected for offset
float get_value(uint8_t times = 1) { return read_average(times) - _offset; };
// converted to proper units.
float get_units(uint8_t times = 1);
// TARE
// call tare to calibrate zero
void tare(uint8_t times = 10) { _offset = read_average(times); };
float get_tare() { return -_offset * _scale; };
bool tare_set() { return _offset != 0; };
// CORE "CONSTANTS" -> read datasheet
// GAIN values: 128, 64 32 [only 128 tested & verified]
void set_gain(uint8_t gain = 128) { _gain = gain; };
uint8_t get_gain() { return _gain; };
// SCALE > 0
void set_scale(float scale = 1.0) { _scale = 1 / scale; };
float get_scale() { return 1 / _scale; };
// OFFSET > 0
void set_offset(long offset = 0) { _offset = offset; };
long get_offset() { return _offset; };
// CALIBRATION
// clear the scale
// call tare() to set the zero offset
// put a known weight on the scale
// call callibrate_scale(weight)
// scale is calculated.
void callibrate_scale(uint16_t weight, uint8_t times = 10);
// POWER MANAGEMENT
void power_down();
void power_up();
// TIME OF LAST READ
uint32_t last_read() { return _lastRead; };
// PRICING (idem calories?)
float get_price(uint8_t times = 1) { return get_units(times) * _price; };
void set_unit_price(float price) { _price = price; };
float get_unit_price() { return _price; };
private:
uint8_t _dataPin;
uint8_t _clockPin;
uint8_t _gain = 128; // default channel A
long _offset = 0;
float _scale = 1;
uint32_t _lastRead = 0;
float _price = 0;
};
// -- END OF FILE --

View File

@@ -0,0 +1,211 @@
//******************************************************************************************
// File: PS_HX711.h
// Authors: Malcolm Dobson + Tim OCallaghan (Based on original programming by Dan G Ogorchock & Daniel J Ogorchock (Father and Son) )
//
// Summary: PS_HX711 is a class which implements only the SmartThings "Sensor" device capability.
// It inherits from the st::PollingSensor class. The current version is made to be used as a framework
// for more complicated programming
//
// Create an instance of this class in your sketch's global variable section
// For Example: st::PS_HX711 sensor1(F("weight1"), 120, 0);
//
// st::PS_HX711() constructor requires the following arguments
// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
// - long interval - REQUIRED - the polling interval in seconds
// - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time
//
// This class supports receiving configuration data from the SmartThings cloud via the ST App. A user preference
// can be configured in your phone's ST App, and then the "Configure" tile will send the data for all sensors to
// the ST Shield. For PollingSensors, this data is handled in the beSMart() function.
//
// TODO: Determine a method to persist the ST Cloud's Polling Interval data
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2015-01-03 Dan & Daniel Original Creation
// 2017-08-30 Dan Ogorchock Modified comment section above to comply with new Parent/Child Device Handler requirements
// 2017-10-20 Allan (vseven) Modified original PS_Illuminance library for use with a generic sensor
// 2017-12-28 Dan Ogorchock Fixed bug with improper init() definition
// 2021-01-19 Malcolm Dobson Redone to support HX711 weight sensor
// 2021-01-23 Tim OCallaghan Pass pins on constructor and add storing of calibration factor in EEPROM and don't tare in init
// 2021-01-24 Malcolm Dobson Save the offset also in eeprom and then read and set in init
// 2022-02-08 Dan Ogorchock Minor comments clean up
//
//
//******************************************************************************************
#include "PS_HX711.h"
#include "Constants.h"
#include "Everything.h"
#include <EEPROM.h>
const int calVal_eepromAdress = 0;
namespace st
{
//private
//public
//constructor - called in your sketch's global variable declaration section
PS_HX711::PS_HX711(const __FlashStringHelper *name, byte dout_pin, byte clk_pin, unsigned int interval, int offset):
PollingSensor(name, interval, offset), m_nSensorValue(0)
{
m_n_dout_pin = dout_pin;
m_n_clk_pin = clk_pin;
}
//destructor
PS_HX711::~PS_HX711()
{
}
//Incoming data handler (receives commands and/or configuration data from the hub)
void PS_HX711::beSmart(const String &str)
{
String s = str.substring(str.indexOf(' ') + 1);
String cmd = s.substring(0, s.indexOf(':'));
if(cmd==F("calibrate")) {
cmd = s.substring(s.indexOf(':') + 1);
calibrate(cmd.toFloat());
}
else if(s==F("tare")) {
tare();
}
else if(s==F("updated")) {
updated();
}
else if (s.toInt() != 0) {
st::PollingSensor::setInterval(s.toInt() * 1000);
if (st::PollingSensor::debug) {
Serial.print(F("PS_HX711::beSmart set polling interval to "));
Serial.println(s.toInt());
}
}
else {
if (st::PollingSensor::debug)
{
Serial.print(F("PS_HX711::beSmart cannot convert "));
Serial.print(s);
Serial.println(F(" to an Integer."));
}
}
}
void PS_HX711::init() {
Serial.println(F("Initiating the PS_HX711 class"));
// setup HX711
scale.begin(m_n_dout_pin, m_n_clk_pin); // Start scale on specified pins
//get calibration factor from eeprom
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(512);
#endif
EEPROM.get(calVal_eepromAdress, calibration_factor);
EEPROM.get(calVal_eepromAdress + sizeof(int), calibration_offset);
Serial.println(F("PS_HX711::init read the EEPROM"));
Serial.println(calibration_factor);
Serial.println(calibration_offset);
scale.wait_ready(); //Ensure scale is ready, this is a blocking function
scale.set_scale();
scale.set_offset(calibration_offset); // Sets the calibration offset.
Serial.println(F("Scale Set"));
// tjo commenting out tare();
}
void PS_HX711::calibrate(const float weight)
{
float raw;
Serial.print(F("calibrate routine called with parameter:"));
Serial.println(weight);
if (weight > 0.0)
{
scale.wait_ready();
raw = scale.read_average(5);
calibration_factor = (raw - calibration_offset) / weight;
WriteEEPROM(calVal_eepromAdress, calibration_factor);
Serial.println(F("calibration saved to EEPROM"));
updated();
}
else {
Serial.println(F("calibration weight must be positive"));
}
}
//called periodically to re-zero the scale
void PS_HX711::tare()
{
scale.wait_ready();
scale.tare(); // Tare scale on startup
scale.wait_ready();
updated();
calibration_offset = scale.read_average(5);
WriteEEPROM(calVal_eepromAdress + sizeof(int), calibration_offset);
Serial.print(F("Scale Zeroed, with offset:"));
Serial.println(calibration_offset);
}
//function to get data from sensor and queue results for transfer to hub
void PS_HX711::getData()
{
Serial.println(F("getData routine called"));
//Get the data
float raw;
scale.wait_ready();
raw = scale.read_average(5); // Read average of 5 raw value from scale
Serial.print(F("Raw: "));
Serial.print(raw);
Serial.print(F(", Calibration factor: ")); // Prints calibration factor.
Serial.print(calibration_factor);
Serial.print(F(", lastValue: ")); // Prints lastValue .
Serial.println(lastValue);
// Assign the data to the sensor value. m_nSensorValue is a
// ST_Anything standard for the value of the sensor.
// Only publish new value if difference of 500 or more in raw value
if (abs(lastValue - raw) >= 500) {
float reading;
scale.wait_ready(); // Wait till scale is ready, this is blocking if your hardware is not connected properly.
scale.set_scale(calibration_factor); // Sets the calibration factor.
reading = scale.get_units(5); // Read average of 5 values in g/Kg
lastValue = raw;
String m_nSensorValue = String(reading, 2);
// To make it easier to debug print out our name and sensor value before sending it
Serial.print(getName());
Serial.println(m_nSensorValue);
// Send the value to our parent which will then update the device handler
Everything::sendSmartString(getName() + " " + String(m_nSensorValue));
}
}
void PS_HX711::WriteEEPROM(int address, int value)
{
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(512);
#endif
EEPROM.put(address, value);
#if defined(ESP8266)|| defined(ESP32)
EEPROM.commit();
#endif
}
}

View File

@@ -0,0 +1,93 @@
//******************************************************************************************
// File: PS_HX711.h
// Authors: Malcolm Dobson + Tim OCallaghan (Based on original programming by Dan G Ogorchock & Daniel J Ogorchock (Father and Son) )
//
// Summary: PS_HX711 is a class which implements only the SmartThings "Sensor" device capability.
// It inherits from the st::PollingSensor class. The current version is made to be used as a framework
// for more complicated programming
//
// Create an instance of this class in your sketch's global variable section
// For Example: st::PS_HX711 sensor1(F("weight1"), 120, 0);
//
// st::PS_HX711() constructor requires the following arguments
// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
// - long interval - REQUIRED - the polling interval in seconds
// - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time
//
// This class supports receiving configuration data from the SmartThings cloud via the ST App. A user preference
// can be configured in your phone's ST App, and then the "Configure" tile will send the data for all sensors to
// the ST Shield. For PollingSensors, this data is handled in the beSMart() function.
//
// TODO: Determine a method to persist the ST Cloud's Polling Interval data
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2015-01-03 Dan & Daniel Original Creation
// 2017-08-30 Dan Ogorchock Modified comment section above to comply with new Parent/Child Device Handler requirements
// 2017-10-20 Allan (vseven) Modified original PS_Illuminance library for use with a generic sensor
// 2017-12-28 Dan Ogorchock Fixed bug with improper init() definition
// 2021-01-19 Malcolm Dobson Redone to support HX711 weight sensor
// 2021-01-23 Tim OCallaghan Pass pins on constructor and add storing of calibration factor in EEPROM and don't tare in init
// 2021-01-24 Malcolm Dobson Save the offset also in eeprom and then read and set in init
// 2022-02-08 Dan Ogorchock Minor comments clean up
//
//
//******************************************************************************************
#include "PollingSensor.h"
#include <HX711.h>
namespace st
{
class PS_HX711: public PollingSensor
{
private:
char m_nSensorValue; //converted to a string so all data can be passed in one call
HX711 scale;
int calibration_factor = -16900; // Defines factor we'll use for calibrating.
int calibration_offset = 0; // Defines zero offset we'll use for calibrating.
int lastValue = -1;
byte m_n_dout_pin; //esp pin for hx711 dout
byte m_n_clk_pin; //esp pin for hx711 clk
public:
//constructor - called in your sketch's global variable declaration section
//this can be modified to accepting more varialbes into it by modifying the
//line. For example if you wanted to assign a pin or a variable you could add
//a ", int myVariable" after the offset below and then use that within your program.
//you would need to also update the associated line in the .cpp file.
PS_HX711(const __FlashStringHelper *name, byte dout_pin, byte clk_pin, unsigned int interval, int offset);
//destructor
virtual ~PS_HX711();
//SmartThings Shield data handler (receives configuration data from ST - polling interval, and adjusts on the fly)
virtual void beSmart(const String &str);
//initialization routine
virtual void init();
//calibrate scale
void calibrate(const float weight);
//reset scale to zero
void tare();
//update device, so next read will publish a new value
inline void updated() {lastValue = -1;}
void WriteEEPROM(int address, int value);
//function to get data from sensor and queue results for transfer to ST Cloud
virtual void getData();
//gets
inline byte getSensorValue() const {return m_nSensorValue;}
//sets
// set a calibration factor so the scale report accurate weight
void setCalibration(const int factor) {calibration_factor = factor;}
inline void clearLastValue() {lastValue = -1;}
};
}