TIMER ESP ARDUINO
//
// ESP8266 Timer Example
// SwitchDoc Labs October 2015
//
extern "C" {
#include "user_interface.h"
}
os_timer_t myTimer;
bool tickOccured;
// start of timerCallback
void timerCallback(void *pArg) {
tickOccured = true;
} // End of timerCallback
void user_init(void) {
/*
os_timer_setfn - Define a function to be called when the timer fires
void os_timer_setfn(
os_timer_t *pTimer,
os_timer_func_t *pFunction,
void *pArg)
Define the callback function that will be called when the timer reaches zero. The pTimer parameters is a pointer to the timer control structure.
The pFunction parameters is a pointer to the callback function.
The pArg parameter is a value that will be passed into the called back function. The callback function should have the signature:
void (*functionName)(void *pArg)
The pArg parameter is the value registered with the callback function.
*/
os_timer_setfn(&myTimer, timerCallback, NULL);
/*
os_timer_arm - Enable a millisecond granularity timer.
void os_timer_arm(
os_timer_t *pTimer,
uint32_t milliseconds,
bool repeat)
Arm a timer such that is starts ticking and fires when the clock reaches zero.
The pTimer parameter is a pointed to a timer control structure.
The milliseconds parameter is the duration of the timer measured in milliseconds. The repeat parameter is whether or not the timer will restart once it has reached zero.
*/
os_timer_arm(&myTimer, 1000, true);
} // End of user_init
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println("");
Serial.println("--------------------------");
Serial.println("ESP8266 Timer Test");
Serial.println("--------------------------");
tickOccured = false;
user_init();
}
void loop() {
if (tickOccured == true)
{
Serial.println("Tick Occurred");
tickOccured = false;
}
yield(); // or delay(0);
}
HTML CODE
<html>
<a href="gpio/1">open</a><br>
<a href="gpio/0">close</a>
</html>
ARDUINO CODE
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio/0 will set the GPIO2 low,
* http://server_ip/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
const char* ssid = "Telecom-44934373";
const char* password = "WpBxZ8JyV8ha2J0CfR34w5st";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
s += "<h1><a href='gpio/1'>open</a><br><a href='gpio/0'>close</a><br>";
s += "\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);
client.flush();
// Prepare the response
// Match the request
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
s += "<h1><a href='gpio/1'>open</a><br><a href='gpio/0'>close</a><br>";
s += "\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
ESP12 CONNECTING
Just for information, my experience. I am using a esp8266-12 and a Crius USB FTDI interface.
Connections like in
http://cdn.instructables.com/FCH/0MSC/I ... .LARGE.jpg
CH_PD to VCC (permanent)
GPIO2 to VCC (permanent)
GPIO15 to GND (permanent)
GPIO0 to GND (for firmware flash, only during boot-up)
RX to TX
TX to RX
No resistors or capacitors.
Esptool always terminating at 7% or 11% with error "Failed to write to target flash."
No error in Arduino IDE, but Sketch wont start (no reply in Terminal).
Changing ESP_RAM_BLOCK and ESP_FLASH_BLOCK in esptool.py did the trick. Esptool successful, and sketch running ok. (see post #13640 in this thread)
A similar setup with an ESP8266-01 was running perfect even without changes to esptool.py. - See more at: http://www.esp8266.com/viewtopic.php?f=13&t=1246&start=36#sthash.K3rIPaNx.dpuf