Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

Send arduino data to its application - Ketai libraries

$
0
0

Hello, the goal of my project is to control a laboratory arm that needs to take a methane bottle, I have to send on my tablet the methane content of the bottle. So here is my code processing and arduino:

Code Processing

` //Programme 2222

import controlP5.*; import android.content.Intent; import android.os.Bundle; import ketai.net.bluetooth.*; import ketai.ui.*; import ketai.net.*;

PFont fontMy; boolean bReleased = true; //Pas de changement d'état de la réponse si le doigt reste //appuyé sur l'écran. KetaiBluetooth bt; boolean isConfiguring = true; String info = ""; KetaiList klist; ArrayList devicesDiscovered = new ArrayList(); int c1,c2; float n,n1; PFont p = createFont("Arial",40); int sliderValue = 100; int c = 0; boolean ON_OFF = false; String methane = "";

ControlP5 cp5; Textarea myTextarea; Println console; Button b; Toggle t01;

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);

orientation(LANDSCAPE);

cp5 = new ControlP5(this);

cp5.enableShortcuts();

myTextarea = cp5.addTextarea("consoleE") .setPosition(0, height/12) .setSize(width/2, height/4) .setFont(createFont("", 15)) .setLineHeight(14) .setColor(color(0)) .setColorBackground(color(186, 186, 186)) .setColorForeground(color(255, 100)) ; myTextarea = cp5.addTextarea("consoleP") .setPosition(0, height/3) .setSize(width/2, height/5) .setFont(createFont("", 15)) .setLineHeight(14) .setColor(color(0)) .setColorBackground(color(186, 186, 186)) .setColorForeground(color(255, 100)); console = cp5.addConsole(myTextarea);

cp5.addToggle("ON_OFF") .setPosition(width/21, height/1.2) .setSize(width/9,height/13);

cp5.addButton("Monter") .setPosition(width/1.4, width/21) .setSize(width/10,height/12); cp5.addButton("Descendre") .setPosition(width/1.4, width/6) .setSize(width/10,height/12); cp5.addButton("Gauche") .setPosition(width/1.67, width/9.5) .setSize(width/10,height/12); cp5.addButton("Droite") .setPosition(width/1.205, width/9.5) .setSize(width/10,height/12);

//Début de connexion Bluetooth. bt.start(); //Sélection de l'application. isConfiguring = true; //Couleur de fond fontMy = createFont("SansSerif", 40); textFont(fontMy); textSize(50); text("SHIPWARS", 280, 55);

}

void draw(){

//Si la sélection de l'application est valide. if (isConfiguring) { ArrayList names; background(78, 93, 75); klist = new KetaiList(this, bt.getPairedDeviceNames()); isConfiguring = false; } else { background(77, 73, 73); //Change le font de l'application //Affichage de la reception des données. fill(255); noStroke(); textAlign(LEFT); text(info, 20, 104); }

noStroke();

fill(0); textSize(20); text("Connexion bluetooth: ", 5, height/20);

fill(0); textSize(20); text("Taux de méthane: " + methane + " ppm", width/7, height/1.6); text("Alerte: ", width/6.4, height/1.4); fill(255); ellipse(width/3.5, height/1.42, width/28, height/17.3);

stroke(0); fill(255);

//Placement de joystick noStroke();

fill(255); noStroke(); //Côter gauche rect(width/2,0,width/200 ,height); //Ligne 1 fill(255); rect(0, height/13.5, width/2, height/120); //Ligne 2 fill(255); rect(0, height/1.88, width/2, height/120); //Ligne 3 fill(255); rect(0, height/1.3, width/2, height/120); //Côter droite //Ligne 1 fill(255); rect( width/2,height/2.3, width/2, height/120);

noStroke();

if(ON_OFF){ //Envoie des données bluetooth byte[] data = {'a','l','l','u','m','e','\r'}; bt.broadcast(data); //Premier envoi "eteint". bReleased = false; if(mousePressed == false) { bReleased = true; } fill(0, 255, 0); ellipse(width/5, height/1.15, width/28, height/17.3); fill(255); ellipse(width/3.2, height/1.15, width/28, height/17.3);

} else { byte[] data = {'e','t','e','i','n','t','\r'}; bt.broadcast(data); //Premier envoi "eteint". bReleased = false; if(mousePressed == false) { bReleased = true; } fill(255); ellipse(width/5, height/1.15, width/28, height/17.3); fill(255, 0, 0); ellipse(width/3.2, height/1.15, width/28, height/17.3);

} } void onKetaiListSelection(KetaiList klist) { String selection = klist.getSelection(); bt.connectToDeviceByName(selection); //Présence d'une Liste. klist = null; }

//Méthode de gestion des évenements (dans la librairie Ketai), permettant de //recevoir les données. void onBluetoothDataEvent(String who, byte[] data) { if (isConfiguring) return; //Réception. info += new String(data); //Effacer si le mot trop long. if(info.length() > 150) info = "";

methane += new String(data); //Effacer si le mot trop long. if(methane.length() > 150) methane = ""; } public void controlEvent(ControlEvent theEvent) { println(theEvent.getController().getName()); n = 0; } public void consoleE(int theValue){

} public void consoleP(int theValue){

}`

Code Arduino :

`//fw commenté le 22/06/2015. //Led pin 13 commandée IHM Bluetoothandroid Bp arduino //et affigage du message des états de la LED;

include <SoftwareSerial.h>

include <LiquidCrystal.h> //Introduction de la bibliothèque

SoftwareSerial mySerial(12,7); // RX, TX en (10,7)...

char chByte = 0; // Byte arrivant reçu. String strInput = ""; // Contenu dans l'octet. String Led_Rouge = "eteint";//5 caractéres (5 lignes). String Led_Verte = "allume";//5 caractéres (5 lignes).

const int AOUTpin = A0; //the AOUT pin of the methane sensor goes into analog pin A0 of the arduino int methane; // déclaration de la variable

void setup() { mySerial.begin(9600); //Vitesse RS232 9600 Bauds //vérifier que le débit utilisé est le même que dans le terminal série. pinMode(13, OUTPUT); //Affectation led pin 13 en sortie. pinMode(5, OUTPUT); pinMode(2, OUTPUT); Serial.begin(9600); }

void loop() { while (mySerial.available() > 0)//Octets positifs présents dans la file d'attente //série. {

chByte = mySerial.read();//Lire les 8 bits présents dans la variable "chByte". if (chByte == '\r') { //Comparaison du message en début d'octet. if(strInput.equals(Led_Rouge)) { digitalWrite(13, HIGH);//Alors allumer la led en pin 13. delay(3000);//Délai d'allumage de la LED avant extinction. digitalWrite(13, LOW);//Alors eteindre la led en pin 13. }

else if(strInput.equals(Led_Verte)) { digitalWrite(5, HIGH);//Alors allumer la led en pin 13. delay(3000);//Délai d'allumage de la LED avant extinction. digitalWrite(5, LOW);//Alors eteindre la led en pin 13. } //Effacer le mot contenu en début d'octet. strInput = ""; } else { strInput += chByte; } } Serial.print("A0:"); Serial.println(analogRead(AOUTpin)); methane = analogRead(AOUTpin); //reads the analaog value from the methane sensor's AOUT pin methane = methane5/102350; //convertis la valeur
Serial.println(methane); delay(1000); }`


Viewing all articles
Browse latest Browse all 941

Trending Articles