From dd4e2037da5fe4a66b6859feea40d79aeb2c3c43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 22 Jan 2017 23:47:58 +0100 Subject: [PATCH] I2C and ADC121 implementation --- code/espurna/config/arduino.h | 2 +- code/espurna/config/general.h | 9 ++++++ code/espurna/config/sensors.h | 28 +++++++++++++---- code/espurna/emon.ino | 58 +++++++++++++++++++++++++++++++---- code/espurna/espurna.ino | 3 ++ code/espurna/i2c.ino | 43 ++++++++++++++++++++++++++ code/platformio.ini | 3 +- 7 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 code/espurna/i2c.ino diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 489c42b8..70ba02ef 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -35,7 +35,7 @@ //#define ENABLE_DHT 1 //#define ENABLE_DS18B20 1 -//#define ENABLE_EMON 1 +#define ENABLE_EMON 1 //#define ENABLE_HLW8018 1 //#define ENABLE_RF 1 //#define ENABLE_FAUXMO 0 diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 4d0e3b5a..fba55293 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -95,6 +95,15 @@ #define MQTT_USE_GETTER "" #define MQTT_USE_SETTER "" +// ----------------------------------------------------------------------------- +// I2C +// ----------------------------------------------------------------------------- + +#define ENABLE_I2C 1 +#define I2C_SDA_PIN 4 +#define I2C_SCL_PIN 14 +#define I2C_CLOCK_STRETCH_TIME 200 +#define I2C_SCL_FREQUENCY 1000 // ----------------------------------------------------------------------------- // DOMOTICZ diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index fa69ee5a..88f486e0 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -35,16 +35,32 @@ // Enable support by passing ENABLE_EMON=1 build flag //-------------------------------------------------------------------------------- -#define EMON_CURRENT_PIN 0 +#define EMON_ANALOG_PROVIDER 0 +#define EMON_ADC121_PROVIDER 1 + +#define EMON_PROVIDER EMON_ADC121_PROVIDER + +#if EMON_PROVIDER == EMON_ANALOG_PROVIDER + #define EMON_CURRENT_PIN 0 + #define EMON_ADC_BITS 10 + #define EMON_REFERENCE_VOLTAGE 1.0 + #define EMON_CURRENT_PRECISION 1 + #define EMON_CURRENT_OFFSET 0.25 +#endif + +#if EMON_PROVIDER == EMON_ADC121_PROVIDER + #define EMON_ADC121_ADDRESS 0x50 + #define EMON_ADC_BITS 12 + #define EMON_REFERENCE_VOLTAGE 3.3 + #define EMON_CURRENT_PRECISION 2 + #define EMON_CURRENT_OFFSET 0 +#endif + +#define EMON_CURRENT_RATIO 30 #define EMON_SAMPLES 1000 #define EMON_INTERVAL 10000 #define EMON_MEASUREMENTS 6 -#define EMON_ADC_BITS 10 -#define EMON_REFERENCE_VOLTAGE 1.0 -#define EMON_CURRENT_PRECISION 1 -#define EMON_CURRENT_OFFSET 0.25 #define EMON_MAINS_VOLTAGE 230 -#define EMON_CURRENT_RATIO 180 #define EMON_POWER_TOPIC "/power" //-------------------------------------------------------------------------------- diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index c9abb550..741f114e 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -9,11 +9,49 @@ Copyright (C) 2016-2017 by Xose Pérez #if ENABLE_EMON #include +#include "brzo_i2c.h" + +// ADC121 Registers + +#define ADC121_REG_RESULT 0x00 +#define ADC121_REG_ALERT 0x01 +#define ADC121_REG_CONFIG 0x02 +#define ADC121_REG_LIMITL 0x03 +#define ADC121_REG_LIMITH 0x04 +#define ADC121_REG_HYST 0x05 +#define ADC121_REG_CONVL 0x06 +#define ADC121_REG_CONVH 0x07 + EmonLiteESP emon; double _current = 0; unsigned int _power = 0; +// ----------------------------------------------------------------------------- +// Provider +// ----------------------------------------------------------------------------- + +unsigned int currentCallback() { + + #if EMON_PROVIDER == EMON_ANALOG_PROVIDER + return analogRead(EMON_CURRENT_PIN); + #endif + + #if EMON_PROVIDER == EMON_ADC121_PROVIDER + uint8_t buffer[2]; + brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY); + buffer[0] = ADC121_REG_RESULT; + brzo_i2c_write(buffer, 1, false); + brzo_i2c_read(buffer, 2, false); + brzo_i2c_end_transaction(); + unsigned int value; + value = (buffer[0] & 0x0F) << 8; + value |= buffer[1]; + return value; + #endif + +} + // ----------------------------------------------------------------------------- // EMON // ----------------------------------------------------------------------------- @@ -30,10 +68,6 @@ double getCurrent() { return _current; } -unsigned int currentCallback() { - return analogRead(EMON_CURRENT_PIN); -} - void powerMonitorSetup() { // backwards compatibility @@ -53,6 +87,15 @@ void powerMonitorSetup() { ); emon.setPrecision(EMON_CURRENT_PRECISION); + #if EMON_PROVIDER == EMON_ADC121_PROVIDER + uint8_t buffer[2]; + buffer[0] = ADC121_REG_CONFIG; + buffer[1] = 0x00; + brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY); + brzo_i2c_write(buffer, 2, false); + brzo_i2c_end_transaction(); + #endif + apiRegister("/api/power", "power", [](char * buffer, size_t len) { snprintf(buffer, len, "%d", _power); }); @@ -97,7 +140,10 @@ void powerMonitorLoop() { float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat(); - //DEBUG_MSG("[ENERGY] Power now: %dW\n", int(_current * mainsVoltage)); + char current[6]; + dtostrf(_current, 5, 2, current); + DEBUG_MSG("[ENERGY] Current: %sA\n", current); + DEBUG_MSG("[ENERGY] Power: %dW\n", int(_current * mainsVoltage)); // Update websocket clients char text[20]; @@ -112,7 +158,7 @@ void powerMonitorLoop() { measurements = 0; char power[6]; - snprintf(power, "%d", 6, _power); + snprintf(power, 6, "%d", _power); mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power); #if ENABLE_DOMOTICZ domoticzSend("dczPowIdx", power); diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index cafc7f22..3461e6b4 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -107,6 +107,9 @@ void setup() { webSetup(); ntpSetup(); + #if ENABLE_I2C + i2cSetup(); + #endif #if ENABLE_FAUXMO fauxmoSetup(); #endif diff --git a/code/espurna/i2c.ino b/code/espurna/i2c.ino new file mode 100644 index 00000000..1063d143 --- /dev/null +++ b/code/espurna/i2c.ino @@ -0,0 +1,43 @@ +/* + +I2C MODULE + +Copyright (C) 2017 by Xose Pérez + +*/ + +#if ENABLE_I2C + +#include "brzo_i2c.h" + +void i2cScan() { + + uint8_t address; + uint8_t response; + uint8_t buffer[1]; + int nDevices = 0; + + for (address = 1; address < 128; address++) { + + brzo_i2c_start_transaction(address, I2C_SCL_FREQUENCY); + brzo_i2c_ACK_polling(1000); + response = brzo_i2c_end_transaction(); + + if (response == 0) { + Serial.printf("[I2C] Device found at address 0x%02X\n", address); + nDevices++; + } else if (response != 32) { + Serial.printf("[I2C] Unknown error at address 0x%02X\n", address); + } + } + + if (nDevices == 0) Serial.println("[I2C] No devices found"); + +} + +void i2cSetup() { + brzo_i2c_setup(I2C_SDA_PIN, I2C_SCL_PIN, I2C_CLOCK_STRETCH_TIME); + i2cScan(); +} + +#endif diff --git a/code/platformio.ini b/code/platformio.ini index cb9ca7eb..ff2d6fe8 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -16,6 +16,7 @@ lib_deps = NtpClientLib OneWire DallasTemperature + Brzo I2C https://bitbucket.org/xoseperez/justwifi.git https://bitbucket.org/xoseperez/hlw8012.git https://bitbucket.org/xoseperez/fauxmoesp.git @@ -88,7 +89,7 @@ extra_script = pio_hooks.py build_flags = -g -Wl,-Tesp8266.flash.1m128.ld -DDEBUG_PORT=Serial -DSONOFF upload_speed = 115200 upload_port = "192.168.4.1" -upload_flags = --auth=fibonacci --port 8266 +upload_flags = --auth=Algernon1 --port 8266 [env:sonoff-dht22-debug] platform = espressif8266