I am quite new to Arduino so maybe this is a simple problem, but I have not been able to find a solution.
I have a Intel Galileo Gen. 2 board. I am trying to write a simple program that offers a web server where, upon request, It returns the value obtained from a sensor (any text would do for my problem). I have succesfully configured the network and obtained the value but every request to the server gets a "It works!" message as a response. I have tried with different browsers (from chromw to lynx) and different nework locations.
This is the code I am using:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x98, 0x4F, 0xEE, 0x05, 0x65, 0x02 };
char SENSORNAME[ ] = "galileo01";
IPAddress ip(192, 168, 15, 177);
IPAddress dnServer(8, 8, 8, 8);
EthernetServer server(80);
int sensorPin = A0; // select the input pin for the potentiometer
int readValue = 0;
int prevValue = 0;
void setup() {
Serial.begin(9600);
system("ifconfig enp0s20f6 down ");
system("ip link set enp0s20f6 name eth0");
system("ifconfig eth0 up");
Ethernet.begin(mac, ip, dnServer);
server.begin();
}
void loop() {
readValue = analogRead(sensorPin);
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.print("{\"sensor\":\"");
client.print(SENSORNAME);
client.print( "\",\"value\":\"");
client.print(readValue);
client.println("\"}");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
What I am doing wrong?
Thanks.
UPDATE:
What I see is that when making the request the loop() function is not handling it. I can see the sensor reading results, but it never goes through the if clause:
EthernetClient client = server.available();
if (client) {
Is there any internal web server? How do I set it off?
I have finally found it. Default system comes with a running web server that was not allowing to set a new server on port 80. Adding this code has solved the isue:
system("systemctl stop lighttpd > /dev/ttyGS0");
system("systemctl stop disable > /dev/ttyGS0");