Hi there,
I used the Web example to communicate with the Arduino board. It works fine for analog output, like external LED and Servo-Motor signals.
Now I tried to read out the signal from an ambient light sensor ( sparkfun). It also works fine, printing out in serial.
But once a server request is coming in, the light sensor is gone.
Output
setup()
IP Address: 192.168.178.46
signal strength (RSSI):-56 dBm
Ready to sense some light!
Ambient Light Reading: 7 Lux << perfect!
Reading settings...
Gain: 0.125 Integration Time: 100
First web request in loop():
new client
GET /ledAn HTTP/1.1
Accept: /
Host: arduino1
User-Agent: blah
Test 'light.begin() '
Could not communicate with the sensor!
Ambient Light Reading: 255 Lux << bad
client disconnected
What is happening? How is the light sensor disabled by a web request? What errors did I make?
Please help.
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
Serial.println("new client");
String currentLine = ""; // make a String to hold incoming data from the client
if (client.connected()) {
int bytes = client.available();
if (bytes > 0) {
// read the whole http request
String request = "";
for (int i = 0; i < bytes; i++) {
char c = client.read();
request += c;
}
// print the http request
Serial.println(request);
if (request.indexOf("ledAn") != -1) { digitalWrite(ledPin, HIGH); };
if (request.indexOf("ledAus") != -1) { digitalWrite(ledPin, LOW); };
if (request.indexOf("motor") != -1) { motor(); };
if (request.indexOf("drueck") != -1) { knopfdruecken(); };
}
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
};
if (light.begin())
Serial.println("Ready to sense some light!");
else
Serial.println("Could not communicate with the sensor!");
luxVal = light.readLight();
client.print("Ambient Light Reading: ");
client.print(luxVal);
client.println(" Lux");
client.println(luxVal);
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
Hopefully it will help.
#include <Wire.h>
#include "WiFiS3.h"
#include <Servo.h>
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h"
#define AL_ADDR 0x48
int keyIndex = 0; // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
const int ledPin = 13;
WiFiServer server(80);
SparkFun_Ambient_Light light(AL_ADDR);
// Possible values: .125, .25, 1, 2
// Both .125 and .25 should be used in most cases except darker rooms.
// A gain of 2 should only be used if the sensor will be covered by a dark
// glass.
float gain = .125;
// Possible integration times in milliseconds: 800, 400, 200, 100, 50, 25
// Higher times give higher resolutions and should be used in darker light.
int zeit = 800;
long luxVal = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Wire.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true)
;
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
Serial.println("Motor");
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(0);
Serial.println("Light");
if (light.begin())
Serial.println("Ready to sense some light!");
else
Serial.println("Could not communicate with the sensor!");
luxVal = light.readLight();
Serial.print("Ambient Light Reading: ");
Serial.print(luxVal);
Serial.println(" Lux");
Serial.println(luxVal);
light.setGain(gain);
light.setIntegTime(zeit);
Serial.println("Reading settings...");
Serial.print("Gain: ");
float gainVal = light.readGain();
Serial.print(gainVal, 3);
Serial.print(" Integration Time: ");
int timeVal = light.readIntegTime();
Serial.println(timeVal);
}
/* this works
void loop() {
luxVal = light.readLight();
Serial.print("Ambient Light Reading: ");
Serial.print(luxVal);
Serial.println(" Lux");
delay(1000);
}
*/
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
Serial.println("new client");
String currentLine = ""; // make a String to hold incoming data from the client
if (client.connected()) {
int bytes = client.available();
if (bytes > 0) {
// read the whole http request
String request = "";
for (int i = 0; i < bytes; i++) {
char c = client.read();
request += c;
}
// print the http request
Serial.println(request);
if (request.indexOf("ledAn") != -1) { digitalWrite(ledPin, HIGH); };
if (request.indexOf("ledAus") != -1) { digitalWrite(ledPin, LOW); };
}
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
};
if (light.begin())
Serial.println("Ready to sense some light!");
else
Serial.println("Could not communicate with the sensor!");
luxVal = light.readLight();
client.print("Ambient Light Reading: ");
client.print(luxVal);
client.println(" Lux");
client.println(luxVal);
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Here the whole picture -
To the right, the request
Below the answer, with 'Ambient Light Reading: Lux' 225, which means failure.
Serial output shows first successful sensor reading, but after the request the 'could not communicate' message.
May be helpful, if this part is invoked, the Sensor gives a '0' in the setup
/* while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Found a 'solution' by myself. However, the code is fragile, as the web server implementation seems to be. For example, when I change client.println to client.print, it stops working. Additionally, not everything comes through.
But so far I can use it as input for the solar energy logic.
Here is the code:
/*
WiFi Web Server
Find the full UNO R4 WiFi Network documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-web-server
*/
#include <Wire.h>
#include <WiFiS3.h>
#include "/home/me/Desktop/Arduino/Test/WiFiWebServer/arduino_secrets.h"
#include <SparkFun_VEML6030_Ambient_Light_Sensor.h>
#define AL_ADDR 0x48
int status = WL_IDLE_STATUS;
WiFiServer server(80);
SparkFun_Ambient_Light light(AL_ADDR);
// Possible values: .125, .25, 1, 2
// Both .125 and .25 should be used in most cases except darker rooms.
// A gain of 2 should only be used if the sensor will be covered by a dark
// glass.
float gain = .125;
// Possible integration times in milliseconds: 800, 400, 200, 100, 50, 25
// Higher times give higher resolutions and should be used in darker light.
int zeit = 800;
long luxVal = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Wire.begin();
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true)
;
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
Serial.println("Light");
if (light.begin())
Serial.println("Ready to sense some light!");
else
Serial.println("Could not communicate with the sensor!");
luxVal = light.readLight();
delay(1000);
Serial.print("Ambient Light Reading: ");
Serial.println(" Lux");
Serial.println(luxVal);
light.setGain(gain);
light.setIntegTime(zeit);
Serial.println("Reading settings...");
Serial.print("Gain: ");
float gainVal = light.readGain();
Serial.print(gainVal, 3);
Serial.print(" Integration Time: ");
int timeVal = light.readIntegTime();
Serial.println(timeVal);
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
Serial.println("new client");
if (client.connected()) {
int bytes = client.available();
if (bytes > 0) {
String request = "";
for (int i = 0; i < bytes; i++) {
char c = client.read();
request += c;
}
delay(1000);
if (light.begin())
Serial.println("Ready to sense some light!");
else
Serial.println("Could not communicate with the sensor!");
luxVal = light.readLight();
client.println(luxVal);
};
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
After flashing the firmware, it stopped working ..