I am working with Arduino Yun and camera connected to its USB Host.
I have added to arduino Adafruit NRF8001 BLE module and hacked the camera so I can use my smartphone to execute several options like power on/off the camera and start recording for instance - this part works good. On top of that one of the commands sent from smartphone is to execute python script which is on the openWRT side that uploads contents of the camera SD card to dropbox. And here is my problem because as soon as I try to execute the linux script the bluetooth connections drops (and the script is not executed - so it drops when it tries to connect to openWRT I guess, using bridge).
I am suspecting that is has to do something with the UART or rx/tx communication but I can’t seem to pinpoint what exactly goes wrong. Maybe it is because I didn’t fully understood Arduino Yun’s architecture. I am rather a beginner and I would be glad to hear any pointers where to look or what to take into the consideration. Thank you in advance for any help.
I do realise that is quite specific, but I was hoping that maybe someone will notice some obvious mistake
And last but not least the Arduino code:
//LIBRARIES
#include <SPI.h>
#include <Adafruit_BLE_UART.h>
#include <Bridge.h>
#include <Process.h>
//#include <Adafruit_BLE_Firmata.h>
//#include <Boards.h>
//#include <Shifter.h>
//#include <Console.h>
//BLUETOOTH
#define ADAFRUITBLE_REQ 3
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST 4
Adafruit_BLE_UART uart = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
static bool isBTConnected = false;
void initBluetooth () {
uart.setRXcallback(rxCallback);
uart.setACIcallback(aciCallback);
uart.setDeviceName(PSTR("test")); /* 7 characters max! */
Serial.print(F("BT:"));
if (uart.begin())
Serial.println(F("OK"));
else
Serial.println(F("FAILED"));
}
void aciCallback(aci_evt_opcode_t event) {
switch (event)
{
case ACI_EVT_DEVICE_STARTED:
// Serial.println(F("Advertising started"));
break;
case ACI_EVT_CONNECTED:
isBTConnected = true;
//Serial.println(F("Connected!"));
break;
case ACI_EVT_DISCONNECTED:
isBTConnected = false;
// Serial.println(F("Disconnected or advertising timed out"));
break;
default:
// Serial.print(F("ACI OPCODE:"));
Serial.println(event);
break;
}
}
void rxCallback(uint8_t *buffer, uint8_t len) {
uint8_t target_len = 4;
const char* target = "open";
uint8_t counter = 0;
char ByteReceived;
//float test_output;
//test_output = 5,5;
Serial.print(F("Received "));
Serial.print(len);
Serial.print(F(" bytes: "));
for (int i = 0; i < len; i++) {
char ch = (char)buffer[i];
Serial.print(ch);
ByteReceived = ch;
switch (ByteReceived) {
case 'p':
power();
uart.print("Power Received!");
break;
case 'm':
change_mode();
uart.print("Mode changed");
break;
case 's':
shutter();
uart.print("Recording");
break;
case 'd':
uart.print("DEBUG");
delay(2000);
upload2dropbox();
uart.print("File uploaded!");
break;
}
}
}
//CONTROLS
void upload2dropbox() {
int spk = 4;
int freq = 800;
pinMode(spk, OUTPUT);
//I was debuging and after this line BLE disconects
tone(spk, freq);
delay(16);
noTone(spk);
Process p;
//p.begin("curl");
//p.runShellCommandAsynchronously("./dropbox_uploader.sh -k upload /mnt/sda1/test2.txt test2.txt");
//p.addParameter("./dropbox_uploader.sh -k upload /mnt/sda1/test2.txt test2.txt");
//p.run();
//Serial.flush();
//p.close();
p.begin("python");
p.addParameter("/root/dropbox_run.py");
p.run();
p.close();
tone(spk, freq);
delay(16);
noTone(spk);
tone(spk, freq);
delay(16);
noTone(spk);
}
void power() { //Turn the power ON or OFF
int spk = 6;
int freq = 800;
pinMode(spk, OUTPUT);
int powerButton = A5;
digitalWrite(powerButton, HIGH);
delay(500); // Wait half a second
digitalWrite(powerButton, LOW);
tone(spk, freq);
delay(16);
noTone(spk);
delay(100);
tone(spk, freq);
delay(16);
noTone(spk);
delay(100);
tone(spk, freq);
delay(16);
noTone(spk);
delay(100);
Serial.println(F("Camera is turned ON or OFF"));
}
void change_mode() { //Change the mode of the camera
int spk = 6;
int freq = 800;
pinMode(spk, OUTPUT);
int modeButton = A3;
int led = 7;
digitalWrite(modeButton, HIGH);
digitalWrite(led, HIGH);
delay(500); // Wait half a second
digitalWrite(modeButton, LOW);
digitalWrite(led, LOW);
tone(spk, freq);
delay(16);
noTone(spk);
//int led = 7;
//shifter.setPin(1, HIGH);
//shifter.write();
//digitalWrite(led, HIGH);
//delay(500); // Wait half a second
//shifter.setPin(1, LOW);
//shifter.write();// Release the mode button.
//digitalWrite(led, LOW);
Serial.println(F("Mode is changed"));
}
void shutter() { //Shutter control
int spk = 6;
int freq = 800;
pinMode(spk, OUTPUT);
int shutterButton = A4;
int led = 7;
digitalWrite(shutterButton, HIGH); // Activate the shutter button
digitalWrite(led, HIGH);
delay(500); // Wait half a second
digitalWrite(shutterButton, LOW); // Release the shutter button.
digitalWrite(led, LOW);
delay(1000);
tone(spk, freq);
delay(50);
noTone(spk);
delay(100);
tone(spk, freq);
delay(50);
noTone(spk);
Serial.println(F("Recording is ON or OFF"));
}
//SETUP
void setup () {
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
pinMode(9, OUTPUT);
pinMode(7, OUTPUT);
pinMode(11, OUTPUT);
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
Serial.println(F("BT+ETH"));
Bridge.begin();
initBluetooth();
}
void loop () {
uart.pollACI();
digitalWrite(5, HIGH);
}
To make things clear this is the python script I am calling:
#!/usr/bin/python
import sys
import subprocess
subprocess.call(['./dropbox_uploader.sh', '-k', 'upload', '/mnt/sdb1/test2.txt', 'test2.txt'])
As you can see I am actually only calling the .sh script and by default it would be cool to just call it but while trying to solve the problem I though maybe an .sh file is making some problems so I wrapped it into python. Probably this is not the case but I guess it is important to mention it.