ESP8266 with Teensy

Status
Not open for further replies.

satsatt

Member
This ESP WiFi2Serial Module is so much fun. I could get 2 Modules here in Germany but i had to do some modification in order to flash the firmware update to 0018000922. I used a stereo microscope for the soldering and if anyone needs artificial insemination ...? ;) i think now i am fit for it.
I hoked it up directly to Serial1 on the Teensy3.1 and supported it with a breadboard powersupply of 3.3V
It costs me some long nights to adapt some code from https://hackaday.io/project/3072-esp8266-retro-browser for my heavy testing. It has no delay() function in the main loop as most of the other codes have and it runs with different Baud rates nevertheless.
If anyone can make a library out of it, it could be useful.

Code:
/* ESP8266 RetroBrowser for Teensy3.1 (direct connection , only separate 3,3V for ESP)
originated from http://hackaday.io/project/3072/instructions
	
*/
	
#define SSID        "MAX" // WLAN SSID
#define PASS        "87xxxxxxxxxxxx" // WLAN PW

#define DEST_HOST   "retro.hackaday.com"
#define DEST_IP     "192.254.235.21"

#define TIMEOUT     10000 // ms connection to AP maybe slow
#define  CONTINUE    false
#define  HALT        true


#define CPU_RESTART_ADDR (uint32_t *)0xE000ED0C
#define CPU_RESTART_VAL 0x5FA0004
#define CPU_RESTART (*CPU_RESTART_ADDR = CPU_RESTART_VAL); //may restart Teensy with macro CPU_RESTART but loose USB connection

#define ECHO_COMMANDS // Un-comment to echo AT+ commands to serial monitor

// Print error message and loop stop.
void errorHalt(String msg)
{
  Serial.println(msg);
  Serial.println("*HALT*");
  //CPU_RESTART //xxx
  while(true){};
}

// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
  byte current_char   = 0;
  byte keyword_length = keyword.length();
  
  // Fail if the target string has not been sent by deadline.
  long deadline = millis() + TIMEOUT;
  while(millis() < deadline)
  {
    if (Serial1.available())
    {
      char ch = Serial1.read();
      Serial.write(ch);
      if (ch == keyword[current_char])
        if (++current_char == keyword_length)
        {
          Serial.println();
          return true;
        }
    }
  }
  return false;  // Timed out
}

// Read and echo all available module output.
// (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.)
void echoFlush()
  {while(Serial1.available()) Serial.write(Serial1.read());}

  
// Echo module output until 3 newlines encountered.
// (Used when we're indifferent to "OK" vs. "no change" responses.)
void echoSkip()
{
  echoFind("\n");        // Search for nl at end of command echo
  echoFind("\n");        // Search for 2nd nl at end of response.
  echoFind("\n");        // Search for 3rd nl at end of blank line.
}

// Send a command to the module and wait for acknowledgement string
// (or flush module output if no ack specified).
// Echoes all data received to the serial monitor.
boolean echoCommand(String cmd, String ack, boolean halt_on_fail)
{
  Serial1.println(cmd);
  #ifdef ECHO_COMMANDS
    Serial.print("-->"); Serial.println(cmd);
  #endif
  
  // If no ack response specified, skip all available module output.
  if (ack == "")
    echoSkip();
  else
    // Otherwise wait for ack.
    if (!echoFind(ack))          // timed out waiting for ack string 
      if (halt_on_fail)			 // CONTINUE=false - skip critical halt
        errorHalt(cmd+" *failed");// Critical failure halt. ( ack not empty and not found and Halt true ) 
      else
        return false;            // Let the caller handle it.
  Serial.println("*==================*"); // request and answer OK
  return true;                   // ack blank or ack found
}

// Connect to the specified wireless network.
boolean connectWiFi()
{
  String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\"";
			 //AT+CWJAP="SSID","12345678"
    if (echoCommand(cmd, "OK", CONTINUE)) // Join Access Point
  {
    Serial.println("*connected to WiFi*");
	
    return true;
  }
  else
  {
    Serial.println("*Connection to WiFi failed.");
    return false;
  }
}

// ******** SETUP ********
void setup()  
{
  Serial.begin(115200);         // Arduino serial Monitor via USB Teensy3.1
  Serial1.begin(115200);        // Communication with ESP8266 
  
  Serial1.setTimeout(TIMEOUT);
  
  delay(5000); // wait for serial init
  // echoCommand("AT+CIOBAUD=115200", "BAUD->115200", CONTINUE); // or 9600 if you like it - slow need hard reset
  while(!Serial); // wait for open USBser
  Serial.println("*ESP8266 Demo*\n\r*0018000922 !*");
  Serial.println("*on Teensy 3.1*");
  echoCommand("AT+CHELLO", "OK", CONTINUE);
  echoCommand("AT+RST", "[System Ready, Vendor:www.ai-thinker.com]", HALT);    // Reset & test if the module is ready  
  Serial.println("*Module is ready.");
  
  echoCommand("AT+GMR", "OK", CONTINUE);   // Retrieves the firmware ID (version number) of the module. 
  // Get connection status 
  echoCommand("AT+CIPSTATUS", "OK", CONTINUE); // Status:TCP/IP
  echoCommand("AT+CWMODE=1", "", HALT);    // Station mode - maybe " no change " only
  echoCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode. 
  echoCommand("AT+CWLAP", "OK", CONTINUE); // List available access points
  
  
  echoCommand("AT+CIPMUX=1", "OK", HALT);    // Allow multiple connections (we'll only use the first).
    echoCommand("AT+CIPMODE?", "OK", HALT);  // default=0 
  //connect to the wifi
  boolean connection_established = false;
  for(int i=0;i<5;i++)
  {
			
	if(connectWiFi())
    {
      
	  connection_established = true;
	  echoCommand("AT+CIFSR", "OK", HALT);         // Echo IP address. also 0.0.0.0 is possible but bad)
      break;
    }
  }
  if (!connection_established) errorHalt("*Connection failed");
  
  delay(5000);
  //echoCommand("AT+CWSAP?", "OK", CONTINUE); // set the parameters of AP and Test - maybe only first time useful
    
}

// ******** LOOP ********
void loop() 
{
  // Establish TCP connection
  // AT+CIPSTART=0,"TCP","192.254.235.21",80
  String cmd = "AT+CIPSTART=0,\"TCP\",\""; cmd += DEST_IP; cmd += "\",80";
  
    if (!echoCommand(cmd, "Linked", HALT)) return; //no ip//Link typ ERROR
  // Get connection status 
  echoCommand("AT+CIPSTATUS", "OK", CONTINUE); // Status:3 ist ok
  
  // Build HTTP request. GET / HTTP/1.1
                       //Host: retro.hackaday.com:80
  cmd = "GET / HTTP/1.1\r\nHost: "; cmd += DEST_HOST; cmd += ":80\r\n\r\n";
  
  // Ready the module to receive raw data
  if (!echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", HALT))
  {
    echoCommand("AT+CIPCLOSE", "", CONTINUE);
    Serial.println("*Connection timeout.");
    return;
  }
  
  // Send the raw HTTP request
  echoCommand(cmd, "OK", CONTINUE);  // GET
  
  // Loop forever echoing data received from destination server.
  while(true)
    while (Serial1.available())
      Serial.write(Serial1.read());
      
  errorHalt("ONCE ONLY"); // never happens to me
}

is this while (Serial1.available()) maybe not efficient for Teensy because Serial1 is always available after Serial1.begin() even if the buffer is empty?
 
Last edited:
This ESP WiFi2Serial Module is so much fun. I could get 2 Modules here in Germany but i had to do some modification in order to flash the firmware update to 0018000922.
So you have version 1 with only 3.3v, gnd, rx and tx pins active? do you mind sharing how you got this version to accept new firmware?
 
So you have version 1 with only 3.3v, gnd, rx and tx pins active?

Yes i have Version ESP-05. For firmwareupdate you only need access to GPIO0 to set to GND while flashing. I cut near the pin and soldered at this pin. I also cut and made another connection for CH_EN/PD. For normal Mode connect both again to 3.3V.

ESP_05mod.PNG

Then use the update as mentioned here http://blog.electrodragon.com/cloud-updating-your-wi07c-esp8266-now/

to update to 0901 and 0902. I used the same flasher for the not cloud version v09.2.2ATFirmware.bin from www.ai-thinker.com. Don't be confused - the modul still reports 0902 afterwards.

good luck and don't bother with this crappy XTCOM_Util if you have Win7.
 
Last edited:
Nice work. I have a few more of these coming in shortly. Not sure if CH_PD and GPIO0 is broken out, so this is helpful. Are you planning on customizing the firmware o just use it as is with Teensy?
 
Are you planning on customizing the firmware

I have seen some new flash version v0923 on the chinese website but my chinese and my skills are fare from sufficient to understand whats going on. I assume the pro's are flashing changed areas of the firmware separately for more functionality.

This thing is so cheap that you can easily combine it with an ATiny when an Teensy is an overkill. ( maybe for an WiFi switch )

My goal is to connect it with lab instruments ( but i need a 2 way communication ) or even read the temp or humidity values from 433MHz sensors ( even from my neighbors - hi ) where i have no wired Internet to visualize them on a website.

But i have no knowledge how to send to and receive data from a customized website with buttons and gauges yet.
For now i am very confused about JSON Java Phyton Ajax ... and so on. ( so please remember i studied chemistry )
 
Last edited:
My goal is to connect it with lab instruments ( but i need a 2 way communication ) or even read the temp or humidity values from 433MHz sensors ( even from my neighbors - hi ) where i have no wired Internet to visualize them on a website.

If I understand you correctly you want this setup: Instrument <-serial-> Teensy 3.0 <-serial-> ESP8266. If all you need is to convert serial data to TCP or UDP packets to feed a separate web server and vice verse the standard firmware can do that. I have this up and running with one way communication, but I have not reason to believe it would be an issue with 2 ways.

If you want the ESP8266 to be the webserver communicating with the Teensy3 you will need a custom firmware - there is an example here... http://www.esp8266.com/viewtopic.php?f=6&t=376
 
Last edited:
Yes this is the idea thank's - but compiling my own firmware goes beyond my skills for now. I found someone else who did it but no useful detailed information included for me.
http://youtu.be/Zuq7CZwrsAg

Sorry if I was not clear. If all you want to do is to use esp8266 as an WIFI to UART bridge it is completely possible with the standard firmware. your link is referring to a webserver running on the esp8266.

This scenario is possible with the standard firmware:
So lets say you wanted to your instrument data on your android phone - you would create an app that would be an UDP or TCP client sending requests to the ESP8266, which pass it on to teensy via UART. Teensy polls the instrument data and send it via UART to ESP8266 which turns it into an UDP or TCP package.
 
Yes i understand - this would be already really nice but to be completely honest, i need at least the data ( better also the to send commandos for the instruments and some valves /switches ) in a MSExcel environment for further analysis. ( and real time control ).

But if i dismiss automatic control for now your suggestion for an android app seems sweet enough. ( just managed my first " Hello World" app hiii ).

Btw i am searching for some already finished Fritzing files for the different ESP modules but i guess i have to create them for myself. I got some ESP-02 with half hole pads which have totally different pin locations as the standard 02 one.

Update: i made a fritzing part for this module, maybe someone likes it.
ESP8266 ESP02n.fzpz
 
Last edited:
This link http://www.esp8266.com/viewtopic.php?f=6&t=376 is about flashing a webserver on the ESP but first i need some howto serial communicating between Teensy and ESP and receiving/sending it wireless on the "other side"

Maybe if it would be possible for teensy to format the data like TCP/IP modbus protokoll i could use modbus on the MSwindows side to collect and send data?
 
Last edited:
Status
Not open for further replies.
Back
Top