Hello I am controlling a arduino through html/php using processing basically what it does is it has a txt file in the web server that holds the value for the state of a led that gets changed through html/css the processing scetch runs in a loop and checks to see if the state has changed anytime the state changes its coresponding command runs here is my code
processing:
import processing.serial.*;
Serial port;
void setup() {
/* This part must be altered to fit your local settings. The number in brackets after "Serial.list()" is where you declare what COM port your Arduino is connected to.
If you get error messages, try a different number starting from 0 (e.g. 0, 1, 2, 3...) . */
port = new Serial(this, Serial.list()[1], 9600); // Open the port that the Arduino board is connected to, at 9600 baud
}
void draw() {
String onoroff[] = loadStrings("http://192.168.0.143/LEDstate.txt"); // Insert the location of your .txt file
if (onoroff[0].equals("0") == true) {
println(" - TELLING ARDUINO TO TURN LED1 OFF");
port.write('0');
}
else if (onoroff[0].equals("1") == true) {
println(" - TELLING ARDUINO TO TURN LED1 ON");
port.write('1');
} else if (onoroff[0].equals("2") == true) {
println(" - TELLING ARDUINO TO TURN LED2 On");
port.write('2'); // Send "L" over serial to set LED to LOW
} else if (onoroff[0].equals("3") == true) {
println(" - TELLING ARDUINO TO TURN LED2 OFF");
port.write('3');
} else if (onoroff[0].equals("4") == true) {
println(" - TELLING ARDUINO TO TURN LED3 ON");
port.write('4');
}else if (onoroff[0].equals("5") == true) {
println(" -TELLING ARDUINO TO TURN LED3 OFF");
port.write('5');
}
delay(0); // Set your desired interval here, in milliseconds
}
arduino sketch:
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int incomingByte;
String stringRead;
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == '0') {
digitalWrite(led1, LOW);
}
if (incomingByte == '1') {
digitalWrite(led1, HIGH);
}
if (incomingByte == '2') {
digitalWrite(led2, HIGH);
}
if (incomingByte == '3') {
digitalWrite(led2, LOW);
}
if (incomingByte == '4') {
digitalWrite(led3, HIGH);
}
if (incomingByte == '5') {
digitalWrite(led3, LOW);
}
}
}
html/css:
<html>
<title>hpa arduino controller</title>
<body>
<center>
<head id="header">
<img src="http://192.168.0.143/images/arduinobg.png"/>
</head>
</center>
<div class="right" id="main">
<p id="mainp1">
welcome the H.P.A html,Processing,Arduino controller to get things
started turn on your arduino unit by conecting it to a power source
it is prefered that you set up your arduino unit up around your main home
computer so that you can easily hook it to you computer but dont wory
you cant also use it through local internet if you want it set up somewhere
else. once set up and everything is ready to go goto your computer and run the program
that says html_processin_arduino all systems should now bo up and running
to control your units connected to the arduino unit just type in localhost into your browser
and you should see a page pop up with controls and this quick guide<p>
</div>
<div class="left" id="controls">
<center><h4 id="controlsh4"> Controls </h4></center>
<p id="labels">Led1:</p>
<p>
<b><a id="a1on"href="led.php?state=1">ON</a></b> /
<b><a id="a1off" href="led.php?state=0">OFF</a></b>
</p>
<p id="labels">Led2:</p>
<p>
<b><a id="a1on"href="led.php?state=2">ON</a></b> /
<b><a id="a1off" href="led.php?state=3">OFF</a></b>
</p>
<p id="labels">Led3:</p>
<p>
<b><a id="a1on"href="led.php?state=4">ON</a></b> /
<b><a id="a1off" href="led.php?state=5">OFF</a></b>
</p>
</div>
</body>
</html>
<style>
#labels{
color: #E25D15;
}
#controlsh4{
color: #E39C0B;
}
#mainp1{
color: #E39C0B;
}
img{
width: 50%;
height: 25%;
}
#controls{
padding: 5px;
height: 100%;
width: 22%;
background: #00747D;
}
#a1off:visited{
color: grey;
}
#a1on{
color: grey;
}
#a1on:hover{
color: green;
}
#a1off:hover{
color: red;
}
a{
text-decoration: none;
}
#header{
background-color: #00747D;
}
#main{
align: right;
width: 75%;
height: 100%;
background-color: #079CA3;
}
.right {
float: right;
padding: 10px;
}
.left {
float: left;
padding: 0px;
}
.center {
float: center;
padding: 0px;
}
</style>
could someone PLEASE tell me how to instead of using an interval i could use a string to pass through and could someone PLEASE help me make my code better by telling me if i have an error