Programming and Learning Nodemcu with Arduino and making a temperature and Humidity web Server

Whats is nodemcu

Nodemcu: nodemcu is Esp8266 based development board .it can be used with Arduino or with nodemcu build OS for it .this is very cheap board approximately available only under Rs.500 India and in other countries .the board basically have very versatile application that can be done on this board. the board is consist of a Main Controller which is ESP8266 and USB to serial converter IC . this the cheapest board available with Wi-fi Capabilities . the processor is clocked at 160Mhz which quite for many application the board has a 4 Mb of Flash memory so that the large build can be made without any Issues.

Nodemcu Board



Now we understand the basic of Nodemcu now we can start coding in Arsuino with the board nodemcu . Let's start !!

First example of Nodemcu as a web server for temperature and humidity .

Component required : 
  1. A nodemcu Board
  2. DHT 22 sensor
  3. Jumper wires
  4. Microusb cable
  5.  Resistor 10k (if your sensor breakout don't)
  6. Led (optional)
  7. Arduino Installed on Pc (linux or Windows)
  8. A personal wifi network or mobile Hotspot
Connect the board with sensor as shown in fig below.

Now the connection are done just plugin the usb to pc and open arduino 

Makesure the nodemcu v.1 board is selected in baord manager menu and the correct com port is also selected . (If you do not know the installatie step of nodemcu in arduino please refer Arduino page on this blog link here)

connection  Photo 



Now make a new file in Arduino and paste this code 
// Include Libraries
#include "Arduino.h"
#include "DHT.h"


// Pin Definitions
#define DHT_PIN_DATA 5



// Global variables and defines

// object initialization
DHT dht(DHT_PIN_DATA);


// define vars for testing menu
const int timeout = 10000;       //define timeout of 10 sec
char menuOption = 0;
long time0;

// Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
void setup() 
{
    // Setup Serial which is useful for debugging
    // Use the Serial Monitor to view printed messages
    Serial.begin(9600);
    while (!Serial) ; // wait for serial port to connect. Needed for native USB
    Serial.println("start");
    
    dht.begin();
    menuOption = menu();
    
}

// Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop.
void loop() 
{
    
    
    if(menuOption == '1') {
    // DHT22/11 Humidity and Temperature Sensor - Test Code
    // Reading humidity in %
    float dhtHumidity = dht.readHumidity();
    // Read temperature in Celsius, for Fahrenheit use .readTempF()
    float dhtTempC = dht.readTempC();
    Serial.print(F("Humidity: ")); Serial.print(dhtHumidity); Serial.print(F(" [%]\t"));
    Serial.print(F("Temp: ")); Serial.print(dhtTempC); Serial.println(F(" [C]"));

    }
    
    if (millis() - time0 > timeout)
    {
        menuOption = menu();
    }
    
}



// Menu function for selecting the components to be tested
// Follow serial monitor for instrcutions
char menu()
{

    Serial.println(F("\nWhich component would you like to test?"));
    Serial.println(F("(1) DHT22/11 Humidity and Temperature Sensor"));
    Serial.println(F("(menu) send anything else or press on board reset button\n"));
    while (!Serial.available());

    // Read data from serial monitor if received
    while (Serial.available()) 
    {
        char c = Serial.read();
        if (isAlphaNumeric(c)) 
        {   
            
            if(c == '1') 
    Serial.println(F("Now Testing DHT22/11 Humidity and Temperature Sensor"));
            else
            {
                Serial.println(F("illegal input!"));
                return 0;
            }
            time0 = millis();
            return c;
        }
    }
}