ESP8266

From Noisebridge
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

CyanBread bb.png

 ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄ 
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌ ▀▀▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ 
▐░▌          ▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌          ▐░▌▐░▌          ▐░▌          
▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌          ▐░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ 
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ ▐░░░░░░░░░▌  ▄▄▄▄▄▄▄▄▄█░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░░░░░░░░░░░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌
▐░▌                    ▐░▌▐░▌          ▐░▌       ▐░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░▌       ▐░▌▐░▌       ▐░▌
▐░█▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄█░▌▐░▌          ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌          ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
 ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀            ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀                                                                                           

See also:


Servo Demo

from time import sleep

servo = machine.PWM(machine.Pin(13), freq=50)

def wipe():
 d = 30
 while d < 120:
  servo.duty(d)
  d = d + 1

def swipe():
 d = 120
 while d > 30:
  servo.duty(d)
  d = d - 1

def loop():
 while 1:
  wipe()
  sleep(0.5)
  swipe()
  sleep(0.5)

loop()


The ESP8266 is a small, low-cost wifi-talking board. It's the new center of the Internet of Things. Originally intended as a "wifi modem", it exposes the WiFi interface over AT-style commands.

Some hackers immediately noticed there is a general-purpose microcontroller on the box, and made a firmware for it that takes Lua programs. Now you don't need another microcontroller. Sweet!

Several additional community efforts have also been initiated and are generally discussed at http://esp8266.com one of the newer developmentss is the addition of integrated support in the Arduino IDE, further details follow in the Software section below.

Boot Modes

To load new firmware, connect GPIO0 to ground, and momentarily connect RESET to ground, or cycle the power. You can then disconnect GPIO0 and program the chip over Serial with 115,200 baud. Then reset or power cycle after the firmware upload completes.

reset causes:
        0: 
        1: normal boot
        2: reset pin
        3: software reset
        4: watchdog reset

    boot device:
        0:
        1: ram
        3: flash

Hardware


  -15cm-
		  ESP-01 Module Pinout and Wiring:
+---------+
| |-| |---|	    Vcc to 3.3v power
| | |-|   |	    EN/CH_PD & Rst each to Vcc w/10k Resistor
| |---- ..|  |	    GPIO0 to Ground & Reset for firmware update
|         | 25cm    RxD to Serial TxD & TxD to Serial RxD
| G I I R |  |	      74,480 bps for bootloader output
| n O O x |           115,200 bps for upload and debug
| d 2 0 D |
| * * * * |
| * * * * |
+---------+
  T E R V
  x N s c
  D   t c

For full specs, see [1]. Important facts:

  • 3.3v *only* - 5v will let out the majikul smoke
  • Power draw < 250 mA
  • Talks 802.1n, supports most major auth types.

There are a number of ESP8266 hardware versions. The ones of interest are:

  • ESP-01: 8 pins (basically one I/O plus power, etc.). Breadboard friendly 2x4 header (2.54mm), but not useful standalone
  • ESP-12: 16 pins (I/O, power, 9 GPIO). Non-breadboard friendly: 2mm pin spacing
  • ESP-12e: 22 pins, same as ESP-12, with an additional 6 pins on the back edge adding 1 I/O and SPI connections

Sources

Most modules are available from this ebay store http://stores.ebay.com/tomyuen007/ for $3 and up, ships from US, generally less than a week for delivery.

Software

espressif SDK

https://github.com/espressif/ESP8266_RTOS_SDK

Toolchain setup on VM: http://www.esp8266.com/wiki/doku.php?id=toolchain

Loading Firmware: http://www.esp8266.com/wiki/doku.php?id=loading_firmware

Arduino IDE

Enhancements to the Arduino IDE in versions 1.6.4 and later have enabled support for the esp8266 and the ability to upload new firmware. Version 1.6.5 r5 or later is recommended.

The current version of the IDE can be downloaded from https://www.arduino.cc/en/Main/Software

Two additional steps are required to add esp8266 support to the Arduino IDE

  1. Open the preferences menu in the Arduino IDE and add http://arduino.esp8266.com/stable/package_esp8266com_index.json into "Additional Board Manager URLs" field
  2. Open Boards Manager from "Tools > Board: ______ > Boards Manager..." menu, scroll down to esp8266, click to select it and then click the install button.

Details about esp8266 support can be found at https://github.com/esp8266/Arduino

Once it downloads you'll see "ESP8266 Modules" section added to the list of target boards under "Tools > Board: ______>". You can use the "Generic ESP8266 Module" option for programming ESP-## modules using a 3.3v USB-Serial connector.

You'll need a USB to Serial cable/dongle for programming the board, connecting Ground/Rx/Tx. If you're using the cheap USB dongles, like the AI branded ones using a CH340G chip, you may also need to install the drivers.

In order to enable the ESP8266 to accept new firmware, temporarily connect GPIO0 to ground, and cycle the power.

LUA

There are a wide variety of firmware builds available for the chip. Of interest is the software NodeMCU, which turns the serial port in to a Lua REPL. Yesac is working on an environment within NodeMCU for doing TFTP and some other junk.

Uploading firmware is easy with esptool

Projects

NUB plug

NUBplug.png


IoT X'ample

Breadboarded multi-function device using ESP-12 module, PIR sensor, buzzer, indicator LEDs and light sensor.

See additional PIR info ESP8266/PIR

BoardRoom bb.png

The following code can be used on the above diagrammed hardware. Using a web browser the ESP8266 will respond the following url requests

/*

   AS IS NO GUARANTEE NO WARRANTY

*/

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "YourWiFi";
const char *password = "WiFiPassword";

ESP8266WebServer server ( 80 );

const int pir = 4;
const int led = 14;
const int buzzer = 12;

void handleRoot() {
	
	char temp[400];
	int sec = millis() / 1000;
	int min = sec / 60;
	int hr = min / 60;

	snprintf ( temp, 400,

"<html>\
  <head>\
    <title>PIR Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h1>Hello from ESP8266 PIR!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p>Motion: %s</p>\
    <p>Ambient: %d%</p>\
  </body>\
</html>",

		hr, min % 60, sec % 60, digitalRead(pir)?"true":"false", round(analogRead(A0)/10.24)
	);
	server.send ( 200, "text/html", temp );
}

void handleBuzz() {
  	
	char temp[400];
	int sec = millis() / 1000;
	int min = sec / 60;
	int hr = min / 60;

	snprintf ( temp, 400,

"<html>\
  <head>\
    <title>BUZZ!!!</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #ff0000; }\
    </style>\
  </head>\
  <body>\
    <h1>BUZZ!!!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p>Motion: %s</p>\
  </body>\
</html>",

		hr, min % 60, sec % 60, digitalRead(pir)?"true":"false"
	);
	server.send ( 200, "text/html", temp );

        digitalWrite(buzzer, HIGH);
        delay(300);
        digitalWrite(buzzer, LOW);
}
  

void handleNotFound() {
	String message = "File Not Found\n\n";
	message += "URI: ";
	message += server.uri();
	message += "\nMethod: ";
	message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
	message += "\nArguments: ";
	message += server.args();
	message += "\n";

	for ( uint8_t i = 0; i < server.args(); i++ ) {
		message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
	}

	server.send ( 404, "text/plain", message );
}

void setup ( void ) {
	pinMode ( led, OUTPUT );
	digitalWrite ( led, 0 );
         
        pinMode(buzzer, OUTPUT);
        digitalWrite(buzzer, 0);
        
        pinMode(pir, INPUT);

	Serial.begin ( 115200 );
	WiFi.begin ( ssid, password );
	Serial.println ( "" );

	// Wait for connection
	while ( WiFi.status() != WL_CONNECTED ) {
		delay ( 500 );
		Serial.print ( "." );
	}

	Serial.println ( "" );
	Serial.print ( "Connected to " );
	Serial.println ( ssid );
	Serial.print ( "IP address: " );
	Serial.println ( WiFi.localIP() );

	server.on ( "/", handleRoot );
        server.on("/buzz", handleBuzz);
	server.on ( "/hello", []() {
		server.send ( 200, "text/plain", "hi, how ya doin?" );
	} );
	server.onNotFound ( handleNotFound );
	server.begin();
	Serial.println ( "HTTP server started" );
}

void loop ( void ) {
        
        if (digitalRead(pir)) {
          // Do something interesting on motion
        } else {
          // Nothing new here
        }

        // LED on if motion == true
        digitalWrite(led, digitalRead(pir));

	server.handleClient();
}