This Sample Program allows the Teensy to wirelessly serve a webpage.
Code:
/* Based on:
* ====== ESP8266 Demo ======
* (Updated Dec 14, 2014)
* Ray Wang @ Rayshobby LLC
* http://rayshobby.net/?p=9734
* ==========================
*
* Modified by R. Wozniak
* Compiled with Arduino 1.60 and Teensyduino 1.21b6
* ESP8266 Firmware: AT21SDK95-2015-01-24.bin
*
* Change SSID and PASS to match your WiFi settings.
* The IP address is displayed serial upon successful connection.
*/
#define BUFFER_SIZE 1024
#define SSID "YOUR-SSID" // change this to match your WiFi SSID
#define PASS "your password" // change this to match your WiFi password
#define PORT "15164" // using port 8080 by default
char buffer[BUFFER_SIZE];
// 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()<t+timeout) {
if(Serial1.available()) {
buffer[i++]=Serial1.read();
if(i>=len) {
if(strncmp(buffer+i-len, term, len)==0) {
found=true;
break;
}
}
}
}
buffer[i]=0;
Serial.print(buffer);
return found;
}
void setup() {
// assume esp8266 operates at 115200 baud rate
// change if necessary to match your modules' baud rate
Serial1.begin(115200); // Teensy Hardware Serial port 1 (pins 0 and 1)
Serial.begin(115200); // Teensy USB Serial Port
delay(5000);
Serial.println("begin.");
setupWiFi();
// print device IP address
Serial.print("device ip addr: ");
Serial1.println("AT+CIFSR");
wait_for_esp_response(1000);
}
bool read_till_eol() {
static int i=0;
if(Serial1.available()) {
buffer[i++]=Serial1.read();
if(i==BUFFER_SIZE) i=0;
if(i>1 && buffer[i-2]==13 && buffer[i-1]==10) {
buffer[i]=0;
i=0;
Serial.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);
Serial.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: 300\r\n";
String content="";
content += "<!DOCTYPE html>";
content += "<html>";
content += "<body>";
content += " <h1> Abraham Lincoln's Gettysburg Address </h1> <br/> ";
content += "<p> <strong> Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. <br/>";
content += " <br/>";
content += "Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. <br/>";
content += " <br/>";
content += "But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth. <br/>";
content += " <br/>";
content += "Abraham Lincoln <br/>";
content += "November 19, 1863 </strong> </p> <br/>";
content += " <br/>";
content += " <p> <font color=#880088> This message brought to you by - TEENSY 3.1 & ESP8266 </font> </p>";
content += " <p> Teensy server uptime ";
Serial.print("***************************************** UPTIME = ");
Serial.println(millis());
content += "<font color=#0000FF> ";
content += String(millis());
content += " milliseconds </font> </p>";
content += "</body>";
content += "</html>";
content += "<br />\n";
content += "\r\n";
header += "Content-Length:";
header += (int)(content.length());
header += "\r\n\r\n";
Serial1.print("AT+CIPSEND=");
Serial1.print(ch_id);
Serial1.print(",");
Serial1.println(header.length()+content.length());
if(wait_for_esp_response(2000)) {
//delay(100);
Serial1.print(header);
Serial1.print(content);
}
else {
Serial1.print("AT+CIPCLOSE=");
Serial1.println(ch_id);
}
}
void setupWiFi() {
// turn on echo
Serial1.println("ATE1");
wait_for_esp_response(1000);
// try empty AT command
//Serial1.println("AT");
//wait_for_esp_response(1000);
// set mode 1 (client)
Serial1.println("AT+CWMODE=3");
wait_for_esp_response(1000);
// reset WiFi module
Serial1.print("AT+RST\r\n");
wait_for_esp_response(1500);
//join AP
Serial1.print("AT+CWJAP=\"");
Serial1.print(SSID);
Serial1.print("\",\"");
Serial1.print(PASS);
Serial1.println("\"");
wait_for_esp_response(5000);
// start server
Serial1.println("AT+CIPMUX=1");
wait_for_esp_response(1000);
//Create TCP Server in
Serial1.print("AT+CIPSERVER=1,"); // turn on TCP service
Serial1.println(PORT);
wait_for_esp_response(1000);
Serial1.println("AT+CIPSTO=30");
wait_for_esp_response(1000);
Serial1.println("AT+GMR");
wait_for_esp_response(1000);
Serial1.println("AT+CWJAP?");
wait_for_esp_response(1000);
Serial1.println("AT+CIPSTA?");
wait_for_esp_response(1000);
Serial1.println("AT+CWMODE?");
wait_for_esp_response(1000);
Serial1.println("AT+CIFSR");
wait_for_esp_response(5000);
Serial1.println("AT+CWLAP");
wait_for_esp_response(5000);
Serial.println("---------------*****##### READY TO SERVE #####*****---------------");
}
Here's a sample of the serial console output:
Code:
begin.
ready
ATE1
OK
AT+CWMODE=3
OK
AT+RST
OK
AT+CWJAP="XXXXXXXX","************************"
ets Jan 8 2013,rst cause:4, boot mode:(3,7)
wdt reset
load 0x40100000, len 816, room 16
tail 0
chksum 0x8d
load 0x3ffe8000, len 788, room 8
tail 12
chksum 0xcf
ho 0 tail 12 room 4
load 0x3ffe8314, len 288, room 12
tail 4
chksum 0xcf
csum 0xcf
2nd boot version : 1.2
SPI Speed : 40MHz
SPI Mode : QIO
SPI Flash Size : 4Mbit
jump to run user1
z
ready
AT+CIPMUX=1
OK
AT+CIPSERVER=1,15164
OK
AT+CIPSTO=30
OK
AT+GMR
AT version:0.21.0.0
SDK version:0.9.5
OK
AT+CWJAP?
+CWJAP:"NetworkG"
OK
AT+CIPSTA?
+CIPSTA:"192.168.1.50"
OK
AT+CWMODE?
+CWMODE:3
OK
AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:9f:48:d8"
+CIFSR:STAIP,"192.168.1.50"
+CIFSR:STAMAC,"18:fe:34:9f:48:d8"
OK
AT+CWLAP
+CWLAP:(2,"COLLEEN",-97,"c8:b3:73:35:c4:63",1)
+CWLAP:(4,"HOME-A2B2",-91,"00:1d:d5:bc:a2:b0",1)
+CWLAP:(0,"xfinitywifi",-88,"06:1d:d5:bc:a2:b0",1)
+CWLAP:(3,"NetworkG",-28,"e4:f4:c6:05:ba:4a",11)
+CWLAP:(4,"girlnextdoor",-87,"f8:d1:11:22:82:e2",4)
+CWLAP:(1,"F3GM0",-91,"00:1f:90:e4:bd:55",6)
+CWLAP:(3,"TEEDA-UP",-90,"6c:b0:ce:7e:9f:20",10)
OK
---------------*****##### READY TO SERVE #####*****---------------
0,CONNECT
+IPD,0,341:GET / HTTP/1.1
Host: 173.62.187.203:15164
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
1,CONNECT
-> serve homepage
***************************************** UPTIME = 34362
AT+CIPSEND=0,1959
OK
> HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Ref0088> This message brought to you by - TEENSY 3.1 & ESP8266 </font> </p> <p> Teensy server uptime <font color=#0000FF> 34362 milliseconds </font> </p></body></html><br />
SEND OK
0,CLOSED
+IPD,1,281:GET /favicon.ico HTTP/1.1
Host: 173.62.187.203:15164
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
-> serve homepage
***************************************** UPTIME = 36616
AT+CIPSEND=1,1959
OK
> HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Ref0088> This message brought to you by - TEENSY 3.1 & ESP8266 </font> </p> <p> Teensy server uptime <font color=#0000FF> 36616 milliseconds </font> </p></body></html><br />
SEND OK
1,CLOSED
0,CONNECT
1,CONNECT
+IPD,0,367:GET / HTTP/1.1
Host: 173.62.187.203:15164
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Here's a sample image of the Webpage and the Battery operated Wireless Teensy Server:

At least for now you can link directly to the Teensy Webserver yourself: http://173.62.187.203:15164/
I'm not sure how long my dynamic IP address will last.