Hi all!
I have a code at the moment which is measuring different sensors, and sending the results to an app which I have made.
I am doing this by creating a HTML web page with the data on it. However, my data, which sends every 3 seconds, only works 6-7 times before i get a consistent error- that my HTML page is not available.
Wondering if it has something to do with my code? Any suggestions
#define DHTPIN 11
#include "DHT.h"
#define DHTTYPE DHT22
#include <SPI.h> //try??? idk
#include <WiFiNINA.h>
int soilPin = A0;
int tempPin = 11;
int lightPin = A2;
char ssid[] = "iiNet580D53"; // your network SSID (name)
char pass[] = "hY2KfZcCNW"; // your network password
// int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiServer server(80); //declare server object and spedify port, 80 is port used for internet
int status = WL_IDLE_STATUS; //status of wifi
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(soilPin, INPUT);
pinMode(tempPin, INPUT);
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
Serial.println(WiFi.localIP());
delay(5000);
}
server.begin();
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
if (c == '\n') { // if the byte is a newline character
if (currentLine.length() == 0) {
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("<meta http-equiv=\"refresh\" content=\"5\">");
float h = dht.readHumidity();
float t = dht.readTemperature();
client.print("Humidity: ");
client.print(h);
client.println(" %\t");
client.print("
");
client.print("Temperature: ");
client.print(t-4);
client.println(" *C ");
client.print("
");
client.print("Soil Moisture: ");
client.print(analogRead(soilPin));
client.print("
");
client.print("Light Resistance: ");
client.println(analogRead(lightPin));
client.print("Ohms");
delay(3000);
// client.println("
"); // idk
// client.println("</html>"); // idk
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
}
}