Monday, January 16, 2017

WiFi Temperature Display

Recently my indoor/outdoor thermometer died and I was tired of paying $35 or more for something that would just die again after a year (and was terribly inaccurate to boot!) My solution was to just make a small display that gets the temperature from my personal weather station.

This blog post describes how I built my weather station based on a Raspberry Pi. I added to that software the capability for a network client to connect and request data. My remote display is powered by an ESP8266-01 module which has a processor and a WiFi interface. The -01 version only provides two GPIO pins, but that is enough for an I2C bus interface.

The only complication I ran into is that the LED display requires 5V but the ESP is 3V only. I solved that by powering the display directly from the power adapter and powering the ESP using a voltage adapter. This is the converter that I used, but I don't recommend it. It provides an adjustable output of 0.8 to 20V but is larger than a simple 3V regulator would be. It was also very difficult to adjust accurately. If you use one of these, be sure to adjust the voltage down before connecting it to the ESP or it will kill it.





The other parts I used were pretty straight-forward:
- a 7-segment LED display with I2C backpack https://www.adafruit.com/products/878
- a 6 pin header for connecting a serial interface for programming the device
- a small bit of prototyping board
- a project case from Radio Shack
- an old cell phone charger (very few amps required, so any would work)


Here is a look at the inside and the circuit diagram.



The code could be modified to display any data that you could get from the internet or local network. There is likely a web site (Weather Underground maybe) that will provide real-time weather data by zip code that could be used to display current outdoor temperature.

Here is my code. I used the Adafruit LED backpack library but had to make one change. The GPIO pins used for the I2C bus are hard-coded and these had to change to match the capabilities of the ESP.
This also can serve as a simple socket client program.

#define WIFINAME "Put Your SSID Here"
#define PASSWORD "Put Your Passwor Here"
#define HOSTNAME "IP address of host"
#define HOSTPORT 33333

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <Wire.h>

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_7segment matrix = Adafruit_7segment();

WiFiClient client;


void setup() {
  matrix.begin(0x70);
  matrix.print(0x1111, HEX);
  matrix.writeDisplay();

  WiFi.mode(WIFI_STA);                        // WiFi client mode
  WiFi.begin(WIFINAME,PASSWORD);              // connect to WiFi
  while (WiFi.status() != WL_CONNECTED) {     // wait for wifi connection
    delay(500);
  }  
}


void loop()
{
  unsigned long t1;
  char resp[100];
  char c;
  int i;
  float ot;

  if (client.connect(HOSTNAME, HOSTPORT))
  {
    client.println("ot\n");
    t1 = millis();
    i = 0;
    memset(resp,0,100);
    while (true)
    {
      if (client.available())
      {
        c = client.read();
        if (c<' ') break;
        resp[i] = c;
        i++;
      }
      if (i>98) break;
      if ((millis()-t1)>5000) break;
    }
    ot = atof(resp);
    matrix.print(ot,1);
    matrix.writeDisplay();
  }
  else
  {
    matrix.print(0xEEEE, HEX);   
    matrix.writeDisplay();
  }

  client.stop();
  delay(10000);
}

No comments:

Post a Comment