Skip to content

Instantly share code, notes, and snippets.

@abachman
Created November 20, 2018 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abachman/c009ac5e853ac572149e0bf69e50a4b5 to your computer and use it in GitHub Desktop.
Save abachman/c009ac5e853ac572149e0bf69e50a4b5 to your computer and use it in GitHub Desktop.
Arduino ❤️ Processing
/*
Publish from an ESP8266 to a generic TCP server.
This sketch uses the simplest protocol possible
to send numbers over the wire.
*/
#include <ESP8266WiFi.h>
const char* ssid = "";
const char* password = "";
// The server that all clients will connect to. host and port are dependent on
// the computer / network you would like to connect to.
const char* host = "10.10.18.106"; // this can be a hostname or an IP address, you will need to get it
// from your running processing sketch
const int port = 12345;
// Use WiFiClient class to create TCP connections
WiFiClient client;
// This function will attempt to connect to the server at host:port until it is
// available.
void connectToServer() {
while(!client.connected()) {
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(2000);
} else {
Serial.println("connected!");
}
}
}
#define BUTTON_PIN_1 5
#define BUTTON_PIN_2 4
// add more button pins as needed here
void setup() {
Serial.begin(115200);
delay(10);
// set button pin as an input
pinMode(BUTTON_PIN_1, INPUT);
pinMode(BUTTON_PIN_2, INPUT);
// set up extra button pins here
// We start by connecting to a WiFi network
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// while the wifi is connecting, this loop will print "......"
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
Serial.println("My IP address: ");
Serial.println(WiFi.localIP());
// And then connecting to a specific server
connectToServer();
}
int counter = 0;
// button states for button1 and button2
bool current[] = { false, false };
bool last[] = { false, false };
int loc[] = { 10, 10 };
int val = 0;
void loop() {
// grab the current state of the button.
// we have to flip the logic because we are
// using a pullup resistor.
if(digitalRead(BUTTON_PIN_1) == LOW)
current[0] = true;
else
current[0] = false;
if(digitalRead(BUTTON_PIN_2) == LOW)
current[1] = true;
else
current[1] = false;
// return if the value hasn't changed
if(current[0] == last[0] && current[1] == last[1])
return;
bool doSend = false;
if (current[0]) {
loc[0] += 10;
val += 5;
doSend = true;
}
if (current[1]) {
loc[1] += 10;
val -= 5;
doSend = true;
}
last[0] = current[0];
last[1] = current[1];
// our protocol!
if (doSend) {
String payload = String(val) + "\n";
counter++;
Serial.print("sending payload ");
Serial.print(counter);
Serial.print(" -> ");
Serial.println(payload);
// This will send the request to the server, printing the bytes of the payload
// across the network.
client.print(payload);
}
if (!client.connected()) {
Serial.println("server went away, reconnecting...");
connectToServer();
}
delay(10);
}
/**
* based on "Shared Drawing Canvas (Server)" by Alexander R. Galloway from the
* built-in Processing examples.
*/
import processing.net.*;
/// required global network variables
Server s;
Client c;
int port = 12345;
// global variables for dealing with incoming values
String input;
int data;
byte NEWLINE = 10; // the ASCII byte value of "\n"
void setup()
{
size(450, 450);
background(204);
stroke(0);
fill(0);
s = new Server(this, port); // Start a simple server on port 12345
println("server running at " + Server.ip() + ":" + port);
rectMode(CENTER);
}
long messageCount = 0;
void draw() {
// Receive data from a client. The available() function gives us a new network Client object.
c = s.available();
if (c != null) {
println("client connected " + c.ip());
// redraw background
background(204);
// read first line of incoming data
input = c.readStringUntil(NEWLINE); // read up to the next newline character
messageCount++;
// debugging message
data = Integer.parseInt(input.trim()); // assume input is a single integer value
println("reading message from client at " + c.ip() + " <- input=", input, "; data=", data);
rect(width/2, height/2, data, data);
}
}
// ClientEvent message is generated when a client disconnects.
void disconnectEvent(Client someClient) {
print("client " + someClient.ip() + " has disconnected");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment