This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:current [2020/07/20 11:26] – external edit 127.0.0.1 | en:iot:examples:current [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 7: | Line 7: | ||
Needed libraries: | Needed libraries: | ||
- | < | + | < |
The example code above shows the alternating current value to current topic " | The example code above shows the alternating current value to current topic " | ||
<code c> | <code c> | ||
- | |||
#include < | #include < | ||
#include < | #include < | ||
Line 18: | Line 17: | ||
#include < | #include < | ||
+ | #define WIFI_NAME " | ||
+ | #define WIFI_PASSWORD " | ||
+ | |||
+ | // Pin definition for the current sensor | ||
#define ADC_PIN A0 | #define ADC_PIN A0 | ||
+ | // create a objects | ||
EnergyMonitor emon1; | EnergyMonitor emon1; | ||
Ticker adcTicker; | Ticker adcTicker; | ||
bool adcFlag; | bool adcFlag; | ||
- | |||
uint16_t adcSampleCount; | uint16_t adcSampleCount; | ||
void setAdcFlag() | void setAdcFlag() | ||
{ | { | ||
- | adcFlag=true; | + | |
+ | | ||
} | } | ||
+ | // Function started after the connection to the server is established. | ||
void iot_connected() | void iot_connected() | ||
{ | { | ||
Line 40: | Line 45: | ||
void setup() | void setup() | ||
{ | { | ||
+ | // setting up serial connection parameter | ||
Serial.begin(115200); | Serial.begin(115200); | ||
Serial.println(" | Serial.println(" | ||
- | | + | |
+ | //iot.setConfig(" | ||
+ | // | ||
+ | |||
+ | | ||
+ | iot.printConfig(); | ||
+ | // Initialize IoT library | ||
iot.setup(); | iot.setup(); | ||
- | | + | // Current: input pin, calibration |
- | emon1.current(ADC_PIN, | + | emon1.current(ADC_PIN, |
- | // | + | |
adcSampleCount = 1500; | adcSampleCount = 1500; | ||
+ | // Start function setAdcFlag 0.5 second interval | ||
adcTicker.attach(0.5, | adcTicker.attach(0.5, | ||
} | } | ||
Line 55: | Line 67: | ||
void loop() | void loop() | ||
{ | { | ||
+ | // IoT behind the plan work, it should be periodically called | ||
iot.handle(); | iot.handle(); | ||
+ | // If adcFlag is set, then send information to server | ||
if(adcFlag) | if(adcFlag) | ||
{ | { | ||
Line 62: | Line 76: | ||
double val = emon1.calcIrms(adcSampleCount); | double val = emon1.calcIrms(adcSampleCount); | ||
String msg = String(val); | String msg = String(val); | ||
+ | // publishing measured current value to a MQTT broker | ||
iot.publishMsg(" | iot.publishMsg(" | ||
+ | // sending measured current value to a computer | ||
Serial.println(msg); | Serial.println(msg); | ||
} | } | ||
- | |||
} | } | ||
</ | </ | ||