Created
February 16, 2020 10:08
-
-
Save ashif-ismail/9b54ec93578c3dd1566b322108f65c07 to your computer and use it in GitHub Desktop.
Java file to connect to Arduino and get and plot the sensor data in line graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.ashif.smartrelay.activity; | |
import android.app.PendingIntent; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.databinding.DataBindingUtil; | |
import android.hardware.usb.UsbDevice; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import com.anychart.AnyChart; | |
import com.anychart.AnyChartView; | |
import com.anychart.chart.common.dataentry.DataEntry; | |
import com.anychart.chart.common.dataentry.ValueDataEntry; | |
import com.anychart.charts.Cartesian; | |
import com.anychart.core.cartesian.series.Line; | |
import com.anychart.data.Mapping; | |
import com.anychart.data.Set; | |
import com.anychart.enums.Anchor; | |
import com.anychart.enums.MarkerType; | |
import com.anychart.enums.TooltipPositionMode; | |
import com.anychart.graphics.vector.Stroke; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import me.ashif.smartrelay.R; | |
import me.ashif.smartrelay.databinding.ActivityMainBinding; | |
public class MainActivity extends AppCompatActivity { | |
private static final String ACTION_USB_PERMISSION = "usb_intent"; | |
private ActivityMainBinding mBinding; | |
private UsbManager usbManager; | |
private UsbDevice device; | |
private Object connection; | |
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (intent.getAction().equals(ACTION_USB_PERMISSION)) { | |
boolean granted = | |
intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); | |
if (granted) { | |
connection = usbManager.openDevice(device); | |
serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection); | |
if (serialPort != null) { | |
if (serialPort.open()) { | |
setUiEnabled(true); | |
serialPort.setBaudRate(9600); | |
serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8); | |
serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1); | |
serialPort.setParity(UsbSerialInterface.PARITY_NONE); | |
serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); | |
serialPort.read(mCallback); | |
} else { | |
System.out.println("serial port status : not open"); | |
} | |
} else { | |
System.out.println("serial port status : NULL"); | |
} | |
} else { | |
System.out.println("no permission to access the serial port"); | |
} | |
} else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { | |
setupArduinoConnection(); | |
} else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { | |
serialPort.close(); | |
} | |
}; | |
}; | |
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { | |
@Override | |
public void onReceivedData(byte[] arg0) { | |
String data = null; | |
try { | |
data = new String(arg0, "UTF-8"); | |
// creates sensor data object as the new temperature | |
// from the sensor is received and plots the graph for | |
// the incoming value | |
SensorData sensorData = new SensorData(); | |
sensorData.setTemperature(data); | |
setupChart(sensorData); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); | |
setupArduinoConnection(); | |
//setupChart(); | |
} | |
private void setupArduinoConnection() { | |
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList(); | |
if (!usbDevices.isEmpty()) { | |
boolean keep = true; | |
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) { | |
device = entry.getValue(); | |
int deviceVID = device.getVendorId(); | |
if (deviceVID == 0x2341) | |
{ | |
PendingIntent pi = PendingIntent.getBroadcast(this, 0, | |
new Intent(ACTION_USB_PERMISSION), 0); | |
usbManager.requestPermission(device, pi); | |
keep = false; | |
} else { | |
connection = null; | |
device = null; | |
} | |
if (!keep) | |
break; | |
} | |
} | |
} | |
private void setupChart(SensorData sensorData) { | |
AnyChartView anyChartView = findViewById(R.id.any_chart_view); | |
Cartesian cartesian = AnyChart.line(); | |
cartesian.animation(true); | |
cartesian.padding(10d, 20d, 5d, 20d); | |
cartesian.crosshair().enabled(true); | |
cartesian.crosshair() | |
.yLabel(true) | |
.yStroke((Stroke) null, null, null, (String) null, (String) null); | |
cartesian.tooltip().positionMode(TooltipPositionMode.POINT); | |
cartesian.title("Temp in Deg v/s Time (sec)"); | |
cartesian.yAxis(0).title("Temperature in Degrees"); | |
cartesian.xAxis(0).labels().padding(5d, 5d, 5d, 5d); | |
List<DataEntry> seriesData = new ArrayList<>(); | |
// Add the value in the chart for the new incoming data | |
seriesData.add(new ValueDataEntry("0s", Integer.valueOf(sensorData.getTemperature()))); | |
Set set = Set.instantiate(); | |
set.data(seriesData); | |
Mapping series1Mapping = set.mapAs("{ x: 'x', value: 'value' }"); | |
Line series1 = cartesian.line(series1Mapping); | |
series1.name("Temperature"); | |
series1.hovered().markers().enabled(true); | |
series1.hovered().markers() | |
.type(MarkerType.CIRCLE) | |
.size(4d); | |
series1.tooltip() | |
.position("right") | |
.anchor(Anchor.LEFT_CENTER) | |
.offsetX(5d) | |
.offsetY(5d); | |
cartesian.legend().enabled(true); | |
cartesian.legend().fontSize(13d); | |
cartesian.legend().padding(0d, 0d, 10d, 0d); | |
anyChartView.setChart(cartesian); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment