I am working on an Android app that will constantly receive data from an Arduino HC-05 and store it in a database. For now I am focusing on receiving the data properly (so just displaying it on the screen is my next step).
I have followed this guide (PDF) to setup a basic app that will receive data from the Arduino. However, it doesn't include a section about using the received data. I've tried adding two lines of code to the handleMessage function in order to display the received data on screen, but I don't see any difference in the textview (it stays "Hello World!").
I know that my Arduino is sending the data just fine, since on another app called "Serial Bluetooth Terminal" it is displayed properly. Here is the code for the Handler class:
Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
switch(msg.what) {
case 1:
String writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
// 2 lines of code I've added here:
TextView tempTextView = (TextView) findViewById(R.id.text_view);
tempTextView.setText(writeMessage);
break;
}
}
};
Aside from the 2 lines, the code is basically the same as the final stage of the guide. And here is the code for the Arduino (which currently just sends "1 2.00 3.00" over and over):
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(0, 1); //RX|TX
int Temp= 1;
float Ph = 2;
float Ec = 3;
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop() {
Serial.print(Temp);
Serial.print(" ");
delay(100);
Serial.print(Ph);
Serial.print(" ");
delay(100);
Serial.print(Ec);
Serial.print(" ");
delay(100);
Serial.print('\n');
if(Serial.available())
BTSerial.write(Serial.read());
}
Related
I’m pretty new to Arduino and Processing and and this is also my first time asking a question about coding on the internet.
I’m currently trying to do the following thing for a school project: I want to send a String with data from sensors on my Arduino to a android phone or tablet over bluetooth.
For simplicity’s sake I reduced the code in complexity, removing every part of my code that isn’t trying to facilitate the transfer of a String from Arduino to Processing on Android. The following code is simply trying to send the String “S01E” to the processing app on my phone, saving the String into my “String info” and displaying that string in form of a text(info, 20, 110); element.
Additional Info:
Arduino bluetooth module is a “Bluefruit EZ-Ling BLuetooth Shield”.
Phone is Samsung Galaxy S8.
Processing App is generated with Android mode of Processing.
My phone is successfully paired with the arduino over bluetooth.
The code compiles successfully, but it only shows my text(“test text”, 20, 120); on my phone, but not my text(info, 20, 110);, which I assume means that the String is not being received and the info String stays empty.
How can i proceed from here? Are there some obvious problems in my code. And how can I properly debug my code with the weird stack of technologies that I use?
Processing Code:
import netP5.*;
import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;
KetaiBluetooth bt;
boolean isConfiguring = true;
String info = "";
KetaiList klist;
ArrayList devicesDiscovered = new ArrayList();
// States of the two sensors
int B1in = 0;
int B2in = 0;
//********************************************************************
// The following code is required to enable bluetooth at startup.
//********************************************************************
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bt = new KetaiBluetooth(this);
}
void onActivityResult(int requestCode, int resultCode, Intent data) {
bt.onActivityResult(requestCode, resultCode, data);
}
void setup() {
size(displayWidth, displayHeight);
frameRate(10);
orientation(PORTRAIT);
background(255);
stroke(160);
fill(50);
//start listening for BT connections
bt.start();
//at app start select device…
isConfiguring = true;
}
void draw() {
background(255);
text(info,20,110);
text("test text",20,120);
println(info);
}
void onKetaiListSelection(KetaiList klist)
{
String selection = klist.getSelection();
bt.connectToDeviceByName(selection);
//dispose of list for now
klist = null;
}
//Call back method to manage data received
void onBluetoothDataEvent(String who, byte[] data) {
if (isConfiguring)
return;
//received
info = new String(data);
}
And here is my Arduino code:
#include <SoftwareSerial.h>
SoftwareSerial bt(2,3); // RX, TX
// Enthält den String, der an den PC geschickt wird
String data = "S01E";
// Serielle Schnittstelle einrichten, pinModes setzen
void setup() {
bt.begin(9600);
Serial.begin(9600);
}
void loop() {
Serial.println(data);
}
I also asked the same Question on discourse.processing.org. The link to my Question:
https://discourse.processing.org/t/sending-string-from-arduino-to-processing-app-on-android-over-bluetooth/6106
Could you try this?
void setup() {
isConfiguring = true;
size(displayWidth, displayHeight);
frameRate(10);
orientation(PORTRAIT);
background(255);
stroke(160);
fill(50);
//start listening for BT connections
bt.start();
//at app start select device…
isConfiguring = false;
}
It seems the function your using to retrieve data from the Bluetooth event is simply returning because of your Boolean flag is never changing from true.
I am trying to create an IoT based smart house system. It contains of my android application which sends and receives messages from/to PubNub (subscribes and publishes on certain channels) and Arduino UNO board with some LEDs and movement sensor (PIR). My android app seems to be working fine, it sends and receives messages, but the problem is with Arduino. In the beginning it receives messages from PubNub but after couple (exactly 2) publishes it stops receiving messages, it can just publish. Here is my arduino code:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char pubkey[] = "<pub key>";
char subkey[] = "<sub key>";
char channel[] = "publish";
char channel1[] = "subscribe";
char uuid[] = "Arduino";
volatile int pirpin = 2;
iotbridge arduino;
void initialize(){
Serial.begin(9600);
Serial.println("Serial set up");
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
pinMode(pirpin, INPUT);
while (!Ethernet.begin(mac)) {
Serial.println("Ethernet setup error");
delay(1000);
}
Serial.println("Ethernet set up");
}
void do_something(String value){
Serial.println("in the callback");
Serial.println(value);
}
void pin_ISR() {
arduino.send(channel1, "\"Movement\"");
digitalWrite(6, HIGH);
delay(50);
}
void setup()
{
initialize();
arduino.init( pubkey, subkey, uuid);
Serial.println("PubNub set up");
attachInterrupt(0, pin_ISR, RISING);
}
void loop()
{
String returnmessage;
Ethernet.maintain();
Serial.println("waiting for a message");
returnmessage = arduino.connect(channel);
// callback function of sorts, to work with the received message
do_something(returnmessage);
Serial.println();
}
I am just testing the receiving and sending messages so it does not contain LED control. It uses interrupt on pin2 and when it becomes HIGH Arduino publishes a message, which should be received by android app. Thank you for your help in advance.
I'm wondering if anybody can help me figure out what is causing the data I am sending to become corrupt.
My setup is currently an Arduino pro mini with a HM-10 bluetooth module connected (I have also tried HM-11 Module too) and an Android application to receive the bluetooth data.
Module setup: http://letsmakerobots.com/node/38009
If I send data with big enough intervals then the data is fine, but if I send the data continuously I see messages getting mixed up and lost. To test this I send "$0.1,0.2,0.3,0.4,0.5" to the Android application from the Arduino, sometimes the stream of data appears to send fine but other times it is really quite scrambled. Please see the below graphs that demonstrate this:
Good case:
Bad case:
Arduino code:
String inputString = ""; //Hold the incoming data.
boolean stringComplete = false; //Determines if the string is complete.
boolean realtime = false;
void setup()
{
Serial.begin(9600);
delay(500);
Serial.print("AT+START");
delay(500);
}
void loop()
{
if(stringComplete)
{
if(inputString.equals("rStart"))
{
Serial.println("$startACK");
realtime = true;
}
else if(inputString.equals("stop"))
{
Serial.println("$stopACK");
realtime = false;
}
else{
Serial.print(inputString);
}
inputString = "";
stringComplete = false;
}
if(realtime)
{
Serial.println("$0.1,0.2,0.3,0.4,0.5,0.6");
delay(10);
}
}
void serialEvent() {
while (Serial.available())
{
// get the new byte:
char inChar = (char)Serial.read();
if (inChar == '\n')
{
stringComplete = true;
}
else
{
inputString += inChar;
}
}
}
The Android side just receives the data and then parses it in an IntentService:
#Override
protected void onHandleIntent(Intent intent) {
//Incoming command.
String rawData = intent.getStringExtra(DataProcessingIntentService.REQUEST);
//Append our new data to our data helper.
Log.i(this.getClass().getName(), "Previous Raw: (" + DataProcessingHelper.getInstance().getData() + ")");
DataProcessingHelper.getInstance().appendData(rawData);
Log.i(this.getClass().getName(), "New Raw: (" + DataProcessingHelper.getInstance().getData() + ")");
commandStartIndex = DataProcessingHelper.getInstance().getData().indexOf("$");
commandEndIndex = DataProcessingHelper.getInstance().getData().indexOf("\n");
//Set this as the data starting point.
if(commandStartIndex != -1){
DataProcessingHelper.getInstance().offsetData(commandStartIndex);
}
//Ensure that a command has been found and that the end index is after the starting index.
if(commandStartIndex != -1 && commandEndIndex > commandStartIndex){
//Remove the command structure from the command.
command = DataProcessingHelper.getInstance().getData().substring(commandStartIndex+1, commandEndIndex-1); //Remove the \r\n end command.
DataProcessingHelper.getInstance().offsetData(commandEndIndex+1);
if(command.length() > 1){
//Split the data out of the comand.
splitData = command.split(",");
Log.i(this.getClass().getName(), "Broadcasting the processed data. (" + command + ")");
//Broadcast data.
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(DataProcessingIntentService.RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(DataProcessingIntentService.RESPONSE, splitData);
sendBroadcast(broadcastIntent);
}else{
Log.e(this.getClass().getName(), "Command is less than 1 character long!");
}
}
}
Thank you for any help!
I have now figured out what was causing this problem. It appears that BLE only supports a maximum of 20 bytes per a transaction. The time between these transactions is different depending on what you are using. I'm currently using notifications which means that I can send 20 bytes every 7.5 milliseconds maximum. I have opted for 10 milliseconds to be safe. I will now need to look into breaking up packets into 20 bytes maximum to ensure no data corruption.
I'm porting a game to use Google Play Game Services with multiplayer support. I'm using RealTimeSocket instead of realtime message because the game already has socket support.
To get the socket I call GamesClient.getRealTimeSocketForParticipant, and then I could get input and output streams as use it as a usual socket.
My problem is that if a device receives data before the call to getRealTimeSocketForParticipant, I will not be able to read this data. For instance:
Device A calls getRealTimeSocketForParticipant.
Device A sends "Hello".
Device B calls getRealTimeSocketForParticipant.
Device B receives nothing.
Device A sends "World".
Device B receives "World".
I have modified one of the example projects (ButtonClicker) and replicated the problem here. I have modified the code to use realtime socket, and modified the startGame method to this:
String mReceivedData = "";
byte mNextByteToSend = 0;
void startGame(boolean multiplayer)
{
mMultiplayer = multiplayer;
updateScoreDisplay();
switchToScreen(R.id.screen_game);
findViewById(R.id.button_click_me).setVisibility(View.VISIBLE);
GamesClient client = getGamesClient();
String myid = mActiveRoom.getParticipantId(client.getCurrentPlayerId());
ArrayList<String> ids = mActiveRoom.getParticipantIds();
String remoteId = null;
for(int i=0; i<ids.size(); i++)
{
String test = ids.get(i);
if( !test.equals(myid) )
{
remoteId = test;
break;
}
}
//One of devices should sleep in 5 seconds before start
if( myid.compareTo(remoteId) > 0 )
{
try
{
//The device that sleeps will loose the first bytes.
Log.d(TAG, "Sleeping in 5 seconds...");
Thread.sleep(5*1000);
}
catch(Exception e)
{
}
}
else
{
Log.d(TAG, "No sleep, getting socket now.");
}
try
{
final RealTimeSocket rts = client.getRealTimeSocketForParticipant(mRoomId, remoteId);
final InputStream inStream = rts.getInputStream();
final OutputStream outStream = rts.getOutputStream();
final TextView textView =((TextView) findViewById(R.id.score0));
//Thread.sleep(5*1000); Having a sleep here instead minimizes the risk to get the problem.
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
#Override
public void run()
{
try
{
int byteToRead = inStream.available();
for(int i=0; i<byteToRead; i++)
{
mReceivedData += " " + inStream.read();
}
if( byteToRead > 0 )
{
Log.d(TAG, "Received data: " + mReceivedData);
textView.setText(mReceivedData);
}
Log.d(TAG, "Sending: " + mNextByteToSend);
outStream.write(mNextByteToSend);
mNextByteToSend++;
h.postDelayed(this, 1000);
}
catch(Exception e)
{
}
}
}, 1000);
}
catch(Exception e)
{
Log.e(TAG, "Some error: " + e.getMessage(), e);
}
}
The code ensures that one of the two devices sleeps 5 seconds before the call to getRealTimeSocketForParticipant. For the device that doesn't sleep the output will be something like:
No sleep, getting socket now.
Sending: 0
Sending: 1
Sending: 2
Sending: 3
Sending: 4
Received data: 0
Sending: 5
Received data: 0 1
Sending: 6
Received data: 0 1 2
That's expected, no data lost. But for the other device I get this:
Sleeping in 5 seconds...
Received data: 4
Sending: 0
Received data: 4 5
Sending: 1
Received data: 4 5 6
Sending: 2
Received data: 4 5 6 7
Sending: 3
The first bytes are lost. Is there anyway to avoid this?
If i'm understanding the API correctly, the messages exchanged through a real time socket are unrealiable, so you can't always have assurance that all players received all messages you sent. I couldn't find info about the network protocol used by RealTimeSocket, but I suspect it's UDP.
If that's really the case, I'm afraid there's little you can do short of implementing some sort of handshake yourself. Choose one device (ex.: the one with the lowest ID) to be the "synchronizer", and have it create a set with every other device. Send a message ("SYN") such as "where are you? x y z" (not literally, of course) every second, until the others respond "I'm here! (y)" ("ACK"). Remove from the set the devices that sent a response, until the set is empty. At this point, send everyone a "game's starting!" and go on.
Note that any of these messages can be lost: if the "ACK" is lost, next time the "SYN" is sent the device should answer again. If the "game's starting" message is lost, tough luck, the device will keep waiting until it receives a different message, at such point it should consider itself free to start (though delayed).
One last note: even if the underlying protocol is TCP, it's still not 100% reliable, no protocol is. See this question for more info, if you don't know this fact already.
I'm running trough a curious bug where I receive bad values from a serial connection. I created a post earlier but no one have responded yet.
[other problem] : Bluetooth SPP (serial) glitchs (Android)
Because I would like to finish this project soon I think I will have to implement a "patch" that will filter all the values. If the new value is around +/- 15% to my previous value I will keep it and if it exceed this cutoff range I will discard it. Here is the code where I receive the values :
hBluetooth = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECEIVE_MESSAGE: // If we receive a message
byte[] readBuf = (byte[]) msg.obj;
String stringIncome = new String(readBuf, 0, msg.arg1); // Create string from byte array
stringBuilder.append(stringIncome);
int endOfLineIndex = stringBuilder.indexOf("\r\n"); // Determine the end-of-line
Log.e(TAG, "Line"+endOfLineIndex);
if (endOfLineIndex > 0) { // If we are at the end-of-line we parsed all the data that was sent
rmsgBluetooth = stringBuilder.substring(0, endOfLineIndex); // The string is extracted in a string object rmsgBluetooth
stringBuilder.setLength(0);
Could some one help me to implement a filter that will output the retained value in a string ? Thanks.
Here is my solution :
sensorReading = Float.parseFloat(rmsgBluetooth);
if(Math.abs(sensorReadingOld - sensorReading)<10 || sensorReadingOld == 0 ){
sensorReadingOld = sensorReading;
}
else {
sensorReading = sensorReadingOld;
}