Added preliminary Lora sender code
This commit is contained in:
parent
6b41889595
commit
cf68aa8c62
|
@ -0,0 +1 @@
|
|||
*/*-backups/*.zip
|
|
@ -1,12 +1,12 @@
|
|||
#
|
||||
# Project Configuration File
|
||||
#
|
||||
# A detailed documentation with the EXAMPLES is located here:
|
||||
# http://docs.platformio.org/en/latest/projectconf.html
|
||||
#
|
||||
|
||||
# A sign `#` at the beginning of the line indicates a comment
|
||||
# Comment lines are ignored.
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[platformio]
|
||||
default_envs = emonesp-nodemcu32s, emonesp_spiffs
|
||||
|
@ -14,34 +14,36 @@ data_dir = src/data
|
|||
|
||||
[common]
|
||||
version = -DBUILD_TAG=1.11
|
||||
platform = https://github.com/platformio/platform-espressif32.git
|
||||
platform = espressif32@^6.6.0
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
lib_deps =
|
||||
PubSubClient
|
||||
https://github.com/me-no-dev/ESPAsyncWebServer.git
|
||||
me-no-dev/AsyncTCP @ ^1.1.1
|
||||
circuitsetup/ATM90E32
|
||||
plapointe6/HAMqttDevice@^1.4.0
|
||||
plapointe6/HAMqttDevice@^1.4.0
|
||||
https://github.com/Aircoookie/ESPAsyncWebServer.git
|
||||
RadioLib
|
||||
ArduinoJson
|
||||
monitor_speed = 115200
|
||||
board_build.flash_mode = dio
|
||||
board_build.flash_mode = qio
|
||||
board_build.f_cpu = 240000000L
|
||||
board_build.f_flash = 40000000L
|
||||
upload_speed = 921600
|
||||
upload_resetmethod = nodemcu
|
||||
platform_packages =
|
||||
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32#master
|
||||
platform_packages =
|
||||
framework-arduinoespressif32
|
||||
board_build.filesystem = littlefs
|
||||
|
||||
[env:emonesp-nodemcu32s]
|
||||
board = nodemcu-32s
|
||||
platform = ${common.platform}
|
||||
framework = ${common.framework}
|
||||
lib_deps = ${common.lib_deps}
|
||||
build_flags =
|
||||
${common.version}
|
||||
-DCS_PLATFORM=CS_P_ESP32
|
||||
-DWIFI_LED=LED_BUILTIN
|
||||
-DENABLE_DEBUG
|
||||
-DCORE_DEBUG_LEVEL=2
|
||||
lib_deps =
|
||||
${common.lib_deps}
|
||||
build_flags =
|
||||
${common.version}
|
||||
-DCS_PLATFORM=CS_P_ESP32
|
||||
-DWIFI_LED=LED_BUILTIN
|
||||
-DENABLE_DEBUG
|
||||
-DCORE_DEBUG_LEVEL=2
|
||||
monitor_speed = ${common.monitor_speed}
|
||||
board_build.flash_mode = ${common.board_build.flash_mode}
|
||||
board_build.f_cpu = ${common.board_build.f_cpu}
|
||||
|
@ -57,19 +59,35 @@ upload_speed = ${common.upload_speed}
|
|||
upload_resetmethod = ${common.upload_resetmethod}
|
||||
targets = buildfs
|
||||
|
||||
|
||||
[env:emonesp_deploy]
|
||||
extends = env:emonesp-nodemcu32s
|
||||
build_src_flags = !(test -z $TRAVIS_TAG && echo '${common.version}') || echo '-DBUILD_TAG='$TRAVIS_TAG
|
||||
|
||||
# http://docs.platformio.org/en/latest/platforms/espressif8266.html#using-arduino-framework-with-staging-version
|
||||
# Staging Arduino framework and common lib version
|
||||
[env:emonesp_staging]
|
||||
extends = env:emonesp-nodemcu32s
|
||||
platform_packages =
|
||||
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git
|
||||
lib_deps =
|
||||
https://github.com/knolleary/pubsubclient,
|
||||
https://github.com/me-no-dev/ESPAsyncWebServer.git,
|
||||
https://github.com/me-no-dev/AsyncTCP.git,
|
||||
https://github.com/CircuitSetup/ATM90E32.git
|
||||
platform_packages =
|
||||
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git
|
||||
lib_deps =
|
||||
https://github.com/knolleary/pubsubclient,
|
||||
https://github.com/fhessel/esp32_https_server.git
|
||||
|
||||
[env:heltec_wifi_lora_32_V3]
|
||||
board = heltec_wifi_lora_32_V3
|
||||
platform = ${common.platform}
|
||||
framework = ${common.framework}
|
||||
lib_deps =
|
||||
${common.lib_deps}
|
||||
build_flags =
|
||||
${common.version}
|
||||
-DWIFI_LED=LED_BUILTIN
|
||||
# -DENABLE_DEBUG
|
||||
-DCORE_DEBUG_LEVEL=1
|
||||
-DWIFI_BUTTON=0
|
||||
-DHELTECLORA32V3
|
||||
-DENABLE_LORASENDER
|
||||
monitor_speed = ${common.monitor_speed}
|
||||
board_build.f_cpu = ${common.board_build.f_cpu}
|
||||
board_build.partitions = custom.csv
|
||||
upload_speed = ${common.upload_speed}
|
||||
upload_resetmethod = ${common.upload_resetmethod}
|
||||
board_build.filesystem = ${common.board_build.filesystem}
|
|
@ -44,6 +44,10 @@ Original can be found here : https://github.com/CircuitSetup/Expandable-6-Channe
|
|||
- [X] Change port number to always use 2 digits for correct sorting
|
||||
- [X] Display active power as 'VA' on web interface
|
||||
- [ ] Add auto-calibration method
|
||||
- [ ] Rewrite OTA system
|
||||
- [ ] Support ESP32-S3
|
||||
- [ ] Move from SPIFFS to LittleFS
|
||||
- [ ] Drop support for ESP8266 (shouldn't work though)
|
||||
|
||||
### Calibration well known values
|
||||
- SCT-006 20A/25mA : 10895
|
||||
|
|
|
@ -0,0 +1,540 @@
|
|||
/* ATM90E32 Energy Monitor Functions
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 whatnick,Ryzee and Arun
|
||||
|
||||
Modified to use with the CircuitSetup.us Split Phase Energy Meter by jdeglavina
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "ATM90E32.h"
|
||||
#include "config.h"
|
||||
|
||||
ATM90E32::ATM90E32(void){
|
||||
}
|
||||
|
||||
ATM90E32::~ATM90E32() {
|
||||
// end
|
||||
}
|
||||
|
||||
/* CommEnergyIC - Communication Establishment */
|
||||
/*
|
||||
- Defines Register Mask
|
||||
- Treats the Register and SPI Comms
|
||||
- Outputs the required value in the register
|
||||
*/
|
||||
unsigned short ATM90E32::CommEnergyIC(unsigned char RW, unsigned short address, unsigned short val)
|
||||
{
|
||||
unsigned char* data = (unsigned char*)&val;
|
||||
unsigned char* adata = (unsigned char*)&address;
|
||||
unsigned short output;
|
||||
unsigned short address1;
|
||||
|
||||
//SPI interface rate is 200 to 160k bps. It will need to be slowed down for EnergyIC
|
||||
#if !defined(ENERGIA) && !defined(ESP8266) && !defined(ESP32) && !defined(ARDUINO_ARCH_SAMD)
|
||||
SPISettings settings(200000, MSBFIRST, SPI_MODE0);
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266)
|
||||
SPISettings settings(200000, MSBFIRST, SPI_MODE1);
|
||||
#endif
|
||||
|
||||
#if defined(ESP32)
|
||||
SPISettings settings(200000, MSBFIRST, SPI_MODE3);
|
||||
#endif
|
||||
|
||||
#if defined(ARDUINO_ARCH_SAMD)
|
||||
SPISettings settings(200000, MSBFIRST, SPI_MODE3);
|
||||
#endif
|
||||
|
||||
// Switch MSB and LSB of value
|
||||
output = (val >> 8) | (val << 8);
|
||||
val = output;
|
||||
|
||||
// Set R/W flag
|
||||
address |= RW << 15;
|
||||
|
||||
// Swap byte address
|
||||
address1 = (address >> 8) | (address << 8);
|
||||
address = address1;
|
||||
|
||||
// Transmit & Receive Data
|
||||
#if !defined(ENERGIA)
|
||||
SPI.beginTransaction(settings);
|
||||
#endif
|
||||
|
||||
// Chip enable and wait for SPI activation
|
||||
digitalWrite(_cs, LOW);
|
||||
delayMicroseconds(10);
|
||||
|
||||
// Write address byte by byte
|
||||
for (byte i = 0; i < 2; i++)
|
||||
{
|
||||
SPI.transfer(*adata);
|
||||
adata++;
|
||||
}
|
||||
|
||||
/* Must wait 4 us for data to become valid */
|
||||
delayMicroseconds(4);
|
||||
|
||||
// READ Data
|
||||
// Do for each byte in transfer
|
||||
if (RW)
|
||||
{
|
||||
for (byte i = 0; i < 2; i++)
|
||||
{
|
||||
*data = SPI.transfer(0x00);
|
||||
data++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (byte i = 0; i < 2; i++)
|
||||
{
|
||||
SPI.transfer(*data);
|
||||
data++;
|
||||
}
|
||||
}
|
||||
|
||||
// Chip enable and wait for transaction to end
|
||||
digitalWrite(_cs, HIGH);
|
||||
delayMicroseconds(10);
|
||||
#if !defined(ENERGIA)
|
||||
SPI.endTransaction();
|
||||
#endif
|
||||
|
||||
output = (val >> 8) | (val << 8); // reverse MSB and LSB
|
||||
return output;
|
||||
}
|
||||
|
||||
int ATM90E32::Read32Register(signed short regh_addr, signed short regl_addr) {
|
||||
int val, val_h, val_l;
|
||||
val_h = CommEnergyIC(READ, regh_addr, 0xFFFF);
|
||||
val_l = CommEnergyIC(READ, regl_addr, 0xFFFF);
|
||||
val = CommEnergyIC(READ, regh_addr, 0xFFFF);
|
||||
|
||||
val = val_h << 16;
|
||||
val |= val_l; //concatenate the 2 registers to make 1 32 bit number
|
||||
|
||||
return (val);
|
||||
}
|
||||
|
||||
double ATM90E32::CalculateVIOffset(unsigned short regh_addr, unsigned short regl_addr /*, unsigned short offset_reg*/) {
|
||||
//for getting the lower registers of Voltage and Current and calculating the offset
|
||||
//should only be run when CT sensors are connected to the meter,
|
||||
//but not connected around wires
|
||||
uint32_t val, val_h, val_l;
|
||||
uint16_t offset;
|
||||
val_h = CommEnergyIC(READ, regh_addr, 0xFFFF);
|
||||
val_l = CommEnergyIC(READ, regl_addr, 0xFFFF);
|
||||
val = CommEnergyIC(READ, regh_addr, 0xFFFF);
|
||||
|
||||
val = val_h << 16; //move high register up 16 bits
|
||||
val |= val_l; //concatenate the 2 registers to make 1 32 bit number
|
||||
val = val >> 7; //right shift 7 bits - lowest 7 get ignored - V & I registers need this
|
||||
val = (~val) + 1; //2s compliment
|
||||
|
||||
offset = val; //keep lower 16 bits
|
||||
//CommEnergyIC(WRITE, offset_reg, (signed short)val);
|
||||
return uint16_t(offset);
|
||||
}
|
||||
|
||||
double ATM90E32::CalculatePowerOffset(unsigned short regh_addr, unsigned short regl_addr /*, unsigned short offset_reg*/) {
|
||||
//for getting the lower registers of energy and calculating the offset
|
||||
//should only be run when CT sensors are connected to the meter,
|
||||
//but not connected around wires
|
||||
uint32_t val, val_h, val_l;
|
||||
uint16_t offset;
|
||||
val_h = CommEnergyIC(READ, regh_addr, 0xFFFF);
|
||||
val_l = CommEnergyIC(READ, regl_addr, 0xFFFF);
|
||||
val = CommEnergyIC(READ, regh_addr, 0xFFFF);
|
||||
|
||||
val = val_h << 16; //move high register up 16 bits
|
||||
val |= val_l; //concatenate the 2 registers to make 1 32 bit number
|
||||
val = (~val) + 1; //2s compliment
|
||||
|
||||
offset = val; //keep lower 16 bits
|
||||
//CommEnergyIC(WRITE, offset_reg, (signed short)val);
|
||||
return uint16_t(offset);
|
||||
}
|
||||
|
||||
double ATM90E32::CalibrateVI(unsigned short reg, unsigned short actualVal) {
|
||||
//input the Voltage or Current register, and the actual value that it should be
|
||||
//actualVal can be from a calibration meter or known value from a power supply
|
||||
uint16_t gain, val, m, gainReg;
|
||||
//sample the reading
|
||||
val = CommEnergyIC(READ, reg, 0xFFFF);
|
||||
val += CommEnergyIC(READ, reg, 0xFFFF);
|
||||
val += CommEnergyIC(READ, reg, 0xFFFF);
|
||||
val += CommEnergyIC(READ, reg, 0xFFFF);
|
||||
|
||||
//get value currently in gain register
|
||||
switch (reg) {
|
||||
case UrmsA: {
|
||||
gainReg = UgainA; }
|
||||
case UrmsB: {
|
||||
gainReg = UgainB; }
|
||||
case UrmsC: {
|
||||
gainReg = UgainC; }
|
||||
case IrmsA: {
|
||||
gainReg = IgainA; }
|
||||
case IrmsB: {
|
||||
gainReg = IgainB; }
|
||||
case IrmsC: {
|
||||
gainReg = IgainC; }
|
||||
}
|
||||
|
||||
gain = CommEnergyIC(READ, gainReg, 0xFFFF);
|
||||
m = actualVal;
|
||||
m = ((m * gain) / val);
|
||||
gain = m;
|
||||
|
||||
//write new value to gain register
|
||||
CommEnergyIC(WRITE, gainReg, gain);
|
||||
|
||||
return(gain);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Parameters Functions*/
|
||||
/*
|
||||
- Gets main electrical parameters,
|
||||
such as: Voltage, Current, Power, Energy,
|
||||
and Frequency, and Temperature
|
||||
|
||||
*/
|
||||
// VOLTAGE
|
||||
double ATM90E32::GetLineVoltageA() {
|
||||
unsigned short voltage = CommEnergyIC(READ, UrmsA, 0xFFFF);
|
||||
return (double)voltage / 100;
|
||||
}
|
||||
double ATM90E32::GetLineVoltageB() {
|
||||
unsigned short voltage = CommEnergyIC(READ, UrmsB, 0xFFFF);
|
||||
return (double)voltage / 100;
|
||||
}
|
||||
double ATM90E32::GetLineVoltageC() {
|
||||
unsigned short voltage = CommEnergyIC(READ, UrmsC, 0xFFFF);
|
||||
return (double)voltage / 100;
|
||||
}
|
||||
|
||||
// CURRENT
|
||||
double ATM90E32::GetLineCurrentA() {
|
||||
unsigned short current = CommEnergyIC(READ, IrmsA, 0xFFFF);
|
||||
return (double)current / 1000;
|
||||
}
|
||||
double ATM90E32::GetLineCurrentB() {
|
||||
unsigned short current = CommEnergyIC(READ, IrmsB, 0xFFFF);
|
||||
return (double)current / 1000;
|
||||
}
|
||||
double ATM90E32::GetLineCurrentC() {
|
||||
unsigned short current = CommEnergyIC(READ, IrmsC, 0xFFFF);
|
||||
return (double)current / 1000;
|
||||
}
|
||||
|
||||
double ATM90E32::GetLineCurrentN() {
|
||||
unsigned short current = CommEnergyIC(READ, IrmsN, 0xFFFF);
|
||||
return (double)current / 1000;
|
||||
}
|
||||
|
||||
// ACTIVE POWER
|
||||
double ATM90E32::GetActivePowerA() {
|
||||
int val = Read32Register(PmeanA, PmeanALSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetActivePowerB() {
|
||||
int val = Read32Register(PmeanB, PmeanBLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetActivePowerC() {
|
||||
int val = Read32Register(PmeanC, PmeanCLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetTotalActivePower() {
|
||||
int val = Read32Register(PmeanT, PmeanTLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
|
||||
// Active Fundamental Power
|
||||
double ATM90E32::GetTotalActiveFundPower() {
|
||||
int val = Read32Register(PmeanTF, PmeanTFLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
|
||||
// Active Harmonic Power
|
||||
double ATM90E32::GetTotalActiveHarPower() {
|
||||
int val = Read32Register(PmeanTH, PmeanTHLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
|
||||
|
||||
// REACTIVE POWER
|
||||
double ATM90E32::GetReactivePowerA() {
|
||||
int val = Read32Register(QmeanA, QmeanALSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetReactivePowerB() {
|
||||
int val = Read32Register(QmeanB, QmeanBLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetReactivePowerC() {
|
||||
int val = Read32Register(QmeanC, QmeanCLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetTotalReactivePower() {
|
||||
int val = Read32Register(QmeanT, QmeanTLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
|
||||
// APPARENT POWER
|
||||
double ATM90E32::GetApparentPowerA() {
|
||||
int val = Read32Register(SmeanA, SmeanALSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetApparentPowerB() {
|
||||
int val = Read32Register(SmeanB, SmeanBLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetApparentPowerC() {
|
||||
int val = Read32Register(SmeanC, SmeanCLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
double ATM90E32::GetTotalApparentPower() {
|
||||
int val = Read32Register(SmeanT, SAmeanTLSB);
|
||||
return (double)val * 0.00032;
|
||||
}
|
||||
|
||||
// FREQUENCY
|
||||
double ATM90E32::GetFrequency() {
|
||||
unsigned short freq = CommEnergyIC(READ, Freq, 0xFFFF);
|
||||
return (double)freq / 100;
|
||||
}
|
||||
|
||||
// POWER FACTOR
|
||||
double ATM90E32::GetPowerFactorA() {
|
||||
signed short pf = (signed short) CommEnergyIC(READ, PFmeanA, 0xFFFF);
|
||||
return (double)pf / 1000;
|
||||
}
|
||||
double ATM90E32::GetPowerFactorB() {
|
||||
signed short pf = (signed short) CommEnergyIC(READ, PFmeanB, 0xFFFF);
|
||||
return (double)pf / 1000;
|
||||
}
|
||||
double ATM90E32::GetPowerFactorC() {
|
||||
signed short pf = (signed short) CommEnergyIC(READ, PFmeanC, 0xFFFF);
|
||||
return (double)pf / 1000;
|
||||
}
|
||||
double ATM90E32::GetTotalPowerFactor() {
|
||||
signed short pf = (signed short) CommEnergyIC(READ, PFmeanT, 0xFFFF);
|
||||
return (double)pf / 1000;
|
||||
}
|
||||
|
||||
// MEAN PHASE ANGLE
|
||||
double ATM90E32::GetPhaseA() {
|
||||
unsigned short angleA = (unsigned short) CommEnergyIC(READ, PAngleA, 0xFFFF);
|
||||
return (double)angleA / 10;
|
||||
}
|
||||
double ATM90E32::GetPhaseB() {
|
||||
unsigned short angleB = (unsigned short) CommEnergyIC(READ, PAngleB, 0xFFFF);
|
||||
return (double)angleB / 10;
|
||||
}
|
||||
double ATM90E32::GetPhaseC() {
|
||||
unsigned short angleC = (unsigned short) CommEnergyIC(READ, PAngleC, 0xFFFF);
|
||||
return (double)angleC / 10;
|
||||
}
|
||||
|
||||
// TEMPERATURE
|
||||
double ATM90E32::GetTemperature() {
|
||||
short int atemp = (short int) CommEnergyIC(READ, Temp, 0xFFFF);
|
||||
return (double)atemp;
|
||||
}
|
||||
|
||||
/* Gets the Register Value if Desired */
|
||||
// REGISTER
|
||||
double ATM90E32::GetValueRegister(unsigned short registerRead) {
|
||||
return (double) CommEnergyIC(READ, registerRead, 0xFFFF); //returns value register
|
||||
}
|
||||
|
||||
// REGULAR ENERGY MEASUREMENT
|
||||
|
||||
// FORWARD ACTIVE ENERGY
|
||||
// these registers accumulate energy and are cleared after being read
|
||||
double ATM90E32::GetImportEnergy() {
|
||||
unsigned short ienergyT = CommEnergyIC(READ, APenergyT, 0xFFFF);
|
||||
return (double)ienergyT / 100 / 3200; //returns kWh
|
||||
}
|
||||
// unsigned short ienergyA = CommEnergyIC(READ, APenergyA, 0xFFFF);
|
||||
// unsigned short ienergyB = CommEnergyIC(READ, APenergyB, 0xFFFF);
|
||||
// unsigned short ienergyC = CommEnergyIC(READ, APenergyC, 0xFFFF);
|
||||
|
||||
// FORWARD REACTIVE ENERGY
|
||||
double ATM90E32::GetImportReactiveEnergy() {
|
||||
unsigned short renergyT = CommEnergyIC(READ, RPenergyT, 0xFFFF);
|
||||
return (double)renergyT / 100 / 3200; //returns kWh
|
||||
}
|
||||
// unsigned short renergyA = CommEnergyIC(READ, RPenergyA, 0xFFFF);
|
||||
// unsigned short renergyB = CommEnergyIC(READ, RPenergyB, 0xFFFF);
|
||||
// unsigned short renergyC = CommEnergyIC(READ, RPenergyC, 0xFFFF);
|
||||
|
||||
// APPARENT ENERGY
|
||||
double ATM90E32::GetImportApparentEnergy() {
|
||||
unsigned short senergyT = CommEnergyIC(READ, SAenergyT, 0xFFFF);
|
||||
return (double)senergyT / 100 / 3200; //returns kWh
|
||||
}
|
||||
// unsigned short senergyA = CommEnergyIC(READ, SenergyA, 0xFFFF);
|
||||
// unsigned short senergyB = CommEnergyIC(READ, SenergyB, 0xFFFF);
|
||||
// unsigned short senergyC = CommEnergyIC(READ, SenergyC, 0xFFFF);
|
||||
|
||||
// REVERSE ACTIVE ENERGY
|
||||
double ATM90E32::GetExportEnergy() {
|
||||
unsigned short eenergyT = CommEnergyIC(READ, ANenergyT, 0xFFFF);
|
||||
return (double)eenergyT / 100 / 3200; //returns kWh
|
||||
}
|
||||
// unsigned short eenergyA = CommEnergyIC(READ, ANenergyA, 0xFFFF);
|
||||
// unsigned short eenergyB = CommEnergyIC(READ, ANenergyB, 0xFFFF);
|
||||
// unsigned short eenergyC = CommEnergyIC(READ, ANenergyC, 0xFFFF);
|
||||
|
||||
// REVERSE REACTIVE ENERGY
|
||||
double ATM90E32::GetExportReactiveEnergy() {
|
||||
unsigned short reenergyT = CommEnergyIC(READ, RNenergyT, 0xFFFF);
|
||||
return (double)reenergyT / 100 / 3200; //returns kWh
|
||||
}
|
||||
// unsigned short reenergyA = CommEnergyIC(READ, RNenergyA, 0xFFFF);
|
||||
// unsigned short reenergyB = CommEnergyIC(READ, RNenergyB, 0xFFFF);
|
||||
// unsigned short reenergyC = CommEnergyIC(READ, RNenergyC, 0xFFFF);
|
||||
|
||||
|
||||
|
||||
/* System Status Registers */
|
||||
unsigned short ATM90E32::GetSysStatus0() {
|
||||
return CommEnergyIC(READ, EMMIntState0, 0xFFFF);
|
||||
}
|
||||
unsigned short ATM90E32::GetSysStatus1() {
|
||||
return CommEnergyIC(READ, EMMIntState1, 0xFFFF);
|
||||
}
|
||||
unsigned short ATM90E32::GetMeterStatus0() {
|
||||
return CommEnergyIC(READ, EMMState0, 0xFFFF);
|
||||
}
|
||||
unsigned short ATM90E32::GetMeterStatus1() {
|
||||
return CommEnergyIC(READ, EMMState1, 0xFFFF);
|
||||
}
|
||||
|
||||
/* BEGIN FUNCTION */
|
||||
/*
|
||||
- Define the pin to be used as Chip Select
|
||||
- Set serialFlag to true for serial debugging
|
||||
- Use SPI MODE 0 for the ATM90E32
|
||||
*/
|
||||
void ATM90E32::begin(int pin, unsigned short lineFreq, unsigned short pgagain, unsigned short ugain, unsigned short igainA, unsigned short igainB, unsigned short igainC)
|
||||
{
|
||||
_cs = pin; // SS PIN
|
||||
_lineFreq = lineFreq; //frequency of power
|
||||
_pgagain = pgagain; //PGA Gain for current channels
|
||||
_ugain = ugain; //voltage rms gain
|
||||
_igainA = igainA; //CT1
|
||||
_igainB = igainB; //CT2 - not used for single split phase meter
|
||||
_igainC = igainC; //CT2 for single split phase meter - CT3 otherwise
|
||||
|
||||
pinMode(_cs, OUTPUT);
|
||||
|
||||
/* Enable SPI */
|
||||
SPI.begin(ATM90_SCK, ATM90_MISO, ATM90_MOSI);
|
||||
|
||||
Serial.println("Connecting to ATM90E32");
|
||||
#if defined(ENERGIA)
|
||||
SPI.setBitOrder(MSBFIRST);
|
||||
SPI.setDataMode(SPI_MODE0);
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV16);
|
||||
#endif
|
||||
|
||||
//calculation for voltage sag threshold - assumes we do not want to go under 90v for split phase and 190v otherwise
|
||||
//determine proper low and high frequency threshold
|
||||
unsigned short vSagTh;
|
||||
unsigned short sagV;
|
||||
unsigned short FreqHiThresh;
|
||||
unsigned short FreqLoThresh;
|
||||
if (_lineFreq == 4485 || _lineFreq == 4231)
|
||||
{
|
||||
sagV = 90;
|
||||
FreqHiThresh = 61 * 100;
|
||||
FreqLoThresh = 59 * 100;
|
||||
}
|
||||
else
|
||||
{
|
||||
sagV = 190;
|
||||
FreqHiThresh = 51 * 100;
|
||||
FreqLoThresh = 49 * 100;
|
||||
}
|
||||
|
||||
vSagTh = (sagV * 100 * sqrt(2)) / (2 * _ugain / 32768);
|
||||
|
||||
//Initialize registers
|
||||
CommEnergyIC(WRITE, SoftReset, 0x789A); // 70 Perform soft reset
|
||||
CommEnergyIC(WRITE, CfgRegAccEn, 0x55AA); // 7F enable register config access
|
||||
CommEnergyIC(WRITE, MeterEn, 0x0001); // 00 Enable Metering
|
||||
|
||||
CommEnergyIC(WRITE, SagPeakDetCfg, 0x143F); // 05 Sag and Voltage peak detect period set to 20ms
|
||||
CommEnergyIC(WRITE, SagTh, vSagTh); // 08 Voltage sag threshold
|
||||
CommEnergyIC(WRITE, FreqHiTh, FreqHiThresh); // 0D High frequency threshold
|
||||
CommEnergyIC(WRITE, FreqLoTh, FreqLoThresh); // 0C Lo frequency threshold
|
||||
CommEnergyIC(WRITE, EMMIntEn0, 0xB76F); // 75 Enable interrupts
|
||||
CommEnergyIC(WRITE, EMMIntEn1, 0xDDFD); // 76 Enable interrupts
|
||||
CommEnergyIC(WRITE, EMMIntState0, 0x0001); // 73 Clear interrupt flags
|
||||
CommEnergyIC(WRITE, EMMIntState1, 0x0001); // 74 Clear interrupt flags
|
||||
CommEnergyIC(WRITE, ZXConfig, 0xD654); // 07 ZX2, ZX1, ZX0 pin config - set to current channels, all polarity
|
||||
|
||||
//Set metering config values (CONFIG)
|
||||
CommEnergyIC(WRITE, PLconstH, 0x0861); // 31 PL Constant MSB (default) - Meter Constant = 3200 - PL Constant = 140625000
|
||||
CommEnergyIC(WRITE, PLconstL, 0xC468); // 32 PL Constant LSB (default) - this is 4C68 in the application note, which is incorrect
|
||||
CommEnergyIC(WRITE, MMode0, _lineFreq); // 33 Mode Config (frequency set in main program)
|
||||
CommEnergyIC(WRITE, MMode1, _pgagain); // 34 PGA Gain Configuration for Current Channels - 0x002A (x4) // 0x0015 (x2) // 0x0000 (1x)
|
||||
CommEnergyIC(WRITE, PStartTh, 0x1D4C); // 35 All phase Active Startup Power Threshold - 50% of startup current = 0.02A/0.00032 = 7500
|
||||
CommEnergyIC(WRITE, QStartTh, 0x1D4C); // 36 All phase Reactive Startup Power Threshold
|
||||
CommEnergyIC(WRITE, SStartTh, 0x1D4C); // 37 All phase Apparent Startup Power Threshold
|
||||
CommEnergyIC(WRITE, PPhaseTh, 0x02EE); // 38 Each phase Active Phase Threshold = 10% of startup current = 0.002A/0.00032 = 750
|
||||
CommEnergyIC(WRITE, QPhaseTh, 0x02EE); // 39 Each phase Reactive Phase Threshold
|
||||
CommEnergyIC(WRITE, SPhaseTh, 0x02EE); // 3A Each phase Apparent Phase Threshold
|
||||
|
||||
//Set metering calibration values (CALIBRATION)
|
||||
CommEnergyIC(WRITE, PQGainA, 0x0000); // 47 Line calibration gain
|
||||
CommEnergyIC(WRITE, PhiA, 0x0000); // 48 Line calibration angle
|
||||
CommEnergyIC(WRITE, PQGainB, 0x0000); // 49 Line calibration gain
|
||||
CommEnergyIC(WRITE, PhiB, 0x0000); // 4A Line calibration angle
|
||||
CommEnergyIC(WRITE, PQGainC, 0x0000); // 4B Line calibration gain
|
||||
CommEnergyIC(WRITE, PhiC, 0x0000); // 4C Line calibration angle
|
||||
CommEnergyIC(WRITE, PoffsetA, 0x0000); // 41 A line active power offset FFDC
|
||||
CommEnergyIC(WRITE, QoffsetA, 0x0000); // 42 A line reactive power offset
|
||||
CommEnergyIC(WRITE, PoffsetB, 0x0000); // 43 B line active power offset
|
||||
CommEnergyIC(WRITE, QoffsetB, 0x0000); // 44 B line reactive power offset
|
||||
CommEnergyIC(WRITE, PoffsetC, 0x0000); // 45 C line active power offset
|
||||
CommEnergyIC(WRITE, QoffsetC, 0x0000); // 46 C line reactive power offset
|
||||
|
||||
//Set metering calibration values (HARMONIC)
|
||||
CommEnergyIC(WRITE, POffsetAF, 0x0000); // 51 A Fund. active power offset
|
||||
CommEnergyIC(WRITE, POffsetBF, 0x0000); // 52 B Fund. active power offset
|
||||
CommEnergyIC(WRITE, POffsetCF, 0x0000); // 53 C Fund. active power offset
|
||||
CommEnergyIC(WRITE, PGainAF, 0x0000); // 54 A Fund. active power gain
|
||||
CommEnergyIC(WRITE, PGainBF, 0x0000); // 55 B Fund. active power gain
|
||||
CommEnergyIC(WRITE, PGainCF, 0x0000); // 56 C Fund. active power gain
|
||||
|
||||
//Set measurement calibration values (ADJUST)
|
||||
CommEnergyIC(WRITE, UgainA, _ugain); // 61 A Voltage rms gain
|
||||
CommEnergyIC(WRITE, IgainA, _igainA); // 62 A line current gain
|
||||
CommEnergyIC(WRITE, UoffsetA, 0x0000); // 63 A Voltage offset - 61A8
|
||||
CommEnergyIC(WRITE, IoffsetA, 0x0000); // 64 A line current offset - FE60
|
||||
CommEnergyIC(WRITE, UgainB, _ugain); // 65 B Voltage rms gain
|
||||
CommEnergyIC(WRITE, IgainB, _igainB); // 66 B line current gain
|
||||
CommEnergyIC(WRITE, UoffsetB, 0x0000); // 67 B Voltage offset - 1D4C
|
||||
CommEnergyIC(WRITE, IoffsetB, 0x0000); // 68 B line current offset - FE60
|
||||
CommEnergyIC(WRITE, UgainC, _ugain); // 69 C Voltage rms gain
|
||||
CommEnergyIC(WRITE, IgainC, _igainC); // 6A C line current gain
|
||||
CommEnergyIC(WRITE, UoffsetC, 0x0000); // 6B C Voltage offset - 1D4C
|
||||
CommEnergyIC(WRITE, IoffsetC, 0x0000); // 6C C line current offset
|
||||
|
||||
CommEnergyIC(WRITE, CfgRegAccEn, 0x0000); // 7F end configuration
|
||||
}
|
|
@ -0,0 +1,327 @@
|
|||
/* ATM90E32 Energy Monitor Functions
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 whatnick,Ryzee and Arun
|
||||
|
||||
Modified to use with the CircuitSetup.us Split Phase Energy Meter by jdeglavina
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifndef ATM90E32_h
|
||||
#define ATM90E32_h
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#define WRITE 0 // WRITE SPI
|
||||
#define READ 1 // READ SPI
|
||||
#define DEBUG_SERIAL 1
|
||||
|
||||
/* STATUS REGISTERS */
|
||||
#define MeterEn 0x00 // Metering Enable
|
||||
#define ChannelMapI 0x01 // Current Channel Mapping Configuration
|
||||
#define ChannelMapU 0x02 // Voltage Channel Mapping Configuration
|
||||
#define SagPeakDetCfg 0x05 // Sag and Peak Detector Period Configuration
|
||||
#define OVth 0x06 // Over Voltage Threshold
|
||||
#define ZXConfig 0x07 // Zero-Crossing Config
|
||||
#define SagTh 0x08 // Voltage Sag Th
|
||||
#define PhaseLossTh 0x09 // Voltage Phase Losing Th
|
||||
#define INWarnTh 0x0A // Neutral Current (Calculated) Warning Threshold
|
||||
#define OIth 0x0B // Over Current Threshold
|
||||
#define FreqLoTh 0x0C // Low Threshold for Frequency Detection
|
||||
#define FreqHiTh 0x0D // High Threshold for Frequency Detection
|
||||
#define PMPwrCtrl 0x0E // Partial Measurement Mode Power Control
|
||||
#define IRQ0MergeCfg 0x0F // IRQ0 Merge Configuration
|
||||
|
||||
/* EMM STATUS REGISTERS */
|
||||
#define SoftReset 0x70 // Software Reset
|
||||
#define EMMState0 0x71 // EMM State 0
|
||||
#define EMMState1 0x72 // EMM State 1
|
||||
#define EMMIntState0 0x73 // EMM Interrupt Status 0
|
||||
#define EMMIntState1 0x74 // EMM Interrupt Status 1
|
||||
#define EMMIntEn0 0x75 // EMM Interrupt Enable 0
|
||||
#define EMMIntEn1 0x76 // EMM Interrupt Enable 1
|
||||
#define LastSPIData 0x78 // Last Read/Write SPI Value
|
||||
#define CRCErrStatus 0x79 // CRC Error Status
|
||||
#define CRCDigest 0x7A // CRC Digest
|
||||
#define CfgRegAccEn 0x7F // Configure Register Access Enable
|
||||
|
||||
/* LOW POWER MODE REGISTERS - NOT USED */
|
||||
#define DetectCtrl 0x10
|
||||
#define DetectTh1 0x11
|
||||
#define DetectTh2 0x12
|
||||
#define DetectTh3 0x13
|
||||
#define PMOffsetA 0x14
|
||||
#define PMOffsetB 0x15
|
||||
#define PMOffsetC 0x16
|
||||
#define PMPGA 0x17
|
||||
#define PMIrmsA 0x18
|
||||
#define PMIrmsB 0x19
|
||||
#define PMIrmsC 0x1A
|
||||
#define PMConfig 0x10B
|
||||
#define PMAvgSamples 0x1C
|
||||
#define PMIrmsLSB 0x1D
|
||||
|
||||
/* CONFIGURATION REGISTERS */
|
||||
#define PLconstH 0x31 // High Word of PL_Constant
|
||||
#define PLconstL 0x32 // Low Word of PL_Constant
|
||||
#define MMode0 0x33 // Metering Mode Config
|
||||
#define MMode1 0x34 // PGA Gain Configuration for Current Channels
|
||||
#define PStartTh 0x35 // Startup Power Th (P)
|
||||
#define QStartTh 0x36 // Startup Power Th (Q)
|
||||
#define SStartTh 0x37 // Startup Power Th (S)
|
||||
#define PPhaseTh 0x38 // Startup Power Accum Th (P)
|
||||
#define QPhaseTh 0x39 // Startup Power Accum Th (Q)
|
||||
#define SPhaseTh 0x3A // Startup Power Accum Th (S)
|
||||
|
||||
/* CALIBRATION REGISTERS */
|
||||
#define PoffsetA 0x41 // A Line Power Offset (P)
|
||||
#define QoffsetA 0x42 // A Line Power Offset (Q)
|
||||
#define PoffsetB 0x43 // B Line Power Offset (P)
|
||||
#define QoffsetB 0x44 // B Line Power Offset (Q)
|
||||
#define PoffsetC 0x45 // C Line Power Offset (P)
|
||||
#define QoffsetC 0x46 // C Line Power Offset (Q)
|
||||
#define PQGainA 0x47 // A Line Calibration Gain
|
||||
#define PhiA 0x48 // A Line Calibration Angle
|
||||
#define PQGainB 0x49 // B Line Calibration Gain
|
||||
#define PhiB 0x4A // B Line Calibration Angle
|
||||
#define PQGainC 0x4B // C Line Calibration Gain
|
||||
#define PhiC 0x4C // C Line Calibration Angle
|
||||
|
||||
/* FUNDAMENTAL/HARMONIC ENERGY CALIBRATION REGISTERS */
|
||||
#define POffsetAF 0x51 // A Fund Power Offset (P)
|
||||
#define POffsetBF 0x52 // B Fund Power Offset (P)
|
||||
#define POffsetCF 0x53 // C Fund Power Offset (P)
|
||||
#define PGainAF 0x54 // A Fund Power Gain (P)
|
||||
#define PGainBF 0x55 // B Fund Power Gain (P)
|
||||
#define PGainCF 0x56 // C Fund Power Gain (P)
|
||||
|
||||
/* MEASUREMENT CALIBRATION REGISTERS */
|
||||
#define UgainA 0x61 // A Voltage RMS Gain
|
||||
#define IgainA 0x62 // A Current RMS Gain
|
||||
#define UoffsetA 0x63 // A Voltage Offset
|
||||
#define IoffsetA 0x64 // A Current Offset
|
||||
#define UgainB 0x65 // B Voltage RMS Gain
|
||||
#define IgainB 0x66 // B Current RMS Gain
|
||||
#define UoffsetB 0x67 // B Voltage Offset
|
||||
#define IoffsetB 0x68 // B Current Offset
|
||||
#define UgainC 0x69 // C Voltage RMS Gain
|
||||
#define IgainC 0x6A // C Current RMS Gain
|
||||
#define UoffsetC 0x6B // C Voltage Offset
|
||||
#define IoffsetC 0x6C // C Current Offset
|
||||
#define IoffsetN 0x6E // N Current Offset
|
||||
|
||||
/* ENERGY REGISTERS */
|
||||
#define APenergyT 0x80 // Total Forward Active
|
||||
#define APenergyA 0x81 // A Forward Active
|
||||
#define APenergyB 0x82 // B Forward Active
|
||||
#define APenergyC 0x83 // C Forward Active
|
||||
#define ANenergyT 0x84 // Total Reverse Active
|
||||
#define ANenergyA 0x85 // A Reverse Active
|
||||
#define ANenergyB 0x86 // B Reverse Active
|
||||
#define ANenergyC 0x87 // C Reverse Active
|
||||
#define RPenergyT 0x88 // Total Forward Reactive
|
||||
#define RPenergyA 0x89 // A Forward Reactive
|
||||
#define RPenergyB 0x8A // B Forward Reactive
|
||||
#define RPenergyC 0x8B // C Forward Reactive
|
||||
#define RNenergyT 0x8C // Total Reverse Reactive
|
||||
#define RNenergyA 0x8D // A Reverse Reactive
|
||||
#define RNenergyB 0x8E // B Reverse Reactive
|
||||
#define RNenergyC 0x8F // C Reverse Reactive
|
||||
|
||||
#define SAenergyT 0x90 // Total Apparent Energy
|
||||
#define SenergyA 0x91 // A Apparent Energy
|
||||
#define SenergyB 0x92 // B Apparent Energy
|
||||
#define SenergyC 0x93 // C Apparent Energy
|
||||
|
||||
|
||||
/* FUNDAMENTAL / HARMONIC ENERGY REGISTERS */
|
||||
#define APenergyTF 0xA0 // Total Forward Fund. Energy
|
||||
#define APenergyAF 0xA1 // A Forward Fund. Energy
|
||||
#define APenergyBF 0xA2 // B Forward Fund. Energy
|
||||
#define APenergyCF 0xA3 // C Forward Fund. Energy
|
||||
#define ANenergyTF 0xA4 // Total Reverse Fund Energy
|
||||
#define ANenergyAF 0xA5 // A Reverse Fund. Energy
|
||||
#define ANenergyBF 0xA6 // B Reverse Fund. Energy
|
||||
#define ANenergyCF 0xA7 // C Reverse Fund. Energy
|
||||
#define APenergyTH 0xA8 // Total Forward Harm. Energy
|
||||
#define APenergyAH 0xA9 // A Forward Harm. Energy
|
||||
#define APenergyBH 0xAA // B Forward Harm. Energy
|
||||
#define APenergyCH 0xAB // C Forward Harm. Energy
|
||||
#define ANenergyTH 0xAC // Total Reverse Harm. Energy
|
||||
#define ANenergyAH 0xAD // A Reverse Harm. Energy
|
||||
#define ANenergyBH 0xAE // B Reverse Harm. Energy
|
||||
#define ANenergyCH 0xAF // C Reverse Harm. Energy
|
||||
|
||||
/* POWER & P.F. REGISTERS */
|
||||
#define PmeanT 0xB0 // Total Mean Power (P)
|
||||
#define PmeanA 0xB1 // A Mean Power (P)
|
||||
#define PmeanB 0xB2 // B Mean Power (P)
|
||||
#define PmeanC 0xB3 // C Mean Power (P)
|
||||
#define QmeanT 0xB4 // Total Mean Power (Q)
|
||||
#define QmeanA 0xB5 // A Mean Power (Q)
|
||||
#define QmeanB 0xB6 // B Mean Power (Q)
|
||||
#define QmeanC 0xB7 // C Mean Power (Q)
|
||||
#define SmeanT 0xB8 // Total Mean Power (S)
|
||||
#define SmeanA 0xB9 // A Mean Power (S)
|
||||
#define SmeanB 0xBA // B Mean Power (S)
|
||||
#define SmeanC 0xBB // C Mean Power (S)
|
||||
#define PFmeanT 0xBC // Mean Power Factor
|
||||
#define PFmeanA 0xBD // A Power Factor
|
||||
#define PFmeanB 0xBE // B Power Factor
|
||||
#define PFmeanC 0xBF // C Power Factor
|
||||
|
||||
#define PmeanTLSB 0xC0 // Lower Word (Tot. Act. Power)
|
||||
#define PmeanALSB 0xC1 // Lower Word (A Act. Power)
|
||||
#define PmeanBLSB 0xC2 // Lower Word (B Act. Power)
|
||||
#define PmeanCLSB 0xC3 // Lower Word (C Act. Power)
|
||||
#define QmeanTLSB 0xC4 // Lower Word (Tot. React. Power)
|
||||
#define QmeanALSB 0xC5 // Lower Word (A React. Power)
|
||||
#define QmeanBLSB 0xC6 // Lower Word (B React. Power)
|
||||
#define QmeanCLSB 0xC7 // Lower Word (C React. Power)
|
||||
#define SAmeanTLSB 0xC8 // Lower Word (Tot. App. Power)
|
||||
#define SmeanALSB 0xC9 // Lower Word (A App. Power)
|
||||
#define SmeanBLSB 0xCA // Lower Word (B App. Power)
|
||||
#define SmeanCLSB 0xCB // Lower Word (C App. Power)
|
||||
|
||||
/* FUND/HARM POWER & V/I RMS REGISTERS */
|
||||
#define PmeanTF 0xD0 // Total Active Fund. Power
|
||||
#define PmeanAF 0xD1 // A Active Fund. Power
|
||||
#define PmeanBF 0xD2 // B Active Fund. Power
|
||||
#define PmeanCF 0xD3 // C Active Fund. Power
|
||||
#define PmeanTH 0xD4 // Total Active Harm. Power
|
||||
#define PmeanAH 0xD5 // A Active Harm. Power
|
||||
#define PmeanBH 0xD6 // B Active Harm. Power
|
||||
#define PmeanCH 0xD7 // C Active Harm. Power
|
||||
#define UrmsA 0xD9 // A RMS Voltage
|
||||
#define UrmsB 0xDA // B RMS Voltage
|
||||
#define UrmsC 0xDB // C RMS Voltage
|
||||
#define IrmsN 0xDC // Calculated N RMS Current
|
||||
#define IrmsA 0xDD // A RMS Current
|
||||
#define IrmsB 0xDE // B RMS Current
|
||||
#define IrmsC 0xDF // C RMS Current
|
||||
|
||||
#define PmeanTFLSB 0xE0 // Lower Word (Tot. Act. Fund. Power)
|
||||
#define PmeanAFLSB 0xE1 // Lower Word (A Act. Fund. Power)
|
||||
#define PmeanBFLSB 0xE2 // Lower Word (B Act. Fund. Power)
|
||||
#define PmeanCFLSB 0xE3 // Lower Word (C Act. Fund. Power)
|
||||
#define PmeanTHLSB 0xE4 // Lower Word (Tot. Act. Harm. Power)
|
||||
#define PmeanAHLSB 0xE5 // Lower Word (A Act. Harm. Power)
|
||||
#define PmeanBHLSB 0xE6 // Lower Word (B Act. Harm. Power)
|
||||
#define PmeanCHLSB 0xE7 // Lower Word (C Act. Harm. Power)
|
||||
///////////////// 0xE8 // Reserved Register
|
||||
#define UrmsALSB 0xE9 // Lower Word (A RMS Voltage)
|
||||
#define UrmsBLSB 0xEA // Lower Word (B RMS Voltage)
|
||||
#define UrmsCLSB 0xEB // Lower Word (C RMS Voltage)
|
||||
///////////////// 0xEC // Reserved Register
|
||||
#define IrmsALSB 0xED // Lower Word (A RMS Current)
|
||||
#define IrmsBLSB 0xEE // Lower Word (B RMS Current)
|
||||
#define IrmsCLSB 0xEF // Lower Word (C RMS Current)
|
||||
|
||||
/* PEAK, FREQUENCY, ANGLE & TEMP REGISTERS*/
|
||||
#define UPeakA 0xF1 // A Voltage Peak - THD+N on ATM90E36
|
||||
#define UPeakB 0xF2 // B Voltage Peak
|
||||
#define UPeakC 0xF3 // C Voltage Peak
|
||||
///////////////// 0xF4 // Reserved Register
|
||||
#define IPeakA 0xF5 // A Current Peak
|
||||
#define IPeakB 0xF6 // B Current Peak
|
||||
#define IPeakC 0xF7 // C Current Peak
|
||||
#define Freq 0xF8 // Frequency
|
||||
#define PAngleA 0xF9 // A Mean Phase Angle
|
||||
#define PAngleB 0xFA // B Mean Phase Angle
|
||||
#define PAngleC 0xFB // C Mean Phase Angle
|
||||
#define Temp 0xFC // Measured Temperature
|
||||
#define UangleA 0xFD // A Voltage Phase Angle
|
||||
#define UangleB 0xFE // B Voltage Phase Angle
|
||||
#define UangleC 0xFF // C Voltage Phase Angle
|
||||
|
||||
class ATM90E32
|
||||
{
|
||||
private:
|
||||
unsigned short CommEnergyIC(unsigned char RW, unsigned short address, unsigned short val);
|
||||
int _cs;
|
||||
unsigned short _lineFreq;
|
||||
unsigned short _pgagain;
|
||||
unsigned short _ugain;
|
||||
unsigned short _igainA;
|
||||
unsigned short _igainB;
|
||||
unsigned short _igainC;
|
||||
|
||||
int Read32Register(signed short regh_addr, signed short regl_addr);
|
||||
|
||||
public:
|
||||
/* Construct */
|
||||
ATM90E32(void);
|
||||
/* Destruct */
|
||||
~ATM90E32(void);
|
||||
|
||||
/* Initialization Functions */
|
||||
void begin(int pin, unsigned short lineFreq, unsigned short pgagain, unsigned short ugain, unsigned short igainA, unsigned short igainB, unsigned short igainC);
|
||||
|
||||
double CalculateVIOffset(unsigned short regh_addr, unsigned short regl_addr/*, unsigned short offset_reg*/);
|
||||
double CalculatePowerOffset(unsigned short regh_addr, unsigned short regl_addr/*, unsigned short offset_reg*/);
|
||||
double CalibrateVI(unsigned short reg, unsigned short actualVal);
|
||||
|
||||
/* Main Electrical Parameters (GET)*/
|
||||
double GetLineVoltageA();
|
||||
double GetLineVoltageB();
|
||||
double GetLineVoltageC();
|
||||
|
||||
double GetLineCurrentA();
|
||||
double GetLineCurrentB();
|
||||
double GetLineCurrentC();
|
||||
double GetLineCurrentN();
|
||||
|
||||
double GetActivePowerA();
|
||||
double GetActivePowerB();
|
||||
double GetActivePowerC();
|
||||
double GetTotalActivePower();
|
||||
|
||||
double GetTotalActiveFundPower();
|
||||
double GetTotalActiveHarPower();
|
||||
|
||||
double GetReactivePowerA();
|
||||
double GetReactivePowerB();
|
||||
double GetReactivePowerC();
|
||||
double GetTotalReactivePower();
|
||||
|
||||
double GetApparentPowerA();
|
||||
double GetApparentPowerB();
|
||||
double GetApparentPowerC();
|
||||
double GetTotalApparentPower();
|
||||
|
||||
double GetFrequency();
|
||||
|
||||
double GetPowerFactorA();
|
||||
double GetPowerFactorB();
|
||||
double GetPowerFactorC();
|
||||
double GetTotalPowerFactor();
|
||||
|
||||
double GetPhaseA();
|
||||
double GetPhaseB();
|
||||
double GetPhaseC();
|
||||
|
||||
double GetTemperature();
|
||||
|
||||
/* Gain Parameters (GET)*/
|
||||
double GetValueRegister(unsigned short registerRead);
|
||||
|
||||
/* Energy Consumption */
|
||||
double GetImportEnergy();
|
||||
double GetImportReactiveEnergy();
|
||||
double GetImportApparentEnergy();
|
||||
double GetExportEnergy();
|
||||
double GetExportReactiveEnergy();
|
||||
|
||||
/* System Status */
|
||||
unsigned short GetSysStatus0();
|
||||
unsigned short GetSysStatus1();
|
||||
unsigned short GetMeterStatus0();
|
||||
unsigned short GetMeterStatus1();
|
||||
};
|
||||
#endif
|
|
@ -134,7 +134,7 @@ void config_save_mqtt(String server, String topic, String prefix, String user, S
|
|||
preferences.end();
|
||||
}
|
||||
|
||||
//for CircuitSetup 6 channel energy meter
|
||||
// Save calibration settings
|
||||
void config_save_cal(AsyncWebServerRequest * request)
|
||||
{
|
||||
char req[12];
|
||||
|
|
|
@ -29,17 +29,15 @@
|
|||
#ifndef _EMONESP_CONFIG_H
|
||||
#define _EMONESP_CONFIG_H
|
||||
|
||||
#define ATM90_MOSI (33)
|
||||
#define ATM90_MISO (34)
|
||||
#define ATM90_SCK (7)
|
||||
|
||||
#include "energy_meter.h"
|
||||
#include <Arduino.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <Preferences.h> // Move EEPROM to Preferences
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Load and save the EmonESP config.
|
||||
//
|
||||
// This initial implementation saves the config to the EEPROM area of flash
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// Global config varables
|
||||
|
||||
// Wifi Network Strings
|
||||
|
|
|
@ -28,10 +28,12 @@
|
|||
#include "input.h"
|
||||
#include "config.h"
|
||||
#include "mqtt.h"
|
||||
#include "lora_sender.h"
|
||||
|
||||
// for ATM90E32 energy meter
|
||||
#include <SPI.h>
|
||||
#include <ATM90E32.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#ifdef ENABLE_OLED_DISPLAY
|
||||
#include <Adafruit_GFX.h>
|
||||
|
@ -50,13 +52,6 @@ unsigned long startMillis;
|
|||
unsigned long currentMillis;
|
||||
const int period = 1000; //time interval in ms to send data
|
||||
|
||||
/****** Chip Select Pins ******/
|
||||
/*
|
||||
Each chip has its own CS pin (2 per board). The main board must be pins 5 and 4.
|
||||
*/
|
||||
const int CS1[NUM_BOARDS] = { 5, 0, 27, 2, 13, 14, 15 };
|
||||
const int CS2[NUM_BOARDS] = { 4, 16, 17, 21, 22, 25, 26 };
|
||||
|
||||
char measurement[16];
|
||||
|
||||
/* Initialize ATM90E32 library for each IC */
|
||||
|
@ -147,9 +142,9 @@ void energy_meter_loop()
|
|||
freq = sensor_ic1[0].GetFrequency();
|
||||
temp = sensor_ic1[0].GetTemperature();
|
||||
|
||||
//Serial.println("Temp:" + String(temp) + "C");
|
||||
//Serial.println("Freq:" + String(freq) + "Hz");
|
||||
//Serial.println("V1:" + String(voltage1) + "V V2:" + String(voltage2) + "V");
|
||||
Serial.println("Temp:" + String(temp) + "C");
|
||||
Serial.println("Freq:" + String(freq) + "Hz");
|
||||
Serial.println("V1:" + String(voltage1) + "V V2:" + String(voltage2) + "V");
|
||||
|
||||
strcpy(result, "temp:");
|
||||
dtostrf(temp, 2, 1, measurement);
|
||||
|
@ -171,12 +166,13 @@ void energy_meter_loop()
|
|||
|
||||
for (i = 0; i < NUM_BOARDS; i ++)
|
||||
{
|
||||
JsonDocument doc;
|
||||
unsigned short sys0_1 = sensor_ic1[i].GetSysStatus0(); //EMMState0
|
||||
unsigned short sys0_2 = sensor_ic2[i].GetSysStatus0();
|
||||
if (sys0_1 == 65535 || sys0_1 == 0 || sys0_2 == 65535 || sys0_2 == 0)
|
||||
{
|
||||
/* Print error message if we can't talk to the master board */
|
||||
if (i == 0) DBUGS.println("Error: Not receiving data from the energy meter - check your connections");
|
||||
if (i == 0) DBUGS.println("Error: Not receiving data from the energy meter - check your connections");
|
||||
/* If no response, go to next board */
|
||||
continue;
|
||||
}
|
||||
|
@ -247,50 +243,37 @@ void energy_meter_loop()
|
|||
result_json += sprintf(result_json, ",\"va\":%.2f", vaPowerCT[j]);
|
||||
result_json += sprintf(result_json, ",\"v\":%.2f}", voltageCT[j]);
|
||||
|
||||
char CTID[8];
|
||||
sprintf(CTID, "CT%02d", i*NUM_INPUTS+j+1);
|
||||
|
||||
sprintf(result + strlen(result), ",CT%02d:", i*NUM_INPUTS+j+1);
|
||||
dtostrf(currentCT[j], 2, 4, measurement);
|
||||
strcat(result, measurement);
|
||||
doc[CTID]["I"] = measurement;
|
||||
|
||||
sprintf(result + strlen(result), ",PF%02d:", i*NUM_INPUTS+j+1);
|
||||
dtostrf(powerFactorCT[j], 2, 3, measurement);
|
||||
strcat(result, measurement);
|
||||
doc[CTID]["PF"] = measurement;
|
||||
|
||||
sprintf(result + strlen(result), ",W%02d:", i*NUM_INPUTS+j+1);
|
||||
dtostrf(realPowerCT[j], 2, 2, measurement);
|
||||
strcat(result, measurement);
|
||||
doc[CTID]["P"] = measurement;
|
||||
|
||||
sprintf(result + strlen(result), ",VA%02d:", i*NUM_INPUTS+j+1);
|
||||
dtostrf(vaPowerCT[j], 2, 2, measurement);
|
||||
strcat(result, measurement);
|
||||
doc[CTID]["S"] = measurement;
|
||||
|
||||
sprintf(result + strlen(result), ",V%02d:", i*NUM_INPUTS+j+1);
|
||||
dtostrf(voltageCT[j], 2, 2, measurement);
|
||||
strcat(result, measurement);
|
||||
doc[CTID]["V"] = measurement;
|
||||
}
|
||||
Serial.println(result_json);
|
||||
Serial.println("");
|
||||
lora_sender_message(doc);
|
||||
}
|
||||
strcpy(result_json, "]}");
|
||||
|
||||
#ifdef ENABLE_OLED_DISPLAY
|
||||
/* Write meter data to the display */
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.println("V1:" + String(voltage1) + "V");
|
||||
display.println("V2:" + String(voltage2) + "V");
|
||||
display.println("CT1:" + String(currentCT1) + "A");
|
||||
display.println("CT2:" + String(currentCT2) + "A");
|
||||
display.println("CT3:" + String(currentCT3) + "A");
|
||||
display.println("CT4:" + String(currentCT4) + "A");
|
||||
display.println("CT5:" + String(currentCT5) + "A");
|
||||
display.println("CT6:" + String(currentCT6) + "A");
|
||||
display.println("Total W:" + String(totalWatts) + "W");
|
||||
/*
|
||||
display.println("Freq: " + String(freq) + "Hz");
|
||||
display.println("PF: " + String(powerFactor));
|
||||
display.println("Chip Temp: " + String(temp) + "C");
|
||||
*/
|
||||
display.display();
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -26,8 +26,17 @@
|
|||
#ifndef _ENERGY_METER
|
||||
#define _ENERGY_METER
|
||||
|
||||
#define NUM_BOARDS 7
|
||||
#define NUM_INPUTS 6
|
||||
#ifdef HELTECLORA32V3
|
||||
#define NUM_BOARDS 3
|
||||
#define NUM_INPUTS 3
|
||||
const int CS1[NUM_BOARDS] = { 20, 19, 26 };
|
||||
const int CS2[NUM_BOARDS] = { 20, 19, 26 };
|
||||
#else
|
||||
#define NUM_BOARDS 7
|
||||
#define NUM_INPUTS 6
|
||||
const int CS1[NUM_BOARDS] = { 5, 0, 27, 2, 13, 14, 15 };
|
||||
const int CS2[NUM_BOARDS] = { 4, 16, 17, 21, 22, 25, 26 };
|
||||
#endif
|
||||
|
||||
#define NUM_CHANNELS (NUM_BOARDS*NUM_INPUTS)
|
||||
|
||||
|
@ -59,7 +68,7 @@
|
|||
For meter > v1.3:
|
||||
7305 - 9v AC Transformer - Jameco 157041
|
||||
*/
|
||||
#define VOLTAGE_GAIN_DEFAULT 13839
|
||||
#define VOLTAGE_GAIN_DEFAULT 18067
|
||||
|
||||
/*
|
||||
When PGA Gain is set to 0
|
||||
|
|
|
@ -153,35 +153,33 @@ static void wifi_start()
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event)
|
||||
{
|
||||
switch (event) {
|
||||
case SYSTEM_EVENT_WIFI_READY:
|
||||
case ARDUINO_EVENT_WIFI_READY:
|
||||
DBUGS.println("WiFi interface ready");
|
||||
break;
|
||||
case SYSTEM_EVENT_SCAN_DONE:
|
||||
case ARDUINO_EVENT_WIFI_SCAN_DONE:
|
||||
DBUGS.println("Completed scan for access points");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
case ARDUINO_EVENT_WIFI_STA_START:
|
||||
DBUGS.println("WiFi client started");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_STOP:
|
||||
case ARDUINO_EVENT_WIFI_STA_STOP:
|
||||
DBUGS.println("WiFi clients stopped");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||
DBUGS.print("Connected to SSID: ");
|
||||
DBUGS.println(esid.c_str());
|
||||
client_disconnects = 0;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||
DBUGS.println("Disconnected from WiFi access point");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
|
||||
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE:
|
||||
DBUGS.println("Authentication mode of access point has changed");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP: {
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP: {
|
||||
IPAddress myAddress = WiFi.localIP();
|
||||
char tmpStr[40];
|
||||
sprintf(tmpStr, "%d.%d.%d.%d", myAddress[0], myAddress[1], myAddress[2], myAddress[3]);
|
||||
|
@ -197,39 +195,39 @@ void WiFiEvent(WiFiEvent_t event)
|
|||
client_retry = false;
|
||||
}
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_LOST_IP:
|
||||
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
||||
DBUGS.println("Lost IP address and IP address is reset to 0");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
|
||||
case ARDUINO_EVENT_WPS_ER_SUCCESS:
|
||||
DBUGS.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
|
||||
case ARDUINO_EVENT_WPS_ER_FAILED:
|
||||
DBUGS.println("WiFi Protected Setup (WPS): failed in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
|
||||
case ARDUINO_EVENT_WPS_ER_TIMEOUT:
|
||||
DBUGS.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_PIN:
|
||||
case ARDUINO_EVENT_WPS_ER_PIN:
|
||||
DBUGS.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_START:
|
||||
case ARDUINO_EVENT_WIFI_AP_START:
|
||||
DBUGS.println("WiFi access point started");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STOP:
|
||||
case ARDUINO_EVENT_WIFI_AP_STOP:
|
||||
DBUGS.println("WiFi access point stopped");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STACONNECTED:
|
||||
case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
|
||||
DBUGS.println("Client connected");
|
||||
apClients++;
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STADISCONNECTED:
|
||||
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
|
||||
DBUGS.println("Client disconnected");
|
||||
apClients--;
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STAIPASSIGNED:
|
||||
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
|
||||
DBUGS.println("Assigned IP address to client");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_PROBEREQRECVED:
|
||||
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED:
|
||||
DBUGS.println("Received probe request");
|
||||
break;
|
||||
default:
|
||||
|
@ -237,62 +235,6 @@ void WiFiEvent(WiFiEvent_t event)
|
|||
}
|
||||
}
|
||||
|
||||
#else //ESP8266
|
||||
void wifi_onStationModeGotIP(const WiFiEventStationModeGotIP &event)
|
||||
{
|
||||
IPAddress myAddress = WiFi.localIP();
|
||||
char tmpStr[40];
|
||||
sprintf(tmpStr, "%d.%d.%d.%d", myAddress[0], myAddress[1], myAddress[2], myAddress[3]);
|
||||
ipaddress = tmpStr;
|
||||
DBUGS.print("Connected, IP: ");
|
||||
DBUGS.println(tmpStr);
|
||||
|
||||
// Copy the connected network and ipaddress to global strings for use in status request
|
||||
connected_network = esid;
|
||||
|
||||
// Clear any error state
|
||||
client_disconnects = 0;
|
||||
client_retry = false;
|
||||
}
|
||||
|
||||
void wifi_onStationModeDisconnected(const WiFiEventStationModeDisconnected &event)
|
||||
{
|
||||
DBUGF("WiFi disconnected: %s",
|
||||
WIFI_DISCONNECT_REASON_UNSPECIFIED == event.reason ? "WIFI_DISCONNECT_REASON_UNSPECIFIED" :
|
||||
WIFI_DISCONNECT_REASON_AUTH_EXPIRE == event.reason ? "WIFI_DISCONNECT_REASON_AUTH_EXPIRE" :
|
||||
WIFI_DISCONNECT_REASON_AUTH_LEAVE == event.reason ? "WIFI_DISCONNECT_REASON_AUTH_LEAVE" :
|
||||
WIFI_DISCONNECT_REASON_ASSOC_EXPIRE == event.reason ? "WIFI_DISCONNECT_REASON_ASSOC_EXPIRE" :
|
||||
WIFI_DISCONNECT_REASON_ASSOC_TOOMANY == event.reason ? "WIFI_DISCONNECT_REASON_ASSOC_TOOMANY" :
|
||||
WIFI_DISCONNECT_REASON_NOT_AUTHED == event.reason ? "WIFI_DISCONNECT_REASON_NOT_AUTHED" :
|
||||
WIFI_DISCONNECT_REASON_NOT_ASSOCED == event.reason ? "WIFI_DISCONNECT_REASON_NOT_ASSOCED" :
|
||||
WIFI_DISCONNECT_REASON_ASSOC_LEAVE == event.reason ? "WIFI_DISCONNECT_REASON_ASSOC_LEAVE" :
|
||||
WIFI_DISCONNECT_REASON_ASSOC_NOT_AUTHED == event.reason ? "WIFI_DISCONNECT_REASON_ASSOC_NOT_AUTHED" :
|
||||
WIFI_DISCONNECT_REASON_DISASSOC_PWRCAP_BAD == event.reason ? "WIFI_DISCONNECT_REASON_DISASSOC_PWRCAP_BAD" :
|
||||
WIFI_DISCONNECT_REASON_DISASSOC_SUPCHAN_BAD == event.reason ? "WIFI_DISCONNECT_REASON_DISASSOC_SUPCHAN_BAD" :
|
||||
WIFI_DISCONNECT_REASON_IE_INVALID == event.reason ? "WIFI_DISCONNECT_REASON_IE_INVALID" :
|
||||
WIFI_DISCONNECT_REASON_MIC_FAILURE == event.reason ? "WIFI_DISCONNECT_REASON_MIC_FAILURE" :
|
||||
WIFI_DISCONNECT_REASON_4WAY_HANDSHAKE_TIMEOUT == event.reason ? "WIFI_DISCONNECT_REASON_4WAY_HANDSHAKE_TIMEOUT" :
|
||||
WIFI_DISCONNECT_REASON_GROUP_KEY_UPDATE_TIMEOUT == event.reason ? "WIFI_DISCONNECT_REASON_GROUP_KEY_UPDATE_TIMEOUT" :
|
||||
WIFI_DISCONNECT_REASON_IE_IN_4WAY_DIFFERS == event.reason ? "WIFI_DISCONNECT_REASON_IE_IN_4WAY_DIFFERS" :
|
||||
WIFI_DISCONNECT_REASON_GROUP_CIPHER_INVALID == event.reason ? "WIFI_DISCONNECT_REASON_GROUP_CIPHER_INVALID" :
|
||||
WIFI_DISCONNECT_REASON_PAIRWISE_CIPHER_INVALID == event.reason ? "WIFI_DISCONNECT_REASON_PAIRWISE_CIPHER_INVALID" :
|
||||
WIFI_DISCONNECT_REASON_AKMP_INVALID == event.reason ? "WIFI_DISCONNECT_REASON_AKMP_INVALID" :
|
||||
WIFI_DISCONNECT_REASON_UNSUPP_RSN_IE_VERSION == event.reason ? "WIFI_DISCONNECT_REASON_UNSUPP_RSN_IE_VERSION" :
|
||||
WIFI_DISCONNECT_REASON_INVALID_RSN_IE_CAP == event.reason ? "WIFI_DISCONNECT_REASON_INVALID_RSN_IE_CAP" :
|
||||
WIFI_DISCONNECT_REASON_802_1X_AUTH_FAILED == event.reason ? "WIFI_DISCONNECT_REASON_802_1X_AUTH_FAILED" :
|
||||
WIFI_DISCONNECT_REASON_CIPHER_SUITE_REJECTED == event.reason ? "WIFI_DISCONNECT_REASON_CIPHER_SUITE_REJECTED" :
|
||||
WIFI_DISCONNECT_REASON_BEACON_TIMEOUT == event.reason ? "WIFI_DISCONNECT_REASON_BEACON_TIMEOUT" :
|
||||
WIFI_DISCONNECT_REASON_NO_AP_FOUND == event.reason ? "WIFI_DISCONNECT_REASON_NO_AP_FOUND" :
|
||||
WIFI_DISCONNECT_REASON_AUTH_FAIL == event.reason ? "WIFI_DISCONNECT_REASON_AUTH_FAIL" :
|
||||
WIFI_DISCONNECT_REASON_ASSOC_FAIL == event.reason ? "WIFI_DISCONNECT_REASON_ASSOC_FAIL" :
|
||||
WIFI_DISCONNECT_REASON_HANDSHAKE_TIMEOUT == event.reason ? "WIFI_DISCONNECT_REASON_HANDSHAKE_TIMEOUT" :
|
||||
"UNKNOWN");
|
||||
|
||||
client_disconnects++;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void wifi_setup() {
|
||||
|
||||
#ifdef WIFI_LED
|
||||
|
@ -300,48 +242,11 @@ void wifi_setup() {
|
|||
digitalWrite(WIFI_LED, wifiLedState);
|
||||
#endif
|
||||
|
||||
randomSeed(analogRead(0));
|
||||
randomSeed(analogRead(1));
|
||||
|
||||
// If we have an SSID configured at this point we have likely
|
||||
// been running another firmware, clear the results
|
||||
/*
|
||||
if(wifi_is_client_configured()) {
|
||||
WiFi.persistent(true);
|
||||
|
||||
#ifdef ESP32
|
||||
WiFi.disconnect(false,true);
|
||||
#else
|
||||
WiFi.disconnect();
|
||||
ESP.eraseConfig();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Stop the WiFi module
|
||||
WiFi.persistent(false);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
*/
|
||||
#ifdef ESP32
|
||||
WiFi.setHostname(esp_hostname);
|
||||
#else
|
||||
WiFi.hostname(esp_hostname);
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
WiFi.setHostname(esp_hostname);
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
|
||||
#else
|
||||
static auto _onStationModeConnected = WiFi.onStationModeConnected([](const WiFiEventStationModeConnected & event) {
|
||||
DBUGF("Connected to %s", event.ssid.c_str());
|
||||
});
|
||||
static auto _onStationModeGotIP = WiFi.onStationModeGotIP(wifi_onStationModeGotIP);
|
||||
static auto _onStationModeDisconnected = WiFi.onStationModeDisconnected(wifi_onStationModeDisconnected);
|
||||
static auto _onSoftAPModeStationConnected = WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected & event) {
|
||||
apClients++;
|
||||
});
|
||||
static auto _onSoftAPModeStationDisconnected = WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected & event) {
|
||||
apClients--;
|
||||
});
|
||||
#endif
|
||||
|
||||
wifi_start();
|
||||
|
||||
if (MDNS.begin(esp_hostname)) {
|
||||
|
|
|
@ -34,9 +34,6 @@
|
|||
#ifdef ESP32
|
||||
#include <WiFi.h>
|
||||
#include <ESPmDNS.h> // Resolve URL for update server etc.
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266mDNS.h> // Resolve URL for update server etc.
|
||||
#endif
|
||||
|
||||
//was causing ESP to crash in AP mode
|
||||
|
|
|
@ -28,9 +28,6 @@
|
|||
#include "emonesp.h"
|
||||
#include "http.h"
|
||||
|
||||
WiFiClient client; // Create class for HTTP TCP connections get_http()
|
||||
WiFiClientSecure client_ssl; // Create class for HTTPS TCP connections get_https()
|
||||
|
||||
static char request[MAX_DATA_LEN+100];
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
@ -39,14 +36,11 @@ static char request[MAX_DATA_LEN+100];
|
|||
// -------------------------------------------------------------------
|
||||
|
||||
String get_http(const char * host, const char * url, int port, const char * fingerprint) {
|
||||
WiFiClientSecure * http;
|
||||
|
||||
if (fingerprint) {
|
||||
http = &client_ssl;
|
||||
} else {
|
||||
http = (WiFiClientSecure *) &client;
|
||||
}
|
||||
HTTPClient http;
|
||||
return ("ok");
|
||||
|
||||
/* TODO : completetly rewrite this function !
|
||||
// Use WiFiClient class to create TCP connections
|
||||
if (!http->connect(host, port, HTTP_TIMEOUT*1000)) {
|
||||
DBUGS.printf("%s:%d\n", host, port); //debug
|
||||
|
@ -75,7 +69,7 @@ String get_http(const char * host, const char * url, int port, const char * fing
|
|||
String line = http->readStringUntil('\r');
|
||||
DBUGS.println(line); //debug
|
||||
if (line.startsWith("HTTP/1.1 200 OK")) {
|
||||
return ("ok");
|
||||
|
||||
}
|
||||
}
|
||||
#ifndef ESP32
|
||||
|
@ -83,5 +77,5 @@ String get_http(const char * host, const char * url, int port, const char * fing
|
|||
return ("HTTPS fingerprint no match");
|
||||
}
|
||||
#endif
|
||||
return ("error " + String(host));
|
||||
return ("error " + String(host));*/
|
||||
}
|
||||
|
|
|
@ -35,8 +35,7 @@
|
|||
|
||||
#include <Arduino.h>
|
||||
#include <Print.h>
|
||||
#include <WiFiClient.h> // http GET request
|
||||
#include <WiFiClientSecure.h> // Secure https GET request
|
||||
#include <HTTPClient.h>
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// HTTP or HTTPS GET Request
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
#include "config.h"
|
||||
#include "lora_sender.h"
|
||||
#include <SPI.h>
|
||||
#include <RadioLib.h>
|
||||
|
||||
// counter to keep track of transmitted packets
|
||||
int count = 0;
|
||||
|
||||
volatile bool transmittedFlag = true;
|
||||
|
||||
int transmissionState = RADIOLIB_ERR_NONE;
|
||||
|
||||
SPIClass fspi_bus(HSPI);
|
||||
SPISettings spiSettings(2000000, MSBFIRST, SPI_MODE0);
|
||||
SX1262 radio = new Module(LORA_NSS, LORA_DIO1, LORA_NRST, LORA_BUSY, fspi_bus, spiSettings);
|
||||
|
||||
void lora_sender_setup() {
|
||||
fspi_bus.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS);
|
||||
Serial.println("Setting LoRa Sender");
|
||||
int transmissionState = RADIOLIB_ERR_NONE;
|
||||
|
||||
Serial.print(F("[SX1262] Initializing ... "));
|
||||
int state = radio.begin(869.0,500,12,5,0x34,21,20);
|
||||
if (state == RADIOLIB_ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
}
|
||||
|
||||
radio.setPacketSentAction(setFlag);
|
||||
}
|
||||
|
||||
// this function is called when a complete packet
|
||||
// is transmitted by the module
|
||||
// IMPORTANT: this function MUST be 'void' type
|
||||
// and MUST NOT have any arguments!
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
ICACHE_RAM_ATTR
|
||||
#endif
|
||||
void setFlag(void) {
|
||||
// we sent a packet, set the flag
|
||||
transmittedFlag = true;
|
||||
Serial.println(F("Transmission flag set to true"));
|
||||
}
|
||||
|
||||
void lora_sender_loop() {
|
||||
if(transmittedFlag) {
|
||||
|
||||
if (transmissionState == RADIOLIB_ERR_NONE) {
|
||||
// packet was successfully sent
|
||||
Serial.println(F("transmission finished!"));
|
||||
|
||||
// NOTE: when using interrupt-driven transmit method,
|
||||
// it is not possible to automatically measure
|
||||
// transmission data rate using getDataRate()
|
||||
|
||||
} else {
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(transmissionState);
|
||||
|
||||
}
|
||||
|
||||
// clean up after transmission is finished
|
||||
// this will ensure transmitter is disabled,
|
||||
// RF switch is powered down etc.
|
||||
radio.finishTransmit();
|
||||
}
|
||||
}
|
||||
|
||||
void lora_sender_message(JsonDocument& doc) {
|
||||
// if(transmittedFlag) {
|
||||
// transmittedFlag = false;
|
||||
char msgpack[256];
|
||||
Serial.println(F("Sending packet"));
|
||||
doc["hostname"] = String("powerfly-") + String((uint32_t)(ESP.getEfuseMac() >> 16), HEX);
|
||||
serializeJsonPretty(doc,Serial);
|
||||
Serial.println("");
|
||||
serializeJson(doc, msgpack);
|
||||
int state = radio.transmit(msgpack);
|
||||
if (state == RADIOLIB_ERR_NONE) {
|
||||
// the packet was successfully transmitted
|
||||
Serial.println(F("success!"));
|
||||
|
||||
// print measured data rate
|
||||
Serial.print(F("[SX1262] Datarate:\t"));
|
||||
Serial.print(radio.getDataRate());
|
||||
Serial.println(F(" bps"));
|
||||
|
||||
} else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
|
||||
// the supplied packet was longer than 256 bytes
|
||||
Serial.println(F("too long!"));
|
||||
|
||||
} else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
|
||||
// timeout occured while transmitting packet
|
||||
Serial.println(F("timeout!"));
|
||||
} else {
|
||||
// some other error occurred
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
|
||||
}
|
||||
delay(3000);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef _LORA_SENDER_H
|
||||
#define _LORA_SENDER_H
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#ifdef HELTECLORA32V3
|
||||
#define LORA_MOSI (10)
|
||||
#define LORA_MISO (11)
|
||||
#define LORA_SCK (9)
|
||||
#define LORA_NSS (8)
|
||||
#define LORA_NRST (12)
|
||||
#define LORA_DIO1 (14)
|
||||
#define LORA_BUSY (13)
|
||||
#endif
|
||||
|
||||
extern void lora_sender_setup();
|
||||
extern void lora_sender_loop();
|
||||
extern void lora_sender_message(JsonDocument& doc);
|
||||
void setFlag(void);
|
||||
|
||||
#endif // _LORA_SENDER_H
|
|
@ -44,6 +44,8 @@ extern const char *esp_hostname;
|
|||
|
||||
void ota_setup()
|
||||
{
|
||||
|
||||
/* TODO: This function should be completely rewritten
|
||||
// Start local OTA update server
|
||||
ArduinoOTA.setHostname(esp_hostname);
|
||||
ArduinoOTA
|
||||
|
@ -80,6 +82,7 @@ void ota_setup()
|
|||
});
|
||||
|
||||
ArduinoOTA.begin();
|
||||
*/
|
||||
}
|
||||
|
||||
void ota_loop()
|
||||
|
@ -98,12 +101,13 @@ String ota_get_latest_version()
|
|||
|
||||
t_httpUpdate_return ota_http_update()
|
||||
{
|
||||
/* TODO : full rewrite */
|
||||
WiFiClient client;
|
||||
#ifdef ENABLE_WDT
|
||||
feedLoopWDT();
|
||||
#endif
|
||||
SPIFFS.end(); // unmount filesystem
|
||||
MYFS.end(); // unmount filesystem
|
||||
t_httpUpdate_return ret = httpUpdate.update(client,"http://" + String(u_host) + String(u_url) + "?tag=" + currentfirmware);
|
||||
SPIFFS.begin(); //mount-file system
|
||||
MYFS.begin(); //mount-file system
|
||||
return ret;
|
||||
}
|
|
@ -41,8 +41,8 @@
|
|||
#include "web_server.h"
|
||||
#include "ota.h"
|
||||
#include "input.h"
|
||||
//#include "emoncms.h"
|
||||
#include "mqtt.h"
|
||||
#include "lora_sender.h"
|
||||
|
||||
#ifdef ENABLE_ENERGY_METER
|
||||
#include "energy_meter.h"
|
||||
|
@ -68,10 +68,12 @@ void setup() {
|
|||
config_load_settings();
|
||||
delay(200);
|
||||
|
||||
DBUGS.println("Initializing Wifi");
|
||||
// Initialise the WiFi
|
||||
wifi_setup();
|
||||
delay(200);
|
||||
|
||||
DBUGS.println("Starting web server");
|
||||
// Bring up the web server
|
||||
web_server_setup();
|
||||
delay(200);
|
||||
|
@ -90,8 +92,11 @@ void setup() {
|
|||
energy_meter_setup();
|
||||
#endif
|
||||
|
||||
DBUGLN("Server started");
|
||||
#ifdef ENABLE_LORASENDER
|
||||
lora_sender_setup();
|
||||
#endif
|
||||
|
||||
DBUGLN("Server started");
|
||||
} // end setup
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
@ -113,10 +118,11 @@ void loop()
|
|||
energy_meter_loop();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_LORASENDER
|
||||
// lora_sender_loop();
|
||||
#endif
|
||||
|
||||
boolean gotInput = input_get(input);
|
||||
if (gotInput) {
|
||||
DBUGS.println(".");
|
||||
}
|
||||
|
||||
if (wifi_client_connected()) {
|
||||
if (mqtt_server != 0) {
|
||||
|
@ -126,5 +132,4 @@ void loop()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // end loop
|
||||
|
|
|
@ -104,7 +104,7 @@ void dumpRequest(AsyncWebServerRequest *request) {
|
|||
// -------------------------------------------------------------------
|
||||
bool requestPreProcess(AsyncWebServerRequest *request, AsyncResponseStream *&response, const char *contentType = "application/json")
|
||||
{
|
||||
dumpRequest(request);
|
||||
//dumpRequest(request);
|
||||
|
||||
if (wifi_mode_is_sta() && www_username != "" &&
|
||||
false == request->authenticate(www_username.c_str(), www_password.c_str())) {
|
||||
|
@ -134,11 +134,11 @@ void handleHome(AsyncWebServerRequest *request) {
|
|||
return request->requestAuthentication();
|
||||
}
|
||||
|
||||
if (SPIFFS.exists("/home.html")) {
|
||||
request->send(SPIFFS, "/home.html");
|
||||
if (MYFS.exists("/home.html")) {
|
||||
request->send(MYFS, "/home.html");
|
||||
} else {
|
||||
request->send(200, "text/plain",
|
||||
"/home.html not found, have you flashed the SPIFFS?");
|
||||
"/home.html not found, have you flashed the filesystem ?");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -726,14 +726,14 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient *client, AwsEventTy
|
|||
|
||||
void web_server_setup()
|
||||
{
|
||||
SPIFFS.begin(); // mount the fs
|
||||
MYFS.begin(); // mount the fs
|
||||
|
||||
// Add the Web Socket server
|
||||
ws.onEvent(onWsEvent);
|
||||
server.addHandler(&ws);
|
||||
|
||||
// Setup the static files
|
||||
server.serveStatic("/", SPIFFS, "/")
|
||||
server.serveStatic("/", MYFS, "/")
|
||||
.setDefaultFile("index.html")
|
||||
.setAuthentication(www_username.c_str(), www_password.c_str());
|
||||
|
||||
|
|
|
@ -28,19 +28,17 @@
|
|||
#ifndef _EMONESP_WEB_SERVER_H
|
||||
#define _EMONESP_WEB_SERVER_H
|
||||
|
||||
#include <FS.h> // SPIFFS file-system: store web server html, CSS etc.
|
||||
#include <FS.h> // file-system: store web server html, CSS etc.
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include <SPIFFS.h>
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h> //https://github.com/me-no-dev/AsyncTCP
|
||||
#elif defined(ESP8266)
|
||||
#include <Hash.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#endif
|
||||
#include <ESPAsyncWebServer.h> //https://github.com/me-no-dev/ESPAsyncWebServer
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
//#include <SPIFFS.h>
|
||||
//#define MYFS SPIFFS
|
||||
|
||||
#define MYFS LittleFS
|
||||
#include <LittleFS.h>
|
||||
|
||||
extern AsyncWebServer server;
|
||||
extern AsyncWebSocket ws;
|
||||
|
|
Loading…
Reference in New Issue