Question:
I'm trying to send temperature data from arduino uno to server through ESP8266. But I unable to even configure WiFi, Please provide me code.?
Jitendra
2015-06-25 23:17:04 UTC
I'm trying to send temperature data from arduino uno to server through ESP8266. But I unable to even configure WiFi, Please provide me code.?
Three answers:
?
2015-06-26 21:11:59 UTC
The following worked for me, just copied off the internet. The Uno serves a simple web page in your browser with analog values from the Arduino. The hardware serial of the Uno is connected to the USB programming cable as usual, but also connected to the ESP8266. Therefore, if you need to change your program and upload new code, you have to remove the wires (or module) to the ESP8266 or the program won't load due to the serial conflict. Just reattach the wires or module after programming, and it will work. You need the software serial to get the IP address your router assigned to the ESP8266. Then, just open a browser with that IP and port and you see a simple web page with the analog values.



/* ====== ESP8266 Demo ======

* Print out analog values

* (Updated Dec 14, 2014)

* ==========================

*

* Change SSID and PASS to match your WiFi settings.

* The IP address is displayed to soft serial upon successful connection.

*

* Ray Wang @ Rayshobby LLC

* http://rayshobby.net/?p=9734

*/



// comment this part out if not using LCD debug



#define BUFFER_SIZE 512



#define SSID "dlink" // change this to match your WiFi SSID

#define PASS "yourpasswordhere" // change this to match your WiFi password

#define PORT "8080" // using port 8080 by default, some other ports won't work



char buffer[BUFFER_SIZE];





// If using Software Serial for debug

// Use the definitions below

#include // need this library, talks to a separate terminal program for debug

SoftwareSerial dbg(10,11); // use pins 10,11 for software serial -use FTDI usb adapter and terminal program

// otherwise, you won't know the IP address to put in your browser. Might be like 192.168.1.108:8080

//But your router assigns this IP so need to see what it really is.

#define esp Serial



/*

// If your MCU has dual USARTs (e.g. ATmega644)

// Use the definitions below

#define dbg Serial // use Serial for debug

//#define esp Serial1 // use Serial1 to talk to esp8266

*/



// By default we are looking for OK\r\n

char OKrn[] = "OK\r\n";

byte wait_for_esp_response(int timeout, char* term=OKrn) {

unsigned long t=millis();

bool found=false;

int i=0;

int len=strlen(term);

// wait for at most timeout milliseconds

// or if OK\r\n is found

while(millis()
if(esp.available()) {

buffer[i++]=esp.read();

if(i>=len) {

if(strncmp(buffer+i-len, term, len)==0) {

found=true;

break;

}

}

}

}

buffer[i]=0;

dbg.print(buffer);

return found;

}



void setup() {



// assume esp8266 operates at 115200 baud rate

// change if necessary to match your modules' baud rate

esp.begin(9600); // I changed mine to 9600 prior to this sketch



dbg.begin(9600);

dbg.println("begin.");



setupWiFi();



// print device IP address

dbg.print("device ip addr:");

esp.println("AT+CIFSR");

wait_for_esp_response(1000);

}



bool read_till_eol() {

static int i=0;

if(esp.available()) {

buffer[i++]=esp.read();

if(i==BUFFER_SIZE) i=0;

if(i>1 && buffer[i-2]==13 && buffer[i-1]==10) {

buffer[i]=0;

i=0;

dbg.print(buffer);

return true;

}

}

return false;

}



void loop() {

int ch_id, packet_len;

char *pb;

if(read_till_eol()) {

if(strncmp(buffer, "+IPD,", 5)==0) {

// request: +IPD,ch,len:data

sscanf(buffer+5, "%d,%d", &ch_id, &packet_len);

if (packet_len > 0) {

// read serial until packet_len character received

// start from :

pb = buffer+5;

while(*pb!=':') pb++;

pb++;

if (strncmp(pb, "GET /", 5) == 0) {

wait_for_esp_response(1000);

dbg.println("-> serve homepage");

serve_homepage(ch_id);

}

}

}

}

}



void serve_homepage(int ch_id) {

String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\nRefresh: 5\r\n";



String content="";

// output the value of each analog input pin

for (int analogChannel = 0; analogChannel < 6; analogChannel++) {

int sensorReading = analogRead(analogChannel);

content += "analog input ";

content += analogChannel;

content += " is ";

content += sensorReading;

content += "
\n";

}



header += "Content-Length:";

header += (int)(content.length());

header += "\r\n\r\n";

esp.print("AT+CIPSEND=");

esp.print(ch_id);

esp.print(",");

esp.println(header.length()+content.length());

if(wait_for_esp_response(2000, "> ")) {

esp.print(header);

esp.print(content);

} else {

esp.print("AT+CIPCLOSE=");

esp.println(ch_id);

}

}





void setupWiFi() {

// try empty AT command

esp.println("AT");

wait_for_esp_response(1000);



// set mode 1 (client)

esp.println("AT+CWMODE=1");

wait_for_esp_response(1000);



// reset WiFi module

esp.print("AT+RST\r\n");

wait_for_esp_response(1500);

delay(3000);



// join AP

esp.print("AT+CWJAP=\"");

esp.print(SSID);

esp.print("\",\"");

esp.print(PASS);

esp.println("\"");

// this may take a while, so wait for 5 seconds

wait_for_esp_response(5000);



esp.println("AT+CIPSTO=30");

wait_for_esp_response(1000);



// start server

esp.println("AT+CIPMUX=1");

wait_for_esp_response(1000);



esp.print("AT+CIPSERVER=1,"); // turn on TCP service

esp.println(PORT);

wait_for_esp_response(1000);





}
Sydney
2015-06-25 23:18:21 UTC
I honestly have no clue, I'm sorry
Matt
2015-06-25 23:17:15 UTC
a


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...