TerraControl - With NodeMCU Webserver




About: Technology geek, love discovering new possibilities...

UPDATE 3: Version 1.3

I'm working on making the code as user friendly as possible but it's going to be a long run. I'd like to have separate html and css files but I'm still figuring it out. For now, few changes...:

  • ! ! ! included mDNS protocol, so from now on you don't need IP address to connect to your NodeMCU, simply put terracontrol.local in your browser and you are done (you have to be on the same network, of course)
  • separate file for setting up the variables (setting.h). Unzip the file to your Projects folder, when you open the *.ino file, setting.h should be opened as well.

UPDATE 2: Version 1.2

  • improved graph displaying range
  • new values in graphs are moved to the end of array, not starting from the beginning again
  • improved light setting - it is now unlimited (ON time can now be later than OFF time)
  • code for manual defining your own server is in one place and commented by default (i.e. it is on automatic setting)
  • clearer information in serial monitor
  • unified function for min/max values in array
  • new function for printing out minute values

UPDATE 1: Please see the version 1.1 I got the graphs and statistics working! Well, sort of...the range is still not as I want it to be, but at least now it is correctly displaying min and max. Plus new mouseover feature for the individual values in the graph.

After my first attempt to create controlled terrarium using Arduino board I got my hands on NodeMCU 12E board and I knew it was going to be a big step up!

I took me a few days before I began to understand how this board works (thanks to a lot of instructables here and google of course) and the possibilities it had. It think I'm on the right path to create exactly what I was dreaming about for several years...

So what is TerraControl v1.2 capable of?

  1. 2 automatically controlled relays (light timer and heating)
  2. 2 manually controlled relays (fan, second heating)
  3. GMT time change
  4. Simple graphs with highest/lowest temperature/humidity over the last 24 hours
  5. Monitoring temperature and humidity in terrarium
  6. All accessible and adjustable through webserver using HTML and CSS

What is necessary:

  • given the nature of NodeMCU board (its output is only 3.3v) you will either have to buy 3.3V relay board, or modify 5v board, or buy I2C logic converter module - for example - $0.9
  • 5V source (I'm using older usb charger)
  • wires
  • solder
  • case/box
  • Arduino IDE

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Getting the Parts Together

Connecting these parts is easy, just look at the source code and keep in mind that GPIO's of the NodeMCU board is different from the actual boards description (as seen on the trird picture):

//Define sensor pins
#define SENSOR_IN 15 //D8
#define SENSOR_IN_Type DHT22

//Define Relay pins
#define relayLight 5 //D1
#define relayHeat 4 //D2
#define relayFan 12 //D6
#define relayHeat2 14 //D5

i.e. DHT sensor pin goes to D8 (board's D3, D4, D8 can't be used as output but can be used as input), and the relay pins accordingly to the code. Remember, if you are using 5V relay, you need to modify the relay board or use I2C logic converter.

! ! ! IMPORTANT! When uploading the code to the board, you have to disconnect the DHT sensor, otherwise you will get an error when attempting to upload ! ! !

All parts can be powered with 5v power adapter

Step 2: Setup and Customization

Before we upload the code, there are few things that needs to be set up in setting.h:

//You WiFi AP
const char ssid[] = "SSID Name"; // insert your WiFi AP name
const char pass[] = "password"; // insert your WiFi password

// T E R R A R I U M S E T T I N G
float tempMin = 24; // temperature in Celsius for switching the heating ON
float tempMax = 30; // te
mperature in Celsius for switching the heating OFF

int humMin = 50; // minimum humidity in %
int humMax = 70; //
maximum humidity in %

// hour and minute for light to go ON
int lightOn_hour = 7;
int lightOn_min = 0;

// hour and minute for light to go OFF
int lightOff_hour = 20;
int lightOff_min = 30;

// Central European Time (1 for winter time)
int timeZone = 2;

Uncomment the following part of the code if you know how to define your server manually or just run the code and get addresses from the serial monitor.

IPAddress ip(192, 168, 0, 111); //Node static IP
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, gateway, subnet);

All done? Great, let's move on...

Step 3: Alwas ON/OFF Relay Connection

One thing I wanted was the relay board to be used as little as possible. As you probably know, relays have two possible ways of connection: ON when not used and ON when used. So I connected the light and heating to "ON when not used" (heating is almost always ON and lights are ON for about 13-14 hours every day) and fan and heating 2 to "ON when used" (I barely need to use one of them).

That is why the code for the same function is using different values:

if (heatVal == 1) {
} else {
client.println("OFF"); }


if (heat2Val == 1) {
} else {
client.println("ON"); }

You can of course modify the code according to your needs.

Now just connect the DHT sensor and let's look at the result!

Step 4: Webserver

When you open the webserver you will see simple page with all information about your terrarium and some features:

  • Light ON/OFF time can be adjusted (step are: 1 hour for hour setting and 5 min for minute setting). At the moment ON time has to be earlier that OFF time (ON 22:30 and OFF 0:30 will not work - yet) - fixed in version 1.2
  • Temperature setting (steps are 0.5 degree Celsius)
  • Manualy turn ON/OFF other two relays - Fan and Heat 2 and adjust timezone when the time changes

if needed, change your timezones in following part of the code:

if (request.indexOf("/TIMEZONE_SWITCH") != -1) {
if (timeZone == 1) {
timeZone = 2;
} else {
timeZone = 1;
} }

  • Webserver is using automatic time synchronization

Step 5: Disclosure

I know that the HTML and CSS code could be much more simple and the coding is not really user friendly but for the moment it works as it is supposed to (only the graphs are not very accurate but I'm still working on them) and I will get to these points when I start working on version number two. I have already decided to use external power supply (in this version I just stripped the 5v adapter and soldered it inside the box) and I also want the power cables to be more accessible and easier to connect/disconnect. I hope you guys (and your pets) will appreciate this instructable, if you do, please leave a short comment. And of course, suggestions are more than welcome! Thank you

Beyond the Comfort Zone Contest

Participated in the
Beyond the Comfort Zone Contest

1 Person Made This Project!


  • Made with Math Contest

    Made with Math Contest
  • Multi-Discipline Contest

    Multi-Discipline Contest
  • Robotics Contest

    Robotics Contest

31 Discussions


5 months ago on Step 5

Hi. Fantastic project. I am new to HTML thing But it looks interesting.. Will look into learning a bit more about it.
Re the question below... "connection diagram" This would be very helpful in helping people like myself to complete the project.

1 reply

Reply 5 months ago

Thank you! I don't have connection diagram but the connection of components is very easy and described above. BTW check out my newer project - TerraControl 3.0 with Blynk support


Question 7 months ago

Good afternoon, you can send a connection diagram of all the elements of your project

5 answers

Answer 7 months ago

Hey, the connection is described in step 1:

#define SENSOR_IN 15 //D8

#define relayLight 5 //D1
#define relayHeat 4 //D2
#define relayFan 12 //D6
#define relayHeat2 14 //D5

the power connection depends on the used parts (some relay boards are using 5v, other 3.3v

I also recommend you to check out my updated version - TerraConrol 3.0 (link below in the comments). It is much easier to setup


Reply 7 months ago

I can not connect the temperature sensor ???

10 months ago

Hey nice instructable. If only this had humidity UP automation for my moisture generator and humidity down so it can turn on the fan when the humidity is too much.


11 months ago

Eine tolle Sache.Ich möchte gern über dyndns aus dem Internet auf das Modul zugreifen.
Es soll ein "Login " mit Benutzername und Passwort erfolgen.
Was muss man im Code eintragen,damit das geht.
Kann mir einer helfen.Danke


Question 1 year ago

hello, i need simple one look like my photo

my esp8266 give string via serial and use "RTClib.h" 1307 to make time and clock and then save time and clock with received data to micro sd card

now i want to show my serial received data and to id1 -id4

and download sd save file over browser as txt file looks like report.txt file in sd card can you make it for me?

i write the serial read and save sd with time part by my self


1 year ago

hi dear PraetorianCZ and all others i want to build a thermostat.i have a nodemcu board and LCD and high level trigger relay and dht11 now i can see and control min and max with button and LCD,and only i can see the temperature and humidity in HTML with web server and i cant control that., i write the form for input but i don't know how to write code to Equal the text box input number to variables of min and max.please help me.i am new in arduino and esp world.sorry about my bad English.plz change my code
ino file:http://www.filehosting.org/file/details/721840/dalichai.ino


1 year ago

This code: wemos D1 mini ESP8266 wifi modul +adafruit I2C oled display.


1 year ago

The modified code.




1 year ago

HI guys. I update the code. Added I2C oled diplay. ;-)


2 years ago

Hey guys I have a problem with setting the correct fan operation at low humidity and missing the on / off switch, please help

2 replies

Reply 1 year ago

Hey, not sure how to check what is wrong without the code but I see you are using the first version with LCD where Arduino was used. I moved to NodeMCU and Wifi control for this reason...to many things to setup for LCD. I recommend to try it or wait for version 3, which I will be posting in few days, hopefully.


Reply 1 year ago

Hi, great, both projects I launched,
I have in both problem with relay control from moisture,
I'm waiting impatiently for another project.


2 years ago

I wanna run it an an mega 2560 need help