Thursday, March 31, 2016

Web Server Data Logger

Temperature Data logging from sensor to server, This tutorial to show how you can send sensor data to a web server using esp8266 IoT WiFi Module, This tutorial is useful to understand in depth when we design projects using esp8266 IoT WiFi Module

ESP8266 WiFi Module ADC is present on ESP-12 Module not on the ESP-01,
ESP8266 accepts 0 to 1V as ADC input so we are connecting LM35 Temperature sensor it gives 10mV Per degree centigrade so we don't have to worry about calibration.

ESP8266 WiFi Module contains only one ADC Channel A0.

Programming of ADC is same as Arduino.

Step 1: Circuit Connections


Step 2: Programming the esp-12 (ESPino)

In This code we have refreshed web page automatically every 5 Seconds using 
<meta http-equiv='refresh' content='5'/>\


snprintf ( temp, 400, This is used to combine temperature reading and web page.
/*
 * Copyright (c) 2015, Circuits4You.com
 * All rights reserved.
/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

/* Set these to your desired credentials. */
const char *ssid = "Circuits4You";
const char *password = "password";

ESP8266WebServer server(80);

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {
  digitalWrite ( led, 1 );
 
  int analog;
  analog = analogRead(A0);  //Read Analog Voltage of ADC Pin
  analog = analog/10;       //10mV is 1C
  snprintf ( temp, 400,
"<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>Circuits4You.com Temperature Monitoring Server Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <p>Temperature: %04d</p>\
  </body>\
</html>", analog
  );
  server.send ( 200, "text/html", temp );
  digitalWrite ( led, 0 );
}

void setup() {
 delay(1000);

        pinMode(LED_BUILTIN, OUTPUT);
 /* You can remove the password parameter if you want the AP to be open. */
 WiFi.softAP(ssid);

 IPAddress myIP = WiFi.softAPIP();
 server.on("/", handleRoot);
 server.begin();
}

void loop() {
   server.handleClient();
}

Step 3: Testing of results
1. Connect your phone or Laptop using wifi to the Network Circuit4You
2. Enter IP:192.168.4.1 in browser.
3. You will see temperature reading continuously updating at every 5 seconds

You can find more stuff in project section
Your comments and suggestions are welcome, you can post your projects also


LED On/OFF Server

LED On/Off Control Server
In this demo we will see our first real IoT control application, here we are controlling on board LED of ESP-01 Module using web server. Basically internet of thing is used to monitor and control the devices, this is our basic control application it can be extended to control multiple home appliances and monitor temperatures and various parameters of house. code is kept as simple as possible to make it useful for many applications and understanding.

Step 1: Hardware Setup
1. ESP-01 Module (you can use ESP-12 also)
2. 3.3V Power Supply for ESP-01 Module
3. Programming setup (USB to Serial Converter)

Circuit Connections:

Step 2: Programming

Design your web page as per your requirements.

HTML Webpage Code stored as header file mainPage.h:

Save this as mainPage.h
const char MAIN_page[] PROGMEM = R"=====(
<HTML>
<TITLE>
REMOTE LED ON/OFF CONTROL
</TITLE>
 <BODY>
  <FORM method="post" action="/form">
  <INPUT TYPE=SUBMIT name=submit VALUE="ON">
  <INPUT TYPE=SUBMIT name=submit VALUE="OFF">
  LED Status: @@LED@@
  </FORM>
 </BODY>
</HTML>
)=====";

ESP-01 Code LEDControl.C:

/*
 * Copyright (c) 2015, Circuits4You.com
 * All rights reserved.
/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

#include "mainPage.h"

/* Set these to your desired credentials. */
const char *ssid = "LEDServer";
const char *password = "password";

ESP8266WebServer server(80);
String LEDStatus;
//=======================================================================
//                    handles main page 192.168.4.1
//=======================================================================
/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {
  String s = MAIN_page;    
  s.replace("@@LED@@", LEDStatus);
  server.send(200, "text/html", s);    
}

//=======================================================================
//                    Handle Set Date/Time Settings
//=======================================================================
void handleForm() {
  String t_state = server.arg("submit");
  
//Change LED State as per request
  if(t_state=="ON")
  {
    LEDStatus="ON";    
    digitalWrite(LED_BUILTIN, LOW);       //LED Turned on
  }
  
  if(t_state=="OFF")
  {
    LEDStatus="OFF";    
    digitalWrite(LED_BUILTIN, HIGH);      //LED Turned off  
  }

  server.sendHeader("Location", "/");
  server.send(302, "text/plain", "Updated-- Press Back Button");  //This Line Keeps It on Same Page
   
  delay(500);
}
//=======================================================================
//                    Power on setup
//=======================================================================

void setup() {
  delay(1000);
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  server.on("/", handleRoot);
  server.on("/form", handleForm);
 
  server.begin();
  pinMode(LED_BUILTIN, OUTPUT);
}

//=======================================================================
//                    Main Program Loop
//=======================================================================
void loop() {
  server.handleClient();
}
//=======================================================================

Step 3: Uploading Code
1. To upload code on ESP-01 Press GPIO0 Switch to make it Low during power on or reset. to put it in programming mode.

2. During programming you will see the Blue LED Blinking.

Step 4: Testing
1. Turn on WiFi on phone or Laptop
2. Connect to LEDServer Network that is from ESP-01
3. Open Web Browser enter IP: 192.168.4.1
4. Press On/Off button on webpage you see
that's it.

Conclusion:
Now you have clear concept of IOT possibilities, this code is made such a way that you can have bidirectional communication from browser to ESP-01.

Understanding ESP8266 IO Pins

Original Content Source: http://cholla.mmto.org/esp8266/pins/

This is tricky and confusing. This biggest problem is your point of view. You can be considering the esp8266 chip itself, a module like the ESP-12 or ESP-12E, or the pins on the edge of a product like a NodeMCU board. On top of that, many pins can be mapped to multiple functions. And for yet more excitement, different software has given things different names. We have the GPIO numbers in the bare hardware. NodeMCU has assigned pin numbers (and made a significant change in them at one point in time). Arduino has yet another name/number scheme of its own. So ... you have been warned.

I am using mostly NodeMCU dev boards (of a recent vintage that adds some signals), but sometimes deal with bare ESP-12 modules, and I throw in the Sparkfun "Thing" dev board for extra variety.
The above GPIO pin allocation link is especially helpful. Along with this table:



This makes it clear why GPIO 6-11 are in general unavailable, they are connected to the flash chip! If the flash is connected in QIO mode (4 data lines), all 6 are unavailable. If the flash is connected in DIO mode (2 data lines), GPIO 9 and 10 can be used.
The 4 pins with "MT" in their names are part of an HSPI interface (Hardware SPI)

The bottom line

Here is part of a table from some C code I wrote. It is useful to me in showing the MUX names and NodeMCU aliases. In addition it shows what "extra stuff" is hooked up to the pins. Note that some pins cannot be used as they are connected to the flash and uart.
This is in "GPIO order".
    PERIPHS_IO_MUX_GPIO0_U,     /* 0 - D3 */
    PERIPHS_IO_MUX_U0TXD_U,     /* 1 - uart */
    PERIPHS_IO_MUX_GPIO2_U,     /* 2 - D4 - little LED on ESP-12E module */
    PERIPHS_IO_MUX_U0RXD_U,     /* 3 - uart */
    PERIPHS_IO_MUX_GPIO4_U,     /* 4 - D2 */
    PERIPHS_IO_MUX_GPIO5_U,     /* 5 - D1 */
    Z,     /* 6 - flash chip SPI */
    Z,     /* 7 - flash chip SPI */
    Z,     /* 8 - flash chip SPI */
    PERIPHS_IO_MUX_SD_DATA2_U,  /* 9  - D11 (SD2) (flash chip if QIO) */
    PERIPHS_IO_MUX_SD_DATA3_U,  /* 10 - D12 (SD3) (flash chip if QIO) */
    Z,     /* 11 - flash chip SPI */
    PERIPHS_IO_MUX_MTDI_U,      /* 12 - D6 */
    PERIPHS_IO_MUX_MTCK_U,      /* 13 - D7 */
    PERIPHS_IO_MUX_MTMS_U,      /* 14 - D5 */
    PERIPHS_IO_MUX_MTDO_U       /* 15 - D8 */
    ---    /* 16 - D0 - little LED on NodeMCU devkit board */

NodeMCU: pins and signals

A peek at the schematic shows that it is possible to connect 5 volt power to the Vin pin (it has the same connection as the USB 5 volt.
Below is a diagram of an ESP-12e module (which is what is mounted on the NodeMCU devel boards that I have). This is it, viewed from the top.





The silkscreen on my ESP-12e modules shows some differences:
REST is actually RST (reset)
CH_PD is labeled EN on my unit
TXD is labeled TXD0 on my unit
RXD is labeled RXD0 on my unit
Labeling RXD0 and TXD0 suggest there may be another uart; maybe so.
On many diagrams (and the silkscreen on many units) GPIO-4 and GPIO-5 are swapped. The above matches my silkscreen and experiment (see below) indicates this is correct.
The extra 6 pins on the bottom of the ESP-12e module are there and they are hooked up! They go to pins on the NodeMCU board that were previously marked "reserved", which is a nice bonus.

Hardware Connections LED Blink Test

My First Program LED Blink on ESP8266-12
Let's to check that everything is working properly we will test LED blink program on ESP-12 (ESPino), For this test you can use ESP-01 also, this is taken on internal Blue led present on esp8266 WiFi Module, Different modules have different connection for internal led, for esp-12 LED is connected to GPIO2, and on esp-01 it is on GPIO1 which is TXD0, remember that you can use GPIO2 instead of LED_BUILTIN. Maximum IO pin current is 15mAmps so take care if you are connecting external LED.

ESP8266 Pin diagram:

ESP-01:
esp-01 pin connections
RX and TX: Pins are used for serial communication and programming. TTL 3.3V logic voltage levels

GPIO2: are general purpose IO pin.

GPIO0: To put esp-01 or esp8266 in programming mode we have to make "GPIO 0" low at power on / reset

VCC : +3.3V from LM1117-3.3

GND : Ground

CH_PD: Connect this pin to 3.3V. connecting this pin to GND puts the ESP8266 in power down mode.

ESP-12 Pin Diagram:
esp-12 pin labels are written on bottom side


ESP8266 Circuit Connections for LED Blink Test:
esp8266 Wifi Module contains a blue LED on board which is internally connected to GPIO2.


Programming Steps:
1. Connect GPIO0 to Ground.
2. Connect USB to Serial Converter to RX,TX,GND Lines
3. Turn On the circuit, Blue LED blinks once while turning on the circuit if everything is ok.
4. Program the esp-12 using Arduino software.
5. While Programming on board blue LED starts flashing.

LED Blink Sample Code:

/*
 ESP8266 Blink
 Blink the blue LED on the ESP-01 module
 This example code is in the public domain
 
 The blue LED on the ESP-01 module is connected to GPIO1 
 (which is also the TXD pin; so we cannot use Serial.print() at the same time)
 
 Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because 
                                    // it is acive low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}

Points to Remember:
1. GPIOs are multiplexed with RX TX Lines
in ESP-01 module GPIO1 is also TXD Pin and internal LED pin.

2. GPIO2 on ESP-12 is connected to TXD and internal blue LED.

3. Do not forget to GPIO15 to pull low using 1K resistor.

4. Connect CH_PD pin to 3.3V through 1K resistor.


Software Requirements of IOT Programming

Take your fear away for programming IOT

Let me clear you first Programming of IOT esp8266 is done in Arduino software and uses same commands that we use for Arduino Uno

Steps to software setup:
Step 1: Download latest Arduino Software from www.arduino.cc
Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).

Step 2: Download Arduino Core for ESP8266 WiFi Chip from GitHub
Arduino Core for ESP8266 project brings support for ESP8266 chip to the Arduino environment. It lets you write sketches using familiar Arduino functions and libraries, and run them directly on ESP8266, no external microcontroller required.
ESP8266 Arduino core comes with libraries to communicate over WiFi using TCP and UDP, set up HTTP, mDNS, SSDP, and DNS servers, do OTA updates, use a file system in flash memory, work with SD cards, servos, SPI and I2C peripherals.
Step 3: Installing with Boards Manager
Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).
  • Install Arduino 1.6.8 from the Arduino website.
  • Start Arduino and open Preferences window.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLsfield. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install esp8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).

Select ESP8266 then press install
This pakage installation is from internet, your computer must be connected to internet it is around 250 MBytes. Read details on GitHub
Step 4: After installing everything you may find trouble in compiling missing .h files
4.1 Go to your arduino installation folder (where your arduino.exe is) in this folder create folder "portable"
4.2 copy and paste  copy from "C:\Documents and Settings\Administrator\Application Data\Arduino" (check that this address is shown while compilation error missing .h file)
4.3 copy all contains (folder naming "pakages" and "staging" and surrounding files)
4.4 paste the copied contains in "portable" folder that you created in Arduino folder.
Check that compilation works now. If all ok go ahead to test it on hardware



Wednesday, March 30, 2016

Getting Started With IOT

IOT Hardware Requirements to start application development

What do you need to buy for getting started?

1. esp-12 Module
2. USB to Serial Converter
3. 3.3V Power Supply

1. esp-12 Module or esp-01 Module

Choosing WiFi IOT module is very easy, I recommend you to go with ESP-12 having connections on both sides and also on bottom to get more IOs and Interfaces.

ESP-12 That I use (Recommended):

ESP-12 Without SPI Interface:


ESP-01:
 Useful where size requirement is small and you want to control one or two Relays (Device on/off), this module dose not contain ADC So not suitable for sensor interface.


2. Which USB to Serial converter is best suitable?
FT232RL based usb to serial converter is best suitable with voltage levels selection jumpers is recommended. keep usb to serial converter on 3.3V level

3. A 3.3V power supply
You can make or buy power supply using LM1117-3.3V Regulator.
Components required to make 3.3V power supply1. LM1117-3.32. 10uF/16V Capacitor
Circuit Diagram:

The ESP8266 runs on anything from 2.8 to 3.5 volts, so a couple of AA batteries will work. Most of us use a 3.3 volt regulated power source. Maximum current draw is 300 mA, so use a supply that can supply 500 mA to be safe. More is better. A weak or poor quality supply will cause the board to reset unexpectedly and make debugging difficult. The NodeMCU Development Board has a USB to 3.3V power supply on the board.

IOT Introduction

IOT for a Smarter World


Extending the current Internet and providing connection, communication, and inter-networking between devices and physical objects, or "Things," is a growing trend that is often referred to as the Internet of Things.

“The technologies and solutions that enable integration of real world data and services into the current information networking technologies are often described under the umbrella term of the Internet of Things (IoT)

IOT in simple words connecting sensors, devices to internet (monitoring and control over internet).

for example Home automation, In IOT you can control your electrical appliances from internet and also get file/theft alert on your tweeter, email, etc we will see it in project section how to do that?

Not to take more time on discussing IOT, Lets get started.....