Hey
Hope you are all well !
So I'm trying to create a simple web server capable of demonstrate my Environmental shield readings ( temp, humidity etc etc). I'm using Arduino MKR WiFi 1010. Could someone please tell what's wrong with the coding? I can't get the reading ( I got an error message). I know I'm asking much but I would really appreciate the help since this is for my final project.
#include <SPI.h>
#include <SD.h>
#include <WiFiNINA.h>
#include <Arduino_MKRENV.h>
#include "arduino_secrets.h"
#define POLL_RATE 2*1000
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int R1 = 1;
int R2 = 2;
int led = LED_BUILTIN;
uint32_t elaps=0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
//while (!Serial);
Serial.println("Access Point Web Server");
pinMode(led, OUTPUT);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while (1);
}
if (!SD.begin(4)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.print("Creating access point named: ");
Serial.println(ssid);
status = WiFi.beginAP(ssid, pass);
if (status != WL_AP_LISTENING) {
Serial.println("Creating access point failed");
// don't continue
while (true);
}
// start the web server on port 80
server.begin();
// you're connected now, so print out the status
printWiFiStatus();
}
void loop() {
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if (status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println("Device connected to AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println("Device disconnected from AP");
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
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
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
String body = readENV();
// the content of the HTTP response follows the header:
client.print(body);
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
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
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /removelog?")) {
SD.remove("datalog.csv");
Serial.println("REMOVING DATALOG");
}
if (currentLine.endsWith("GET /?relay1=high")) {
digitalWrite(R1,HIGH);
}
if (currentLine.endsWith("GET /?relay1=low")) {
digitalWrite(R1,LOW);
}
if (currentLine.endsWith("GET /?relay2=high")) {
digitalWrite(R2,HIGH);
}
if (currentLine.endsWith("GET /?relay2=low")) {
digitalWrite(R2,LOW);
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
if(millis()-elaps > POLL_RATE){
writeLog();
elaps = millis();
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
String readENV(){
float temperature = ENV.readTemperature();
float humidity = ENV.readHumidity();
float pressure = ENV.readPressure()*10;
float illuminance = ENV.readLux();
float uva = ENV.readUVA();
float uvb = ENV.readUVB();
float uvIndex = ENV.readUVIndex();
String html = "";
html += "<meta charset="UTF-8" http-equiv="refresh" content="10;url=http://192.168.4.1/\"><style>";
html += "body {background-color:#22A6A6;}";
html += "h1 {font-family:roboto; color: white;}";
html += "h2 {font-family:roboto;}";
html += "p {font-family:roboto;}";
html += "";
html += "
Environmental Shield Monitor
";html += "
Now:
";html += "
Temperature: ";
html += temperature;
html+= " °C
Umidity: ";
html += humidity;
html += " %
Pressure: ";
html += pressure;
html += " hPa
Illuminance: ";
html += illuminance;
html += " lx
UV A: ";
html += uva;
html += "
UV B: ";
html += uvb;
html += "
UV index: ";
html += uvIndex;
html += "
<form action="/" method="get">Relay 1: <button name= "relay1" type="submit" value="low">OFF<button name= "relay1" type="submit" value="high">ON
";
html += "
Relay 2: <button name= "relay2" type="submit" value="low">OFF<button name= "relay2" type="submit" value="high">ON
html += "<form action="/removelog" method="get"><button type="submit">DELETE LOG";
html += "<p align="center"><img src=""+imgFromSD("img.txt")+"">";
//html += "";
html += "";
return html;
}
void writeLog(){
float temperature = ENV.readTemperature();
float humidity = ENV.readHumidity();
float pressure = ENV.readPressure()*10;
float illuminance = ENV.readLux();
float uva = ENV.readUVA();
float uvb = ENV.readUVB();
float uvIndex = ENV.readUVIndex();
String data = "";
if(!SD.exists("datalog.csv")){
data +="TEMPERATURE, HUMIDITY, PRESSURE, ILLUMINANCE, UVA, UVB, UVINDEX\n";
Serial.println("Header");
}
data += temperature;
data += ",";
data += humidity;
data += ",";
data += pressure;
data += ",";
data += illuminance;
data += ",";
data += uva;
data += ",";
data += uvb;
data += ",";
data += uvIndex;
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile) {
dataFile.println(data);
dataFile.close();
}
Serial.println("Log updated");
}
String imgFromSD(String filename){
String imgStr = "";
File dataFile = SD.open("img.txt");
if (dataFile) {
imgStr = dataFile.readString();
}
Serial.println(imgStr);
return imgStr;
}
By the way I've found this code here : https://create.arduino.cc/projecthub/officine-innesto/expose-your-iot-bundle-kit-info-trough-a-wifi-web-server-1db49c