0% found this document useful (0 votes)
19 views3 pages

Wireless WM

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Wireless WM

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <TrueRMS.

h>
#include <WiFi.h>
#include <esp_now.h>
#include "MCP_ADC.h"
#include "ESP32TimerInterrupt.h"

#define TIMER1_INTERVAL_MS 0.55 //interrupt interval 0.6 + 1000 RMS

//#define RMS_WINDOW 20 // rms window of 20 samples, means 2 periods @50Hz


#define RMS_WINDOW 1000 // rms window of 40 samples, means 4 periods @50Hz
//#define RMS_WINDOW 25 // rms window of 25 samples, means 3 periods @60Hz
//#define RMS_WINDOW 50 // rms window of 50 samples, means 6 periods @60Hz

#define MCP_DIN 6
#define MCP_DO 7
#define MCP_CLK 5
#define MCP_CS 4

#define SLAVE_HARDWARE_DEVICE_NAME "M" // Target slave name

typedef struct struct_esp{


char Name_ID[10] = SLAVE_HARDWARE_DEVICE_NAME;
float realPwr;
float rmsVal1;
float rmsVal2;
float apparentPwr;
float pf;
}struct_esp;
struct_esp espNow_data;

esp_now_peer_info_t peerInfo;
uint8_t Slave_MAC_addr[6];

unsigned long nextLoop;


int acVolt;
int acCurr;
float acVoltRange = 810; // peak-to-peak voltage scaled down to 0-5V is 700V
(=700/2*sqrt(2) = 247.5Vrms max).
float acCurrRange = 285; // peak-to-peak current scaled down to 0-5V is 5A
(=5/2*sqrt(2) = 1.77Arms max).
uint32_t val1,val2;

Power acPower; // create an instance of Power


MCP3202 mcp(MCP_DIN, MCP_DO, MCP_CLK);
ESP32Timer ITimer1(1);

int i=0;

bool IRAM_ATTR TimerHandler1(void * timerNo) // this is the function called by


interrupt every 1ms
{
static int channel = 0;
if(channel == 0)
{
val1 = mcp.read(channel);
//acPower.update2(val);
channel = 1;
}
else
{
val2 = mcp.read(channel);
//acPower.update1(val);
channel = 0;
}
//Serial.println("Out interrupt");
if(channel == 0)
{
acPower.update(mcp.read(0),mcp.read(1)); //values update after getting reading
from chan0 & chan1
i++; // every interrupt this hsould increments (period now is 1ms)
}
return true;
}

// MY bad
void setup() { // run once:
Serial.begin(115200);
// configure for automatic base-line restoration and single scan mode:
acPower.begin(acVoltRange, acCurrRange, RMS_WINDOW, ADC_12BIT, BLR_ON,
SGL_SCAN);
acPower.start(); //start measuring

mcp.begin(4);
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000,
TimerHandler1)) //interrupt init
{
Serial.print(F("Starting ITimer1 OK, millis() = "));
Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
initESPNow();
Serial.print("MCP Channels: ");
Serial.print(mcp.channels());
Serial.print("max Value: ");
Serial.println(mcp.maxValue());
}

void loop() {
//Serial.println("Raw adc value0 : " + String(val1));
Serial.println("Interrupt itterations (i index): " + String(i));
acPower.publish();
Serial.print(acPower.rmsVal1,1);
Serial.print(", ");
Serial.print(acPower.rmsVal2,1);
Serial.print(", ");
Serial.print(acPower.apparentPwr,1);
Serial.print(", ");
Serial.print(acPower.realPwr,1);
Serial.print(", ");
Serial.print(acPower.pf,2);
Serial.print(", ");
Serial.print(acPower.dcBias1);
Serial.print(", ");
Serial.println(acPower.dcBias2);
espNow_data.realPwr = acPower.realPwr;
espNow_data.rmsVal1 = acPower.rmsVal1;
espNow_data.rmsVal2 = acPower.rmsVal2;
espNow_data.apparentPwr = acPower.apparentPwr;
espNow_data.pf = acPower.pf;
esp_now_send(Slave_MAC_addr, (uint8_t *)&espNow_data, sizeof(espNow_data));
acPower.start(); // In single scan mode, restart the acquisition after
publishing.
delay(50);
}

void initESPNow() {
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) { // Init ESP-NOW
Serial.println("Error initializing ESP-NOW");
return;
}
for (int i=0; i<6; i++){Slave_MAC_addr[i] = 0xFF;}
memcpy(peerInfo.peer_addr, Slave_MAC_addr, 6); // add Register peer
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo); // Add peer

esp_now_register_recv_cb(OnDataRecv); // Register for a callback function that


will be called when data is received
esp_now_register_send_cb(OnDataSent);

/
***********************************************************************************
***********
*

***********************************************************************************
************/
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
if (status == 0) {
//Serial.println("Delivery Success :)");
}
else {
//Serial.println("Delivery Fail :(");
}
}

/
***********************************************************************************
***********
*

***********************************************************************************
************/
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {

You might also like