Adafruit VCNL 4010 proximity sensor in lieu of switch for WiFi LED via MQTT

I thought I would share my project:

/*This sketch was designed by Nick Sebring December 9th, 2017.
 * Connect to Wifi via Wemos D-1 Mini
 * Display Wifi Connection
 * Display IP address assigned by router
 * Display MQTT Connection
 * Get Adafruit VCNL 4010 proximity sensor readings to turn on/off led via MQTT
 * Without any human interaction except for placing a chart in the bin.

/*Wiring diagram
 * 3V      VIN
 * G       GND
 * D2      SDA
 * D1      SCL
 * D5      LED (Dont forget the resistor!)

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include "Adafruit_VCNL4010.h"

const char* ssid = "MYSSID";
const char* password = "MYPASSWORD";
const char* mqtt_server = "MQTT.IP.ADDRESS";

WiFiClient espClient;
PubSubClient client(espClient);

boolean reconnect() {  // **********************************************************
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println (F("Contacting MQTT server..."));
    // Attempt to connect
    if (client.connect("PSChartSensor")) {  //assign a "client name".  Each wemos must have a unique name
      Serial.println (F("connected"));

      return client.connected();
      Serial.print (F("Failed to connect. "));
      Serial.println (F(" Attempting connection again in 3 seconds"));
      // Wait 3 seconds before retrying
      //      delay(3000);
      return 0;
const int ledPin = D5;// for visual that signal was sent

Adafruit_VCNL4010 vcnl;

void setup()
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);// Set led off at start-up

    client.setServer(mqtt_server, 1883);
  // Connect to WiFinetwork
  Serial.print (F("Connecting to "));
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print (F("."));
  Serial.println (F(""));
  Serial.println (F("WiFi connected"));
  //  Print the IP address
  Serial.print (F("Local IP: "));

  Serial.println("VCNL4010 test");
  if (! vcnl.begin()) {
    Serial.println("Sensor not found :(");
    while (1);
  Serial.println("Found VCNL4010");


void loop()

  if (!client.connected())

  if (vcnl.readProximity() > 2150)//2125 seems to be ideal without plastic cover
    client.publish("Charts/PS", "0");
    Serial.println (F("Charts Up!"));
    digitalWrite(ledPin, HIGH);
    client.publish("Charts/PS", "1");
    Serial.println (F("No Charts!"));
    digitalWrite(ledPin, LOW);//

I need some expert advice.

I finally had time to convert this from breadboard to soldering it all together. Before I put it into a tiny project box, I wanted to test it out.

The VCNL Proximity sensor sits just on top of the metal box of the Wemos D1-mini with a small gap to ensure no shorts.

On the breadboard, It works great. Soldered together.... When I plug it in, both red and green light come on and I have to reboot the Wemos several times before it logs into the wifi and connects to the MQTT broker. Once that occurs, it works great. Any suggestions why I would have to reboot MANY times before it connects? Could the VCNL practically piggybacking the Wemos interfere with the onboard antenna? Maybe, but then why does the red and green LED come on at bootup and remain on?

In a hurry to leave work. I accidentally left it on my desk. I will post a picture tomorrow.


If your Wemos is in a metal box your going to ruin it's wifi signal range and depending on the size of the metal box maybe rf reflections are adding to the problem

Thank you for the reply. It’s going in a thin plastic box. Right now it’s just the wemos and sensor powered via usb. Bringing my laptop to work to see if I can diagnose what’s going on. I may have to desolder and reconfigure how I have the sensor connected. I want it 100% before I insert it into the plastic box and deploy it as a finished project.

Maybe you misunderstood. The Wemos D1-mini has a small metal box on it. I think it’s an RF shield. The sensor is about the same size. So I mounted it just above it with a small gap.

Here’s the pictures of my setup.



OK... Desoldered, turned the sensor sideways, gave it a bit more air gap and ditched the LEDs.
Connects to the WiFi and MQTT server promptly and starts publishing remote LED "on" or "off".
For now: I couldn't be happier. Here's the updated pics.




Installed into "project box".



Maybe you misunderstood. The Wemos D1-mini has a small metal box on it. I think it's an RF shield. The sensor is about the same size. So I mounted it just above it with a small gap.

Sorry, yes I did misunderstand the bit about a metal box but from your subsequent posts and images the point about low quality wifi signal still stood as you had the LED's projecting under the antenna. I see you have now removed them and things are working okay.

I only added the LEDs because I was having connectivity issues and wanted a visual que of what’s happening. Now that those issues are resolved, i may just leave them out.