Skip to content

Instantly share code, notes, and snippets.

@wouterds
Created January 28, 2019 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wouterds/3d4a1f91880ff7de4f4492ff1552b46f to your computer and use it in GitHub Desktop.
Save wouterds/3d4a1f91880ff7de4f4492ff1552b46f to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <ESP8266WebServer.h>
#include <TSL2561.h>
const char *ssid = "Wouter's Place";
const char *password = "";
ESP8266WebServer server(80);
TSL2561 tsl2561(TSL2561_ADDR_FLOAT);
void handleRoot() {
// Read illuminance
float visible = tsl2561.getLuminosity(TSL2561_VISIBLE);
float full = tsl2561.getLuminosity(TSL2561_FULLSPECTRUM);
float ir = tsl2561.getLuminosity(TSL2561_INFRARED);
String response = "";
response += "{\"illuminance\":{";
response += "\"visible\":";
response += visible;
response += ",\"full\":";
response += full;
response += ",\"ir\":";
response += ir;
response += "}";
response += "}";
server.send(200, "application/json", response);
}
void setup(void)
{
Wire.begin(0, 2);
Serial.begin(9600);
Serial.println();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to wifi, ip: ");
Serial.println(WiFi.localIP());
Serial.println("Setting up TSL2561..");
if (!tsl2561.begin())
{
Serial.println("Could not find TSL2561 sensor, check wiring!");
while (1);
}
Serial.println("Starting server..");
server.on("/", handleRoot);
server.begin();
Serial.println("Web server running!");
}
void loop()
{
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment