#include #include #include #include #define ETH 10 #define SD_CS 4 #define UDPlocalPort 8888 //----------------------------------------------------------------- // Crayfish life-support system //----------------------------------------------------------------- // 共通変数設定 //----------------------------------------------------------------- //(1)温度計測用 float GW_Temp; //(2)FANコントローラ用 int GW_FAN; //(3)照度計測用 int GW_CDS; //(4)湿度計測用 float GW_HUM; //Ethernet用 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; EthernetClient client; //NTP用 int tcnt = 0; unsigned long epoch; IPAddress timeServer(133, 243, 238, 164); // time.nist.gov NTP server const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets // A UDP instance to let us send and receive packets over UDP EthernetUDP Udp; //modem ERRカウント用 int GW_ERRCNT; //----------------------------------------------------------------- // セットアップ処理 //----------------------------------------------------------------- void setup() { pinMode(ETH, OUTPUT); pinMode(SD_CS, OUTPUT); pinMode(8, OUTPUT); //ETHERNET を選択 digitalWrite(SD_CS, HIGH); digitalWrite(ETH,LOW); //modem ERRカウント 初期化 digitalWrite(8,LOW); GW_ERRCNT = 0; delay(100); Serial.begin(9600); Serial.flush(); // this check is only needed on the Leonardo: while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection: //Serial.println("IP config..."); if (Ethernet.begin(mac) == 0) { //Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: for(;;) ; } // print your local IP address: //printLocalIP(); //SD CARD INITIALIZATION //Serial.print("Initializing SD card..."); digitalWrite(SD_CS, LOW); digitalWrite(ETH, HIGH); // see if the card is present and can be initialized: if (!SD.begin(SD_CS)) { //Serial.println("Card failed, or not present"); // don't do anything more: return; } //Serial.println("card initialized."); digitalWrite(SD_CS, HIGH); digitalWrite(ETH, LOW); //時刻情報の取得 Udp.begin(UDPlocalPort); GetTime(); } //----------------------------------------------------------------- // Main Loop //----------------------------------------------------------------- void loop() { //---1.データの取得 checkData() ; //---2.httpプロトコルでアクセス delay(1000); AccessHttp(); //接続エラーなしor連続5回未満のエラーの場合 if(GW_ERRCNT < 5){ //---3.1分間隔で再調査(delay時間を除く) delay(46800); //時間調整(前回時刻に60秒足す) epoch = epoch + 60; //前回より10分経過したらNTPサーバより時刻を再取得、DHCPサーバ更新 tcnt = tcnt + 1; if(tcnt > 9){ tcnt = 0; GetTime(); Ethernet.maintain(); //printLocalIP(); } } //接続エラーが連続5回発生した場合 else{ //modem電源を5秒間切断し再起動させる digitalWrite(8,HIGH); delay(5000); digitalWrite(8,LOW); //modem再起動後まで60秒waitする。 delay(60000); //NTPサーバより時刻を再取得、DHCPサーバ更新 tcnt = 0; GetTime(); Ethernet.maintain(); } } //---0.NTPサーバより時刻取得------------------------------------- void GetTime() { // send an NTP packet to a time server sendNTPpacket(timeServer); // wait to see if a reply is available delay(1000); if ( Udp.parsePacket() ) { // We've received a packet, read the data from it Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer //the timestamp starts at byte 40 of the received packet and is four bytes, // or two words, long. First, esxtract the two words: unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); // combine the four bytes (two words) into a long integer // this is NTP time (seconds since Jan 1 1900): unsigned long secsSince1900 = highWord << 16 | lowWord; // now convert NTP time into everyday time: //Serial.print("Unix time = "); // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: epoch = secsSince1900 - seventyYears; //Serial.print("NTP GET Time : "); //serialTime(); } } /* //---IPアドレスのシリアル出力------------------------------------- void printLocalIP(){ Serial.print("My IP address: "); for (byte thisByte = 0; thisByte < 4; thisByte++) { // print the value of each byte of the IP address: Serial.print(Ethernet.localIP()[thisByte], DEC); Serial.print("."); } Serial.println(); } */ // send an NTP request to the time server at the given address unsigned long sendNTPpacket(IPAddress& address) { // set all bytes in the buffer to 0 memset(packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request // (see URL above for details on the packets) packetBuffer[0] = 0b11100011; // LI, Version, Mode packetBuffer[1] = 0; // Stratum, or type of clock packetBuffer[2] = 6; // Polling Interval packetBuffer[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay & Root Dispersion packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; // all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.write(packetBuffer,NTP_PACKET_SIZE); Udp.endPacket(); } //---1.データの取得------------------------------------- void checkData() { float data[4]; char LW_Read; int LW_CNT = 0; int LW_LEN = 0; char LW_STR[12]; Serial.println("1 Get"); // Device No.1 Data Get //10秒受信まち delay(10000); if (Serial.available() > 0) { // 次のバイトが来るまで無限ループで待つ while (Serial.available() > 0) { LW_Read = Serial.read(); if(LW_Read == '.'){ LW_STR[LW_LEN] = LW_Read; LW_LEN++; } else if(LW_Read >= '0' && LW_Read <= '9'){ LW_STR[LW_LEN] = LW_Read; LW_LEN++; } else if(LW_LEN > 0){ LW_LEN = 0; data[LW_CNT] = atof(LW_STR); memset( LW_STR , '\0' , 12 ); LW_CNT++; } } Serial.flush(); //変数に割り当て //(1)温度計測用 GW_Temp = data[0]; //(4)湿度計測用 GW_HUM = data[1]; //(2)FANコントローラ GW_FAN = data[2]; //(3)照度計測用 GW_CDS = data[3]; // 取得情報をSD書き込み writeSDcard(); } //データが取得できない場合はデータ変更なし } //---時刻のシリアル出力------------------------------------- /* void serialTime(){ //時間 = 1日の秒数 86400 で割った余り(GMT+9なので32400秒足す) Serial.print(((epoch + 32400L) % 86400L) / 3600); Serial.print(':'); // print the minute (3600 equals secs per minute) if (((epoch % 3600) / 60) < 10 ) { // In the first 10 minutes of each hour, we'll want a leading '0' Serial.print('0'); } Serial.print(((epoch % 3600) / 60)); Serial.print('\n'); } */ //---SDカードへの記録------------------------------------- void writeSDcard(){ digitalWrite(SD_CS, LOW); digitalWrite(ETH, HIGH); delay(100); File dataFile = SD.open("datalog.txt", FILE_WRITE); // if the file is available, write to it: if (dataFile) { //時刻,温度,FAN状態,CDSレベル,湿度 の形式で記録する。 //時刻 dataFile.print(((epoch + 32400L) % 86400L) / 3600); dataFile.print(':'); if (((epoch % 3600) / 60) < 10 ) { dataFile.print('0'); } dataFile.print((epoch % 3600) / 60); dataFile.print(","); //温度 dataFile.print(GW_Temp); dataFile.print(","); //FAN状態 dataFile.print(GW_FAN); dataFile.print(","); //CDS dataFile.print(GW_CDS); dataFile.print(","); //湿度 dataFile.print(GW_HUM); dataFile.print("\n"); dataFile.close(); //Serial.println("save datalog.txt"); } // if the file isn't open, pop up an error: else { //Serial.println("error opening datalog.txt"); } digitalWrite(SD_CS, HIGH); digitalWrite(ETH, LOW); } //---httpプロトコルでアクセスしてPOST--------------------- void AccessHttp(){ char serverName[] = "api.thingspeak.com"; //デバック用 //Serial.println("connecting..."); if (client.connect(serverName, 80)) { //Serial.println("connected"); delay(1000); //thingspeakにGETする client.print("GET /update?key=\\ここにthingspeakのID\\&field1="); client.print(GW_Temp); client.print("&field2="); client.print(GW_FAN); client.print("&field3="); client.print(GW_CDS); client.print("&field4="); client.print(GW_HUM); client.print(" HTTP/1.0\n"); client.println(""); client.println(""); delay(1000); client.stop(); GW_ERRCNT = 0; } else { // if you didn't get a connection to the server: writeSDcardERR(); //Serial.println("connection failed"); GW_ERRCNT++; } // if the server's disconnected, stop the client: //Serial.println("disconnecting."); client.stop(); } //---SDカードへのERR記録------------------------------------- void writeSDcardERR(){ digitalWrite(SD_CS, LOW); digitalWrite(ETH, HIGH); delay(100); File dataFile = SD.open("datalog.txt", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.print(((epoch + 32400L) % 86400L) / 3600); dataFile.print(':'); if (((epoch % 3600) / 60) < 10 ) { dataFile.print('0'); } dataFile.print((epoch % 3600) / 60); dataFile.print(", ETHERNET FAILED"); dataFile.print("\n"); dataFile.close(); } // if the file isn't open, pop up an error: else { //Serial.println("error opening datalog.txt"); } digitalWrite(SD_CS, HIGH); digitalWrite(ETH, LOW); }