ESP8266 Weather Station API

IMG_20150622_154639-904x678

This is the story of a failed weather station upgrade attempt. About a week ago, I looked at my main weather station and I made a list of things I wanted to improve upon. Specifically, I wanted to make it smaller, use a smaller solar panel, and incorporate WiFi. Instead of ordering proper instruments like a thermometer, barometer, ect, I decided not to wait, and simply use what I had on hand. I called this prototype “TinyWeatherStation” and got developing.

Power:

IMG_20150615_205957

To power this smaller weather station I decided to use a 5v solar panel I had laying around, and a single cell lithium battery from my old Blackberry. Once again, I decided to use the LM317 variable voltage regulator to charge the battery. I used a 1k ohm and a 10k ohm trim-potentiometer to set the voltage to 4.2 volts. I tested this setup under a kilowatt lamp and the charging current peaked at about 200mA, very good. The quiescent current was 0.7mA, a great improvement over the 5mA of the old weather station.

Brains:

IMG_20150622_010853

The brains of this project would be, once again, an AtMega328. The biggest improvement over the previous weather station was the absence of a secondary linear regulator; the AtMega is powered directly off the battery. Now, this is where things began to go wrong: the headers I used to hold the AtMega were the wrong type. I used these pin headers in the last project, but this time they wouldn’t hold the chip properly. The poor connection somehow increased the quiescent current to 7mA. Bleh.

Instruments:

For the instruments, I used a photoresistor, the DS18B20 thermometer, and one of the pins of the AtMega, which measured the voltage of the battery. Getting the photoresistor working was the toughest part. I used the lux meter on my phone, and compared it directly to the voltage of the photoresistor and a 1 million ohm pull-down resistor. The resulting code looked something like this:

float getLux() {
float lux = analogRead(A1);
if (lux >= 1 && lux <= 17) { lux = 120000; } if (lux == 18) { lux = 110000; } if (lux == 19) { lux = 100000; } if (lux == 20) { lux = 90000; } if (lux == 21) { lux = 80000; } if (lux == 22) { lux = 70000; } if (lux >= 23 && lux <= 24) { lux = 60000; } ... if (lux >= 966 && lux <= 1023) { lux = 3; }
return lux;
}

ESP8266:

IMG_20150621_220027

I actually liked using the ESP8266 module. It was very easy to use; AT commands are easily sent to the module by the AtMega through the SoftwareSerial library. Here’s a diagram of code on the AtMega:

Untitled Diagram (1)

The AT commands would look like this:

AT+RST: Resets the module

AT+CWJAP=”WiFiNetwork”,”password”: Connects to WiFi network

AT+CIPSTART=”TCP”,”192.168.1.42″,80: Connect to 192.168.1.42 via port 80

API

Now… here is where it gets interesting. You can transfer data to a webpage through the URL using GET. Here’s an example URL: http://google.ca/?x=20&y=40&z=30

If you were to load that URL in your browser, it would send a GET request for google.ca, along with the GET variables x, y, and z, where x=20, y=40, and z=30. Essentially, you can transfer variables using a GET request, and the ESP8266 is capable of sending GET requests. I began exploiting this transfer of variables by writing a very simple PHP webpage on my home server. Using the following code, I was able to extract the value for the variable “volt”:

$volt=mysql_real_escape_string($_GET['volt']);
echo $volt;

If I were to load that page with a value of 2 for volt in the URL, I would see the number two on my screen. I was able to use the same principle to transfer data from the ESP8266 chip, to a MySQL database. The source code from my project is available here.

Why the project did not work, and what I learned from it:

The main down-fall of this project was the high battery consumption caused by the AtMega, ESP8266, and LM317 charging circuit. In the future I would use a much more efficient switch-mode power supply/charger, and invest in some low-power chips and better headers for said chips. I did however manage to record some voltage data from my balcony before the lithium battery went kaput:

batteryvoltage

Leave a Reply

Your email address will not be published. Required fields are marked *