need some help to send data
Posted: Sat May 02, 2015 4:56 pm
hi, i'm using this code on my ESP-03 :
there is a button on GPIO12 and a led in GPIO13. I can use the button or a GET to the ESP-03 IP to change the led's state. Now i want to send the LED status to a url, http://192.168.1.2:9000/status/on or http://192.168.1.2:9000/status/off , every time the led changes. I'm try to combine my code with WiFiClient example, but i don't know how to do this, please can you help me?
Thanks
Code: Select all
#include <ESP8266WiFi.h>
const char* ssid = "myessid";
const char* password = "mypass";
WiFiServer server(80);
int val=0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
Serial.begin(115200);
delay(10);
pinMode(13, OUTPUT);
digitalWrite(13, 0);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.begin();
pinMode(12, INPUT);
}
void loop() {
buttonState = digitalRead(12);
if (buttonState == HIGH && lastButtonState == LOW) {
delay(100);
if (val == 0) {
digitalWrite(13, HIGH);
val = 1;
}
else {
digitalWrite(13, LOW);
val = 0;
}
}
lastButtonState = buttonState;
WiFiClient client = server.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
String req = client.readStringUntil('\r');
client.flush();
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
client.stop();
return;
}
digitalWrite(13, val);
client.flush();
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
}
there is a button on GPIO12 and a led in GPIO13. I can use the button or a GET to the ESP-03 IP to change the led's state. Now i want to send the LED status to a url, http://192.168.1.2:9000/status/on or http://192.168.1.2:9000/status/off , every time the led changes. I'm try to combine my code with WiFiClient example, but i don't know how to do this, please can you help me?
Thanks