As you could see in the previous entry of the blog light-landing, although it worked fairly well, in certain hours of light, the voltage of the resistive divider that went to the base of the transistor oscillated too much causing the transistor to turn on and off quickly. I put a condenser to eat those oscillations, but obviously, the response became much slower. That is why I considered making a second version. I was doing some tests with Schmitt trigger circuits but I did not agree too much on the result.
In May I made a fairly large purchase of components to China where several ESP8266 modules came in several versions: ESP01, ESP07 and nodeMCU (ESP12E). So since the IoT is fashionable, I started to develop the light sensor again to implement it with these chips. The ESP8266 are a pass, and being able to provide WiFi connectivity to your projects is great.
Given the limited access to the pins that there are (nothing more than GPIO0 and GPIO2), I have seen this module mostly used as a serial peripheral in an arduino controlled project, but for simple sensor nodes it is also valid.
In my case these two pins are just: one for the PIR and one for the Relay. At first I regretted not having access to a third pin that was also the one that has AD converter, to be able to read the value of the LDR, however in the end it has not been a problem.
In one of the many examples that I saw of ESP8266 on the internet, I found a code to request the date and time in UTC to a server, through http. And I started working on that code.
The operation of this version is much more complex but I consider it also more reliable. Knowing the time and date and just needed to know the hours of sunrise and sunset to assess when it is night. Searching the network I found this wonderful API api.sunrise-sunset.org which calculates, in UTC, these hours for a given latitude and longitude. This was the part where I got stuck most, because it was the first time I saw that I was working with web requests, JSON and so on. I tried the ArduinoJson library to decode this one, but it gave me a problem that I did not understand, so for 2 fields that I needed from that JSON, I decoded it by hand.
The grosso of the code takes it to determine if it is night or not, since depending on whether it is summer time or not, in Spain it is GMT +2 or GMT +1, the summer schedule is not the same in each year , the leap years … and other considerations.
In turn, I have implemented that when the PIR skips, the ESP sends a UDP command to another device, which will show in the next entries. For the moment dealing with SQL and PHP to interact with servers and databases is unknown to me, so I will gradually improve the WiFi devices that I have at home.