Created
August 29, 2021 03:44
-
-
Save swanmatch/8f1626814e729f5738d8cc7b233a365a to your computer and use it in GitHub Desktop.
FEMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space | |
#include <CayenneMQTTEthernet.h> | |
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard. | |
char username[] = "Paste your username"; | |
char password[] = "Paste your password"; | |
char clientID[] = "Paste your clientID"; | |
// 電圧 | |
#define VOLT 100 | |
//ms サンプリング間隔 | |
#define SAMPLE_PERIOD 1 | |
// サンプリング数 | |
#define SAMPLE_SIZE 1000 | |
// 読み取るピン | |
#define INPUT_CH 0 | |
// 基準電圧のピン | |
#define BASE_CH 1 | |
// Virtual Pin of the Digital Motion Sensor widget. | |
#define VIRTUAL_CHANNEL 22 | |
void setup() { | |
Serial.begin(9600); | |
Cayenne.begin(username, password, clientID); | |
} | |
void loop() { | |
Cayenne.loop(); | |
float w = powerRead(); | |
Serial.println(w); | |
Cayenne.virtualWrite(VIRTUAL_CHANNEL, w, "analog_sensor", "null"); | |
} | |
// 消費電力を読み取る | |
float powerRead() { | |
float sam = 0; | |
for (int i = 0; i < SAMPLE_SIZE; i++) { | |
delay(SAMPLE_PERIOD); | |
float diff = abs(analogRead(INPUT_CH) - analogRead(BASE_CH)); | |
float voltage = diff * 5 / 1023; | |
sam += voltage; | |
} | |
// 平均電圧 | |
float average = sam / SAMPLE_SIZE; | |
// 測定電流(変流比3000,30Aに対して10Ω負荷抵抗時に0.1V出力 0.1 * (3000 / 10) = 30A ) | |
// https://akizukidenshi.com/catalog/g/gP-08960/ | |
// 抵抗値を200Ωに変更 | |
float a = average * (3000 / 200); | |
// 電力 | |
double w = a * VOLT; | |
return w; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment