Return method

Asked

Viewed 147 times

0

reciso create a method that returns the variable HUMIDITY; However I need to make a Thread to update the value of humidity every little time. (Moisture is read through the Arduino).
You need to thread or not the need?

There goes the code:

public class ConexaoBluetooth {
    public static void main(String[] args) {
    String URL = "btspp://301411281471:1;authenticate=false;encrypt=false";
    int conexao=1;
    try {
        OutputStream outStream;
        InputStream inStream;
            StreamConnection streamConnection = (StreamConnection) Connector.open(URL);
            outStream = streamConnection.openOutputStream();
            inStream = streamConnection.openInputStream();
            while (conexao ==1){
            outStream.write('1'); // envia p/ arduino
            outStream.flush();
            int umidade = inStream.read(); // le a umidade do arduino
            int agua = inStream.read();
            System.out.println("Umidade" + umidade);
            if (agua==1){
            System.out.println("Caixa d'agua vazia");
            }
            if(agua==0){
                System.out.println("Água OK");
            }   
            }
            outStream.close();  
            inStream.close();
    } catch (IOException e) {
            System.out.println("Conexão falhou...");
            conexao=0;
            e.printStackTrace();
        }
    }
}

Note: Main method n is required.

Cod do Arduino:

include <SoftwareSerial.h>
SoftwareSerial bt(6,7 ); // RX  TX
int caractere; //caracter do blue
int umidade;
int statusBoia;
//Porta ligada ao pino IN1 do modulo teste relé
int porta_rele1 = 10;//rele
int pinAgua = 11; //boiA
int pinboia = 9; //led
int pinOFF = 3; //led bluetooth(recebe do java)

void setup()
{

  bt.begin(9600);           // Configuracão da porta serial pora o software para comunicar com o modulo HC-05
  bt.flush();               // confirma e limpa o Buffer
  delay(300);  //

  Serial.begin(9600);
  //Define pinos para o rele como saida
  pinMode(porta_rele1, OUTPUT);
  //nivel de agua
  pinMode(pinAgua,INPUT);
  pinMode(pinboia,OUTPUT);
}
void loop()
{
 //agua
  if(digitalRead(pinAgua)==LOW){
    digitalWrite(pinboia,LOW);
    statusBoia=0;
    // TEM AGUA
  }else{
    digitalWrite(pinboia,HIGH); // N TEM AGUA
    statusBoia=1;
  }
  //fim agua
  umidade = analogRead(A0);
  int Porcento = map(umidade, 1023, 0, 0, 100);

  Serial.print(Porcento);
  Serial.println("%");
  if(Porcento <=60 && digitalRead(pinAgua)==HIGH)
  {
   digitalWrite(porta_rele1, LOW);  //Liga rele 1
    Serial.println("Irrigando...");
  }
  else {
   digitalWrite(porta_rele1, HIGH);  //Desliga rele 1
   Serial.println("Umidade Suficiente...");
  }
  //testes java


  while (bt.available()) {
  caractere = bt.read(); // recebe do java e lga o led
  if(caractere == '1') {
   digitalWrite(pinOFF,HIGH);  }

  bt.write(Porcento);// passando umidade para o java
  bt.flush(); 
  bt.write(statusBoia);
  bt.flush(); 
  }
  delay(1000);
}
  • I don’t understand. First you say you need to create a thread, then you ask if you need to create a thread. That’s right?

  • @jbueno He needs a Thread - when he gets what he wants, the code will get stuck in that class...

  • @jbueno Although the program seems only to do that! Anyway it deserves a timer - I’m sure the sensor connected to Arduino does not support Polling in a while(true)...

  • @Danielgomes now I was curious. Could you explain to me the relationship of the sensor with Polling? I’m not sure what the hardware has to do with the deterrent.

  • I’m not sure if I need to use a Thread, I’m beginner in this part, I tried to do but without success. Help!!

  • @Danielgomes?

  • @Bacco What I meant is that some sensors have a limitation as to the number of times you can consult them. RHT03, for example, if you simply make a while(true), he will probably never return the information.

  • I think they will be able to answer your question without problem - but it would be nice if you could add the code used in the Arduino too, if it’s not too big... :)

  • @Danielgomes according to the Datasheet RHT03, it takes two seconds to read. To use a while(true) would have to send the result in intervals (or a state machine to do other things between one reading and another). If I understood correctly you spoke in the sense that it is not analog and can not read instantaneously, this is it?

  • @Danielgomes Postei o Cod, send data via bluetooth...

  • @Bacco Correto. My point is that we do not have/had access to the Arduino code, and when the program is corrected and there is a constant sensor reading, we need to take care not to read too fast... :)

  • Is serial computer communication working? Which Arduino are you using? Any special reason to use SoftwareSerial.h instead of hardware or another library?

  • Daniel, yes, Arduino UNO, we use it to set and send the data to Java via Bluetooth.

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.