iot talking to webserver - how to

14
IOT - How to talk to Webserver from arduino By Indraneel Ganguli

Upload: indraneel-ganguli

Post on 13-Apr-2017

182 views

Category:

Devices & Hardware


0 download

TRANSCRIPT

Page 1: IOT Talking to Webserver - how to

IOT - How to talk to Webserver from arduinoBy Indraneel Ganguli

Page 2: IOT Talking to Webserver - how to

IntroductionThere has been a lot of talk on IOT (Internet of things) and how its going to be the next big

thing. Devices connected to the internet and talking to each other . This fascinated me. I

thought of embarking on a small mission of finding out and implementing a small scale project

which touches on IOT.

Also I didn't wanted to spend too much money on this project and wanted a cheap way of

building something which will demonstrated IOT working. Started searching on Amazon.in to

find something cheap.

Page 3: IOT Talking to Webserver - how to

The problem statement (created one)The problem statement i created for this project is that i need have a device talk to a

webserver over WIFI and do some action.

Page 4: IOT Talking to Webserver - how to

Need to build stuff cheaply

After a bit of research on Google , found that one can use Arduino UNO for the device (cost - Rs

450 /$6). Arduino UNO doesn't have inbuilt WIFI capability . One needs to add Arduino WIFI

Sheild to Arduino to make it WIFI enable. Arduino WIFI shield is expensive Rs 6000 (~$90). I

wanted a cheap alternative . Found ESP8266 Serial WIFI module (cost Rs 210 /$3). The only

caveat is that integrating ESP8266 with Arduino UNO is bit of hardwork and challenge ( which i

could overcome after slogging for 2 days)

Stuff needed to build one

Page 5: IOT Talking to Webserver - how to

Inventory1. Arduino UNO -

http://www.amazon.in/Development-Board-ATmega328P-ATmega16U2-Arduino/dp/B00H1HR57

6/ref=sr_1_1?ie=UTF8&qid=1453694450&sr=8-1&keywords=arduino+uno

Rs 450/$6

2 ESP8266 -

http://www.amazon.in/ESP8266-Serial-Wireless-Transceiver-Module/dp/B00O34AGSU/ref=sr_1_

1?ie=UTF8&qid=1453693968&sr=8-1&keywords=esp8266

- Rs 210 ($3)

3. Breadboard , jumper wires . etc .

Page 6: IOT Talking to Webserver - how to

Connection Setup1.One needs to connect Arduino UNO with ESP8266 . ESP8266 has the following connections

Page 7: IOT Talking to Webserver - how to

Connection Setup (contd ..)2. Connect Arduino PIN 2 to TX of ESP8266 and Arduino PIN 3 to RX of ESP8266

3. Connect 3.3 V pin of Arduino to Vcc of ESP8266

4. Connect 3.3 V pin tof Arduino to CH_PD of ESP 8266

6 Connect GND pin of Arduino to GND ping of ESP8266.

Page 8: IOT Talking to Webserver - how to

How would it look after connection.

Page 9: IOT Talking to Webserver - how to

Install Arduino IDE on Mac /PC / or LinuxOne need to install Arduino IDE

https://www.arduino.cc/en/Main/Software

Connect USB from Arduino to your PC . You would

see power on lights on Arduino.

Install ESP Library into Arduino IDE

Go to Tools-> Library -> Manage Libraries , in the

search box type ESP8266.

Choose WifiESP by bportaluri - click install .

Page 10: IOT Talking to Webserver - how to

The program code (aka sketch)#include <SoftwareSerial.h>

#define DEBUG true

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.// This means that you need to connect the TX line from the esp to the Arduino's pin 2// and the RX line from the esp to the Arduino's pin 3void setup(){

}

// contd to next page

Page 11: IOT Talking to Webserver - how to

The program code (contd..)void loop(){Serial.begin(115200);esp8266.begin(115200); // your esp's baud rate might be different

sendData("AT+RST\r\n",2000,DEBUG); // reset modulesendData("AT+CWMODE=1\r\n",3000,DEBUG); // configure as access pointsendData("AT+CWJAP=\"WIFISSID"\",\"WIFIPASSWORD\"\r\n",3000,DEBUG);

sendData("AT+CIFSR\r\n",3000,DEBUG); // get ip address

Serial.println("Start of operation") ;//sendData("AT+CIPSTART=\"TCP\",\"x.x.x.x",y\r\n",3000,DEBUG);sendData("AT+CIPSTART=\"TCP\",\"192.168.0.102\",8080\r\n",3000,DEBUG);sendData("AT+CIPSEND=72\r\n",3000,DEBUG);

String sendcommand = "GET http://x.x.x.x/someURL HTTP/1.0\r\n\r\n\r\n";//works for most casesSerial.print(sendcommand.length());

String resp=sendData(sendcommand,5000,DEBUG);

Page 12: IOT Talking to Webserver - how to

The program code (contd..)Serial.println("http response start") ;Serial.println(resp);Serial.println("http response stop") ;

sendData("AT+CIPCLOSE\r\n",10000,DEBUG) ;Serial.println("completed operation") ;delay(30000) ;

}String sendData(String command, const int timeout, boolean debug){String response = "";

esp8266.print(command); // send the read character to the esp8266

long int time = millis();

Page 13: IOT Talking to Webserver - how to

The program code (contd..)while( (time+timeout) > millis()){while(esp8266.available()){

// The esp has data so display its output to the serial window char c = esp8266.read(); // read the next character.response+=c;} }

if(debug){Serial.print(response);}

return response;}

Page 14: IOT Talking to Webserver - how to

ConclusionThis prototype shows on the arduino can talk to webserver over wifi . One can extend this basic working to advance level like switching on LED , FANS , motors controlled. Since I have set arduino as web client , it can connect to public ip (without natting etc).