Skip to content

Instantly share code, notes, and snippets.

@neosarchizo
Created March 8, 2022 09:44
Show Gist options
  • Save neosarchizo/22b97c6697bc2f3a1cff65272a2dfd35 to your computer and use it in GitHub Desktop.
Save neosarchizo/22b97c6697bc2f3a1cff65272a2dfd35 to your computer and use it in GitHub Desktop.
[Processing] PM2008 packet to SQLite
import java.util.ArrayList;
class Packet {
static final int PACKET_STX = 0xAB;
static final int PACKET_ETX = 0x7D;
static final int PACKET_CMD_GET_PM2008 = 0;
static final int PACKET_POS_STX = 0;
static final int PACKET_POS_LEN = 1;
static final int PACKET_POS_CMD = 2;
static final int PACKET_POS_DAT = 3;
private ArrayList<Integer> buffer = new ArrayList<Integer>();
Packet() {
}
Packet(int[] rxBuffer) {
for (int b : rxBuffer) {
buffer.add(b);
}
}
private void clear() {
buffer.clear();
}
public void generate(int cmd, int[] data) {
clear();
buffer.add(PACKET_STX);
// (STX), (LEN), CMD, D.., CS, ETX
buffer.add(data.length + 3);
buffer.add(cmd);
for (int b : data) {
buffer.add(b);
}
int cs = buffer.get(PACKET_POS_STX);
for (int i=1; i<buffer.size(); i++) {
cs ^= buffer.get(i);
}
buffer.add(cs);
buffer.add(PACKET_ETX);
}
public boolean check() {
if (buffer.get(PACKET_POS_STX) != PACKET_STX) {
return false;
}
if (buffer.get(buffer.size() -1) != PACKET_ETX) {
return false;
}
if (buffer.get(PACKET_POS_LEN) + 2 != buffer.size()) {
return false;
}
switch (buffer.get(PACKET_POS_CMD)) {
case PACKET_CMD_GET_PM2008:
{
break;
}
default:
return false;
}
int cs = buffer.get(PACKET_POS_STX);
for (int i=1; i<buffer.size() - 2; i++) {
cs ^= buffer.get(i);
}
if (cs != buffer.get(buffer.size() -2)) {
return false;
}
return true;
}
public int getDataLength() {
return buffer.size() - 5;
}
public int getCmd() {
return buffer.get(PACKET_POS_CMD);
}
public int[] getData() {
int dataLength = getDataLength();
if (dataLength == 0) {
return new int[0];
}
int[] result = new int [dataLength];
for (int i=0; i<dataLength; i++) {
result[i] = buffer.get(PACKET_POS_DAT +i);
}
return result;
}
public byte[] getBytes() {
byte[] bytes = new byte[buffer.size()];
for (int i=0; i<buffer.size(); i++) {
bytes[i] = buffer.get(i).byteValue();
}
return bytes;
}
}
import processing.serial.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
final String QUERY_CREATE_TABLE =
"CREATE TABLE IF NOT EXISTS PM2008 (" +
"ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
"CREATED_AT DATETIME DEFAULT CURRENT_TIMESTAMP," +
"PM_1P0_GRIMM INTEGER NOT NULL," +
"PM_2P5_GRIMM INTEGER NOT NULL," +
"PM_10P_GRIMM INTEGER NOT NULL," +
"PM_1P0_TSI INTEGER NOT NULL," +
"PM_2P5_TSI INTEGER NOT NULL," +
"PM_10P_TSI INTEGER NOT NULL," +
"PM_0P3 INTEGER NOT NULL," +
"PM_0P5 INTEGER NOT NULL," +
"PM_1P0 INTEGER NOT NULL," +
"PM_2P5 INTEGER NOT NULL," +
"PM_5P0 INTEGER NOT NULL," +
"PM_10P INTEGER NOT NULL)";
final String QUERY_INSERT_INTO =
"INSERT INTO PM2008 (" +
"PM_1P0_GRIMM," +
"PM_2P5_GRIMM," +
"PM_10P_GRIMM," +
"PM_1P0_TSI," +
"PM_2P5_TSI," +
"PM_10P_TSI," +
"PM_0P3," +
"PM_0P5," +
"PM_1P0," +
"PM_2P5," +
"PM_5P0," +
"PM_10P" +
") VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
Serial myPort;
ArrayList<Integer> rxBuffer = new ArrayList<Integer>();
int idxBuffer = 0;
int rearLength = 0;
Packet txPacket = new Packet();
Connection connect() {
String url = "jdbc:sqlite:" + sketchPath() + "/data.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
}
catch (SQLException e) {
println(e.getMessage());
}
return conn;
}
void setup() {
//printArray(Serial.list());
// "/dev/tty.usbmodem113201"
try(Connection conn = connect();
Statement stmt = conn.createStatement()) {
stmt.execute(QUERY_CREATE_TABLE);
}
catch(SQLException e) {
println(e.getMessage());
}
Packet packet = new Packet();
// packet.check();
packet.generate(Packet.PACKET_CMD_GET_PM2008, new int[0]);
myPort = new Serial(this, "/dev/tty.usbmodem113201", 9600);
frameRate(1);
txPacket.generate(Packet.PACKET_CMD_GET_PM2008, new int[0]);
}
void draw() {
myPort.write(txPacket.getBytes());
}
void serialEvent(Serial port) {
while (port.available() > 0) {
int b = port.read();
if (b < 0) {
continue;
}
switch(idxBuffer) {
case 0: // STX
{
if (b != Packet.PACKET_STX) {
idxBuffer = 0;
rxBuffer.clear();
break;
}
rxBuffer.add(b);
++idxBuffer;
break;
}
case 1: // LEN
{
if (b < 3) {
idxBuffer = 0;
rxBuffer.clear();
break;
}
rxBuffer.add(b);
++idxBuffer;
rearLength = b;
break;
}
case 2: // CMD
{
if (b != Packet.PACKET_CMD_GET_PM2008) {
idxBuffer = 0;
rxBuffer.clear();
break;
}
rxBuffer.add(b);
++idxBuffer;
break;
}
default:
{
rxBuffer.add(b);
++idxBuffer;
if (idxBuffer >= rearLength + 2) {
int[] bytes = rxBuffer.stream().mapToInt(i -> i).toArray();
idxBuffer = 0;
rxBuffer.clear();
if (b != Packet.PACKET_ETX) {
break;
}
Packet packet = new Packet(bytes);
if (!packet.check())
{
break;
}
int dataLength = packet.getDataLength();
int cmd = packet.getCmd();
int[] data = packet.getData();
switch(cmd) {
case Packet.PACKET_CMD_GET_PM2008:
{
if (dataLength != 25) {
break;
}
if (data[0] != 0) {
break;
}
try(Connection conn = connect();
PreparedStatement pStmt = conn.prepareStatement(QUERY_INSERT_INTO)) {
for (int i=0; i<12; i++) {
pStmt.setInt(i+1, (data[2*i+1] + (data[2* (i+1)] <<8)));
}
pStmt.executeUpdate();
println("added!!");
}
catch (SQLException e) {
println(e.getMessage());
}
break;
}
default:
break;
}
}
break;
}
}
}
}
void keyPressed() {
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment