Fabrication industrielle
Internet des objets industriel | Matériaux industriels | Entretien et réparation d'équipement | Programmation industrielle |
home  MfgRobots >> Fabrication industrielle >  >> Manufacturing Technology >> Processus de fabrication

Arduino - Prendre une photo - Télécharger sur Google Drive

Composants et fournitures

Arduino Mega 2560
× 1
PHPoC WiFi Shield 2 pour Arduino
× 1
Seeed Grove - Kit de caméra série
× 1
Bouclier de base de Seeed Grove
× 1
Bouton
× 1

À propos de ce projet

Si vous êtes débutant, vous pouvez en apprendre davantage sur Arduino ici.

Démonstration

Comment ça marche

1. Connectez-vous au compte Google via OAuth 2.0 pour les appareils IoT afin d'obtenir access_token.

Le processus de connexion est décrit dans ce projet sur Hackster.

2. Lorsque le bouton est enfoncé, Arduino obtient l'image de l'appareil photo, puis la télécharge sur Google Drive à l'aide de access_token via l'API Google Drive.

L'API Google Drive pour le téléchargement de fichier est décrite dans le document Google.

Comment faire

  • Créez un projet Google à partir du portail des développeurs Google et obtenez GOOGLE_CLIENT_ID et GOOGLE_CLIENT_SECRET
  • Remplacer GOOGLE_CLIENT_ID et GOOGLE_CLIENT_SECRET dans le code Arduino
  • Télécharger le fichier login.php sur PHPoC Shield. Voir les instructions
  • Compiler et télécharger le code Arduino via Arduino IDE
  • Voir ip_address du bouclier PHPoC sur Serial Monitor
  • Accédez à la page de connexion sur PHPoC Shield :http://ip_address/login.php et connectez-vous à votre compte Google
  • Appuyez sur le bouton pour prendre une photo
  • Vérifiez votre Google Drive après deux secondes, vous verrez la photo prise dans votre Drive.

Autre plate-forme matérielle

J'ai fait le même projet pour une autre plate-forme matérielle ici.

Le meilleur kit de démarrage Arduino pour débutant

Si vous recherchez un kit Arduino, consultez Le meilleur kit Arduino pour les débutants


Références de fonction

  • Serial.begin()
  • Série.println()
  • retard()
  • millis() ​​​​​​​
  • boucle for
  • boucle while
  • si autre
  • boucle()
  • configuration()
  • String.toInt()
  • String.substring()
  • Chaîne.indexOf()
  • Chaîne.remove()
  • Chaîne.égales()

Code

  • ArduinoGoogleDrive
  • login.php
  • grove_camera.h
ArduinoGoogleDriveArduino
Il s'agit du code principal d'Arduino.
#include #include #include "grove_camera.h"// Remplacez votre GOOGLE_CLIENT_ID et GOOGLE_CLIENT_SECRET iciString GOOGLE_CLIENT_ID ="xxxxxxxxxxxxxxx" String GOOGLE_CLIENT_SECRET ="xxxxxxxxxxxxxxxxxxxxxxxx";PhpocServer websocket_server(80);String http_resp_hearder(PhpocClient &client){ String hearer =""; while(1){ if(client.available()){ String line =client.readLine(); if(ligne =="\r\n") break ; sinon auditeur +=ligne ; } if(!client.connected()){ client.stop(); Pause; } } return hearer;}String http_resp_body(PhpocClient &client){ String body =""; while(1){ if(client.available()){ char c =client.read(); corps +=c; } if(!client.connected()){ client.stop(); Pause; } } return body;}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(){ client PhpocClient ; // Étape 1 : Demandez les codes d'appareil et d'utilisateur if(client.connectSSL("accounts.google.com", 443)){ Serial.println(F("Connected to server")); Corps de chaîne =F("client_id="); corps +=GOOGLE_CLIENT_ID ; body +=F("&scope=https://www.googleapis.com/auth/drive.file"); client.println(F("POST /o/oauth2/device/code HTTP/1.1")); client.println(F("Hôte : comptes.google.com")); client.println(F("Connexion :fermer")); client.println(F("Accepter :*/*")); client.println(F("Type de contenu :application/x-www-form-urlencoded")); client.print(F("Content-Length:")); client.println(body.length()); client.println(); client.print(corps); Chaîne response_hearder =http_resp_hearder(client); String response_body =http_resp_body(client); //Serial.println(response_hearder); //Serial.println(response_body); JSONVar body_json =JSON.parse(response_body); if(JSON.typeof(body_json) =="undefined"){ Serial.println("L'analyse de l'entrée a échoué !"); retourner; } // Étape 2 :gérer la réponse du serveur d'autorisation String device_code =""; Chaîne user_code ="" ; long expires_in =0 ; intervalle entier =0 ; Chaîne verification_url ="" ; bool is_valid =true; if(body_json.hasOwnProperty("device_code")) device_code =body_json["device_code"]; sinon est_valide =faux ; if(body_json.hasOwnProperty("user_code")) user_code =body_json["user_code"]; sinon est_valide =faux ; if(body_json.hasOwnProperty("expires_in")) expires_in =(long) body_json["expires_in"]; sinon est_valide =faux ; if(body_json.hasOwnProperty("interval")) intervalle =(int) body_json["interval"]; sinon est_valide =faux ; if(body_json.hasOwnProperty("verification_url")) verification_url =body_json["verification_url"] ; sinon est_valide =faux ; if(is_valid){ // Étape 3 :Affichez le code utilisateur Serial.print(F("Next, visit ")); Serial.print(verification_url); Serial.print(F(" sur votre ordinateur de bureau ou smartphone et entrez ce code :")); Serial.println(user_code); Message de chaîne ; msg ="{\"fournisseur\":\"google\","; msg +="\"action\":\"CONNEXION\","; msg +="\"verification_url\":\"" + verification_url + "\","; msg +="\"user_code\":\"" + user_code + "\"}" ; websocket_send(msg); // Étape 5 :Serveur d'autorisation de sondage int poll_max =expires_in / interval; corps =F("client_id="); corps +=GOOGLE_CLIENT_ID ; corps +=F("&client_secret="); corps +=GOOGLE_CLIENT_SECRET ; corps +=F("&code="); corps +=code_périphérique ; corps +=F("&grant_type=http://oauth.net/grant_type/device/1.0"); for(int poll_count =0; poll_count  debounceDelay) { if (reading !=buttonState) { buttonState =read; if (buttonState ==HIGH) { return true; } } } lastButtonState =lecture ; return false;}void setup(){ Serial.begin(115200); while(!Série); Phpoc.begin(PF_LOG_SPI | PF_LOG_NET); websocket_server.beginWebSocket("connexion"); Serial.print("Adresse du serveur WebSocket :"); Serial.println(Phpoc.localIP()); pinMode(2, INPUT); cameraInit(CT_JPEG, PR_160x120, JR_640x480);}void loop(){ PhpocClient client =websocket_server.available(); if (client) { Chaîne 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 n'est pas valide, veuillez vous reconnecter"); }}
login.phpPHP
Ce code de fichier est téléchargé sur PHPoC Shield. Il fournit une interface utilisateur Web pour le processus de connexion Google
PHPoC / <?echo system("uname -i")?>
G o o g l e
Connexion pour Arduino




grove_camera.hC/C++
Bibliothèque pour Grove Camera
#define PIC_PKT_LEN 512 //longueur des données de chaque lecture, ne définissez pas cette valeur trop grande car la RAM est limitée#define CAM_ADDR 0//Color Type#define CT_GRAYSCALE_2 0x01#define CT_GRAYSCALE_4 0x02#define CT_GRAYSCALE_8 0x03#define CT_COLOR_12 0x05#define CT_COLOR_16 0x06#define CT_JPEG 0x07//Preview Resolution#define PR_80x60 0x01#define PR_160x120 0x03// JPEG Resolution#define JR_80x64 0x01#define JR_160x128 0x03#define 0x07_cons# ); // 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} ; while (1) { cameraClearRxBuf(); cameraEnvoiCmd(cmd3, 6); if(Serial.readBytes((char *)resp, 6) !=6) continuez ; if(resp[0] ==0xaa &&resp[1] ==(0x0e | camera_address) &&resp[2] ==0x06 &&resp[4] ==0 &&resp[5] ==0) break; }}caméra longueGetPicture(){ char cmd[] ={ 0xaa, 0x04 | adresse_caméra, 0x01, 0x00, 0x00, 0x00 } ; caractère non signé resp[6] ; picTotalLen long non signé =0 ; // longueur de l'image while (1){ cameraClearRxBuf(); cameraEnvoiCmd(cmd, 6); if(Serial.readBytes((char *)resp, 6) !=6) continuez ; if(resp[0] ==0xaa &&resp[1] ==(0x0e | camera_address) &&resp[2] ==0x04 &&resp[4] ==0 &&resp[5] ==0){ Serial.setTimeout (1000); if(Serial.readBytes((char *)resp, 6) !=6) continuez ; if(resp[0] ==0xaa &&resp[1] ==(0x0a | camera_address) &&resp[2] ==0x01){ picTotalLen =(resp[3]) | (resp[4] <<8) | (resp[5] <<16); Pause; } } } camera_packet_num =(picTotalLen) / (PIC_PKT_LEN - 6); camera_last_packet_len =PIC_PKT_LEN; if((picTotalLen % (PIC_PKT_LEN-6)) !=0){ camera_packet_num +=1; camera_last_packet_len =picTotalLen % (PIC_PKT_LEN - 6) + 6 ; } return picTotalLen;}long cameraGetPacket(unsigned int i, char* buf){ char cmd[] ={ 0xaa, 0x0e | adresse_caméra, 0x00, 0x00, 0x00, 0x00 } ; Serial.setTimeout(100); if(i > 8) &0xff; cameraClearRxBuf(); cameraEnvoiCmd(cmd, 6); int pkt_len; if(i <(camera_packet_num - 1)) pkt_len =PIC_PKT_LEN; else pkt_len =camera_last_packet_len; uint16_t cnt =Serial.readBytes((char *)buf, pkt_len); retour cnt; } else { cmd[4] =0xf0; cmd[5] =0xf0 ; cameraEnvoiCmd(cmd, 6); } return "";}

Schémas


Processus de fabrication

  1. Arduino Spybot
  2. FlickMote
  3. Téléviseur maison B-Gone
  4. horloge maîtresse
  5. Trouvez-moi
  6. Puissance Arduino
  7. Tech-TicTacToe
  8. Arduino quadrupède
  9. Google investit pour favoriser l'adoption d'appareils de sécurité IoT