I have an LED array and I am controlling it through Arduino's web browser using the Arduino Uno WiFi Rev 2. So far I was able to get the lighting control into the web browser, now I am trying to get the sensor readings into the web browser. The data is displayed in the serial monitor, so I am trying to get that over to the web browser. I am wanting to grab only the sensor readings and not the excess information. Any ideas or links that will help?
This is the code for the LED control:
#include <SPI.h>
#include <WiFiNINA.h>
#define led 9
// Arduino does not support connecting to 5G networks.
char ssid[] = "SSID";
char pass[] = "PASSWORD";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
String readString;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
WiFiClient client = server.available();
if (client)
{
Serial.println("new client");
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (readString.length() < 100)
{
readString += c;
Serial.write(c);
if (c == '\n') {
client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
client.println("
");
client.println("
");
client.println("<a href=\"/?lightoff\"\">Turn Off Light</a>
");
delay(1);
if(readString.indexOf("?lighton") > 0)
{
digitalWrite(led, HIGH);
delay(1);
}
else{
if(readString.indexOf("?lightoff") > 0)
{
digitalWrite(led, LOW);
delay(1);
}
}
readString="";
delay(1);
client.stop();
Serial.println("client disonnected");
}
}
}
}
}
}
This is the code for the light sensor:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
//Configures the gain and integration time for the TSL2591
void configureSensor(void)
{
tsl.setGain(TSL2591_GAIN_MED);
tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
Serial.println(F("------------------------------------"));
Serial.print (F("Gain: "));
tsl2591Gain_t gain = tsl.getGain();
switch(gain)
{
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
Serial.print (F("Timing: "));
Serial.print((tsl.getTiming() + 1) * 100, DEC);
Serial.println(F(" ms"));
Serial.println(F("------------------------------------"));
}
void setup(void)
{
Serial.begin(9600);
Serial.println(F("Starting Adafruit TSL2591..."));
if (tsl.begin())
{
Serial.println(F("Found a TSL2591 sensor"));
}
else
{
Serial.println(F("No sensor found ..."));
while (1);
}
configureSensor();
}
// The first "flag" for checking the Lux levels. If the Lux level is less than 500 then luxCheck will be 0.
// This is the yellow light
int luxCheck;
// Reads IR and full spectrum then converts to Lux and checks for less than 500 Lux value
void advancedRead(void)
{
sensors_event_t event;
tsl.getEvent(&event);
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
Serial.println(tsl.calculateLux(full, ir), 6);
if ((event.light < 500))
{
luxCheck = 0;
Serial.println(F("WARNING: LUX NOT WITHIN DESIRED RANGE!"));
Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 2);
}
else if ((event.light > 500))
{
luxCheck = 1;
Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 2);
}
}
// Arduino loop function
void loop(void)
{
advancedRead();
delay(2000);
}