-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
I modified webserwer example to control something over internet. I send commands through UART using Serial.write() (to radio transmitter). On the beginning i works ok. But when I use many client.println() to output website code then some chars from Serial.write() go to SPI and i can see them i firefox in website code.
Setup is Arduino Pro 16Mhz w/ ATmega328, W5100 Ethernet shield
When firefox loads page first time it looks ok. When page is reloaded chars from Serial.write() and some garbage are visible in code (causing page to crash).
code:
include <SPI.h>
include <Ethernet.h>
const byte bLEDpin=8;
char cLineBuffer[252];
byte bPlaceInBuffer;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xDE, 0xDE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(4800);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
//delay(2);
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Serial.print("server is at ");
//Serial.println(Ethernet.localIP());
pinMode(bLEDpin,OUTPUT);
digitalWrite(bLEDpin,LOW);
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client)
{
//Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
bPlaceInBuffer=0;
char cCmnd='H';
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if ((c!='\n') && (c!='\r')){
cLineBuffer[bPlaceInBuffer]=c;
if (bPlaceInBuffer<250) {
bPlaceInBuffer++;
};
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<style type=\"text/css\">");
client.println("form { display: inline-block; }");
client.println("#s1 { width: 120px; height: 120px;}");
client.println("</style>");
client.println("</head><body>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
//client.println("<meta http-equiv=\"refresh\" content=\"5\">");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println();
//client.println("<form METHOD=get action=\" http://192.168.1.177/\">");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"x\">");
client.println("<input type=submit value=\"-\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"6\">");
client.println("<input type=submit value=\"FF\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"x\">");
client.println("<input type=submit value=\"-\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<br>");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"a\">");
client.println("<input type=submit value=\"F+L\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"2\">");
client.println("<input type=submit value=\"FOWARD\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"b\">");
client.println("<input type=submit value=\"F+R\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<br>");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"4\">");
client.println("<input type=submit value=\"LEFT\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"s\">");
client.println("<input type=submit value=\"STOP\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"5\">");
client.println("<input type=submit value=\"RIGHT\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<br>");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"x\">");
client.println("<input type=submit value=\"-\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"3\">");
client.println("<input type=submit value=\"REVERSE\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"x\">");
client.println("<input type=submit value=\"-\" id=\"s1\">");
client.println("</form>");
client.println();
client.println("<br>");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"1\">");
client.println("<input type=submit value=\"LED On\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"0\">");
client.println("<input type=submit value=\"LED Off\">");
client.println("</form>");
client.println();
client.println("<br>");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"e\">");
client.println("<input type=submit value=\"servo 1 left\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"f\">");
client.println("<input type=submit value=\"servo 1 right\">");
client.println("</form>");
client.println();
/*
client.println("<br>");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"g\">");
client.println("<input type=submit value=\"servo 2 left\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"h\">");
client.println("<input type=submit value=\"servo 2 right\">");
client.println("</form>");
client.println();
client.println("<br>");
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"i\">");
client.println("<input type=submit value=\"servo 3 left\">");
client.println("</form>");
client.println();
client.println("<form METHOD=get action=\" \">");
client.println("<input type=hidden name=\"cmd\" value=\"j\">");
client.println("<input type=submit value=\"servo 3 right\">");
client.println("</form>");
client.println();*/
client.println("</body></html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
if ((cLineBuffer[0]=='G')&&(cLineBuffer[1]=='E')&&(cLineBuffer[2]=='T'))
/*VERY CRUDE: Always just picks up 11th char of GET line... either the H of...
GET / HTTP... or the character after the tradtional "?cmd=", as in...
GET /?cmd=1 HTTP... */
{
cCmnd=cLineBuffer[10];//Yes: INDEX 10 for 11th character.
//Serial.println(cCmnd);
if (cCmnd=='0') {
digitalWrite(bLEDpin,HIGH);
//Serial.println("zero");
}
if (cCmnd=='1') {
digitalWrite(bLEDpin,LOW);
//Serial.println("jeden");
}
}
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
Serial.write(0xFF);
Serial.write(cCmnd);
Serial.write(cCmnd);
Serial.write(0xFF);
Serial.write(cCmnd);
Serial.write(cCmnd);
Serial.write(0xFF);
Serial.write(cCmnd);
Serial.write(cCmnd);
Serial.write(0xFF);
Serial.write(cCmnd);
Serial.write(cCmnd);
delay(1);
// close the connection:
client.stop();
//Serial.println("client disonnected");
}
}
In page in firefox:
br
form METHOD=get action=" "
input type=hidden name="cmd" value="155˙55nput type=submit value="LED On"
55nput type=submit value="LED On">
/form
form METHOD=get action=" "
input t//˙//˙//˙//˙55˙55˙�
input type=submit value="LED Off"
/form
this ".55" is from:
Serial.write(0xFF);
Serial.write(cCmnd);
Serial.write(cCmnd);
cCmd was ascii "5" this time