Connecting A USB Drive
Finding It
Note that “mydrivename” can be anything you want it to be, its the name you will use to connect to it
string CommandResult;
int DriveMounted = 0;
CommandResult = do_console_command_get_result((char*)"ls -l /dev/disk/by-uuid/");
//cout << "Drive list result: " << CommandResult << endl;
if (CommandResult.find("sda1") != std::string::npos)
{
//----- USB DRIVE FOUND -----
//cout << "USB Device SDA1 Found" << endl;
//----- MOUNT IT -----
CommandResult = do_console_command_get_result((char*)"sudo mkdir /media/mydrivename 2>&1"); //" 2>&1" so we get the error output if the command fails
CommandResult += do_console_command_get_result((char*)"sudo chown -R pi:pi /media/mydrivename 2>&1");
CommandResult += do_console_command_get_result((char*)"sudo mount /dev/sda1 /media/mydrivename -o uid=pi,gid=pi 2>&1");
DriveMounted = 1;
if (CommandResult.find("already mounted") != std::string::npos) //Check for error
DriveMounted = 0;
}
else if (CommandResult.find("sdb1") != std::string::npos)
{
//----- USB DRIVE FOUND -----
//cout << "USB Device SDB1 Found" << endl;
//----- MOUNT IT -----
CommandResult = do_console_command_get_result((char*)"sudo mkdir /media/mydrivename 2>&1");
CommandResult += do_console_command_get_result((char*)"sudo chown -R pi:pi /media/mydrivename 2>&1");
CommandResult += do_console_command_get_result((char*)"sudo mount /dev/sdb1 /media/mydrivename -o uid=pi,gid=pi 2>&1");
DriveMounted = 1;
if (CommandResult.find("already mounted") != std::string::npos) //Check for error
DriveMounted = 0;
}
else if (CommandResult.find("sdc1") != std::string::npos)
{
//----- USB DRIVE FOUND -----
//cout << "USB Device SDC1 Found" << endl;
//----- MOUNT IT -----
CommandResult = do_console_command_get_result((char*)"sudo mkdir /media/mydrivename 2>&1");
CommandResult += do_console_command_get_result((char*)"sudo chown -R pi:pi /media/mydrivename 2>&1");
CommandResult += do_console_command_get_result((char*)"sudo mount /dev/sdc1 /media/mydrivename -o uid=pi,gid=pi 2>&1");
DriveMounted = 1;
if (CommandResult.find("already mounted") != std::string::npos) //Check for error
DriveMounted = 0;
}
if (!DriveMounted)
{
//----- NO USB DRIVE FOUND -----
return;
}
string my_file_path = "/media/mydrivename/hello.txt";
Releasing It After Use
CommandResult = do_console_command_get_result((char*)"sudo umount /media/mydrivename 2>&1");
if (CommandResult.find("busy") != std::string::npos)
{
bcm2835_delay(250);
CommandResult = do_console_command_get_result((char*)"sudo umount /media/mydrivename 2>&1");
}
CommandResult = do_console_command_get_result((char*)"sudo rm -r /media/mydrivename"); //Remove the directory ready for next time
