#include #include #include #include #include //ADXL345 レジスタアドレス #define BW_RATE 0x2C //Data rate and power mode control #define POWER_CTL 0x2D //Power Control Register #define DATA_FORMAT 0x31 //Data format control #define DATAX0 0x32 //X-Axis Data 0 #define SS 5 //デフォルトから設定を変更して再定義(IO15 -> IO5) #define JST 3600*9 //日本時間 //超音波センサのピン #define echoPin 16 #define trigPin 4 //超音波用 float duration, distance; //遮光ピン const int sensorPin = A0; //三軸加速度センサ用定義 char values[10]; int16_t x,y,z; float xg, yg, zg; float avg; //遮光センサ用定義 int sensorState[3]; int sensor_flg = 0; int sensor_count = 0; int find_flg = 0; int counter = 0; int count_send = 0; //時間表示関連 int tmp; char change[5]; char nowtime[25]; char hyphen[2] = "-"; char colon[2] = ":"; char space[4] = "%20"; const char* ssid = ""; //書き換えて const char* password = ""; //書き換えて //サーバーのアドレス const char* server = "54.150.129.137"; //WiFiClient をclientで定義 WiFiClient client; //関数の宣言 void connectWiFi(); void connectServer_acceleration(float,float,float,char); void connectServer_sonic(float,char); void connectServer_shading(int,char); void writeRegister(char,char); void readRegister(char,int16_t,char); void setup() { SPI.begin(); SPI.setDataMode(SPI_MODE3); SPI.setBitOrder(MSBFIRST); //シリアル通信を115200bpsで開始 Serial.begin(115200); // SSをHightに pinMode(SS, OUTPUT); digitalWrite(SS, HIGH); // ADXL345初期化 writeRegister(DATA_FORMAT, 0x03); // ±16g 10bit writeRegister(POWER_CTL, 0x08); // 測定モード //超音波センサのピン設定 pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); //遮光センサのピン設定 pinMode(sensorPin, INPUT_PULLUP); connectWiFi(); configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); } void loop() { delay(50); //delay(500); //時間関係 time_t t; struct tm *tm; //遮光関係 sensorState[sensor_count] = analogRead(sensorPin); if(sensorState[0]==1024&&sensorState[1]==1024&&sensorState[2]==1024){ sensor_flg = 1; }else{ sensor_flg = 0; } sensor_count++; if(sensor_count == 3){ sensor_count = 0; } if (sensor_flg == 1){ if (find_flg == 1){ counter += 1; Serial.print("found:"); Serial.println(counter); } find_flg = 0; } else { find_flg = 1; } //超音波関係 digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) * 0.0344; if (distance >= 400 || distance <= 2){ Serial.print("Distance = "); Serial.println("Out of range"); } else { Serial.print("Distance = "); Serial.print(distance); Serial.println(" cm"); } //ここまで // DATAX0レジスタから6バイトを取得 readRegister(DATAX0, 6, values); // 2Byteのデータを再構成 x = ((int16_t)values[1]<<8)|(int16_t)values[0]; y = ((int16_t)values[3]<<8)|(int16_t)values[2]; z = ((int16_t)values[5]<<8)|(int16_t)values[4]; // 0.03125 = (16*2)/(2^10) xg = x * 0.03125; yg = y * 0.03125; zg = z * 0.03125; Serial.print("X:"); Serial.print(xg); Serial.print("\t"); Serial.print("Y:"); Serial.print(yg); Serial.print("\t"); Serial.print("Z:"); Serial.println(zg); //現在時刻の取得(文字列一つに纏める) t = time(NULL); tm = localtime(&t); memset(nowtime, '\0', sizeof(nowtime)); tmp = tm->tm_year+1900; //int→char sprintf(change, "%d", tmp); //くっ付け strcat(nowtime,change); strcat(nowtime,hyphen); tmp = tm->tm_mon+1; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,hyphen); tmp = tm->tm_mday; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,space); tmp = tm->tm_hour; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,colon); tmp = tm->tm_min; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,colon); tmp = tm->tm_sec; sprintf(change, "%d", tmp); strcat(nowtime,change); Serial.println(nowtime); connectServer_acceleration(xg,yg,zg,nowtime); connectServer_sonic(distance,nowtime); if(counter != count_send){ //現在時刻の取得(文字列一つに纏める) t = time(NULL); tm = localtime(&t); memset(nowtime, '\0', sizeof(nowtime)); tmp = tm->tm_year+1900; //int→char sprintf(change, "%d", tmp); //くっ付け strcat(nowtime,change); strcat(nowtime,hyphen); tmp = tm->tm_mon+1; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,hyphen); tmp = tm->tm_mday; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,space); tmp = tm->tm_hour; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,colon); tmp = tm->tm_min; sprintf(change, "%d", tmp); strcat(nowtime,change); strcat(nowtime,colon); tmp = tm->tm_sec; sprintf(change, "%d", tmp); strcat(nowtime,change); Serial.println(nowtime); connectServer_shading(counter,nowtime); } count_send = counter; } //アクセスポイントに接続 void connectWiFi() { Serial.println(); Serial.println(); Serial.println("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); //もしWiFiに接続してなかったら繰り返す while (WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println ( "" ); Serial.print ( "Connected to " ); Serial.println ( ssid ); } //サーバーに接続し値を送る void connectServer_acceleration(float ax, float ay, float az, char xyzt[]) { if(client.connect(server, 80)){ Serial.println("connected to server"); client.print("GET /~08takuya/phptest/acceleration.php?sensnum="); //client.print("GET /~mizunoshota/cgi-bin/sensLog/sensReceive.py?sensnum="); client.print("0"); client.print("&x="); client.print(ax); client.print("&y="); client.print(ay); client.print("&z="); client.print(az); client.print("&xyztime="); client.print(xyzt); client.print(" HTTP/1.1\r\n"); client.println("HOST: 54.150.129.137"); client.println("Connection: close"); client.println(); } } void connectServer_sonic(float dis,char dist[]) { if(client.connect(server, 80)){ Serial.println("connected to server"); client.print("GET /~08takuya/phptest/acceleration.php?sensnum="); //client.print("GET /~mizunoshota/cgi-bin/sensLog/sensReceive.py?sensnum="); client.print("1"); client.print("&distance="); client.print(dis); client.print("&distime="); client.print(dist); client.print(" HTTP/1.1\r\n"); client.println("HOST: 54.150.129.137"); client.println("Connection: close"); client.println(); } } void connectServer_shading(int count,char cout[]) { if(client.connect(server, 80)){ Serial.println("connected to server"); client.print("GET /~08takuya/phptest/acceleration.php?sensnum="); //client.print("GET /~mizunoshota/cgi-bin/sensLog/sensReceive.py?sensnum="); client.print("2"); client.print("&counter="); client.print(count); client.print("&counttime="); client.print(cout); client.print(" HTTP/1.1\r\n"); client.println("HOST: 54.150.129.137"); client.println("Connection: close"); client.println(); } } void writeRegister(char registerAddress, char value) { // SPI開始時にSSをLOWにする digitalWrite(SS, LOW); // レジスタアドレス送信 SPI.transfer(registerAddress); // レジスタに設定する値送信 SPI.transfer(value); // SPI終了時にCSをHIGHにする digitalWrite(SS, HIGH); } void readRegister(char registerAddress, int16_t numBytes, char * values) { // 書き込みフラグを立てる char address = 0x80 | registerAddress; // 複数バイトフラグを立てる if(numBytes > 1)address = address | 0x40; // SPI開始時にSSをLOWにする digitalWrite(SS, LOW); // 読み出し先レジスタのアドレスを送信 SPI.transfer(address); // 値の読み出し for(int16_t i=0; i