#define BUFFER_SIZE 2048
#define SSID "*******" // change this to match your WiFi SSID
#define PASS "********1" // change this to match your WiFi password
#define PORT "80" // 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() {
Serial1.begin(115200); // TEENSY Hardware Serial port 1 (pins 0 and 1)
Serial.begin(115200); // TEENSY USB Serial Port
delay(2000);
Serial.println("begin.");
setupWiFi();
// print device IP address
Serial.print("device ip addr: ");
Serial1.println("AT+CIFSR");
wait_for_esp_response(1000);
analogReadResolution(13);
}
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(5);
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: 1\r\n";
String content = "";
content += "<!DOCTYPE html>";
content += "<html>";
content += "<body>";
content += " <h1> Checking for T32 & ESP8266</h1> <br/> ";
content += "<font color=#0000FF> ";
float val = analogRead(A0) * 0.40283203125 / 1000;
content += String(val, 3);
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(1000)) {
//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);
Serial.println("---------------*****##### READY TO SERVE #####*****---------------");
}