산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Manufacturing Technology >> 제조공정

Arduino - 사진 찍기 - Google 드라이브에 업로드

구성품 및 소모품

Arduino Mega 2560
× 1
Arduino용 PHPoC WiFi 쉴드 2
× 1
Seeed Grove - 직렬 카메라 키트
× 1
시드 그로브 베이스 실드
× 1
버튼
× 1

이 프로젝트 정보

초보자라면 여기에서 Arduino에 대해 배울 수 있습니다.

데모

작동 원리

1. IoT 기기용 OAuth 2.0을 통해 Google 계정에 로그인하여 access_token을 받으세요.

로그인 프로세스는 Hackster의 이 프로젝트에 설명되어 있습니다.

2. 버튼을 누르면 Arduino가 카메라에서 사진을 가져온 다음 access_token을 사용하여 Google 드라이브에 업로드합니다. Google 드라이브 API를 통해

파일 업로드를 위한 Google Drive API는 Google 문서에 설명되어 있습니다.

방법

<울>
  • Google 개발자 포털에서 Google 프로젝트를 만들고 GOOGLE_CLIENT_ID 및 GOOGLE_CLIENT_SECRET 받기
  • Arduino 코드에서 GOOGLE_CLIENT_ID 및 GOOGLE_CLIENT_SECRET 대체
  • login.php 파일을 PHPoC Shield에 업로드합니다. 지침 참조
  • Arduino IDE를 통해 Arduino 코드 컴파일 및 업로드
  • 시리얼 모니터에서 PHPoC 실드의 ip_address 보기
  • PHPoC 실드 로그인 페이지:http://ip_address/login.php 및 Google 계정에 로그인
  • 사진을 찍으려면 버튼을 누르세요.
  • 2초 후에 Google 드라이브를 확인하면 드라이브에 촬영된 사진이 표시됩니다.
  • 기타 하드웨어 플랫폼

    여기에서 다른 하드웨어 플랫폼에 대해 동일한 프로젝트를 만들었습니다.

    초보자를 위한 최고의 Arduino 스타터 키트

    Arduino 키트를 찾고 있다면 초보자를 위한 최고의 Arduino 키트를 참조하십시오.


    함수 참조

    <울>
  • Serial.begin()
  • Serial.println()
  • 지연()
  • 밀리()
  • for 루프
  • while 루프
  • 다른 경우
  • 루프()
  • 설정()
  • String.toInt()
  • String.substring()
  • String.indexOf()
  • String.remove()
  • String.equals()
  • <섹션 클래스="섹션 컨테이너 섹션 축소 가능" id="코드">

    코드

    <울>
  • ArduinoGoogle드라이브
  • 로그인.php
  • grove_camera.h
  • ArduinoGoogleDriveArduino
    이것은 주요 Arduino 코드입니다.
    #include #include #include "grove_camera.h"// GOOGLE_CLIENT_ID 및 GOOGLE_CLIENT_SECRET hereString GOOGLE_CLIENT_ID ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;xxxxxxxxxusercontentxxxxxx.xxxxxx를 교체하십시오. 문자열 GOOGLE_CLIENT_SECRET ="xxxxxxxxxxxxxxxxxxxxxxxx";PhpocServer websocket_server(80);문자열 http_resp_hearder(PhpocClient &client){ 문자열 청취자 =""; while(1){ if(client.available()){ 문자열 줄 =client.readLine(); if(줄 =="\r\n") 중단; 그렇지 않으면 청취자 +=라인; } if(!client.connected()){ client.stop(); 부서지다; } } 리턴 히더;}String http_resp_body(PhpocClient &client){ String body =""; while(1){ if(client.available()){ char c =client.read(); 본체 +=c; } if(!client.connected()){ client.stop(); 부서지다; } } 반환 본문;}String access_token ="";String refresh_token =""; unsigned long access_token_expire_at =0;void websocket_send(String msg){ char wbuf[256]; msg.toCharArray(wbuf, msg.length() + 1); websocket_server.write(wbuf, msg.length());}void googleDeviceOAuthLogin(){ PhpocClient 클라이언트; // 1단계:기기 및 사용자 코드 요청 if(client.connectSSL("accounts.google.com", 443)){ Serial.println(F("Connected to server")); 문자열 본문 =F("client_id="); 본문 +=GOOGLE_CLIENT_ID; 본문 +=F("&scope=https://www.googleapis.com/auth/drive.file"); client.println(F("POST /o/oauth2/device/code HTTP/1.1")); client.println(F("호스트:accounts.google.com")); client.println(F("연결:닫기")); client.println(F("승인:*/*")); client.println(F("콘텐츠 유형:응용 프로그램/x-www-form-urlencoded")); client.print(F("콘텐츠 길이:")); client.println(본체.길이()); 클라이언트.println(); client.print(본문); 문자열 response_hearder =http_resp_hearder(클라이언트); 문자열 response_body =http_resp_body(클라이언트); //Serial.println(response_hearder); //Serial.println(response_body); JSONVar body_json =JSON.parse(response_body); if(JSON.typeof(body_json) =="undefined"){ Serial.println("파싱 입력 실패!"); 반품; } // 2단계:인증 서버 응답 처리 String device_code =""; 문자열 user_code =""; 긴 만료_인 =0; 정수 간격 =0; 문자열 확인_url =""; 부울 is_valid =true; if(body_json.hasOwnProperty("장치 코드")) device_code =body_json["장치 코드"]; 그렇지 않으면 is_valid =거짓; if(body_json.hasOwnProperty("user_code")) user_code =body_json["user_code"]; 그렇지 않으면 is_valid =거짓; if(body_json.hasOwnProperty("expires_in")) expires_in =(긴) body_json["expires_in"]; 그렇지 않으면 is_valid =거짓; if(body_json.hasOwnProperty("간격")) 간격 =(int) body_json["간격"]; 그렇지 않으면 is_valid =거짓; if(body_json.hasOwnProperty("verification_url")) validation_url =body_json["verification_url"]; 그렇지 않으면 is_valid =거짓; if(is_valid){ // 3단계:사용자 코드 표시 Serial.print(F("다음, 방문 ")); Serial.print(verification_url); Serial.print(F(" 데스크톱 또는 스마트폰에서 다음 코드를 입력하십시오:")); Serial.println(사용자 코드); 문자열 메시지; msg ="{\"제공자\":\"구글\","; 메시지 +="\"작업\":\"로그인\","; msg +="\"verification_url\":\"" + validation_url + "\","; 메시지 +="\"사용자 코드\":\"" + 사용자 코드 + "\"}"; websocket_send(msg); // 5단계:인증 서버 폴링 int poll_max =expires_in / interval; 본문 =F("클라이언트 ID ="); 본문 +=GOOGLE_CLIENT_ID; 본문 +=F("&client_secret="); 본문 +=GOOGLE_CLIENT_SECRET; 본문 +=F("&코드="); 본문 +=device_code; 본문 +=F("&grant_type=http://oauth.net/grant_type/device/1.0"); for(int poll_count =0; poll_count  debounceDelay) { if (읽기 !=buttonState) { buttonState =읽기; if (buttonState ==HIGH) { true를 반환합니다. } } } lastButtonState =읽기; return false;} 무효 setup(){ Serial.begin(115200); 동안(! 직렬); Phpoc.begin(PF_LOG_SPI | PF_LOG_NET); websocket_server.beginWebSocket("로그인"); Serial.print("웹소켓 서버 주소 :"); Serial.println(Phpoc.localIP()); 핀모드(2, 입력); cameraInit(CT_JPEG, PR_160x120, JR_640x480);}무효 루프(){ PhpocClient 클라이언트 =websocket_server.available(); if (클라이언트) { 문자열 ws_str =client.readLine(); if(ws_str =="google\r\n") { googleDeviceOAuthLogin(); } } if(isButtonPressed(2)) { if(access_token !="" &&access_token_expire_at> millis()) cameraToGoogleDrive(); else Serial.println("access_token이 유효하지 않습니다. 다시 로그인하십시오."); }}
    login.phpPHP
    이 파일 코드는 PHPoC 쉴드에 업로드됩니다. Google 로그인 프로세스를 위한 웹 사용자 인터페이스 제공
    PHPoC / <?echo system("uname -i")?>
    <스팬 style="color:#4285F4">G o o g l e
    Arduino에 로그인




    grove_camera.hC/C++
    Grove Camera용 라이브러리
    #define PIC_PKT_LEN 512 //각 읽기 데이터 길이, 램이 제한되어 있으므로 너무 크게 설정하지 마십시오.#define CAM_ADDR 0//색상 유형#define CT_GRAYSCALE_2 0x01#define CT_GRAYSCALE_4 0x02#define CT_8define CT_COLOR_12이 0x05 #는 0x03으로 #가 JR_320x240이 0x05 #가 JR_640x480 0x07const 바이트 camera_address =정의 정의하는 0x01 #가 JR_160x128 정의 JR_80x64을 정의하는 0x01 #가 PR_160x120은 0x03 // JPEG 해상도 # 정의 PR_80x60 정의에는 0x06 #가 CT_JPEG에는 0x07 // 미리 해상도 # 정의 CT_COLOR_16 정의 (CAM_ADDR <<5 ); // addressunsigned int camera_packet_num;unsigned int camera_last_packet_len;void cameraClearRxBuf(){ while (Serial.available()){ Serial.read(); }}void cameraSendCmd(char cmd[], int cmd_len){ for (char i =0; i >8) &0xff ,0}; 동안 (1) { cameraClearRxBuf(); 카메라 센드Cmd(cmd3, 6); if(Serial.readBytes((char *)resp, 6) !=6) 계속; if(resp[0] ==0xaa &&resp[1] ==(0x0e | 카메라 주소) &&resp[2] ==0x06 &&resp[4] ==0 &&resp[5] ==0) 중단; }}긴 카메라GetPicture(){ char cmd[] ={ 0xaa, 0x04 | 카메라 주소, 0x01, 0x00, 0x00, 0x00 }; unsigned char resp[6]; 부호 없는 긴 picTotalLen =0; // 사진 길이 while (1){ cameraClearRxBuf(); 카메라 센드Cmd(cmd, 6); if(Serial.readBytes((char *)resp, 6) !=6) 계속; if(resp[0] ==0xaa &&resp[1] ==(0x0e | 카메라 주소) &&resp[2] ==0x04 &&resp[4] ==0 &&resp[5] ==0){ Serial.setTimeout (1000); if(Serial.readBytes((char *)resp, 6) !=6) 계속; if(resp[0] ==0xaa &&resp[1] ==(0x0a | 카메라 주소) &&resp[2] ==0x01){ picTotalLen =(resp[3]) | (resp[4] <<8) | (resp[5] <<16); 부서지다; } } } 카메라_패킷_num =(picTotalLen) / (PIC_PKT_LEN - 6); 카메라_last_packet_len =PIC_PKT_LEN; if((picTotalLen % (PIC_PKT_LEN-6)) !=0){ 카메라_패킷 번호 +=1; camera_last_packet_len =picTotalLen % (PIC_PKT_LEN - 6) + 6; } return picTotalLen;}long cameraGetPacket(unsigned int i, char* buf){ char cmd[] ={ 0xaa, 0x0e | 카메라 주소, 0x00, 0x00, 0x00, 0x00 }; Serial.setTimeout(100); if(i > 8) &0xff; 카메라 지우기RxBuf(); 카메라센드Cmd(cmd, 6); 정수 pkt_len; if(i <(camera_packet_num - 1)) pkt_len =PIC_PKT_LEN; 그렇지 않으면 pkt_len =카메라_last_packet_len; uint16_t cnt =Serial.readBytes((char *)buf, pkt_len); 반환 cnt; } 그렇지 않으면 { cmd[4] =0xf0; cmd[5] =0xf0; 카메라센드Cmd(cmd, 6); } 반환 "";}

    회로도


    제조공정

    1. Arduino 스파이봇
    2. FlickMote
    3. 수제 TV B-Gone
    4. 마스터 시계
    5. 나를 찾기
    6. Arduino Power
    7. Tech-TicTacToe
    8. Arduino Quadruped
    9. OK Google이 포함된 Bluetooth 음성 제어 기기
    10. IoT 보안 장치 채택을 위한 Google 투자