This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| et:iot:examples:current [2022/08/30 11:28] – tekitatud karllall | et:iot:examples:current [2024/07/04 16:17] (current) – rojarl | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== (Current) Voolumõõtja näidiskood====== | ||
| + | |||
| + | Voolumõõtja peab olema ühendatud anduri mooduliga. Voolumõõtjal on 3,5 mm pistik, mis tuleb ühendada anduri vasakpoolsesesse (A) pesasse. Kontrolleri ja anduri moodulid peavad olema omavahel ühendatud. Voolu sensor peab olema 30A. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Vajaminevad teegid: | ||
| + | < | ||
| + | |||
| + | Näidiskood demonstreerib vahelduvvoolu väärtuse kuvamist vaheldusvoolu teemasse " | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | #include < | ||
| + | |||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_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(" | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | // setting up serial connection parameter | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | | ||
| + | // 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, | ||
| + | adcSampleCount = 1500; | ||
| + | |||
| + | // Start function setAdcFlag 0.5 second interval | ||
| + | adcTicker.attach(0.5, | ||
| + | } | ||
| + | |||
| + | 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(" | ||
| + | // sending measured current value to a computer | ||
| + | Serial.println(msg); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||