diff --git a/README.md b/README.md index 6391644..09f3139 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,60 @@ # arduino-nano-setup Arduino Nano V3.0 ATmega328 CH340G playground on Ubuntu 15.10 + +![Arduino Nano V3.0 ATmega328 CH340G](https://github.com/Pozo/arduino-nano-setup/blob/master/arduino-ano-v3.0-atmega328-ch340g.jpg "Arduino Nano V3.0 ATmega328 CH340G") + +#### Driver (already installed) + + lsmod | grep 'ch' + modinfo ch341 + http://lxr.free-electrons.com/source/drivers/usb/serial/ch341.c + +#### Arduino IDE + +Arduino IDE [here](http://arduino.cc/en/Main/Software). + + sudo ./arduino + +Arduino IDE settings : Tools/Board `Arduino Nano` Tools/Processor `ATmega328` Tools/Programmer `Arduino as ISP` + +#### Debug with Wireshark + + modprobe usbmon + sudo wireshark + +#### Control with Java + + sudo apt-get install librxtx-java + sudo usermod -a -G dialout $USER + sudo adduser $USER dialout + sudo reboot + +##### Run sample app #1 + + mvn exec:java + mvn exec:java -Dport.id=/dev/ttyUSB0 + +##### Run sample app #2 + + mvn clean install + java -jar target/arduino-0.0.1-SNAPSHOT.jar + java -jar -Dport.id=/dev/ttyUSB0 target/arduino-0.0.1-SNAPSHOT.jar + +#### DTR causing a reset + +##### Description + + http://stackoverflow.com/a/35406863 + http://stackoverflow.com/questions/3918032/bash-serial-i-o-and-arduino + +##### Terminal and Arduino IDE serial monitor dumps + +`echo "ON" > /dev/ttyUSB0` - [ubuntu.terminal.echo.pcapnp](https://github.com/Pozo/arduino-nano-setup/blob/master/ubuntu.terminal.echo.pcapnp "ubuntu.terminal.echo.pcapnp") + + +`using Arduino IDE serial monitor` - [arduino.ide.serial.monitor.pcapng](https://github.com/Pozo/arduino-nano-setup/blob/master/arduino.ide.serial.monitor.pcapng "arduino.ide.serial.monitor.pcapng") + + stty -a < /dev/ttyUSB0 + echo "ON" > /dev/ttyUSB0 wont work + echo "OFF" > /dev/ttyUSB0 wont work + diff --git a/arduino-ano-v3.0-atmega328-ch340g.jpg b/arduino-ano-v3.0-atmega328-ch340g.jpg new file mode 100644 index 0000000..23936d5 Binary files /dev/null and b/arduino-ano-v3.0-atmega328-ch340g.jpg differ diff --git a/arduino.ide.serial.monitor.pcapng b/arduino.ide.serial.monitor.pcapng new file mode 100644 index 0000000..f62fed7 Binary files /dev/null and b/arduino.ide.serial.monitor.pcapng differ diff --git a/arduino/.gitignore b/arduino/.gitignore new file mode 100644 index 0000000..bf2a3af --- /dev/null +++ b/arduino/.gitignore @@ -0,0 +1,51 @@ +target +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ diff --git a/arduino/pom.xml b/arduino/pom.xml new file mode 100644 index 0000000..3df55fa --- /dev/null +++ b/arduino/pom.xml @@ -0,0 +1,109 @@ + + 4.0.0 + com.github.pozo + arduino + 0.0.1-SNAPSHOT + + UTF-8 + + + + + org.rxtx + rxtx + 2.1.7 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.6 + 1.6 + UTF-8 + + + + org.apache.maven.plugins + maven-source-plugin + 2.1.2 + + + attach-sources + + jar + + + + + + + org.codehaus.mojo + versions-maven-plugin + 1.3.1 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12.2 + + + **/*Test.java + + + **/*LiveTest.java + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.3 + + true + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + package + + shade + + + + + + com.github.pozo.MainFrame + + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.4.0 + + com.github.pozo.MainFrame + + + + + + \ No newline at end of file diff --git a/arduino/src/main/java/com/github/pozo/Arduino.java b/arduino/src/main/java/com/github/pozo/Arduino.java new file mode 100644 index 0000000..5009cd8 --- /dev/null +++ b/arduino/src/main/java/com/github/pozo/Arduino.java @@ -0,0 +1,85 @@ +package com.github.pozo; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + +import gnu.io.CommPortIdentifier; +import gnu.io.NoSuchPortException; +import gnu.io.PortInUseException; +import gnu.io.SerialPort; +import gnu.io.UnsupportedCommOperationException; + +public class Arduino { + private static final String PROPERTY_PORT_ID = "port.id"; + private static final String PORT_ID = "/dev/ttyUSB0"; + /** Milliseconds to block while waiting for port open */ + private static final int TIME_OUT = 2000; + /** Default bits per second for COM port. */ + private static final int DATA_RATE = 9600; + + private static SerialPort serialPort; + + private static final String OFF_COMMAND = "OFF"; + private static final String ON_COMMAND = "ON"; + + public static String open() { + String result = ""; + try { + String portIdFromProperty = System.getProperty(PROPERTY_PORT_ID); + String portId = (portIdFromProperty==null)? PORT_ID : portIdFromProperty; + serialPort = (SerialPort) CommPortIdentifier.getPortIdentifier(portId).open(Arduino.class.getName(), TIME_OUT); + // useless in this case https://github.com/processing/processing/blob/master/java/libraries/serial/src/processing/serial/Serial.java#L560 + serialPort.setDTR(false); + serialPort.setSerialPortParams(DATA_RATE, + SerialPort.DATABITS_8, + SerialPort.STOPBITS_1, + SerialPort.PARITY_NONE); + + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); + + result = bufferedReader.readLine(); + } catch (PortInUseException e) { + e.printStackTrace(); + } catch (NoSuchPortException e) { + e.printStackTrace(); + } catch (UnsupportedCommOperationException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } + + public static void close() { + if (serialPort != null) { + serialPort.removeEventListener(); + serialPort.close(); + } + + } + + public static String turnOnLED() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); + BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(serialPort.getOutputStream())); + + bufferedWriter.write(ON_COMMAND); + bufferedWriter.flush(); + + return bufferedReader.readLine(); + + } + + public static String turnOffLED() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream())); + BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(serialPort.getOutputStream())); + + bufferedWriter.write(OFF_COMMAND); + bufferedWriter.flush(); + + return bufferedReader.readLine(); + } + +} diff --git a/arduino/src/main/java/com/github/pozo/MainFrame.java b/arduino/src/main/java/com/github/pozo/MainFrame.java new file mode 100644 index 0000000..093a7a5 --- /dev/null +++ b/arduino/src/main/java/com/github/pozo/MainFrame.java @@ -0,0 +1,78 @@ +package com.github.pozo; + +import java.awt.EventQueue; +import java.awt.event.ActionEvent; +import java.io.IOException; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.Box; +import javax.swing.JButton; +import javax.swing.JFrame; + +public class MainFrame { + private JFrame frame; + private final Action turnOnLed = new AbstractAction("ON") { + private static final long serialVersionUID = 335039065675847328L; + + public void actionPerformed(ActionEvent e) { + try { + String response = Arduino.turnOnLED(); + System.out.println(response); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + }; + private final Action turnOffLed = new AbstractAction("OFF") { + private static final long serialVersionUID = -6049179799308301774L; + + public void actionPerformed(ActionEvent e) { + try { + String response = Arduino.turnOffLED(); + System.out.println(response); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + }; + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + Arduino.open(); + MainFrame window = new MainFrame(); + window.frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + + public void run() { + Arduino.close(); + } + })); + } + + public MainFrame() { + initialize(); + } + + private void initialize() { + frame = new JFrame(); + Box bv = Box.createHorizontalBox(); + + frame.setBounds(100, 100, 450, 300); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.add(bv); + JButton button = new JButton(turnOnLed); + bv.add(button); + + JButton buttonOff = new JButton(turnOffLed); + bv.add(buttonOff); + } + +} diff --git a/led-control/led-control.ino b/led-control/led-control.ino new file mode 100644 index 0000000..27135b8 --- /dev/null +++ b/led-control/led-control.ino @@ -0,0 +1,32 @@ +#define ON "ON" +#define OFF "OFF" + +// the setup function runs once when you press reset or power the board +void setup() { + // initialize digital pin 13 as an output. + pinMode(13, OUTPUT); + + Serial.begin(9600); + Serial.setTimeout(50); + + Serial.println("loaded"); + Serial.flush(); +} +String command = ""; + +// the loop function runs over and over again forever +void loop() { + if (Serial.available() > 0) { + // read the incoming byte: + command = Serial.readString(); + Serial.println(command + " ACK"); + Serial.flush(); + command.trim(); + + if (command.equals(ON)) { + digitalWrite(13, HIGH); + } else if (command.equals(OFF)) { + digitalWrite(13, LOW); + } + } +} diff --git a/ubuntu.terminal.echo.pcapng b/ubuntu.terminal.echo.pcapng new file mode 100644 index 0000000..e5a9a73 Binary files /dev/null and b/ubuntu.terminal.echo.pcapng differ