KrisKasprzak
Well-known member
I’m trying to use my Teensy 3.2 and an ESP-01 (which is a small serial-based device) to create an access point (which I’ll call a web server) to serve a web page to various devices such as phones and PC’s. I want the Teensy to measure stuff (analogRead for example), the post the value to the web page. However, I’ll be sending like 30 values to the web page so constant web page refresh is not an option—I’ve tried this and it’s ugly and very slow. I refuse to use and ESP-8266 boards as it takes like 5 minutes to compile—and it ‘aint a Teensy.
I’m reading about JSON strings to send data to the web page using AJAX and this looks promising as only the data updates. I have no clue how to code as web page using JAVA, and JSON strings for updating. I have come across 2 class of examples, neither are applicable to my case
1. Data is measured and sent to a web page using ESP8266 modules using ESP8266 libraries which DON’T talk to a serial based ESP-01
2. Teensy+ESP-01 (the serial unit) sends HTML pages with full page refresh
I have come across this page and one of the examples uses my hardware but sends AT commands (the dial gage example on page 2), but unfortunately I cannot get the gage to display.
https://forum.pjrc.com/threads/27850-A-Guide-To-Using-ESP8266-With-TEENSY-3
Can someone please give me a hand? Ideally give me some working code that I can expand upon.
Edit: Code I'm hacking together ( I guess you can't add code tags upon and edit....
I’m reading about JSON strings to send data to the web page using AJAX and this looks promising as only the data updates. I have no clue how to code as web page using JAVA, and JSON strings for updating. I have come across 2 class of examples, neither are applicable to my case
1. Data is measured and sent to a web page using ESP8266 modules using ESP8266 libraries which DON’T talk to a serial based ESP-01
2. Teensy+ESP-01 (the serial unit) sends HTML pages with full page refresh
I have come across this page and one of the examples uses my hardware but sends AT commands (the dial gage example on page 2), but unfortunately I cannot get the gage to display.
https://forum.pjrc.com/threads/27850-A-Guide-To-Using-ESP8266-With-TEENSY-3
Can someone please give me a hand? Ideally give me some working code that I can expand upon.
Edit: Code I'm hacking together ( I guess you can't add code tags upon and edit....
Code:
/*
Library
[url]https://github.com/bportaluri/WiFiEsp[/url]
*/
#include "WiFiEsp.h"
char ssid[] = "TEST_SERVER"; // your network SSID (name)
char pass[] = "1111111111"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
int reqCount = 0; // number of requests received
int Data0, Data1;
WiFiEspServer server(80);
// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);
void setup()
{
Serial.begin(115200); // initialize serial for debugging
while (!Serial) {}
Serial1.begin(115200); // initialize serial for ESP module
WiFi.init(&Serial1); // initialize ESP module
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true); // don't continue
}
Serial.print("Attempting to start AP ");
Serial.println(ssid);
// uncomment these two lines if you want to set the IP address of the AP
IPAddress localIp(192, 168, 4, 4);
WiFi.configAP(localIp);
// start access point
status = WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);
Serial.println("Access point started");
printWifiStatus();
// start the web server on port 80
server.begin();
Serial.println("Server started");
}
void loop()
{
Data0 = analogRead(A0);
Data1 = analogRead(A1);
WiFiEspClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New client"); // print a message out the serial port
buf.init(); // initialize the circular buffer
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
buf.push(c); // push it to the ring buffer
// you got two newline characters in a row
// that's the end of the HTTP request, so send a response
if (buf.endsWith("\r\n\r\n")) {
sendHttpResponse(client);
break;
}
}
}
// give the web browser time to receive the data
delay(10);
// close the connection
client.stop();
Serial.println("Client disconnected");
}
}
void sendHttpResponse(WiFiEspClient client) {
client.print("<!DOCTYPE html>");
client.print("<html>");
client.print("<body>");
client.print("<h2>What Can JavaScript Do?</h2>");
// I want these Data0 and Data1 to update automatically but not
// reload the whole page
client.print("<br>\r\n");
client.print("Analog input A0: ");
client.print(Data0);
client.print("<br>\r\n");
client.print("Analog input A1: ");
client.print(Data1);
client.print("<br>\r\n");
client.print("</html>\r\n");
// cute little java button will not reload the page
// PS i don't want a manual update button, variables need to refresh
// automatically
client.print("<p id=\"demo\">JavaScript can change HTML content.</p>");
client.print("<button type=\"button\" onclick='document.getElementById(\"demo\").innerHTML = \"Hello JavaScript!\"'>Click Me!</button>");
client.print("</body>");
client.print("</html>");
}
void xxsendHttpResponse(WiFiEspClient client)
{
client.print(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n" // the connection will be closed after completion of the response
"Refresh: 20\r\n" // refresh the page automatically every 20 sec
"\r\n");
client.print("<!DOCTYPE HTML>\r\n");
client.print("<html>\r\n");
client.print("<h1>Hello World!</h1>\r\n");
client.print("Requests received: ");
client.print(++reqCount);
client.print("<br>\r\n");
client.print("Analog input A0: ");
client.print(Data0);
client.print("<br>\r\n");
client.print("Analog input A1: ");
client.print(Data1);
client.print("<br>\r\n");
client.print("</html>\r\n");
}
void printWifiStatus()
{
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in the browser
Serial.println();
Serial.print("To see this page in action, connect to ");
Serial.print(ssid);
Serial.print(" and open a browser to http://");
Serial.println(ip);
Serial.println();
}
Last edited by a moderator: