|
| 1 | +<p><img align="right" width="128" height="54" src="/img/services/serial-arduino-logo.png"></p> |
| 2 | + |
| 3 | +The Serial service allows to communicate with a device that is connected to your pc over a serial port. |
| 4 | + |
| 5 | +The most common example is exchanging data with an [arduino](https://www.arduino.cc/) over USB. |
| 6 | + |
| 7 | +## Credentials |
| 8 | + |
| 9 | +### `port` _(required)_ |
| 10 | + |
| 11 | +The serial port you want to communicate with. Finding the right port is different on every operating system: |
| 12 | + |
| 13 | +**Windows:** |
| 14 | +1. Open Device Manager, and expand the Ports (COM & LPT) list |
| 15 | +2. Note the port name between the braces (e.g. `COM3`) |
| 16 | +  |
| 17 | + |
| 18 | +**Mac:** |
| 19 | +1. Open terminal and type: |
| 20 | + ```shell |
| 21 | + ls /dev/* |
| 22 | + ``` |
| 23 | +2. Note the port number listed for `/dev/tty.usbmodem*` or `/dev/tty.usbserial*.` |
| 24 | + The port number is represented with `*` here. |
| 25 | + |
| 26 | +**Linux:** |
| 27 | +1. Open terminal and type: |
| 28 | + ```shell |
| 29 | + ls /dev/tty* |
| 30 | + ``` |
| 31 | +2. Note the port number listed for `/dev/ttyUSB*` or `/dev/ttyACM*` |
| 32 | + The port number is represented with `*` here |
| 33 | + |
| 34 | +### `baudRate` _(optional)_ |
| 35 | + |
| 36 | +If you want to use a custom baud rate to communicate with the device you can set it here. |
| 37 | +Default value is `9600`. |
| 38 | + |
| 39 | + |
| 40 | +## Plugin development with Serial Input |
| 41 | +Have a look at this simple example plugin that prints incoming data to log: |
| 42 | + |
| 43 | +For more information have a look at the [javadoc](http://docs.codeoverflow.org/chatoverflow-api/org/codeoverflow/chatoverflow/api/io/input/SerialInput.html). |
| 44 | +```java |
| 45 | +import java.util.function.Consumer; |
| 46 | + |
| 47 | +import org.codeoverflow.chatoverflow.api.plugin.configuration.Requirement; |
| 48 | +import org.codeoverflow.chatoverflow.api.io.input.SerialInput; |
| 49 | +import org.codeoverflow.chatoverflow.api.io.event.serial.SerialDataAvailableEvent; |
| 50 | +import org.codeoverflow.chatoverflow.api.plugin.PluginImpl; |
| 51 | +import org.codeoverflow.chatoverflow.api.plugin.PluginManager; |
| 52 | + |
| 53 | +public class TestPlugin extends PluginImpl { |
| 54 | + //require a new twitch chat input |
| 55 | + private Requirement<SerialInput> serialIn = |
| 56 | + require.input.serial("serialIn", "An arduino connected to the serial port", false); |
| 57 | + |
| 58 | + public TestPlugin(PluginManager pluginManager) { |
| 59 | + super(pluginManager); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void setup() { |
| 64 | + //register the message handler that reacts on incoming messages |
| 65 | + twitchIn.get().registerDataAvailableEventHandler(new DataHandler()); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void loop() {} |
| 70 | + |
| 71 | + @Override |
| 72 | + public void shutdown() { |
| 73 | + log("Shutdown!"); |
| 74 | + } |
| 75 | + |
| 76 | + private class DataHandler implements Consumer<SerialDataAvailableEvent> { |
| 77 | + |
| 78 | + @Override |
| 79 | + public void accept(SerialDataAvailableEvent event) { |
| 80 | + //print the incoming string to the log |
| 81 | + log("[Serial]" + event.getAsString()); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +``` |
| 86 | + |
| 87 | +## Plugin development with Serial Output |
| 88 | +Have a look at this simple example plugin that sends `ping` to the connected device |
| 89 | + |
| 90 | +For more information have a look at the [javadoc](http://docs.codeoverflow.org/chatoverflow-api/org/codeoverflow/chatoverflow/api/io/output/SerialOutput.html). |
| 91 | + |
| 92 | +```java |
| 93 | +import java.util.function.Consumer; |
| 94 | +import java.util.List; |
| 95 | +import java.time.LocalTime; |
| 96 | +import java.time.format.DateTimeFormatter; |
| 97 | + |
| 98 | +import org.codeoverflow.chatoverflow.api.plugin.configuration.Requirement; |
| 99 | +import org.codeoverflow.chatoverflow.api.io.output.SerialOutput; |
| 100 | +import org.codeoverflow.chatoverflow.api.plugin.PluginImpl; |
| 101 | +import org.codeoverflow.chatoverflow.api.plugin.PluginManager; |
| 102 | + |
| 103 | +public class TestPlugin extends PluginImpl { |
| 104 | + //require a new twitch chat output |
| 105 | + private Requirement<SerialOutput> serialOut = |
| 106 | + require.output.serial("serialOut", "An arduino connected to the serial port", false); |
| 107 | + |
| 108 | + public TestPlugin(PluginManager pluginManager) { |
| 109 | + super(pluginManager); |
| 110 | + //Set the loop to repeat every 30 seconds (default 1 second) |
| 111 | + loopInterval = 30 * 1000; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void setup() {} |
| 116 | + |
| 117 | + @Override |
| 118 | + public void loop() { |
| 119 | + //Send ping to serial port |
| 120 | + serialOut.getPrintStream().println("ping"); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void shutdown() { |
| 125 | + log("Shutdown!"); |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Example arduino script |
| 131 | + |
| 132 | +An simple ping-pong example script for your arduino. |
| 133 | +```cpp |
| 134 | +String inputString = ""; |
| 135 | +boolean stringComplete = false; |
| 136 | + |
| 137 | +void setup() { |
| 138 | + Serial.begin(9600); |
| 139 | +} |
| 140 | + |
| 141 | +// the loop routine runs over and over again forever |
| 142 | +void loop() { |
| 143 | + while (Serial.available()) { |
| 144 | + char inChar = (char)Serial.read(); |
| 145 | + inputString += inChar; |
| 146 | + if (inChar == '\n') { |
| 147 | + stringComplete = true; |
| 148 | + } |
| 149 | + } |
| 150 | + if (stringComplete) { |
| 151 | + Serial.println("pong"); |
| 152 | + stringComplete = false; |
| 153 | + } |
| 154 | + delay(1); // delay in between reads for stability |
| 155 | +} |
| 156 | +``` |
0 commit comments