Hi,
I'm working on a project using two Vl53l0x sensors and an arduino uno. I have the distance sensors giving me the readings on the serial monitor but my challenge now is to get these readings being displayed on a webpage. I also understand how to display analog I/o pins on a webpage but I'm having a big problem trying to combine the two codes. I have attached my code below. Any help will be highly appreciated. Again my sensor is giving me the readings I need. Also I'm able to display a test code on a webpage using my local ip address. My problem is combining this two codes together to get my outputs on a webpage. I have tried different things but unable to get it to work.
CODE:
#include "Adafruit_VL53L0X.h"
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 0, 0, 4);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
#define VL53L0X_L0X1_I2C_ADDR 0x30
#define VL53L0X_L0X2_I2C_ADDR 0x31
Adafruit_VL53L0X L0X1 = Adafruit_VL53L0X();
Adafruit_VL53L0X L0X2 = Adafruit_VL53L0X();
const byte L1RangeFinderShutdownOutputPin = 8;
const byte L2RangeFinderShutdownOutputPin = 9;
void setup() {
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode (L1RangeFinderShutdownOutputPin, OUTPUT);
pinMode (L2RangeFinderShutdownOutputPin, OUTPUT);
Serial.begin(115200);
// wait until serial port opens for native USB devices
while (! Serial) {
delay(1);
}
digitalWrite (L1RangeFinderShutdownOutputPin,1);
digitalWrite (L2RangeFinderShutdownOutputPin,0);
Serial.println(F("Adafruit VL53L0X L0X1 test"));
if (!L0X1.begin()) {
Serial.println(F("Failed to boot VL53L0X L0X1"));
while(1);
}
L0X1.setAddress(VL53L0X_L0X1_I2C_ADDR);
digitalWrite (L1RangeFinderShutdownOutputPin,1);
digitalWrite (L2RangeFinderShutdownOutputPin,1);
Serial.println("Adafruit VL53L0X L0X2 test");
if (!L0X2.begin()) {
Serial.println(F("Failed to boot VL53L0X L0X2"));
while(1);
}
L0X2.setAddress(VL53L0X_L0X2_I2C_ADDR);
// power
Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// 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) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println("");
VL53L0X_RangingMeasurementData_t L0X1measure;
VL53L0X_RangingMeasurementData_t L0X2measure;
client.print(F("Reading measurementsfrom L0X1 ... "));
L0X1.rangingTest(&L0X1measure, false); // pass in 'true' to get debug data printout!
if (L0X1measure.RangeStatus != 4) { // phase failures have incorrect data
client.print(F("Distance (mm): ")); client.println(L0X1measure.RangeMilliMeter);
} else {
client.println(F(" out of range "));
}
client.print(F("Reading measurementsfrom L0X2 ... "));
L0X2.rangingTest(&L0X2measure, false); // pass in 'true' to get debug data printout!
if (L0X2measure.RangeStatus != 4) { // phase failures have incorrect data
client.print(F("Distance (mm): ")); client.println(L0X2measure.RangeMilliMeter);
} else {
client.println(F(" out of range "));
}
int delta = (L0X1measure.RangeMilliMeter-L0X2measure.RangeMilliMeter);
client.print(F("Distance (mm): ")); client.println(delta);
client.println("");
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;
}
}
}
delay(500);
client.stop();
Serial.println("client disconnected");
}
}