Simple IoT boardと電力計測モジュールINA226PRCを使ったサンプルプログラムです。計測したデータをIFTTTのMakerチャネルに送っています。 サンプリングは0.1秒刻みで、10秒間(100回計測)したら電圧、電流、電力それぞれについて平均、最大値、最小値を計算してIFTTTに送る仕様になっています。
Dependencies: INA226PRC SimpleIoTBoardLib mbed
main.cpp
00001 #include "mbed.h" 00002 #include "ESP8266Interface.h" 00003 #include "TCPSocketConnection.h" 00004 #include "ifttt.h" 00005 #include "INA226.hpp" 00006 #include "float.h" 00007 #include "SoftSerialSendOnry.h" 00008 00009 // ROOP_CNT * SAMPLING[sec] = 1ROOP_TIME[sec] 00010 #define ROOP_CNT 100 00011 #define SAMPLING 0.1 00012 00013 // Please change your enviroment. 00014 const char WIFI_SSID[] = "ssid"; 00015 const char WIFI_PASSWORD[] = "password"; 00016 const char IFTTT_EVENT_NAME[] = "event_name"; 00017 const char IFTTT_SECRET_KEY[] = "secret_key"; 00018 00019 // WIFI setting 00020 ESP8266Interface wifi(dp16, dp15, dp4, WIFI_SSID, WIFI_PASSWORD, 115200); // TX,RX,Reset,SSID,Password,Baud 00021 TCPSocketConnection socket; 00022 IFTTT *ifttt; 00023 00024 //Serial pc(USBTX,USBRX); 00025 SoftSerialSendOnry pc(dp10); 00026 I2C i2c(dp5, dp27); 00027 INA226 VCmonitor(i2c); 00028 00029 // Timer 00030 Timer timer; 00031 float nextUpdateTime = 0.0f; 00032 00033 // Data params 00034 double minW = DBL_MAX, maxW = 0.0, totalW = 0.0; // Power[mW] 00035 double minV = DBL_MAX, maxV = 0.0, totalV = 0.0; // Voltage[mV] 00036 double minC = DBL_MAX, maxC = 0.0, totalC = 0.0; // Current[mA] 00037 unsigned int count = 0; 00038 00039 void postIFTTT(){ 00040 char postData[128]; 00041 sprintf(postData, "%f,%f,%f,%f,%f,%f,%f,%f,%f", totalV/ROOP_CNT, maxV, minV, totalC/ROOP_CNT, maxC, minC, totalW/ROOP_CNT, maxW, minW); 00042 pc.printf("postData:%s\r\n",postData); 00043 ifttt->addIngredients(postData); 00044 ifttt->trigger(IFTTT_POST); 00045 } 00046 00047 void record(){ 00048 nextUpdateTime += SAMPLING; 00049 double V,C,W; 00050 if((VCmonitor.getVoltage(&V) != 0) || (VCmonitor.getCurrent(&C) != 0)){ 00051 pc.printf("Error:VCmonitor does not get voltage or current.\r\n"); 00052 return; 00053 } 00054 count++; 00055 00056 // Calculate power. 00057 W = V * C * 0.001;//[mW] 00058 // pc.printf("%7.2f [mV],%7.2f [mA], %f [mW]\r\n",V ,C ,W); 00059 00060 // update Max ,Min and Average 00061 if(maxW < W){maxW = W;} 00062 if(maxV < V){maxV = V;} 00063 if(maxC < C){maxC = C;} 00064 if(minW > W){minW = W;} 00065 if(minV > V){minV = V;} 00066 if(minC > C){minC = C;} 00067 totalW += W; 00068 totalV += V; 00069 totalC += C; 00070 00071 if(count >= ROOP_CNT){ // 0.1[sec] * 1000 = 10[sec] 00072 pc.printf("AVG %7.2f [mV],%7.2f [mA], %f [mW]\r\n",totalV/ROOP_CNT ,totalC/ROOP_CNT ,totalW/ROOP_CNT); 00073 pc.printf("MIN %7.2f [mV],%7.2f [mA], %f [mW]\r\n",minV, minC, minW); 00074 pc.printf("MAX %7.2f [mV],%7.2f [mA], %f [mW]\r\n",maxV, maxC, maxW); 00075 00076 postIFTTT(); 00077 00078 // clear params. 00079 minW = DBL_MAX, maxW = 0.0, totalW = 0.0; 00080 minV = DBL_MAX, maxV = 0.0, totalV = 0.0; 00081 minC = DBL_MAX, maxC = 0.0, totalC = 0.0; 00082 count = 0; 00083 nextUpdateTime = 0.0f; 00084 timer.reset(); 00085 } 00086 } 00087 00088 void initIFTTT(){ 00089 wifi.init(); //Reset 00090 wifi.connect(); //Use DHCP 00091 00092 pc.printf("IP Address is %s \n\r", wifi.getIPAddress()); 00093 // Initialize ifttt object, add up to 3 optional values, trigger event. 00094 ifttt = new IFTTT(IFTTT_EVENT_NAME, IFTTT_SECRET_KEY, &socket); // EventName, Secret Key, socket to use 00095 } 00096 00097 void initINA226PRC(){ 00098 pc.printf("VCmonitor INA226 TEST Program. (BUILD:[" __DATE__ "/" __TIME__ "])\r\n"); 00099 00100 if(!VCmonitor.isExist()){ 00101 pc.printf("VCmonitor NOT FOUND\r\n"); 00102 while(1){} 00103 } 00104 pc.printf("VCmonitor FOUND\r\n"); 00105 00106 unsigned short val = 0; 00107 if(VCmonitor.rawRead(0x00,&val) != 0){ 00108 pc.printf("VCmonitor READ ERROR\r\n"); 00109 while(1){} 00110 } 00111 pc.printf("VCmonitor Reg 0x00 : 0x%04x\r\n",val); 00112 00113 VCmonitor.setCurrentCalibration(); 00114 } 00115 00116 void init(){ 00117 pc.baud(9600); 00118 initIFTTT(); 00119 initINA226PRC(); 00120 timer.start(); 00121 } 00122 00123 int main() { 00124 init(); 00125 00126 while(1){ 00127 // wait 00128 while(timer.read() < nextUpdateTime){} 00129 record(); 00130 } 00131 }
Generated on Thu Jul 14 2022 07:29:52 by 1.7.2