====== Current example======
The current sensor device has to be connected to the sensor module. Current sensor device has a 3,5 mm plug what has to be connected to the sensor module left jacket A. The controller and sensor modules must be connected.
The current sensor has to be 30A
{{:en:iot:examples:currentpicture1.jpg?200|}}
Needed libraries:
lib_deps = ITTIoT, EmonLib
The example code above shows the alternating current value to current topic "curr". If nothing is measured the current value is NaN.
#include
#include
#include "EmonLib.h"
#include
#define WIFI_NAME "name"
#define WIFI_PASSWORD "password"
// Pin definition for the current sensor
#define ADC_PIN A0
// create a objects
EnergyMonitor emon1;
Ticker adcTicker;
bool adcFlag;
uint16_t adcSampleCount;
void setAdcFlag()
{
// If time, the set adcFlag.
adcFlag = true;
}
// Function started after the connection to the server is established.
void iot_connected()
{
Serial.println("MQTT connected callback");
iot.log("IoT current example!");
}
void setup()
{
// setting up serial connection parameter
Serial.begin(115200);
Serial.println("Booting");
//iot.setConfig("wname", WIFI_NAME);
//iot.setConfig("wpass", WIFI_PASSWORD);
// print json config to serial
iot.printConfig();
// Initialize IoT library
iot.setup();
// Current: input pin, calibration (how big current can it read).
emon1.current(ADC_PIN, 30);
adcSampleCount = 1500;
// Start function setAdcFlag 0.5 second interval
adcTicker.attach(0.5, setAdcFlag);
}
void loop()
{
// IoT behind the plan work, it should be periodically called
iot.handle();
// If adcFlag is set, then send information to server
if(adcFlag)
{
adcFlag = false;
double val = emon1.calcIrms(adcSampleCount);
String msg = String(val);
// publishing measured current value to a MQTT broker
iot.publishMsg("curr", msg.c_str());
// sending measured current value to a computer
Serial.println(msg);
}
}