This code is specifically developed for the Witty cloud development board, but can for sure be modified to be used on any other ESP8266 module. The Witty cloud development board includes a photo resistor which can be used to measure the light intensity of a room or environment.

The code measures the light intensity 6 times and average the value, easy and quick little fix(but for sure not proper the way) to eliminate any spurious noise from the readings of so-called light sensor. It utilises an MQTT client to report/publish the gathered result to anyone who is interested, through an MQTT broker, more on this. One use case could be to store the information for later use in a database. For more information on how utilise this data.

After having published the current values of the light intensity, the ESP-12F module is into deep sleep, a state in which the module consumes much less energy, for 8 seconds, only to reboot and do the same procedure again. Just remember to connect D0/GPIO16 with RST/REST on the development board, otherwise the module won’t boot by itself again, before the power is cut or RST button is pressed.

Connect these two

The code allows for Over-the-Air(OTA) updates, at the beginning of each loop, it is checked if any updates are available for the device, meaning someone is currently trying to broadcast an update to the device.

Last thing be sure to either set your environment variable PLATFORMIO_BUILD_FLAGS OR the build_flag in the `platformio.ini file OR change the defines in the code.

main.cpp

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <LightSensor.h>
#include <ArduinoOTA.h>

#ifndef WIFI_SSID
#define WIFI_SSID "ReplaceThis"
#endif
#ifndef WIFI_PASS
#define WIFI_PASS "ReplaceThis"
#endif
#ifndef MQTT_SERVER
#define MQTT_SERVER "m21.cloudmqtt.com"
#endif
#ifndef MQTT_USER
#define MQTT_USER "ReplaceThis"
#endif
#ifndef MQTT_PASS
#define MQTT_PASS "ReplaceThis"
#endif
#ifndef MQTT_PORT
#define MQTT_PORT 15739 // MQTT_PORT default for me
#endif
#ifndef MQTT_TOPIC
#define MQTT_TOPIC "lightingLivingRoom" // MQTT_TOPIC default for me
#endif

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("alive", "hello world from ESP8266Client1");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup_wifi() {
  WiFi.mode(WIFI_STA);
  WiFi.setPhyMode(WIFI_PHY_MODE_11N); // Low power
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);

  // Hostname defaults to esp8266-[ChipID]
  //ArduinoOTA.setHostname("ESP8266-WITTY-RGB");

  // No authentication by default
  //ArduinoOTA.setPassword((const char *)"admin");

  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("OTA Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup(){
  Serial.begin(115200);
  Serial.println("Booting");
  setup_wifi();
  client.setServer(MQTT_SERVER, MQTT_PORT);
  client.setCallback(callback);
}

void loop(){
  ArduinoOTA.handle();
  if (!client.connected()){
    reconnect();
  }
  client.loop();

  int value = LightSensor.getAveragedValueTimes(6);
  snprintf (msg, 75, "device: %ld lighting: %ld", "0", value);
  Serial.print("Publish message: ");
  Serial.println(msg);
  client.publish(MQTT_TOPIC, msg);

  Serial.println("Going to sleep for 8 sec");
  ESP.deepSleep(1000000*8); // 8 seconds deepsleep
  Serial.println("Sleeping"); // Should not be reached and printed
}

Some resources: