Skip to content
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
.vscode
.vscode

CMakeLists.txt
52 changes: 52 additions & 0 deletions examples/client/client.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <Arduino_RouterBridge.h>

BridgeTCPClient<> client(Bridge);

void setup() {

if (!Bridge.begin()) {
while (true) {}
}

if (!Monitor.begin()) {
while (true) {}
}

}

void loop() {

Monitor.println("\nStarting connection to server...");
/* if you get a connection, report back via serial: */
if (client.connect("arduino.tips", 80) < 0) {
Monitor.println("unable to connect to server");
return;
}

Monitor.println("connected to server");
/* Make an HTTP request: */
size_t w = client.println("GET /asciilogo.txt HTTP/1.1");
w += client.println("Host: arduino.tips");
w += client.println("User-Agent: Arduino");
w += client.println("Connection: close");
w += client.println();

/* if there are incoming bytes available from the server,
* read them and print them:
*/
while (client.connected()) {
size_t len = client.available();
if (len) {
uint8_t buff[len];
client.read(buff, len);
Monitor.write(buff, len);
}
delay(0);
}

/* if the server's disconnected, stop the client: */
Monitor.println();
Monitor.println("disconnecting from server.");
client.stop();
delay(1000);
}
68 changes: 68 additions & 0 deletions examples/clientSSL/clientSSL.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <Arduino_RouterBridge.h>

static const char ca_cert[] = {
"-----BEGIN CERTIFICATE-----\n"
"MIICCTCCAY6gAwIBAgINAgPlwGjvYxqccpBQUjAKBggqhkjOPQQDAzBHMQswCQYD\n"
"VQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEUMBIG\n"
"A1UEAxMLR1RTIFJvb3QgUjQwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAwMDAw\n"
"WjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2Vz\n"
"IExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjQwdjAQBgcqhkjOPQIBBgUrgQQAIgNi\n"
"AATzdHOnaItgrkO4NcWBMHtLSZ37wWHO5t5GvWvVYRg1rkDdc/eJkTBa6zzuhXyi\n"
"QHY7qca4R9gq55KRanPpsXI5nymfopjTX15YhmUPoYRlBtHci8nHc8iMai/lxKvR\n"
"HYqjQjBAMA4GA1UdDwEB/wQEAwIBhjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW\n"
"BBSATNbrdP9JNqPV2Py1PsVq8JQdjDAKBggqhkjOPQQDAwNpADBmAjEA6ED/g94D\n"
"9J+uHXqnLrmvT/aDHQ4thQEd0dlq7A/Cr8deVl5c1RxYIigL9zC2L7F8AjEA8GE8\n"
"p/SgguMh1YQdc4acLa/KNJvxn7kjNuK8YAOdgLOaVsjh4rsUecrNIdSUtUlD\n"
"-----END CERTIFICATE-----\n"
};

BridgeTCPClient<> client(Bridge);

void setup() {

if (!Bridge.begin()) {
while (true) {}
}

if (!Monitor.begin()) {
while (true) {}
}

}

void loop() {
Monitor.println("\nStarting connection to server...");

/* if you get a connection, report back via monitor: */
if (client.connectSSL("arduino.tips", 443, ca_cert) < 0) {
Monitor.println("unable to connect to server");
return;
}

Monitor.println("connected to server");
/* Make aHTTP request: */
size_t w = client.println("GET /asciilogo.txt HTTP/1.1");
w += client.println("Host: arduino.tips");
w += client.println("User-Agent: Arduino");
w += client.println("Connection: close");
w += client.println();

/* if there are incoming bytes available from the server,
* read them and print them:
*/
while (client.connected()) {
size_t len = client.available();
if (len) {
uint8_t buff[len];
client.read(buff, len);
Monitor.write(buff, len);
}
delay(0);
}

/* if the server's disconnected, stop the client: */
Monitor.println();
Monitor.println("disconnecting from server.");
client.stop();
delay(1000);
}
27 changes: 27 additions & 0 deletions examples/server/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# A python sketch that uses RPC Bridge to test the server.ino

import time
from arduino.app_utils import *


def log(msg):
with open("./log.log", "a") as f:
f.write(str(msg) + "\n")

def main():
res = Bridge.call("tcp/connect", "127.0.0.1", 5678)
log(f"Connection attempt id: {res}")

written = Bridge.call("tcp/write", res, "Hello friend")
log(f"Written msg of len: {written}")

time.sleep(1)

ok = Bridge.call("tcp/close", res)
log(f"Closed connection: {ok}")

if __name__ == "__main__":

while True:
main()
time.sleep(1)
46 changes: 46 additions & 0 deletions examples/server/server.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <Arduino_RouterBridge.h>

IPAddress localhost(127, 0, 0, 1);
BridgeTCPServer<> server(Bridge, localhost, 5678);

void setup() {

if (!Bridge.begin()) {
while (true) {}
}

if (!Monitor.begin()) {
while (true) {}
}

server.begin();

}

void loop() {

BridgeTCPClient<> client = server.accept();

if (client.connected() == 1){
Monitor.print("client ");
Monitor.print(client.getId());
Monitor.println(" connected");
}

if (client) {
Monitor.println("A client established a connection");
}

while (client.connected()) {
size_t len = client.available();
if (len) {
Monitor.println("Message received from client");
uint8_t buff[len];
client.read(buff, len);
Monitor.write(buff, len);
}
}

server.disconnect(); // Disconnects the client server-side

}
2 changes: 2 additions & 0 deletions src/Arduino_RouterBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
#include "Arduino.h"
#include "bridge.h"
#include "monitor.h"
#include "tcp_client.h"
#include "tcp_server.h"

#endif //ARDUINO_ROUTER_BRIDGE_H
Loading