Arduino Support for ESP8266 with simple test code
Nodemcu is not just a WiFi module, it also has a decent micro-controller in built. Nodemcu comes with 32-bit Tensilica Processor, Power-Saving Architecture, Compactness, High Durability. In this small tutorial, I will show you how easy it is to add Arduino Support.
Download Arduino IDE from https://www.arduino.cc/en/Main/Software
Install the ESP8266 Board Package
- Select the Preferences under File.
- Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Boards Manager URL’s field under preferences as shown below.
- Next, Select the Board manager under Tools.
- Next, use the Board manager to install the ESP8266 package.
- Now last step Restart the Arduino IDE and Select the specific Board. Go to Tools =>Board and select “Generic ESP8266 Module” Now you can use ESP8266 with Arduino IDE.
Test Example of DHT11 Temperature & Humidity sensor on NodeMCU using Arduino IDE
The DHT11 is chosen because it is lab calibrated, accurate and stable and its signal output is digital. Most important of all, it is relatively inexpensive for the given performance. Below is the pinout of the sensor.
Installing Libraries
- Click here to download the DHT sensor library.
- Unzip the .zip folder and you should get DHT-sensor-library-master folder
- Rename your folder from
DHT-sensor-library-masterto DHT - Move the DHT folder to your Arduino IDE installation libraries folder
- You also need to install the Adafruit Unified Sensor Driver Library.
- Unzip the .zip folder and you should get Adafruit_sensor-master folder
- Rename your folder from
Adafruit_sensor-masterto Adafruit_sensor - Move the DHT_sensor folder to your Arduino IDE installation libraries folder
- Finally, re-open your Arduino IDE
Copy the sketch below to your Arduino IDE. Replace the SSID and password with your own credentials.
// Including the ESP8266 WiFi library #include <ESP8266WiFi.h> #include "DHT.h" // Uncomment one of the lines below for whatever DHT sensor type you're using! #define DHTTYPE DHT11 // DHT 11 // Replace with your network details const char* ssid = "YOUR_NETWORK_NAME"; const char* password = "YOUR_NETWORK_PASSWORD"; // Web Server on port 80 WiFiServer server(80); // DHT Sensor const int DHTPin = 5; // Initialize DHT sensor. DHT dht(DHTPin, DHTTYPE); // Temporary variables static char celsiusTemp[7]; static char fahrenheitTemp[7]; static char humidityTemp[7]; // only runs once on boot void setup() { // Initializing serial port for debugging purposes Serial.begin(115200); delay(10); dht.begin(); // Connecting to WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Starting the web server server.begin(); Serial.println("Web server running. Waiting for the ESP IP..."); delay(10000); // Printing the ESP IP address Serial.println(WiFi.localIP()); } // runs over and over again void loop() { // Listenning for new clients WiFiClient client = server.available(); if (client) { Serial.println("New client"); // bolean to locate when the http request ends boolean blank_line = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n' && blank_line) { // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); strcpy(celsiusTemp,"Failed"); strcpy(fahrenheitTemp, "Failed"); strcpy(humidityTemp, "Failed"); } else{ // Computes temperature values in Celsius + Fahrenheit and Humidity float hic = dht.computeHeatIndex(t, h, false); dtostrf(hic, 6, 2, celsiusTemp); float hif = dht.computeHeatIndex(f, h); dtostrf(hif, 6, 2, fahrenheitTemp); dtostrf(h, 6, 2, humidityTemp); // You can delete the following Serial.print's, it's just for debugging purposes Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.print(" *F"); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); } client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); // your actual web page that displays temperature and humidity client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<head></head><body><h1>ESP8266 - Temperature and Humidity</h1><h3>Temperature in Celsius: "); client.println(celsiusTemp); client.println("*C</h3><h3>Temperature in Fahrenheit: "); client.println(fahrenheitTemp); client.println("*F</h3><h3>Humidity: "); client.println(humidityTemp); client.println("%</h3><h3>"); client.println("</body></html>"); break; } if (c == '\n') { // when starts reading a new line blank_line = true; } else if (c != '\r') { // when finds a character on the current line blank_line = false; } } } // closing the client connection delay(1); client.stop(); Serial.println("Client disconnected."); } }
Open the Arduino IDE serial monitor at a baud rate of 115200. After a few seconds your IP address should appear. Open any browser from a device that is connected to the same router that your ESP is. Then type the IP address and click Enter!, Now you can see temp and humd in browser.
Pin Diagram
- DHT 11 vcc => 5V pin Nodemcu
- DHT 11 GND => GND pin Nodemcu
- DHT 11 Data => D1 pin Nodemcu
Pingback: NodeMcu to DHT Interface in Blynk app - IoTbyHVM
Pingback: Arduino ESP32 support on Windows and Ubuntu - IoTbyHVM
Pingback: Using Mq135 Sensor with InfluxDB - IoTbyHVM
Pingback: ESP32 BLE with DHT11 - How To - IoTbyHVM
Pingback: ESP8266 as a MQTT Broker | How To Make ESP8266 as a MQTT Broker
Pingback: Using Node js and Arduino with LED Blinking Program
Pingback: IoT OS and RTOS for Internet of Things devices - IoTbyHVM
Pingback: ESP32 BLE Tutorials | How to use ESP32 with BLE
Pingback: ESP8266 Static IP Address Using Arduino Example - ESP
Pingback: GPIO pins of ESP8266 and How to use efficiently -IoTbyHVM
Pingback: Heroku | How To use | Getting started with Heroku ...
Pingback: Install Ubuntu Core on Raspberry Pi 2 or 3 - IoTbyHVM
Pingback: How to Fix Ubuntu Update Errors - IoTbyHVM - Bits & Bytes of IoT
Pingback: NodeRed Alternatives - Visual programming tools -IoTbyHVM
Pingback: ESP8266 Web Server | ESP8266 Web Server AP (Access Point)
Pingback: Portable OS - Your pocket operating systems - TopX
Pingback: ESP8266 Parsing JSON - IoTbyHVM - Bits & Bytes of IoT
Pingback: Blynk Tutorials | Blynk IoT Platform | How To use Blynk
Pingback: espcom_sync_failed [ERROR Solved] - IoTbyHVM - Bits & Bytes of IoT
Pingback: Interface LDR module with NodeMCU - IoTbyHVM - Bits & Bytes of IoT
Pingback: Interface MQ135 (Gas Sensor) with NodeMCU - IoTbyHVM
Pingback: Top 10 IoT Cloud Platforms - CompileIoT
Pingback: Arduino IDE | Arduino | Open Source Hardware/Softawre | Arduino Vs RPi
Pingback: Android Things - OS for IoT and embedded devices - IoTbyHVM - Bits & Bytes of IoT
Pingback: InfluxDB | Time Series Database ? | TickStack | Tickscript ?