Hello all
I am working on a CNC project that runs on GRBL, and i want to integrate an IoT system to it.
I made a webpage to monitor and control my machine, i get an Arduino Uno R4 with WiFi module (esp32-s3) that acts as a websocket server to send and receive data from webpage, and send and receive data from GRBL.
When i upload the arduino r4 code
It is connected to my network, i take its ip address and put it in the webpage to allow communication.
Vut on the serial monitor, always the websocket is disconneted, and cant link my webpage to the websocket.
pert
May 25, 2024, 8:14am
2
I moved your topic to an appropriate forum category @naseemaa .
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category " topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum " guide . The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
Line 42. Definitly. There's where your error is.
#include <WiFi.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
const char* ssid = "xxx";
const char* password = "xxx";
WiFiClient client;
WebSocketsClient webSocket;
float startAngle = 0.0;
float degAngle = 0.0;
float realX = 0.0;
float totalAngle = 0.0;
float theoreticalX = 0.0;
float theoreticalY = 0.0;
float theoreticalZ = 0.0;
float correctedAngle = 0.0;
int quadrantNumber = 0;
int previousquadrantNumber = 0;
float numberofTurns = 0.0;
int magnetStatus = 0;
byte lowbyte = 0;
byte highbyte = 0;
int rawAngle = 0;
float slippageThreshold = 0.02;
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to WiFi
connectWiFi();
// Initialize I2C
Wire.begin();
Wire.setClock(800000L);
checkMagnetPresence();
ReadRawAngle();
startAngle = degAngle;
// Initialize WebSocket connection
webSocket.begin("xxx.xxx.x.xx", 8080);
webSocket.onEvent(webSocketEvent);
// Initialize GRBL serial communication
Serial1.begin(115200); // Assuming Serial1 is used for GRBL
}
void loop() {
webSocket.loop();
ReadRawAngle();
correctAngle();
checkQuadrant();
realX = totalAngle / 1.8 * 0.02;
if (abs(theoreticalX - realX) > slippageThreshold) {
Serial.println("Slippage detected!");
slippageThreshold += 0.02; // Increment slippage threshold
DynamicJsonDocument doc(1024);
doc["slippage"]["position"] = realX;
String json;
serializeJson(doc, json);
webSocket.sendTXT(json);
}
readGRBLData();
sendDataToWebpage();
}
void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Attempting to connect to WiFi...");
delay(1000);
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void ReadRawAngle() {
Wire.beginTransmission(0x36);
Wire.write(0x0D);
Wire.endTransmission();
Wire.requestFrom(0x36, 1);
while (Wire.available() == 0);
lowbyte = Wire.read();
Wire.beginTransmission(0x36);
Wire.write(0x0C);
Wire.endTransmission();
Wire.requestFrom(0x36, 1);
while (Wire.available() == 0);
highbyte = Wire.read();
highbyte = highbyte << 8;
rawAngle = highbyte | lowbyte;
degAngle = rawAngle * 0.087890625;
}
void correctAngle() {
correctedAngle = degAngle - startAngle;
if (correctedAngle < 0) {
correctedAngle = correctedAngle + 360;
}
}
void checkQuadrant() {
if (correctedAngle >= 0 && correctedAngle <= 90) {
quadrantNumber = 1;
} else if (correctedAngle > 90 && correctedAngle <= 180) {
quadrantNumber = 2;
} else if (correctedAngle > 180 && correctedAngle <= 270) {
quadrantNumber = 3;
} else if (correctedAngle > 270 && correctedAngle < 360) {
quadrantNumber = 4;
}
if (quadrantNumber != previousquadrantNumber) {
if (quadrantNumber == 1 && previousquadrantNumber == 4) {
numberofTurns++;
} else if (quadrantNumber == 4 && previousquadrantNumber == 1) {
numberofTurns--;
}
previousquadrantNumber = quadrantNumber;
}
totalAngle = (numberofTurns * 360) + correctedAngle;
}
void checkMagnetPresence() {
while ((magnetStatus & 32) != 32) {
magnetStatus = 0;
Wire.beginTransmission(0x36);
Wire.write(0x0B);
Wire.endTransmission();
Wire.requestFrom(0x36, 1);
while (Wire.available() == 0);
magnetStatus = Wire.read();
if ((magnetStatus & 32) != 32) {
Serial.println("Please adjust the magnet!");
}
}
}
void readGRBLData() {
if (Serial1.available()) {
String grblData = Serial1.readStringUntil('\n');
Serial.println("GRBL Data: " + grblData);
parseGRBLData(grblData);
}
}
void parseGRBLData(String grblData) {
// Example of parsing GRBL data
// Assuming GRBL sends positions in the format: "<Idle|MPos:0.000,0.000,0.000|FS:0,0>"
int mPosIndex = grblData.indexOf("MPos:");
if (mPosIndex != -1) {
int fsIndex = grblData.indexOf("|FS:");
if (fsIndex != -1) {
String positions = grblData.substring(mPosIndex + 5, fsIndex);
int firstComma = positions.indexOf(',');
int secondComma = positions.indexOf(',', firstComma + 1);
theoreticalX = positions.substring(0, firstComma).toFloat();
theoreticalY = positions.substring(firstComma + 1, secondComma).toFloat();
theoreticalZ = positions.substring(secondComma + 1).toFloat();
}
}
}
void sendDataToWebpage() {
DynamicJsonDocument doc(1024);
doc["theoretical"]["x"] = theoreticalX;
doc["theoretical"]["y"] = theoreticalY;
doc["theoretical"]["z"] = theoreticalZ;
doc["real"]["x"] = realX;
String json;
serializeJson(doc, json);
webSocket.sendTXT(json);
}
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_CONNECTED:
Serial.println("WebSocket connected");
break;
case WStype_DISCONNECTED:
Serial.println("WebSocket disconnected");
break;
case WStype_TEXT:
Serial.print("[WebSocket] Message received: ");
Serial.write(payload, length);
Serial.println();
break;
case WStype_BIN:
Serial.println("Binary data received");
break;
case WStype_ERROR:
Serial.println("WebSocket error");
break;
}
}
it connects to internet but the websocket do not
I don't see where you connect the websocket to a remote part.
system
Closed
November 21, 2024, 3:26pm
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.