I've been tinkering with my arduino Wifi sketch (uno with esp8266) and a processing sketch.
With the processing.net library and the WiFiEsp library on the arduino side I can use client.print to send strings from the arduino to my PC and print them to the console.
What I want to do now is send other forms of data (like int returns from my sensors) and rather than print them to the console, have the data written somewhere where it can be used to draw on processing. Eventually I want to be sending data from multiple sensors that each can be visualised with a different graph or gauge, ideally in the same processing sketch.
Here is an example of my working code - I tried replacing my client.print statements in loop() with client.write, and using readBytes() on the processing side but no cigar so far. Any and all advice would be welcome at this point!
Thanks for reading.
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(4,7); // RX, TX
#endif
int status = WL_IDLE_STATUS; // the Wifi radio's status
const int fanPin = 11;
const int solenoidPin1 = 12;
const int solenoidPin2 = 13;
int solenoidState1 = HIGH;
int solenoidState2 = HIGH;
unsigned long previousMillis = 0;
const long runTime = 5000;
const long interval = 60000;
const int pressureSensor = A0;
int pressureVal;
int pressure;
void setup()
{
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
pinMode(fanPin, OUTPUT);
analogWrite(fanPin, 150);
}
void loop()
{
// print the network connection information every 10 seconds
const uint16_t port = 5204;
const char * host = "192.168.0.26";
Serial.print("Connecting to host. . .");
WiFiEspClient client;
int pressureVal = analogRead(pressureSensor);
pressure = map(pressureVal, 175, 900, 0, 100);
float bar = pressure;
if(client.connect(host, port)) {
Serial.println("Sending data");
client.print(bar/10);
client.print(" BAR");
}
client.print("Signal strength : ");
client.print(WiFi.RSSI());
Serial.println("Closing connection");
client.stop();
Serial.println();
printCurrentNet();
printWifiData();
delay(5000);
}
void printWifiData()
{
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address
byte mac[6];
WiFi.macAddress(mac);
char buf[20];
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
Serial.print("MAC address: ");
Serial.println(buf);
}
void printCurrentNet()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to
byte bssid[6];
WiFi.BSSID(bssid);
char buf[20];
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
Serial.print("BSSID: ");
Serial.println(buf);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI): ");
Serial.println(rssi);
}
import processing.net.*;
Server myServer;
int dataIn;
void setup() {
size(2000, 800);
background(255);
// Starts a myServer on port 5204
myServer = new Server(this, 5204);
}
void draw() {
// Get the next available client
Client thisClient = myServer.available();
// If the client is not null, and says something, display what it said
if (thisClient !=null) {
String whatClientSaid = thisClient.readString();
if (whatClientSaid != null) {
println(thisClient.ip() + "t" + whatClientSaid);
}
}
}