I am trying to send the sensor data object ot the server and database I have called the server url from the server project so it adds it tot eh database and also created sensor data object which sends the data to the server. When I comment the lines
out, it works fine. But when I Uncomment them it doesn't do anything not sure what the problem is, can someone help me here please:
final MqttTopic rfidTopic = client.getTopic(TOPIC_RFID);
// Convert all the sensor data object to json format
rfidTopic.publish(new MqttMessage(gson.toJson(RFID).getBytes()));
What I have tried:
I have created the sendtoserver method and called it in the validation, so it sends the data to database and server.
package Lightandheating;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import com.google.gson.Gson;
import com.phidget22.DigitalOutput;
import com.phidget22.PhidgetException;
import com.phidget22.RFID;
import com.phidget22.RFIDTagEvent;
import com.phidget22.RFIDTagListener;
import com.phidget22.RFIDTagLostEvent;
import com.phidget22.RFIDTagLostListener;
import mqtt.publisher.PhidgetPublisher;
public class RFIDLight {
public static final String userid = "16038287";
public static final String BROKER_URL = "tcp://iot.eclipse.org:1883";
public static final String TOPIC_RFID = userid + "/rfid_light";
private MqttClient client;
final String[] tagArray = { "1600ee15e9", "tag1", "tagi23","4d004a7bbf23" };
ArrayList<String> tagArray1 = new ArrayList<String>(Arrays.asList(tagArray));
DigitalOutput digOut = new DigitalOutput();
SensorData RFID = new SensorData();
String RFIDJSON = new String();
Gson gson = new Gson();
public static String sensorServerURL = "http://localhost:8080/Household_remote_control_app_IOT_Server/RFIDDao";
public static void main(String[] args) throws PhidgetException, SQLException {
new RFIDLight();
}
public String sendToServer(String RFIDJSON){
URL url;
HttpURLConnection conn;
BufferedReader rd = null;
try {
RFIDJSON = URLEncoder.encode(RFIDJSON, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String fullURL = sensorServerURL + "?sensordata="+RFIDJSON;
System.out.println("Sending data to: "+fullURL);
String line;
String result = "";
try {
url = new URL(fullURL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public RFIDLight() throws PhidgetException {
PhidgetPublisher publisher = new PhidgetPublisher();
RFID rfid = new RFID();
digOut.setChannel(0);
digOut.open(2000);
try {
rfid.addTagListener(new RFIDTagListener() {
public void onTag(RFIDTagEvent e) {
String tagStr = e.getTag();
try {
if (tagArray1.contains(tagStr)) {
System.out.println("Tag read: LIGHT ON: " + tagStr);
publisher.publishRfid(tagStr);
turnOnLight(true);
}
else {
System.out.println("Tag read: LIGHT NOT ON: " + tagStr);
turnOnLight(false);
}
} catch (MqttException mqtte) {
mqtte.printStackTrace();
}
}
});
rfid.addTagLostListener(new RFIDTagLostListener() {
public void onTagLost(RFIDTagLostEvent e) {
String tagStr = e.getTag();
System.out.println("Tag lost: " + tagStr);
}
});
rfid.open(10000);
System.out.println("Device Name " + rfid.getDeviceName());
System.out.println("Serial Number " + rfid.getDeviceSerialNumber());
System.out.println("Device Version " + rfid.getDeviceVersion());
rfid.setAntennaEnabled(true);
System.out.println("\n\nGathering data for 15 seconds\n\n");
try {
Thread.sleep(15000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
rfid.close();
System.out.println("\nClosed RFID");
try {
System.out.println("\n\nGathering data for 15 seconds\n\n");
Thread.sleep(150000);
} catch (Exception ex) {
ex.printStackTrace();
}
} finally {
System.out.println("Closed and exiting...");
}
}
private void turnOnLight(boolean lightState) throws MqttPersistenceException, MqttException{
if (lightState==true) {
try {
digOut.setState(true);
Object message = null;
SensorData RFID = new SensorData("1600ee15e9","LIGHT ON","none","none");
Gson gson = new Gson();
System.out.println("Publishing the light sequence, to TURN the LIGHT ON");
System.out.println("VALID TAG/USER");
System.out.println("Room 101 - LIGHT ON");
RFID.setSensorname("1600ee15e9");
RFID.setSensorvalue("LIGHT ON");
RFID.setUserid("16038287");
RFIDJSON = gson.toJson(RFID);
sendToServer(RFIDJSON);
System.out.println("Success rrfid data have been added to the database!");
String allRFIDDataJson = gson.toJson(RFIDJSON);
System.out.println(allRFIDDataJson);
System.out.println("RFID data successfully in json format!");
} catch (PhidgetException e) {
e.printStackTrace();
}
}
else {
try {
System.out.println("Tag read: LIGHT OFF!: " + lightState);
System.out.println("INVALID TAG!");
System.out.println("LIGHT OFF");
digOut.setState(false);
RFID.setSensorname("4d004a5587");
RFID.setSensorvalue("light NOT on");
RFID.setUserid("16038287");
RFIDJSON = gson.toJson(RFID);
sendToServer(RFIDJSON);
System.out.println("Success rrfid data have been added to the database!");
String allRFIDDataJson = gson.toJson(RFIDJSON);
System.out.println(allRFIDDataJson);
System.out.println("RFID data successfully in json format!");
} catch (PhidgetException e) {
e.printStackTrace();
}
}
}
}