Hi everyone,
I'm rather new to forums in general but I'm trying to setup a web page based yun control so I can eventually control SSRs to turn on and off electronics around my apartment. I based my codes off of someone's "tutorial" on this site. http://www.open-electronics.org/interact-and-remotely-control-arduino-yun-with-ajax/ The page that is generated looks great, but it has a wicked long delay between communications and half the time it either doesn't change the picture I use to show whether the light output is high or low. I'm super basic when it comes to javascript and can't decipher what I'm reading. I am however a little more versed in C/C++ and am capable of figuring out what may be wrong with the Arduino code.
This is the Arduino code that I have. My java/HTML/XML (not to sure which it is) code is posted after. Can someone help me figure out as to why my yun won't connect / have super long delays / not change the picture?
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
int dac[] = {0, 0, 0, 0, 0, 0, 0};
int DigitalPin[] = {2, 4, 7, 8, 9, 10, 11, 12, 13};
int DacPin[] = {3, 5, 6};
YunServer server;
void setup()
{
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(7,INPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.listenOnLocalhost();
server.begin();
}
void loop()
{
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
delay(50);
}
void process(YunClient client)
{
String command = client.readStringUntil('/');
if (command == "digital") {
digitalCommand(client);
}
if (command == "analog") {
analogCommand(client);
}
if (command == "dac") {
dacCommand(client);
}
if (command == "state") {
stateCommand(client);
}
}
void digitalCommand(YunClient client)
{
int pin, value;
pin = client.parseInt();
if (client.read() == '/')
{
value = client.parseInt();
digitalWrite(pin, value);
}
else
{
value = digitalRead(pin);
}
client.print(F("analog,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void dacCommand(YunClient client)
{
int pin, value;
pin = client.parseInt();
if (client.read() == '/')
{
value = client.parseInt();
dac[pin] = value;
analogWrite(pin, value);
}
else {
value = dac[pin];
}
client.print(F("dac,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
void stateCommand(YunClient client)
{
int pin, value;
client.print(F("state"));
for (int thisPin = 0; thisPin < 9; thisPin++)
{
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
for (int thisPin = 0; thisPin < 3; thisPin++)
{
pin = DacPin[thisPin];
value = dac[pin];
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
for (int thisPin = 0; thisPin < 6; thisPin++)
{
value = analogRead(thisPin);
value = analogRead(thisPin);
client.print(F("#A"));
client.print(thisPin);
client.print(F("="));
client.print(value);
}
client.println("");
}