I’m working on a school project where I need to create an app that displays data received from different sensors. I am using an arduino UNO and an ultrasonic sensor, HC-SR04, with the bluetooth module, HC-05, to collect the data, and I’m using MIT App Inventor 2 to create the app on android. However, I can’t get the app to display the data from the sensors or text from the arduino. The code I’m using for arduino is below and the blocks I’m using for MIT App Inventor 2 are attached. Any advice would be greatly appreciated.
char c = BT.read();
state += c;
long duration, distance;
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1;
Serial.print(“Food Level: Low”);
Serial.print(“Food Level: Full”);
Here’s my recommendations on how to work through your project.
First, learn how to post your code on the forum using code tags. They are found at the </> icon at the top left of the tool bar. Before you paste your code between the tags, use ctrl +T which will run the auto format tool in the idea and your code will be properly arranged. You can modify your original posting to do this.
Your code will appear in a scrolling window like this
Your code will be here
Then, forget about the bluetooth for a bit, and write a sketch using the Serial monitor which does exactly what you want it to do. You will need to address the issue of line endings in the if statement where state is tested against the input, and where to reset the state.
Then when all is working as required. Find a Bluetooth terminal app for your phone. I use the Serial Bluetooth Terminal by Kai Morich https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal
Pair the phone, and connect the terminal app with the HC05. Set the line ending settings to match those used on the Serial Monitor when you developed the sketch.
You do not have to use Software Serial to run the Arudino code from the phone. You can use the hardware Serial for bluetooth and no changes will be needed in the code if you disconnect the bluetooth when downloading code and after the code is loaded, you connect the Bluetooth Rx and Tx to the hardware serial port (pins 0 and pin 1) of the Arduino. Cross connect so that TX goes to RX and the RX to TX. You should be using a voltage divider on the Bluetooth Rx from the Arduino to bring the 5v down to 3.3.
The terminal app in the phone should now replace the Serial monitor and the code should work as before.
Finally, we can work on the issue of the App Inventor custom app .