P5.js with ESP8266 WiFi

I succeeded, only one sketch to upload to esp8266 with arduino code and P5js code, it works and is bidirectional.
thanks to jafal for the links and RichardDL, if you want try it maybe the Rasp… clicking inside the circle the led turns on, clicking outside the circle the led turns off. it needs to be improved


//by camper0s

//////////////////////////////////////////////////////////////////////////html
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js"></script>
   <script type="text/javascript">
/////////////////////////////////////////////////////////////////////Processing P5.js
let source
let arduVal1
let arduVal2
let p5
let rot
let interval
let previousMillis
let adc
let stat
let xhr
 
 function setup() {
  createCanvas(700, 700);
  r = random(255);
  g = random(255);
  b = random(255);
textAlign(CENTER);
textSize(width/30);
rot = 0;
p5 = 0;
arduVal1 = 0;
arduVal2 = 0;
interval = 4000;
previousMillis = 0;
source = 0
adc=0;
stat=0;
let xhr = 0; 
}
 function draw() {
let currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
source = new EventSource('/events');
source.addEventListener('newVal', function(e) {
const obj = JSON.parse(e.data);
arduVal1 = obj.out1.toFixed(2); // toFixed(2) = 2 decimal
arduVal2 = obj.out2;
});
previousMillis = currentMillis;
}
background(200,50,20);
noStroke();
fill(20,20,255);
text ("from ardu1 (5s)", width/4, width/14);
text (arduVal1, width/4, width/9);
text ("from ardu2 (5s)", width/1.5, width/14);
text (arduVal2, width/1.5, width/9);

strokeWeight(2);
stroke(r, g, b);
fill(r, g, b, 127);
ellipse(width/2, width/2, width/3.6, width/3.6);

push();
translate(width / 2, width / 2);
fill(0,255,0);
rot = rot+1;
rotate(radians(rot));
rect(-26, -26, 52, 52);
pop();

stroke(255,255,0);
text ("clickMe", width/2, width/2);
}
function mousePressed() {
  // Check if mouse is inside the circle
  let d = dist(mouseX, mouseY, width/2, width/2);
  if (d < 100) {
    // Pick new random color values
    r = random(255);
    g = random(255);
    b = random(255);
stat=1;
}
else if(d > 100 )
{
stat=0;
}
  if (stat == 1) {
  adc=1;
  xhr = new XMLHttpRequest();
  xhr.open("GET", "/update?output="+adc+"&state=0", true);
  xhr.send();
}
else if(stat == 0)
{
 adc=0;
  xhr = new XMLHttpRequest();
  xhr.open("GET", "/update?output="+adc+"&state=0", true);
  xhr.send();
  }
}
////////////////////////////////////////////////////////////////////////html
</script>
</html>)rawliteral";
////////////////////////////////////////////////////////////////////arduino esp826
#include <ESP8266WiFi.h>
#include "ESPAsyncWebServer.h"
#include "ESPAsyncTCP.h"
#include <Arduino_JSON.h>
const char *ssid = "xxxxxdfffvxxx";
const char *password = "xxxxxxxxcxfx";
float ardu1;
int ardu2;
unsigned long previousMillis = 0;
const long interval = 5000;
const char *in1 = "output";
const char *in2 = "state";
String inputMessage1;
String inputMessage2;
AsyncWebServer server(80);
AsyncEventSource events("/events");

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
 WiFi.mode(WIFI_AP_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Setting as a Wi-Fi Station..");
  }
  Serial.print("Station IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Wi-Fi Channel: ");
  Serial.println(WiFi.channel());
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send_P(200, "text/html", index_html);
  });
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request) {
    inputMessage1 = request->getParam(in1)->value();
    inputMessage2 = request->getParam(in2)->value();
    Serial.print("GPIO: ");
    Serial.print(inputMessage1);
    Serial.print(" - Set to: ");
    Serial.println(inputMessage2);
    request->send(200, "text/plain", "OK");
  });
  server.addHandler(&events);
  server.begin();
  delay(4000);
}
void loop() {
  unsigned long currentMillis = millis();
  ardu1 = ardu1 + 0.01;
  ardu2 = random(1, 1234);
  JSONVar myVal;
  myVal["out1"] = ardu1;
  myVal["out2"] = ardu2;
  String jsonStr = JSON.stringify(myVal);

  if (currentMillis - previousMillis >= interval) {
    events.send(jsonStr.c_str(), "newVal", millis(), 1);
    events.send("ping", NULL, millis(), 1);
    previousMillis = currentMillis;
  }
  if (inputMessage1.toInt() == 0) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else if (inputMessage1.toInt() == 1) {
    digitalWrite(LED_BUILTIN, LOW);
  }
}
2 Likes