Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:current [2020/07/20 11:26] – external edit 127.0.0.1en:iot:examples:current [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 7: Line 7:
  
 Needed libraries: Needed libraries:
-<code>lib_deps = ITTIoT@1.0.5, EmonLib</code>+<code>lib_deps = ITTIoT, EmonLib</code>
  
 The example code above shows the alternating current value to current topic "curr". If nothing is measured the current value is NaN. The example code above shows the alternating current value to current topic "curr". If nothing is measured the current value is NaN.
  
 <code c> <code c>
- 
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
Line 18: Line 17:
 #include <Ticker.h> #include <Ticker.h>
  
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "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;+    // If time, the set adcFlag. 
 +    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("Booting");     Serial.println("Booting");
-    iot.printConfig(); // print json config to serial+ 
 +    //iot.setConfig("wname", WIFI_NAME); 
 +    //iot.setConfig("wpass", WIFI_PASSWORD); 
 +     
 +    // print json config to serial 
 +    iot.printConfig(); 
 +    // Initialize IoT library
     iot.setup();     iot.setup();
  
-    //emon1.current(ADC_PIN, iot.cfg->get<double>("stc"));             // Current: input pin, calibration. +    // Current: input pin, calibration (how big current can it read)
-    emon1.current(ADC_PIN, 30);  +    emon1.current(ADC_PIN, 30);
-    //adcSampleCount = iot.cfg->get<uint16_t>("samples");+
     adcSampleCount = 1500;     adcSampleCount = 1500;
  
 +    // Start function setAdcFlag 0.5 second interval
     adcTicker.attach(0.5, setAdcFlag);     adcTicker.attach(0.5, setAdcFlag);
 } }
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("curr", msg.c_str());       iot.publishMsg("curr", msg.c_str());
 +      // sending measured current value to a computer
       Serial.println(msg);       Serial.println(msg);
   }   }
- 
 } }
  
 </code> </code>
  
en/iot/examples/current.1595244384.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0