16. Interface and application programming¶
This week I worked on defining my final project idea and started to getting used to the documentation process.
Group Assignment¶
Andriod¶
I have taken Andriod App development online course before. I used Andriod Studio tool. It only support Andriod devices. It uses Java programming language. You can do lots of things by building native Apps but it requires programming knowledge & time to learn it.
Android Studio is the official Integrated Development Environment (IDE) for Android app development, based on IntelliJ IDEA . On top of IntelliJ’s powerful code editor and developer tools, Android Studio offers even more features that enhance your productivity when building Android apps, such as:
-
A flexible Gradle-based build system
-
A fast and feature-rich emulator
-
A unified environment where you can develop for all Android devices
-
Instant Run to push changes to your running app without building a new APK
-
Code templates and GitHub integration to help you build common app features and import sample code
-
Extensive testing tools and frameworks
-
Lint tools to catch performance, usability, version compatibility, and other problems
-
C++ and NDK support
-
Built-in support for Google Cloud Platform, making it easy to integrate Google Cloud Messaging and App Engine
The Android Studio main window is made up of several logical areas
-
The toolbar lets you carry out a wide range of actions, including running your app and launching Android tools.
-
The navigation bar helps you navigate through your project and open files for editing. It provides a more compact view of the structure visible in the Project window.
-
The editor window is where you create and modify code. Depending on the current file type, the editor can change. For example, when viewing a layout file, the editor displays the Layout Editor.
-
The tool window bar runs around the outside of the IDE window and contains the buttons that allow you to expand or collapse individual tool windows.
-
The tool windows give you access to specific tasks like project management, search, version control, and more. You can expand them and collapse them.
-
The status bar displays the status of your project and the IDE itself, as well as any warnings or messages.
MIT App Inventor overview¶
MIT App Inventor is an intuitive, visual programming environment that allows everyone – even children – to build fully functional apps for smartphones and tablets. It’s much easier to use compared to native development tools & it doesn’t require learning programming languages. I used it to make a mobile App for this assignment.
IT has blocks-based tool facilitates the creation of complex, high-impact apps in significantly less time than traditional programming environments. The MIT App Inventor project seeks to democratize software development by empowering all people, especially young people, to move from technology consumption to technology creation.
Currently, it supports Android devices & the MIT App Inventor for iOS is in beta testing (soon it should support all kind of devices).
Bluetooth Pairing¶
Some problems¶
I have tried to use my input week circuit to check if bluetooth module will work with Attiny44. I have tried to experiment using different code from link1 & link2 . I have tried to connect the TX & RX of the module to different pins like 0,1,4,5 & changed the code accordingly. I couldn’t discover the bluetooth module when I tried to scan for devices using my phone.
These are some of the codes that I have experimented with.
#include <SoftwareSerial.h> //Software Serial Port #define RxD 5 #define TxD 4 #define DEBUG_ENABLED 1 SoftwareSerial blueToothSerial(RxD,TxD); int led = 7; void setup() { pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); setupBlueToothConnection(); pinMode(led,OUTPUT); digitalWrite(led,HIGH); } void loop() { int count=0; char recvChar; while(1){ //check if there's any data sent from the remote bluetooth shield if(blueToothSerial.available()){ recvChar = blueToothSerial.read(); count++ ; if(count%2==0) digitalWrite(led,HIGH); else digitalWrite(led,LOW); } } } void setupBlueToothConnection() { blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05" blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here delay(2000); // This delay is required. //blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable blueToothSerial.print("bluetooth connected!\n"); delay(2000); // This delay is required. blueToothSerial.flush(); }
Another one
#include <SoftwareSerial.h> //Software Serial Port #define TxD 5 //pin 5 de l'ATtiny à relier au pin RXD du module bluetooth (pin5) #define RxD 4 //pin 6 de l'ATtiny à relier au pin TXD du module bluetooth (pin4) SoftwareSerial blueToothSerial(RxD,TxD); int relais = 4; // pin 3 de l'ATtiny qui commande le relais void setup() { pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); blueToothSerial.begin(9600); //delay(2000); // délai nécessaire blueToothSerial.print("Bluetooth connecté !\n"); delay(4000); //Vérifie que tous les relais sont bien inactifs après un reset pinMode(relais,OUTPUT); digitalWrite(relais,LOW); // ouvre le relais } void loop() { char recvChar; while(1) { //vérifie s'il y a des données envoyées à partir du module bluetooth if(blueToothSerial.available()){ recvChar = blueToothSerial.read(); if(recvChar == '1') digitalWrite(relais,HIGH); else digitalWrite(relais,LOW); } } }
Another one
#include <SoftwareSerial.h> //Software Serial Port #define RxD 1 #define TxD 0 #define DEBUG_ENABLED 1 SoftwareSerial blueToothSerial(RxD,TxD); int led = 4; void setup() { pinMode(RxD, INPUT); pinMode(TxD, OUTPUT); setupBlueToothConnection(); pinMode(led,OUTPUT); digitalWrite(led,HIGH); } void loop() { char recvChar; while(1){ //check if there's any data sent from the remote bluetooth shield if(blueToothSerial.available()){ recvChar = blueToothSerial.read(); if(recvChar == '1') digitalWrite(led,HIGH); else digitalWrite(led,LOW); } } } void setupBlueToothConnection() { blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05" blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here delay(2000); // This delay is required. //blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable blueToothSerial.print("bluetooth connected!\n"); delay(2000); // This delay is required. blueToothSerial.flush(); }
We even tried to test with Arduino but all of our attempts failed. We even boughtnew bluetooth modules but it didn’t work. After many attempts the instructor thought of something, it turned out that the bluetooth module was pairing up automatically with another module in another project. Then, our testing worked.
Successful Testing¶
Arduino¶
We tried testing using the code in this link . We first tried it on Arduino. The connections were made similar to this diagram but with 1K & 2K resistors.
/* * Bluetooh Basic: LED ON OFF - Avishkar * Coder - Mayoogh Girish * Website - http://bit.do/Avishkar * Download the App : * This program lets you to control a LED on pin 13 of arduino using a bluetooth module */ char Incoming_value = 0; //Variable for storing Incoming_value void setup() { Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission pinMode(13, OUTPUT); //Sets digital pin 13 as output pin } void loop() { if(Serial.available() > 0) { Incoming_value = Serial.read(); //Read the incoming data and store it into variable Incoming_value Serial.print(Incoming_value); //Print Value of Incoming_value in Serial monitor Serial.print("\n"); //New line if(Incoming_value == '1') //Checks whether value of Incoming_value is equal to 1 digitalWrite(13, HIGH); //If value is 1 then LED turns ON else if(Incoming_value == '0') //Checks whether value of Incoming_value is equal to 0 digitalWrite(13, LOW); //If value is 0 then LED turns OFF } }
HC 05/06 works on serial communication.here the android app is designed sending serial data to the Bluetooth module when certain button is pressed. The Bluetooth module at other end receive the data and send to ardunio through the TX pin of Bluetooth module(RX pin of arduino). The Code fed to arduino check the received data and compares.If received data is 1 the LED turns on turns OFF when received data is 0
I have downloaded the Application from this link. Then, paired my device with HC 05 Bluetooth module1:
-
Turned ON HC 05 Bluetooth module
-
Scanned for available device
-
Paired to HC 05 by entering default password 1234
Then, opened the Application & pressed on paired devices. Selected the Bluetooth module from the List (HC 05) & connected successfully.
Then, pressed ON button to turn ON LED and OFF button to turn OFF LED.
Attiny44¶
I followed the same steps but made some changes in the code & used this diagram as a refrence for connection but with 1K & 2K resistors..
I used Arduino Uno to provide power only. Assigning PA0 as RX, and PA1 as TX & the LED on PA7. And it worked.
#include<SoftwareSerial.h> SoftwareSerial mySerial(PA0, PA1); char Incoming_value = 0; //Variable for storing Incoming_value void setup() { mySerial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission pinMode(PA7, OUTPUT); //Sets digital pin 13 as output pin } void loop() { if(mySerial.available() > 0) { Incoming_value = mySerial.read(); //Read the incoming data and store it into variable Incoming_value mySerial.print(Incoming_value); //Print Value of Incoming_value in Serial monitor mySerial.print("\n"); //New line if(Incoming_value == '1') //Checks whether value of Incoming_value is equal to 1 digitalWrite(PA7, HIGH); //If value is 1 then LED turns ON else if(Incoming_value == '0') //Checks whether value of Incoming_value is equal to 0 digitalWrite(PA7, LOW); //If value is 0 then LED turns OFF } }
MIT App Inventor¶
I tried following this tutorial to make an App that controls the LED in my circuit.
I used the same arduino code as the one I used for testing. From the MIT App Inventor website I had to create an account to log in into the online building application by clicking the “Create apps!” button. Here’s how the design window looks.
I had to connect my smartphone to this project so that I can see how the app is taking shape directly on my smartphone in real time. I had to download the MIT AI2 Companion app from the Play Store and install it on my phone. Then, from the Connect menu from the online editor I selected AI Companion and a barcode appeared which I can scan it or type the code into the smartphone app and the connection between the online editor and the smartphone app will be established.
Layout¶
I started with the layout of the program. First I added some HorizontalArrangements from the layout Palette and set their properties like the height, the width and the alignment to match our program desired look. Then, from the UserInterface Palette I added a ListPicker. The ListPicker will be used for selecting the Bluetooth device to which my smartphone will connect.
Next, I added another HorizontalArrangements & placed a Label in it. This label will indicate whether the smartphone is connected or not to the Bluetooth module and that’s why I set the initial text of this label to “Not Connected”. The next label will be used for displaying the status of the LED, whether is turned off or on. The initial state will be “LED: OFF”. Next we will add the two buttons, ‘ON’ and ‘OFF’ for controlling the LED. I renamed some of the components because it will be easier recognize and use them in the Blocks editor later. I added the BluetoothClient which is a Non-visible component as well as a clock which will be used for the real time indication of the connection status.
Blocks Editor¶
The Blocks Editor gives life to the App. I started with the BluetoothList ListPicker. First added the ‘BeforePicking’ block and attach to it the ‘set Bluetooth Elements’ block. Then, from the BluetoothClient blocks I added the ‘BluetoothClient AddressesAndNames’ block. This set of blocks will set a list of Bluetooth devices which are already paired with my phone so when I click on the ListPicker “Connect Button” the list of all paired devices will show up.
Next, I had to set what will happen after I pick or select a particular Bluetooth module. From the BluetoothClient block I added the ‘call BluetoothClient .Connect address’ block and add the block ‘BluetoothList Selection’ to it, which means that my phone will connect to the Bluetooth address that I have previously selected.
Then, from the Clock blocks I added the “.Timer” block. Within this block I made a real time indication whether the phone is connected or not to the Bluetooth module using the “set Text” block of the label named “Connected”.
After that, I needed to give life to the two buttons. So, when the “ON” button will be clicked we will use the Bluetooth client function “Send1ByteNumber” to send a number to the Arduino Bluetooth module. In my case that’s the number 49 which corresponds to the character ‘1’ according to the ASCII table and that will turn on the LED. Right after that I used the “ReceiveText” BluetoothClient function to receive the incoming String which is send back from the Arduino to the phone. This String is set to the “LEDText” Label.
The same procedure goes for the “OFF” button where the sending number should be changed to 48 which corresponds to character ‘0’.
Then I downloded the apk and installed it on my smartphone by clicking on “Build” menu by either saving it to our computer and then transfer to our phone or scan a QR code for online download of the program. I connected the bluetooth module using breadboard & did the testing.
Accourding to datasheet that the TX & RX pins operate on a 3.3V level. By connecting the RX pin directly to the Arduino circuit or my circuit, there will be a risk of damaging the module. That’s why I made a voltage divider circuit for the Bluetooth module & connected it to my circuit.
The code after slight modification
#include<SoftwareSerial.h> SoftwareSerial mySerial(PA4, PA5);//using SCK and MISO as the Rx TX through the ISP header char Incoming_value = 0; //Variable for storing Incoming_value void setup() { mySerial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission pinMode(PA7, OUTPUT); //Sets digital pin 13 as output pin } void loop() { if(mySerial.available() > 0) { Incoming_value = mySerial.read(); //Read the incoming data and store it into variable Incoming_value mySerial.print(Incoming_value); //Print Value of Incoming_value in Serial monitor mySerial.print("\n"); //New line if(Incoming_value == '1') //Checks whether value of Incoming_value is equal to 1 digitalWrite(PA7, HIGH); //If value is 1 then LED turns ON else if(Incoming_value == '0') //Checks whether value of Incoming_value is equal to 0 digitalWrite(PA7, LOW); //If value is 0 then LED turns OFF } }
I used the FTDI cable to provide power only & replace all other connections by using voltage divider circuit that I made for the Bluetooth module. Then connected main circuit with voltage divider circuit using serial connection.