Hello. I have a very old Netbook that I plan to use as a desktop clock and weather station. It will display a local html page that will get weather readings in json format from different sensors (outside and indoors) and have js scripts manipulate that data. I base my sketch on Ethernet library server example and on ArduinoJson library example.
//v 0.2
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
#include <AM2320.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE0};
IPAddress ip(192, 168, 1, 150);
EthernetServer server(80);
AM2320 thermometer;
void setup() {
Ethernet.init(10); // Most Arduino shields
// Initialize serial port
Serial.begin(9600);
while (!Serial) continue;
Ethernet.begin(mac, ip);
// Start to listen
server.begin();
delay(1000); //waiting for server to set up
Serial.println(F("Server is ready."));
Serial.print(F("Please connect to http://"));
Serial.println(Ethernet.localIP());
}
void loop() {
//runs++;
float myTemp = thermometer.t;
float myHumidity = thermometer.h;
// Wait for an incoming connection
EthernetClient client = server.available();
if (client) {
Serial.println("Got a client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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' && currentLineIsBlank) {
StaticJsonDocument<200> doc;
doc["spot"] = "outside";
doc["temperature"] = myTemp;
doc["humidity"] = myHumidity;
//Serial.println("step1");
//Serial.print("Sending: ");
serializeJson(doc, Serial);
Serial.println();
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Origin: *");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println("Refresh: 10");
client.print("Content-Length: ");
client.println(measureJsonPretty(doc));
client.println();
// Write JSON document
serializeJsonPretty(doc, client);
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(10);
// close the connection:
Serial.println("DEBUG1");
client.stop();
Serial.println("DEBUG2");
}
}
With the above code Arduino ALWAYS pings but many times visiting 192.168.1.150 through any browser (for debugging) gives me "Can't connect".
Any thoughts?