I’ve been trying for a month to transform a php code with these functions into ajax/javascript requests but being a beginner I really have trouble moving forward.
I think I’m on the right track but I don’t know anyone who could help me and tell me if I’m wrong or not.
Here is the php code :
<?php
if(isset($_GET['ip'])){$address=$_GET['ip'];}
$port=8899;
$page = $_SERVER['PHP_SELF'];
$sec = "5";
?>
<meta http-equiv="refresh" content="<?php echo $sec; ?>">
<?php
$rcm='~M601 S1rn';
$rim='~M115rn';
$rtm='~M105rn';
$rhm='~G28rn';
$progres='~M27rn';
$status='~M119rn';
$cal='~M650rn';
$l_on='~M146 r255 g255 b255rn';
$pause='~M25rn';
$resume='~M24rn';
$cancel='~M26rn';
$home='~G28rn';
$buf='';
if (($socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) and
(socket_connect($socket, $address, $port)))
{
$text="Connection successful on IP $address, port $port";
socket_send($socket,utf8_encode($rcm),strlen($rcm),0);
socket_recv($socket, $bufi, 1024, 0);
if(isset($_POST['LED']) && $_POST['LED']=='SWITCH'){ socket_send($socket,utf8_encode($l_on),strlen($l_on),0);socket_recv($socket, $buf, 1024, 0);}
if(isset($_POST['PAUSE']) && $_POST['PAUSE']=='ON'){ socket_send($socket,utf8_encode($pause),strlen($pause),0);socket_recv($socket, $buf, 1024, 0);}
if(isset($_POST['RESUME']) && $_POST['RESUME']=='ON'){ socket_send($socket,utf8_encode($resume),strlen($resume),0);socket_recv($socket, $buf, 1024, 0);}
if(isset($_POST['STOP']) && $_POST['STOP']=='ON'){ socket_send($socket,utf8_encode($cancel),strlen($cancel),0);socket_recv($socket, $buf, 1024, 0);}
if(isset($_POST['HOME']) && $_POST['HOME']=='ON'){ socket_send($socket,utf8_encode($home),strlen($home),0);socket_recv($socket, $buf, 1024, 0);}
socket_send($socket,utf8_encode($rtm),strlen($rtm),0);
socket_recv($socket, $buft, 1024, 0);
socket_send($socket,utf8_encode($progres),strlen($progres),0);
while(!socket_recv($socket, $bufp, 1024, 0));
socket_send($socket,utf8_encode($status),strlen($status),0);
while(!socket_recv($socket, $bufs, 1024, 0));
// socket_recv($socket, $led, 1024, 0);
socket_close($socket);
// echo "Prijate data:<br>";
// echo $bufs; ///////////////////////DEBUG MESSAGE
//Uprava informacii o teplote
$buft=explode('T0:',$buft);
$buft=explode('T1:',$buft[1]);
$temp=explode('/',$buft[0]);
$temp_he=$temp[0];
$temp_hes=$temp[1];
$buft=explode('B:',$buft[1]);
$buft=explode('ok',$buft[1]);
$temp=explode('/',$buft[0]);
$temp_bed=$temp[0];
$temp_beds=$temp[1];
//uprava informacii o progrese.
$bufp=explode('byte',$bufp);
$bufpp=explode('/',$bufp[1]);
$hotovo=$bufpp[0];
$layer=explode('Layer:',$bufp[1]);
$layer=explode('ok',$layer[1]);
$layer=$layer[0];
//uprava informacii o subore
$file=explode('CurrentFile:',$bufs);
$files=explode('ok',$file[1]);
$file=$files[0];
//Uprava informacii o stave
$stav=explode('MoveMode:',$bufs);
$stavs=explode('Status: S:',$stav[1]);
$stav=$stavs[0];
}
else
$text="Unable to connect<pre>".socket_strerror(socket_last_error())."</pre>";
echo "<table border=1 style='width:100%'>";
echo "<tr><td colspan=3 style='width:33%' align=center>Temperature </td> <td colspan=2 style='width:33%' align=center>Progres</td><td style='width:33%' align=center>Control</td> </tr>";
echo "<tr><td> </td><td> SET </td><td> NOW </td><td> File: </td><td> $file </td><td>State: $stav </td></tr>";
echo "<tr><td>HOTEND </td><td> $temp_hes °C </td><td> $temp_he °C </td><td> Done: </td><td> $hotovo % </td><td> </td></tr>";
echo "<tr><td>BED </td><td> $temp_beds °C </td><td> $temp_bed °C </td><td> Layer: </td><td> $layer </td><td><form action='ff.php?ip=$address' method='post'>
<button type='submit' name='LED' value='SWITCH' >LED</button><button type='submit' name='PAUSE' value='ON' >PAUSE</button><button type='submit' name='RESUME' value='ON' >RESUME</button><button type='submit' name='STOP' value='ON' >CANCEL</button><button type='submit' name='HOME' value='ON' >HOME</button>
</form></td></tr>";
echo "</table>";
echo "<br>";
//echo $text;
?>
Here is my ajax code (don’t work) :
function loadStatus() {
const address = new URLSearchParams(window.location.search).get('ip');
if (address) {
$.ajax({
url: `http://${address}:8080`,
method: 'POST',
data: {
command: 'status'
},
success: function(data) {
updateStatus(data);
},
error: function() {
alert('Stream not available');
}
});
}
}
function sendCommand(command) {
const address = new URLSearchParams(window.location.search).get('ip');
if (address) {
$.ajax({
url: `http://${address}:8080`,
method: 'POST',
data: {
command: command
},
success: function(data) {
updateStatus(data);
},
error: function() {
alert('Unable to connect');
}
});
}
}
function updateStatus(data) {
// Assuming data is JSON and parsing it
const status = JSON.parse(data);
const temp_he = status.temp_he;
const temp_hes = status.temp_hes;
const temp_bed = status.temp_bed;
const temp_beds = status.temp_beds;
const hotovo = status.hotovo;
const layer = status.layer;
const file = status.file;
const stav = status.stav;
$('#temp_he').text(temp_he);
$('#temp_hes').text(temp_hes);
$('#temp_bed').text(temp_bed);
$('#temp_beds').text(temp_beds);
$('#hotovo').text(hotovo);
$('#layer').text(layer);
$('#file').text(file);
$('#stav').text(stav);
}
$(document).ready(function() {
loadStreams();
setInterval(refreshFrames, 5000); // Refresh every 5 seconds
$('#controlForm button').click(function(event) {
event.preventDefault();
const command = $(this).val();
sendCommand(command);
});
});
I would like to point out that this code will be made available as open source on Github once the project is completely finished.
Thanks to all the charitable souls who are willing to dedicate a few minutes of their time to help me and guide me in the right direction.
Have a good day.