Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMcE committed Jun 13, 2024
1 parent d81c00d commit b8ba204
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
credentials.h
45 changes: 45 additions & 0 deletions communications.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*************************************
* setup wifi communications Nodemcu
*************************************/

#include "credentials.h"

bool connect_wifi()
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, pass);

// Loops for no more than 10 * (.25 + .25) = 10 seconds.
int loop_count = 0;
while((WiFi.status() != WL_CONNECTED) && (loop_count++ < 20)) {
digitalWrite(LED_BUILTIN, LOW); // Flutter the LED on while we try to connect.
Serial.print("*");
delay(250);
digitalWrite(LED_BUILTIN, HIGH); // Flutter the LED on while we try to connect.
delay(250);
}
if (loop_count >= 20) {
Serial.println("\nFailed to connect, will try again later.");
return false;
}

Serial.println("\nConnected.");
delay(1000);
return true;
}

/* Wrapper for an object method */
long get_rssi() {
return WiFi.RSSI();
}

/***********************************
* Send data to Thingspeak.
***********************************/
void thingspeakPostData(){
Serial.println("thingspeaksenddata started");
int result = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.print("thingspeaksenddata completed, result was ");
Serial.println(result);
}
17 changes: 17 additions & 0 deletions credentials_example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Credentials example file.
Copy this file to credentials.h and substitute your actual values
and then restart your Arduino IDE to load the file as an editor tab.
The variables needed to connect to wifi and push to ThingSpeak.
*/

// Network used to post data to ThingSpeak.
char ssid[] = "YourWifiSSID";
char pass[] = "YourWifiPassword";

// Thingspeak credentials.
unsigned long myChannelNumber = 1059466;
// You can find this in your channel API Keys tab.
const char * myWriteAPIKey = "CHANNEL_WRITE_API_KEY";
80 changes: 80 additions & 0 deletions dht22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "dht22.h"


// Number of times to re-poll the dht for temperature on error.
int temperature_retries = 5;

/**
* Takes Digitial pin index to set up connection.
*/
Dht::Dht() {}

void Dht::sensorPin(int sensor_pin) {
this->setup(sensor_pin, DHTesp::DHT22);
}

/*
* Record the current values, since everything works from them.
*
* If non-number comes back, wait 1 second and retry, up to 5 times.
*/
void Dht::takeMeasurements(int retries_left) {
Serial.println("Dht::takeMeasurements - Begin");

this->moisture = this->getHumidity();
this->temperature = this->getTemperature();

if(!isnan(this->moisture) && !isnan(this->temperature)) {
Serial.println("Dht::takeMeasurements - End");
return;
}

if (retries_left-- > 0) {
delay(1000);
Serial.print("Dht::takeMeasurements - retrying, temp ");
Serial.print(this->temperature);
Serial.print(isnan(this->moisture));
Serial.print(" humidity ");
Serial.print(this->moisture);
Serial.println(isnan(this->moisture));
this->takeMeasurements(retries_left--);
}
}

/**
* Convert cenigrate to farenheit.
*/
float Dht::cToF(float temp_c) {
Serial.print("Dht::cToF, converting temp ");
Serial.print(temp_c);
Serial.println("c");
return (temp_c * 1.8) + 32;
}

float Dht::humidity() {
return this->moisture;
}

float Dht::tempC() {
return this->temperature;
}

float Dht::tempF() {
Serial.println("Dht::tempF begin");
float result = this->cToF(this->temperature);
Serial.print("Dht::tempF, returning");
Serial.println(result);
return result;
}
float Dht::heatIndexC() {
return this->computeHeatIndex(this->temperature, this->moisture, false);
}

float Dht::heatIndexF() {
return this->cToF(this->heatIndexC());
}

float Dht::dewpoint() {
// Based on https://create.arduino.cc/projecthub/tomwyonkman/dht11-dew-point-calculator-67b487.
return (this->tempC() - (100 - this->humidity()) / 5);
}
24 changes: 24 additions & 0 deletions dht22.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// From https://desire.giesecke.tk/index.php/2018/01/30/esp32-dht11.
#include "DHTesp.h"

#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif

class Dht: public DHTesp {
protected:
float moisture, temperature;

public:
Dht();
void sensorPin(int sensor_pin);
void takeMeasurements(int retries_left = 5);
float cToF(float temp_c);
float tempC();
float tempF();
float heatIndexC();
float heatIndexF();
float humidity();
float dewpoint();
};
113 changes: 113 additions & 0 deletions gecko-tank.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Gecko tank monitor.
Monitors one dht22, updates oled temp and humidity display every second.
Posts sensor values to Thingspeak every 5 minutes.
Requirements:
- NodeMCU V2.0
- Thingspeak channel
- dht22 sensor and extension cable to reach from the circuit to inside the terrarium.
- NodeMcu Pinout
D1 <-> DHT22
D2 <-> oled SDA
D2 <-> oled SCA
A0 <-> light circuit
Light circuit
The following all connect in one row:
Photocell, other end to v3.3 (measurement)
10k resistor to GND (pulldown)
Wire to A0 (signal)
*/

// Community libraries.
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"

// Application header files
#include "communications.h"
#include "dht22.h"
#include "light.h"
#include "oled.h"

// Seconds between data posts to thingSpeak, this is 5 minutes.
unsigned long THINGSPEAK_POST_FREQUENCY_SECONDS = 300;
// For tracking loops between posts.
unsigned long thingspeakCountdown;

// Global variables.
WiFiClient client;
Dht dhtHotside;
LightSensor light;

/**
Run once at the beginning of the application.
Initialize the objects and variables, connect to serial monitor and wifi.
*/
void setup() {
Serial.begin(115200);

dhtHotside.sensorPin(D1);
light.sensorPin(A0);

ThingSpeak.begin(client);
oled_setup();

drawText("Connecting to Wifi", "");
while (!connect_wifi()) {
Serial.println("Failed to connect to wifi, retrying in 1 second");
delay(1000);
}

thingspeakCountdown = THINGSPEAK_POST_FREQUENCY_SECONDS;
}

/**
Main program loop.
Gather data, update displays, maybe post data, and then delay for a second.
*/
void loop() {
/* Gather and store all sensor values */
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
dhtHotside.takeMeasurements();
float hotside_temp_f = dhtHotside.tempF();
float hotside_humid_f = dhtHotside.humidity();
float rssi = get_rssi();
int lux = light.percent();

/* Send data to the serial monitor */
char serial_update[100];
sprintf(serial_update,"Hotside=%0.1ff %0.1f%% lux=%d rssi=%0.1f",
hotside_temp_f,
hotside_humid_f,
lux,
rssi
);
Serial.println(serial_update);

/* Send data to the oled */
char temp_text[100];
sprintf(temp_text, "%0.1ff %0.1f%%", hotside_temp_f, hotside_humid_f);
char title[100];
sprintf(title, "lux %d", lux);
drawText(title, temp_text);

// Send data to Thingspeak for storage when it is time.
if (0 == thingspeakCountdown--) {
ThingSpeak.setField(1, hotside_temp_f);
ThingSpeak.setField(2, hotside_humid_f);
ThingSpeak.setField(3, lux);
ThingSpeak.setField(4, rssi);
drawText("Transmitting...", "");
thingspeakPostData();

thingspeakCountdown = THINGSPEAK_POST_FREQUENCY_SECONDS;
}

delay(1000);
}
13 changes: 13 additions & 0 deletions light.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "light.h"

LightSensor::LightSensor() {}

void LightSensor::sensorPin(int pin) {
_pin = pin;
}

// Light value expressed in the range of 0 to 100.
int LightSensor::percent() {
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V).
return analogRead(_pin) * ((float)100 / 1023.0);
}
18 changes: 18 additions & 0 deletions light.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// From https://www.instructables.com/id/ESP8266-Light-Sensor/

#ifndef LightSensor_h
#define LightSensor_h

// Provides analogRead().
#include "Arduino.h"

class LightSensor {
public:
LightSensor();
void sensorPin(int pin);
int percent();
private:
int _pin;
};

#endif
42 changes: 42 additions & 0 deletions oled.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`


SSD1306Wire display(0x3c, D3, D2); //D2=SDK D1=SCK As per labeling on NodeMCU

void oled_setup() {
delay(1000);
Serial.println("");

Serial.println("Initializing OLED Display");
display.init();

display.setFont(ArialMT_Plain_16);
Serial.println("Done initializing OLED Display");

}

void drawFontFaceDemo() {
// clear the display
display.clear();
// Font Demo1
// create more fonts at http://oleddisplay.squix.ch/
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Hello world");
display.setFont(ArialMT_Plain_16);
display.drawString(0, 10, "Hello world");
display.setFont(ArialMT_Plain_24);
display.drawString(0, 26, "Hello world");
// write the buffer to the display
display.display();
}

void drawText(const char * title, const char * text) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, title);
display.setFont(ArialMT_Plain_24);
display.drawString(0, 16, text);
display.display();
}

0 comments on commit b8ba204

Please sign in to comment.