Arduino with java

Hi,
I want to convert arduino code into java code and i have successfully done it for temperature and humidity sensors. I am able to read the temperature readings. But now i want to connect relay also to arduino board . How can i do it with java code?
I am able to connect either relay or temperature and humidity sensor but not both?
Pls suggest me solution for this.

Thanks !

Sounds to me like you are the expert in your chosen field !

Perhaps you can tell us what you have already done.

...R

Why? Personally I'm a big Java fan, been programming with it professionally for years but still, why? C/Prototype isn't that different syntactically and it is the line of least resistance.

You connect the relay to the Arduino with wire, not "java code".

I want to control arduino board with java coding. I want to connect to some sensors to board and get the output in netbeans. How to do that does any can help me?

ritsindia:
I want to control arduino board with java coding. I want to connect to some sensors to board and get the output in netbeans. How to do that does any can help me?

Output the values via the Arduino Serial port and read it in Java ? Where does the relay come into it ?

I want relay to perform some task or give some notification i.e if temperature raises beyond certain limit the relay should switch on or off the AC.
Do u have any suggestion? if i m wrong so pls correct me.

I want relay to perform some task or give some notification i.e if temperature raises beyond certain limit the relay should switch on or off the AC.

And, somehow, you don't think the Arduino can manage to read the temperature sensor and determine if the actual value is above or below some set point? Once the decision is made, only the Arduino can toggle the relay.

So, it sounds like all your PC is going to be doing is evaluating the if statement:

if(currentTemp > setPoint)
  // send message to Arduino to turn relay on
else
  // send message to Arduino to turn relay off

Seems like a waste to me.

I am able to read the temperature readings

What do you mean by this ? On the arduino ? How did you get a java program onto the Arduino ?

It's still not clear what you are trying to do.

It is possible to get the Arduino to communicate with a Java program running on another computer [ although I have never got this to work, because Java doesn't like serial ports, but it works using Processing ].

PaulS:

I want relay to perform some task or give some notification i.e if temperature raises beyond certain limit the relay should switch on or off the AC.

And, somehow, you don't think the Arduino can manage to read the temperature sensor and determine if the actual value is above or below some set point? Once the decision is made, only the Arduino can toggle the relay.

So, it sounds like all your PC is going to be doing is evaluating the if statement:

if(currentTemp > setPoint)

// send message to Arduino to turn relay on
else
  // send message to Arduino to turn relay off




Seems like a waste to me.

can u provide me some suggestions regarding how i can control my AC with arduino? I am working in M2M platform . i was using java to maintain log .

can u provide me some suggestions regarding how i can control my AC with arduino?

What exactly are you planning to control? If you have a thermometer and a relay, and you simply want to turn the relay on when the temperature is high, and off when it is low, that's simple.

I am working in M2M platform

No idea what this means.

i was using java to maintain log .

Or the relevance of this statement.

ritsindia:
I am able to connect either relay or temperature and humidity sensor but not both?
Pls suggest me solution for this.

You need to identify the fault in your hardware or software, and then fix it.

Obviously, you need visibility of your hardware design and your software in order to do that.

Equally obviously, since we have neither, there's no way we could guess what you've done so far or what may be wrong with it.

PaulS:

can u provide me some suggestions regarding how i can control my AC with arduino?

What exactly are you planning to control? If you have a thermometer and a relay, and you simply want to turn the relay on when the temperature is high, and off when it is low, that's simple.

I am working in M2M platform

No idea what this means.

i was using java to maintain log .

Or the relevance of this statement.

M2M means machine to machine communication. I am using arduino duemilanove board and temperature humidity sensor . I am able to get temperature - humidity readings . Since arduino doesnot supports writing its output to a file, so i m using java to read the readings of temperature and humidity and writing tht data to text file.I am having condition tht if temperature raises beyond the limit it should "ON" AC . To do tht On/OFF i m using relay. i have attached my java code u can check it out. I gives me temperature-humidity readings. The problem it detects either sensor or relay.

Answer to your next question-
I am maintaining temperature records of each day in different different locations. So i want the output to write to a file.

As i m new to arduino so if i make any mistake pls correct me.

ArduinoSerialTest.txt (4.73 KB)

arduino code-

#define dht_dpin 3
#define Relay1 5

byte bGlobalErr;
byte dht_dat[5];
void setup()
{
InitDHT();
//pinMode(Relay1,OUTPUT);
Serial.begin(9600);
delay(300);
Serial.println("Humidity and temperature");
delay(2000);
}
void loop(){
ReadDHT();
Serial.print(bGlobalErr);
switch (bGlobalErr){
case 0:
Serial.print("Current humdity = ");
Serial.print(dht_dat[0], DEC);
Serial.print(".");
Serial.print(dht_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht_dat[2], DEC);
Serial.print(".");
Serial.print(dht_dat[3], DEC);
Serial.println("C ");
if(dht_dat[2]>25)
{
digitalWrite(Relay1,HIGH);
}

break;
case 1:
Serial.println("Error 1: DHT start condition 1 not met.");
break;
case 2:
Serial.println("Error 2: DHT start condition 2 not met.");
break;
case 3:
Serial.println("Error 3: DHT checksum error.");
break;
default:
Serial.println("Error: Unrecognized code encountered.");
break;
}
delay(5000);
}
void InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}
void ReadDHT(){
bGlobalErr=0;
byte dht_in;
byte i;
digitalWrite(dht_dpin,LOW);
delay(20);
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);
pinMode(dht_dpin,INPUT);
//delayMicroseconds(40);
dht_in=digitalRead(dht_dpin);
if(dht_in){
bGlobalErr=1;
return;
}
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);
if(!dht_in){
bGlobalErr=2;
return;
}
delayMicroseconds(80);
for (i=0; i<5; i++)
dht_dat = read_dht_dat();
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3;}
};
byte read_dht_dat(){
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
while(digitalRead(dht_dpin)==LOW);
delayMicroseconds(30);
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));
while (digitalRead(dht_dpin)==HIGH);
}
return result;
}

I suggest you start by using the arduino serial monitor to test your arduino code for controlling the AC and receiving the data from the arduino. When that is working, then you can solve the issues with using java on the pc.

zoomkat:
I suggest you start by using the arduino serial monitor to test your arduino code for controlling the AC and receiving the data from the arduino. When that is working, then you can solve the issues with using java on the pc.

I arduino serial monitor it works fine, but not in java code.

Can you give us an example of the data that the Arduino sends to the Serial Monitor.

If that works it should be simple to replace the serial monitor with some Java code that reads and saves the same data. However I'm not sure if this is the best forum for advice on Java programming.

...R

If you just want to capture the serial to a file you could echo the com port to a file, e.g. on windows:

type com1: >> data.log

There are also serial terminal applications that will log to a file, e.g. RealTerm

type com1: >> data.log

You sure that will read incoming data from the com port and write it to a file?

Since arduino doesnot supports writing its output to a file,

It does if you have an SD card reader/writer. The process of writing to a file is exactly the same as writing to the serial port (except that you need to open and close the file).