Super stuck with ESP-01 serial module and I'm desperate for help.

Status
Not open for further replies.

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....


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:
Fixed CODE tags above.
Is the ESP-01 running as AT or Arduino? Which unit is that the code for ESP-01 or ESP8266? Been about 3 years since I did anything with ESP8266 - I did a webserver programmed through Teensy when OTA until OTA was online. Indeed pushing all the sketch code over takes some time - much faster OTA than Serial.
Not sure if it helps anything given the years gone by and the ESP updates - and not sure how the ESP-01 being used etc … but what I last used is :: github.com/Defragster/ArduinoTeensyESP8266
 
Thanks for the reply. I've spent probably 50 hours trying to find any example that creates an access point that allows multiple clients and that can update just the data on a page using these cheap ESP-01 devices. Never came close. This WiFi stuff (including web development) is just not ready for the average joe.

I did fire up an ESP8266 and with first example at this site: http://www.ulasdikme.com/projects/esp8266/esp8266_ajax_websocket.php and it does what I'm looking for...now i'll spend hours hacking it into shape. After I test if my EBYTE libs and ILI9341 drivers work on an ESP8266.

PS how did you get code tags to work? In my reply and if I try to create a new post, i don't get any of the nice tools to add code tags, images, etc. IDK..
 
Clicking the " # " icon gives the following ::
Code:
 // code here

CodeTag.png
 
OK i have a plan and a prototype is working. Refresher as to what i'm trying to do:

1. remote telemetry on a race car sends performance data to the pit where performance data is displayed on an 2.8" TFT (we use teensy 3.2 for send and receive) and it's working like a million bucks--after a few years of debugging noise issues
2. Now we want to broadcast the received data to phones, and a PC for displaying the data on a large TV--hence a web server using an access point as we're never near a router. Forget blue tooth as its only 1:1

#2 is where all heck broke lose as the ESP-01 units that i bought to connect to the Teensy don't have adequate library support for serial use and the ESP8266 has great libs, but too many limitations to be used in our pit display module so I have to somehow us both...Here's what i'm now implementing (prototype works)

1. The pit unit receives data in a struct using a Teensy connected to an EBYTE, TFT, etc, then I send the struct to the ESP8266 via serial (now that is working with easyTransfer lib). Teensy feeds the ESP 50 bytes every second or so)
2. The ESP creates an access point and serves web pages where i parse the struct build and XML string and send to the web pages using AJAX to updated just the values, (this took hacking some example from YouTube)
3. A phone or a PC can connect to the access point, get the web page and display. The PC will display data to a TV through HDMI

The pain now will be writing HTML code to create a pretty web page.

Thanks to my other thread in communicating Teensy and ESP modules...
 
For what it is now worth, it looks like I actually DO have couple of the ESP8266 boards...
In particular I have a set of these: https://smile.amazon.com/gp/product/B01IK9GEQG

Which if I look at their page for this: https://www.makerfocus.com/products...for-esp8266-esp01?_pos=2&_sid=b4dc802da&_ss=r

And as mentioned I believe that these have two UARTS. I ag guessing the the ones marked TX/RX are in this case used by the CP2102...
But I believe there is a second one on Arduino pin 17?=D4(TX) 13=SD1(RX)
But I might be reading that one wrong... But that is what I would check if I were you
 
Status
Not open for further replies.
Back
Top