A Sky Brightness Meter

To follow on from my article regarding setting up the Wemos D1 Mini to work with the Arduino IDE I thought I'd experiment with the idea of a Sky Brightness Meter using a Light Dependent Resistor in a voltage divider with a 1Kohm resistor.

The Wemos board uses an ESP8266 microcontroller that contains built-in Wifi making it the perfect board for a remote device powered with Solar cells and lithium battery. The Wifi connects to a router nearby and sends the sampled Sky value to a web server that displays the sensor value.

The ESP8266 has a single 10 bit A2D input that gives a value from 0 to 1023. By connecting the 1Kohm resistor from the A2D input to ground and the LDR from the A2D input to 3.3Volts we should get a value of '1' in complete darkness and a value of around '600' in bright light. The LDR used in this project is about 200KOhms dark and 600Ohms in bright light.

The LDR sensor should be located inside a plastic tube that limits how much light gets onto the sensor otherwise ambient light could give false readings (from street lighting etc.)

I have been testing using the https://thingspeak.com/channels/581961 website with the soil moisture Arduino code provided by Mathworks.com. We need only add a Wifi AP name, password and the API Key provided from the ThingSpeak account. The sketch uploads every 30 minutes (or 1800 seconds). I recommend setting it to maybe 5 minutes (using a value of 300). The code provides a 'deep sleep' mode for the Arduino to minimise power usage. A link has to be connected on the Wemos board between 'RST' and 'D0' to enable the deep sleep wakeup.

Click HERE to download my modified sketch using a ThingSpeak account. You will need to add your Wifi information and your 'Key' from your own ThingSpeak account.

When the sketch starts it attempts to connect to the specified Wifi network. The serial monitor on the Arduino IDE can be used to monitor progress. The sketch then starts sending Sky Brightness measurements to the website.

The following is the text from the Arduino Serial Monitor window.

Connecting to WiFi
Connecting to WiFi
Sky Brightness = 422
Temperature = 21.00*C
Humidity = 61.00%
Connecting to ThingSpeak for update...
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 2
Connection: close
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: request_method=POST; path=/
X-Request-Id: 990b22c1-7d98-4f50-84ed-b0263ed4531b
X-Runtime: 0.062499
X-Powered-By: Phusion Passenger 4.0.57
Date: Wed, 26 Sep 2018 23:38:43 GMT
Server: nginx/1.9.3 + Phusion Passenger 4.0.57

Goodnight for 300 Seconds

17.09.2018. 05:12

Using The WeMos D1 Mini

Way back in the late 1970's I worked in a computer shop on Bridge Rd in Richmond, Melbourne called 'Silicon Valley' with a ham radio operator, Geoff Atkinson VK3AFA (at that time VK3YFA).

We ran one of the first retail outlets to sell computer and electronics parts. Motorola had the D2 development kit for sale at the time featuring the 6800 microprocessor. This monster had two A4 sized circuit boards connected by a ribbon cable. The Hexadecimal keyboard and the six x seven segment LED displays formed the user input and output. This was my first experience with microcomputers and the wonderful world of programming.

Go forward 40 years and I'm now trying out the WeMos D1 Mini computer that can be programmed using the fantastic Arduino platform.

The D1 Mini uses the ESP8266 processor that includes built-in Wifi and can run off 3 volts. The Micro-USB connector provides 5 volt power as well as the USB interface for uploading programs commonly known as a 'Sketch'.

I thought it might be fun to write an article on how to set up the Arduino software and configure it for the WeMos D1 Mini.

The D1 Mini is a tiny 34mm x 25mm circuit board with 16 power and Input/Output connection pads. The printed Wifi antenna at one end, the micro-USB and reset switch at the other end. Some configurations have the surface mounted parts visible, others have a metal shield.

To get started, if you haven't already, download and install the latest Arduino software on your computer. This is available for Windows, Linux and Apple machines. As of writing this, Version 1.8.7 is available at https://www.arduino.cc/en/Main/Software .

There is also an installation guide available for your particular computer HERE.

Once installed you should be able to start up your Arduino IDE software and get the default sketch page showing in the IDE window. Across the top of the IDE are menu options File Edit Sketch Tools Help. Your default sketch should be named 'sketch_(monthday) | Arduino 1.8.7

Now we need to set up your Arduino IDE to work with the ESP8266 processor on the WeMos board. To do this we need to add the location of the setup information for the D1 Mini. Click on the 'File' menu option and select 'Preferences'. In the box marked as 'Additional Boards Manager URLs' enter the following.
Close the 'Preferences' page by clicking on 'OK'.
Click on the 'Tools' menu and select 'Board' then across to 'Boards Manager'. This should open the 'Boards Manager' window showing all the different boards the Arduino IDE can program. In the search window type 'Wemos'. You should see the ESP8266 board. Click this and select 'Install'. You should then see the installation progress for the ESP8266.

When this is complete close the Boards Manager window and return to the Arduino IDE. Once again select 'Tools', 'Board' and scroll down and click on the Wemos D1 Mini. The Tools menu should now show the Wemos D1 Mini as your selected board.

Ok, time to try our first sketch. The IDE has a built-in library of simple sketches that we can try on our Wemos board. Click on 'File', 'Examples', 'Basics' and select 'Blink'. This simple sketch switches the on-board LED on and off once a second.

We now need to compile our 'Blink' sketch. Click on the 'Sketch' menu and select 'Verify/Compile'. The compiler should report a successful compile of the sketch after a few seconds.

If you look through the code for this sketch you'll notice the documentation section at the top. Good coding practice requires the programmer to write a short description of the software. Descriptive lines should start with a '//' whereas blocks of text should start with a '/*' and end with a '*/' to tell the compiler where the start and finish is.

The two main code blocks are the 'void setup()' and the 'void loop()'. The 'setup' code runs only once at the start and the 'loop' code runs continuously. You should now be able to plug in your WeMos D1 Mini using a USB to Mini-USB cable ready to upload your sketch. With the WeMos connected, select 'Sketch', 'Upload' and you should see the sketch code upload in the monitor window at the bottom of the screen.

Leave the D1 Mini connected, press the reset button on the board and the LED on the board should start blinking. Sketches can use the Micro-USB connection as a serial monitor if you want to get messages from the board. This is under the 'Tools', 'Serial Monitor' option.

Try some of the other sketches in 'File', 'Examples' and see how they compile. Some may have particular requirements depending on how they are written. Explore the other menu options and enjoy using the Arduino IDE.

Cheers, Rob.

13.09.2018. 08:44