// Note: // 1. SD must be formated to FAT16 // 2. As the buffer of softserial has 64 bytes, so the code read 32 bytes each time // 3. Please add the libaray to the lib path //**************************** //SD card 與32S接法 //MOSI - pin 23 //MISO - pin 19 //CLK(SCK)- pin 18 //CS - pin 5 //***************************** //PTC06與32S接法 //RX - pin12 //TX - pin13 //如果tx rx接錯,會收到一連串空白 #include #include #include #include HardwareSerial mySerial(1); //變數設定 byte ZERO = 0x00; byte incomingbyte; //a=起始點,a=-0x0000為預設值,但可能看狀況修改,要查看hex資料 long int ar=-0x0020,j=0,k=0,count=0,i=0; int no=1; String sno; uint8_t MH,ML; boolean EndFlag=0; File myFile; boolean firsttime=true; int touchpin=36; //網路傳輸設定 char SSID[]="you"; char PASSWORD[]=""; //LINE傳輸中介 String host = "your IP"; //中介網址勿修改 String url="/upload"; //中介網址勿修改 String Bearer="bQgDbqrFZDId4cRjWxgRb2u7PtHtZBspIkTzKQSTjtE"; //輸入你的Line token值=Bearer ..... String message="yourmessage"; // 1 opencv / 2 watson WiFiMulti WiFiMulti; //相機命令宣告 void SendResetCmd(); void SetImageSizeCmd(String); void SetBaudRateCmd(); void SendTakePhotoCmd(); void SendReadDataCmd(); void StopTakePhotoCmd(); //檔案上傳函數 void wifisendfile(String); void setup() { Serial.begin(9600); mySerial.begin(115200,SERIAL_8N1,13,12); Serial.print("Initializing SD card..."); if (!SD.begin()) { Serial.println("initialization failed!"); return; } Serial.println("initialization done."); pinMode(touchpin,INPUT); Serial.println("Setup Wifi...."); for(int t = 4; t > 0; t--) { Serial.print("."); delay(1000); } WiFiMulti.addAP(SSID, PASSWORD);//連線到WIFI AP delay(1000); } void loop() { if((WiFiMulti.run() == WL_CONNECTED)) { //網路連線成功 //每1秒感測一次 delay(1000); Serial.println(digitalRead(touchpin)); if (digitalRead(touchpin)) { //設定初始值 byte a[32]; int ii; SetImageSizeCmd("s");//set pic size s=160x120,m=620x240,l=640x480 delay(20); SendResetCmd(); delay(2000); //Wait 2-3 second to send take picture command Serial.println("Take Pic Command"); SendTakePhotoCmd(); //take pic command firsttime=true; while(mySerial.available()>0) { incomingbyte=mySerial.read(); } no=no+1; sno=String(no); myFile = SD.open("/pic" + sno + ".jpg", FILE_WRITE); //open file to save pic Serial.println("Saving to SD"); while(!EndFlag) { //Serial.print("."); j=0; k=0; count=0; SendReadDataCmd(); delay(20);//WAIT if(firsttime) { //第一次可能讀取到表頭,因此要改成k>10,要查看hex資料 while(mySerial.available()>0) { incomingbyte=mySerial.read(); k++; if((k>10)&&(j<32)&&(!EndFlag)) { a[j]=incomingbyte; j++; count++; } } firsttime=false; } else { while(mySerial.available()>0) { incomingbyte=mySerial.read(); k++; if((k>5)&&(j<32)&&(!EndFlag)) { a[j]=incomingbyte; if((a[j-1]==0xFF)&&(a[j]==0xD9)){EndFlag=1;} j++; count++; } } } //顯示hex值,當確認照片正常時,可註解本處 for(j=0;j (bufSize - 1)) { client.write((const uint8_t *)clientBuf, bufSize); clientCount = 0; } } if (clientCount > 0) { client.write((const uint8_t *)clientBuf, clientCount); //Serial.println("Sent LAST buffer"); } // send tail char charBuf3[tail.length() + 1]; tail.toCharArray(charBuf3, tail.length() + 1); client.write(charBuf3); //Serial.print(charBuf3); } Serial.println("end_request"); } String lastline; while(client.connected()) { while(client.available()) { String line = client.readStringUntil('\r'); lastline=line; Serial.print(line); if(line.indexOf("}")>=0 || line.indexOf("")>=0 ) { client.stop(); } } } myFile.close(); Serial.println("closing connection"); //檔案上傳成功 Serial.print("File upload complete"); } //相機命令區 void SendResetCmd() { mySerial.write(0x56); mySerial.write(ZERO); mySerial.write(0x26); mySerial.write(ZERO); } void SetImageSizeCmd(String size) { mySerial.write(0x56); mySerial.write(ZERO); mySerial.write(0x31); mySerial.write(0x05); mySerial.write(0x04); mySerial.write(0x01); mySerial.write(ZERO); mySerial.write(0x19); if (size=="s") { Serial.println("set pic size as 160x120"); mySerial.write(0x22);//160*120 } else if (size=="m") { Serial.println("set pic size as 320x240"); mySerial.write(0x11);//320*240 } else if (size=="l") { Serial.println("set pic size as 640x480"); mySerial.write(ZERO);//640*480 } else { //其他 Serial.println("set pic size as 160x120"); mySerial.write(0x22);//160x120 } } void SetBaudRateCmd() { //seem useless mySerial.write(0x56); mySerial.write(ZERO); mySerial.write(0x24); mySerial.write(0x03); mySerial.write(0x01); mySerial.write(0x0D); mySerial.write(0xA6); } void SendTakePhotoCmd() { mySerial.write(0x56); mySerial.write(ZERO); mySerial.write(0x36); mySerial.write(0x01); mySerial.write(ZERO); } void SendReadDataCmd() { MH=ar/0x100; ML=ar%0x100; mySerial.write(0x56); mySerial.write(ZERO); mySerial.write(0x32); mySerial.write(0x0C); mySerial.write(ZERO); mySerial.write(0x0a); mySerial.write(ZERO); mySerial.write(ZERO); mySerial.write(MH); mySerial.write(ML); mySerial.write(ZERO); mySerial.write(ZERO); mySerial.write(ZERO); mySerial.write(0x20); mySerial.write(ZERO); mySerial.write(0x0A); ar+=0x20; } void StopTakePhotoCmd() { mySerial.write(0x56); mySerial.write(ZERO); mySerial.write(0x36); mySerial.write(0x01); mySerial.write(0x03); }