10000 Rb name change by RoanBrand · Pull Request #3 · RoanBrand/ArduinoSerialToTCPBridgeClient · GitHub
[go: up one dir, main page]

Skip to content

Rb name change #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions LICENSE.txt
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2023, Roan Brand
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Arduino Serial to TCP Bridge Client
# Arduino TCP over Serial Client
Arduino client for the [Serial To TCP Bridge Protocol](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) gateway service.

Open a TCP connection to a server from the Arduino using just serial. No Ethernet/WiFi shields necessary.
Open a TCP over Serial connection to a server from the Arduino using the host. No Ethernet/WiFi shields necessary.
Quickly communicate with other servers and make network apps using minimal hardware.
See [this](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) for more information on the protocol and for the **Protocol Gateway** you will need to run on the host PC the Arduino is connected to serially.

Expand All @@ -19,7 +19,7 @@ These should install automatically when installing this library through the Ardu
## How to
- Get the [Protocol Gateway](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) and build it.
- Change the gateway's config to listen on the Serial port connected to your Arduino and start it.
- Your Arduino app can then use the `ArduinoSerialToTCPBridgeClient` API which is similar to the `EthernetClient` API to make tcp connections to servers as long as they are reachable from the host PC and the gateway service is running.
- Your Arduino app can then use the `TCPOverSerialClient` API which is similar to the `EthernetClient` API to make tcp connections to servers as long as they are reachable from the host PC and the gateway service is running.

## Web Client Example
- Modified version of the Ethernet shield's Web Client example.
Expand All @@ -39,10 +39,10 @@ These should install automatically when installing this library through the Ardu
### Details
- Tested only on Arduino Uno. It would probably not work for the Arduino Due.
- You cannot use the same serial port in your app that is being used by the protocol, e.g. You cannot use `Serial` (Serial0) when the library uses `NeoSerial`, etc. You can modify this lib easily to use other hardware serial ports on bigger boards than the Arduino Uno if you want to free up your USB Serial connection.
- Your app's instance of `ArduinoSerialToTCPBridgeClient` needs to be a pointer and created with `new()`. It doesn't work otherwise and I don't know why yet.
- Your app's instance of `TCPOverSerialClient` needs to be a pointer and created with `new()`. It doesn't work otherwise and I don't know why yet.

- The protocol provides the app an in order, duplicates free and error checked byte stream by adding a CRC32 and simple retry mechanism. See [this](https://en.wikibooks.org/wiki/Serial_Programming/Error_Correction_Methods) for background.
- The **Protocol Gateway** opens a real TCP connection to a set destination on behalf of the **Protocol Client** running on the Arduino, and forwards traffic bi-directionally.
- `ArduinoSerialToTCPBridgeClient` is derived from the standard Arduino `Client` class. This means existing code written for Ethernet/Wi-Fi shields should work with this.
- `TCPOverSerialClient` is derived from the standard Arduino `Client` class. This means existing code written for Ethernet/Wi-Fi shields should work with this.
- `NeoHWSerial` is an alternative for `Serial`, used to gain access to the AVR UART interrupts.
- The protocol cannot run over the standard Serial API or something like software serial because it needs hardware level RX interrupts when bytes arrive.
8 changes: 4 additions & 4 deletions examples/MQTTClient/MQTTClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* Publish character '1' to topic "LedIn" to turn on the Led on the Arduino Uno and '2' to turn it off.
*/

#include <ArduinoSerialToTCPBridgeClient.h>
#include <TCP_over_Serial.h>
#include <PubSubClient.h>

ArduinoSerialToTCPBridgeClient* s; // Protocol Client running over USB Serial
PubSubClient client; // MQTT Client
TCPOverSerialClient* s; // Protocol Client running over USB Serial
PubSubClient client; // MQTT Client

const char* broker = "mqtt.eclipseprojects.io";
const char* ledTopic = "LedIn";
Expand All @@ -25,7 +25,7 @@ uint32_t lastPub = 0;

void setup() {
pinMode(13, OUTPUT);
s = new ArduinoSerialToTCPBridgeClient();
s = new TCPOverSerialClient();
client.setClient(*s);

// MQTT Broker running on same PC the Arduino is connected to.
Expand Down
6 changes: 3 additions & 3 deletions examples/WebClient/WebClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

*/

#include <ArduinoSerialToTCPBridgeClient.h>
#include <TCP_over_Serial.h>

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
Expand All @@ -22,10 +22,10 @@ char server[] = "www.google.com"; // name address for Google (using DNS)
// Initialize the Protocol client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
ArduinoSerialToTCPBridgeClient* client;
TCPOverSerialClient* client;

void setup() {
client = new ArduinoSerialToTCPBridgeClient();
client = new TCPOverSerialClient();

// Open serial communications and wait for port to open:
/*Serial.begin(9600);
Expand Down
4 changes: 2 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
# Datatypes (KEYWORD1)
#######################################

ArduinoSerialToTCPBridgeClient KEYWORD1
TCPOverSerialClient KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

ArduinoSerialToTCPBridgeClient KEYWORD2
TCPOverSerialClient KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
8 changes: 4 additions & 4 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name=ArduinoSerialToTCPBridgeClient
name=TCP over Serial
version=1.1.1
author=Roan Brand <brandroan@gmail.com>
maintainer=Roan Brand <brandroan@gmail.com>
sentence=Open a TCP connection to a server from the Arduino using just Serial. (No Ethernet/WiFi shields necessary)
paragraph=Quickly communicate with other servers and make network apps using minimal hardware. The Protocol Gateway service runs on the host, listens on a Serial port connected to the Arduino, and opens TCP connections on behalf of the Protocol Client runnning on the Arduino, forwarding traffic bi-directionally. The protocol provides the app an in order, duplicates free and error checked byte stream by adding a CRC32 and simple retry mechanism.
sentence=TCP over Serial client connection to a server from the Arduino, using the connected host. (No Ethernet/WiFi shields necessary)
paragraph=Quickly communicate with other servers and make network apps using minimal hardware.
category=Communication
url=https://github.com/RoanBrand/ArduinoSerialToTCPBridgeClient
architectures=*
includes=ArduinoSerialToTCPBridgeClient.h
includes=TCP_over_Serial.h
depends=CRC32 (=2.0.0), NeoHWSerial (=1.6.6), PubSubClient (>=2.8.0)
42 changes: 21 additions & 21 deletions src/ArduinoSerialToTCPBridgeClient.cpp → src/TCP_over_Serial.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "ArduinoSerialToTCPBridgeClient.h"
#include "TCP_over_Serial.h"
#include <NeoHWSerial.h>
#include <CRC32.h>

static ArduinoSerialToTCPBridgeClient* ser0;
static TCPOverSerialClient* ser0;

void rxISR0(uint8_t c) {
ser0->rxCallback(c);
Expand All @@ -26,7 +26,7 @@ ISR(TIMER1_COMPA_vect) {
ackTimeout();
}

ArduinoSerialToTCPBridgeClient::ArduinoSerialToTCPBridgeClient() {
TCPOverSerialClient::TCPOverSerialClient() {
reset();
ser0 = this;
NeoSerial.attachInterrupt(rxISR0);
Expand All @@ -45,7 +45,7 @@ ArduinoSerialToTCPBridgeClient::ArduinoSerialToTCPBridgeClient() {
INVALID_RESPONSE -4
DOMAIN_NOT_FOUND -5
*/
int ArduinoSerialToTCPBridgeClient::connect(IPAddress ip, uint16_t port) {
int TCPOverSerialClient::connect(IPAddress ip, uint16_t port) {
uint8_t destination[6] = {
(uint8_t) ((uint32_t) ip),
(uint8_t) ((uint32_t) ip >> 8),
Expand All @@ -72,7 +72,7 @@ int ArduinoSerialToTCPBridgeClient::connect(IPAddress ip, uint16_t port) {
}

// Maximum host string of 248 bytes.
int ArduinoSerialToTCPBridgeClient::connect(const char *host, uint16_t port) {
int TCPOverSerialClient::connect(const char *host, uint16_t port) {
uint8_t destination[250];
uint8_t len = 0;

Expand Down Expand Up @@ -104,11 +104,11 @@ int ArduinoSerialToTCPBridgeClient::connect(const char *host, uint16_t port) {
return 1;
}

size_t ArduinoSerialToTCPBridgeClient::write(uint8_t b) {
size_t TCPOverSerialClient::write(uint8_t b) {
return write(&b, 1);
}

size_t ArduinoSerialToTCPBridgeClient::write(const uint8_t *buf, size_t size) {
size_t TCPOverSerialClient::write(const uint8_t *buf, size_t size) {
size_t written = 0;

while (written < size) {
Expand Down Expand Up @@ -142,7 +142,7 @@ size_t ArduinoSerialToTCPBridgeClient::write(const uint8_t *buf, size_t size) {
return size;
}

int ArduinoSerialToTCPBridgeClient::available() {
int TCPOverSerialClient::available() {
if (rxBufisFull)
return 256;

Expand All @@ -153,7 +153,7 @@ int ArduinoSerialToTCPBridgeClient::available() {
}
}

int ArduinoSerialToTCPBridgeClient::read() {
int TCPOverSerialClient::read() {
if (available() == 0)
return -1;

Expand All @@ -162,7 +162,7 @@ int ArduinoSerialToTCPBridgeClient::read() {
return ch;
}

int ArduinoSerialToTCPBridgeClient::read(uint8_t *buf, size_t size) {
int TCPOverSerialClient::read(uint8_t *buf, size_t size) {
int have = available();
if (have == 0)
return -1;
Expand All @@ -177,30 +177,30 @@ int ArduinoSerialToTCPBridgeClient::read(uint8_t *buf, size_t size) {
return toRead;
}

int ArduinoSerialToTCPBridgeClient::peek() {
int TCPOverSerialClient::peek() {
return rxBuf[rxBufpH];
}

void ArduinoSerialToTCPBridgeClient::flush() {
void TCPOverSerialClient::flush() {
NeoSerial.flush();
}

void ArduinoSerialToTCPBridgeClient::stop() {
void TCPOverSerialClient::stop() {
writePacket(PROTOCOL_DISCONNECT, NULL, 0);
flush();
reset();
//NeoSerial.end();
}
10000
uint8_t ArduinoSerialToTCPBridgeClient::connected() {
uint8_t TCPOverSerialClient::connected() {
return (state == STATE_CONNECTED) ? 1 : 0;
}

ArduinoSerialToTCPBridgeClient::operator bool() {
TCPOverSerialClient::operator bool() {
return 1;
}

void ArduinoSerialToTCPBridgeClient::reset() {
void TCPOverSerialClient::reset() {
stopAckTimer();
state = STATE_DISCONNECTED;
ackOutstanding = false;
Expand All @@ -212,7 +212,7 @@ void ArduinoSerialToTCPBridgeClient::reset() {
rxBufisFull = false;
}

boolean ArduinoSerialToTCPBridgeClient::writePacket(uint8_t command, uint8_t* payload, uint8_t pLength) {
boolean TCPOverSerialClient::writePacket(uint8_t command, uint8_t* payload, uint8_t pLength) {
if (pLength > 250)
return false;

Expand Down Expand Up @@ -255,7 +255,7 @@ boolean ArduinoSerialToTCPBridgeClient::writePacket(uint8_t command, uint8_t* pa
return true;
}

void ArduinoSerialToTCPBridgeClient::rxCallback(uint8_t c) {
void TCPOverSerialClient::rxCallback(uint8_t c) {
static uint16_t byteCount = 0;
static uint8_t rxState = RX_PACKET_IDLE;

Expand Down Expand Up @@ -346,7 +346,7 @@ void ArduinoSerialToTCPBridgeClient::rxCallback(uint8_t c) {
}

// http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/
void ArduinoSerialToTCPBridgeClient::setupAckTimer() {
void TCPOverSerialClient::setupAckTimer() {
cli();
TCCR1A = 0;
TCCR1B = 0;
Expand All @@ -356,11 +356,11 @@ void ArduinoSerialToTCPBridgeClient::setupAckTimer() {
sei();
}

void ArduinoSerialToTCPBridgeClient::startAckTimer() {
void TCPOverSerialClient::startAckTimer() {
TCCR1B |= (1 << CS12); // 256 prescaler
}

void ArduinoSerialToTCPBridgeClient::stopAckTimer() {
void TCPOverSerialClient::stopAckTimer() {
TCCR1B &= 0xF8; // remove clk source
TCNT1 = 0; // reset counter
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef arduinoserialtotcpbridgeclient_H
#define arduinoserialtotcpbridgeclient_H
#ifndef tcpoverserial_H
#define tcpoverserial_H

#include "Arduino.h"
#include "Client.h"
Expand All @@ -21,9 +21,9 @@
#define RX_PACKET_GOTCOMMAND 2
#define RX_PACKET_GOTPAYLOAD 3

class ArduinoSerialToTCPBridgeClient : public Client {
class TCPOverSerialClient : public Client {
public:
ArduinoSerialToTCPBridgeClient();
TCPOverSerialClient();

virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
Expand Down
0