Wireless beer fridge temperature logger

The forum for discussing all kinds of brewing paraphernalia.
Post Reply
User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Wed Jul 22, 2015 5:38 pm

Just set the devkit board up. Plugged it into the PC and it was recognised as a serial device. Uploaded my temp monitor sketch and it worked first time :)
Dan!

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Thu Jul 23, 2015 1:27 pm

Here's my data logger sketch.

Code: Select all

// Import required libraries
#include <ESP8266WiFi.h>
#include "DHT.h"

// WiFi parameter variables
char ssid[50];
char password[20];

// thingspeak API variable for data logging
char apiKey[20];

const int pollTime = 30000; // How often do we poll for data (ms)
const int networks=1; // Number of WiFi networks known

// WiFi network parameters
// One row per known WiFi network
// For each network
//  column 1 == SSID
//  column 2 == Wifi password
//  column 3 == thingspeak API key to use when logging data

const String wifiNetworks[networks][3]  = 
  { 
    { "YOUR SSID",  "YOUR WIFI PASSWORD",   "YOUR THINGSPEAK API CODE"},  
  };


// Data logging host
const char* host  = "api.thingspeak.com";

// URL info
const String urlPrefix  = String ("GET /update?api_key=");

const String urlSuffix  = String ( " HTTP/1.1\r\n" ) +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n";

// GPIO pin where the DHT sensor data line is connected to
#define DHTPIN 4

// Sensor type, comment in/out as appropriate
//#define DHTTYPE DHT22
#define DHTTYPE DHT11

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);


void setup() {
  
  // Start Serial
  Serial.begin(115200);
  delay(20);
  Serial.println ( "\n**********************************" );
  Serial.println ( "Rusty Sprocket Temperature Monitor" );
  Serial.println ( "Andy Whitworth 2015" );
  Serial.println ( "**********************************" );

  // We start by connecting to a WiFi network
  connectWiFi();

  // Init DHT sensor
  dht.begin();

}

void loop() {
  static int retries = 0;
  static int polls = 0;
  const int httpPort = 80;

  Serial.print("Connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;

  // Connect to server
  if (!client.connect(host, httpPort)) {

    // Could not connect
    Serial.println("Connection failed");

    // Are we still connected to Wifi ?
    if ( WiFi.status() != WL_CONNECTED ) {
      // Nope, try and reconnect
      connectWiFi();
    } else {
      // Still connected, wait a while before attempting to repeat server connection
      if ( retries++ > 60 ) {
        // Tried for a minute so let's reboot the module and start over
        ESP.restart();
      }
      delay ( 1000 );
      
    }
    return;
  }

  retries = 0;
  
  // Reading temperature and humidity
  // DHT11 will give resolution of 1 degree and accuracy +/- 2 degC
  // DHT22 will give resolution of 0.1 degree and accuracy +/- 0.5 degC
  
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  String humidityStr = String(h);
  String tempStr = String(t);
  String uptime = String ( round(millis()/1000));
  
  Serial.print ( "Sending data\nHumidity: " + humidityStr + "\nTemperature: " + tempStr + "\nUptime: " + uptime + " s\n");

    // This will send the request to the server
  client.print(urlPrefix + apiKey + "&field1=" + tempStr + "&field2=" + humidityStr + "&field3=" + polls++ + "&field4=" + uptime + urlSuffix);               
  delay(20);
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
  client.stop();
  
  // Sleep until next reading is due
  delay(pollTime);
 
}


// Function to connect to Wifi

void connectWiFi () {
  int attempts = 0;
  Serial.println();
  Serial.println();

  // Locate a recognised WiFi network
  while ( !findNetwork() ) {
    delay ( 1000 );
    if ( attempts++ > 60 ) {
      // 1 minute and no Wifi network found, if in doubt, Reboot!
      Serial.println ( "Could not find a known network, rebooting...." );
      ESP.restart();
    }    
  }

  attempts = 0;

  // Network found, now connect to it
  Serial.print("Connecting to network : ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
    if ( attempts++ > 60 ) {
      // 1 minute and no Wifi reconnect, if in doubt, Reboot!
      Serial.println ( "Could not connect, rebooting...." );
      ESP.restart();
    }
  }
  Serial.println("\nWiFi connected");  
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  
}



boolean findNetwork ()
{
  boolean selected=false;
  
  // scan for nearby networks:
  Serial.println("** Scanning Networks **");
  int numSsid = WiFi.scanNetworks();
  if (numSsid == -1)
  { 
    Serial.println("No Wifi networks found");
    return selected;
  } 

  // print the list of networks seen:
  Serial.print("Number of available networks: ");
  Serial.println(numSsid);

  Serial.println ( "Network list" );

  // print the network number and name for each network found:
  for (int thisNet = 0; thisNet<numSsid; thisNet++) {
    Serial.print(thisNet);
    Serial.print(") SSID: ");
    Serial.print(WiFi.SSID(thisNet));
    displayEncryptionType(WiFi.encryptionType(thisNet));
    Serial.print("\tSignal: ");
    Serial.print(WiFi.RSSI(thisNet));
    Serial.println(" dBm");
    for ( int i=0; i< networks && !selected ; i++ ) {
      if ( wifiNetworks[i][0]==WiFi.SSID(thisNet)) {
        wifiNetworks[i][0].toCharArray ( ssid,50);
        wifiNetworks[i][1].toCharArray ( password,20);
        wifiNetworks[i][2].toCharArray ( apiKey,20);
        selected=true;
        break;
      }
    }
  }
  if ( selected ) {
    Serial.println ( "\nRecognised Network SSID: " + String ( ssid ) );
  } else {
    Serial.println ( "\nNo recognised network found" );
  }
  return selected;
}


void displayEncryptionType ( byte encType )
{
   Serial.print ( "\tEncryption: " );
  switch ( encType ) {
    case 2:
        Serial.print ( "TKIP (WPA)");
        break;
    case 4:
        Serial.print ( "CCMP (WPA)");
        break;
    case 5:
        Serial.print ( "WEP");
        break;
    case 7:
        Serial.print ( "NONE");   
        break;
    case 8:
        Serial.print ( "AUTO");   
        break;
    default:
      Serial.print ( "Unknown");   
  }
}


You need to change the following to suit your environment:-

Code: Select all

const String wifiNetworks[networks][3]  = 
  { 
    { "YOUR WIFI SSID",  "YOUR WIFI PASSWORD",   "YOUR THINGSPEAK API CODE"},  
  };

Code: Select all

// GPIO pin where the DHT sensor data line is connected to
#define DHTPIN 4

// Sensor type, comment in/out as appropriate
//#define DHTTYPE DHT22
#define DHTTYPE DHT11

And it relies on you having a thingspeak.com account setup with a channel to log the data to, the channel needs to have 4 fields enabled:-

Field 1 = Temp degC
Field 2 = Humidity %
Field 3 = Number of temperature polls since uptime
Field 4 = Uptime in seconds

Code is rough and ready but does the job.

You'll need to install a DHT library to read the sensor. I got mine from here https://codeload.github.com/adafruit/DH ... zip/master

All the above assumes you're using the Arduino IDE with the ESP8266 capability added. Instructions here https://github.com/esp8266/Arduino

Here's the URL for my channel, sensor is currently in the house not the beerfridge!

https://thingspeak.com/channels/46252
As you can see the DHT11 fluctuates quite a bit, its also only got a resolution of 1degC and accuracy of 2degC. The DHT22 is much better and that's what I'd recommend, its a little more expensive but worth it.

Enjoy!
Dan!

Fil
Telling imaginary friend stories
Posts: 5229
Joined: Sun Oct 16, 2011 1:49 pm
Location: Cowley, Oxford

Re: Wireless beer fridge temperature logger

Post by Fil » Thu Jul 23, 2015 6:53 pm

Thanks.... took less than 10 mins to replicate your project following the clear steps..


=D>
ist update for months n months..
Fermnting: not a lot..
Conditioning: nowt
Maturing: Challenger smash, and a kit lager
Drinking: dry one minikeg left in the store
Coming Soon Lots planned for the near future nowt for the immediate :(

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Thu Jul 23, 2015 7:11 pm

Excellent!
Dan!

Belter

Re: Wireless beer fridge temperature logger

Post by Belter » Sat Jul 25, 2015 12:14 pm

Ok I had some spare time today and had a play. Got the ESP866 development board to compile. I have followed all steps and set up a things speak account.

The only thing I can't do is work out how to define pin DHT 11.

You've said:

// GPIO pin where the DHT sensor data line is connected to
#define DHTPIN 4

// Sensor type, comment in/out as appropriate
//#define DHTTYPE DHT22
#define DHTTYPE DHT11

I'm assuming my DHTPIN isn't pin 4?

edit: (I'm using the Supplied Blue plug in DHT 11 in the PINs called DHT 11)

Belter

Re: Wireless beer fridge temperature logger

Post by Belter » Sat Jul 25, 2015 12:22 pm

I've just changed the PIN name to DHT11, it verify's correctly but now wont upload and says:

"sketch uses 300,680 bytes (69%) of program storage space. Maximum is 434,160 bytes.
Global variables use 49,164 bytes (60%) of dynamic memory, leaving 32,756 bytes for local variables. Maximum is 81,920 bytes.
error: cannot access COM1

error: espcomm_open failed"

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Sat Jul 25, 2015 12:26 pm

In the IDE menu see which com ports are available when the device is plugged in and try selecting one of those. Mine is usually com3 or com4.


And pin4 is correct for that Dev board. Make sure you've plugged the sensor in with the grill side facing into the board NOT outwards
Dan!

Belter

Re: Wireless beer fridge temperature logger

Post by Belter » Sat Jul 25, 2015 12:38 pm

Image
uploading images

is this the Port menu you mean?

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Sat Jul 25, 2015 12:45 pm

Yup. I take it you're not on Windows ?
Dan!

Belter

Re: Wireless beer fridge temperature logger

Post by Belter » Sat Jul 25, 2015 12:47 pm

Nope :S


looks to me that the board isn't being recognised? ??

It did initially. Uploaded fine. Now nothing.

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Sat Jul 25, 2015 1:03 pm

Try googling for arduino ide serial. And the OS you're using - Mac?
Dan!

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Sat Jul 25, 2015 1:05 pm

Ah! If you've done one successful upload then the chip will start running the sketch and won't take any more uploads. Turn the board off/on and it will revert back into "waiting for upload" mode, assuming that you've still got the dip switch set to programming mode.
Dan!

Belter

Re: Wireless beer fridge temperature logger

Post by Belter » Sat Jul 25, 2015 1:10 pm

dip switch? Sorry I'm hugely inexperienced. I have dip switches 1 to 8. 7 is set to on.

User avatar
Andy
Virtually comatose but still standing
Posts: 8716
Joined: Fri Nov 18, 2005 1:00 pm
Location: Ash, Surrey
Contact:

Re: Wireless beer fridge temperature logger

Post by Andy » Sat Jul 25, 2015 1:21 pm

Set 8 on when you want to program it then turn device off/on and try programming. Open up the serial monitor before you hit the upload button. If everything works then the sketch will compile, upload and run. You should then see info in the serial monitor window.
Dan!

Belter

Re: Wireless beer fridge temperature logger

Post by Belter » Sat Jul 25, 2015 1:22 pm

Ok i found the link you linked to back a page. This is making some more sense but nothing I have tried so far has worked. I'll keep reading.

Post Reply