Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Serveri kood
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- const char *ssid = "Peldik";
- const char *password = "password";
- ESP8266WebServer server(80);
- void handleRoot() {
- server.send(200, "text/html", "<h1>Hello! from arduino-er!</h1>");
- digitalWrite(D4,HIGH);
- }
- char* htmlBody_help = "<h1>Help</h1><br/>\n"
- "Visit http://192.168.4.1/ to access web server.<br/>\n"
- "Visit http://192.168.4.1/help to access this page.<br/>\n";
- void handleHelp(){
- server.send(200, "text/html", htmlBody_help);
- digitalWrite(D4, HIGH);
- }
- void handlePage(){
- server.send(200, "text/html", "<h1> Valjas</h1>");
- digitalWrite(D4, LOW);
- }
- void setup() {
- delay(200);
- Serial.begin(9600);
- Serial.println();
- WiFi.softAP(ssid, password);
- IPAddress apip = WiFi.softAPIP();
- Serial.print("visit: \n");
- Serial.println(apip);
- server.on("/", handleRoot);
- server.on("/help", handleHelp);
- server.on("/page", handlePage);
- server.begin();
- Serial.println("HTTP server üleval");
- pinMode(D4, OUTPUT);
- }
- void loop() {
- server.handleClient();
- }
- Klient
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266HTTPClient.h>
- #define USE_SERIAL Serial
- ESP8266WiFiMulti WiFiMulti;
- int buttonstate= 0;
- const int buttonpin= 16;
- void setup() {
- USE_SERIAL.begin(115200);
- // USE_SERIAL.setDebugOutput(true);
- USE_SERIAL.println();
- USE_SERIAL.println();
- USE_SERIAL.println();
- const int buttonpin= 16;
- pinMode(buttonpin, INPUT);
- int buttonstate= 0;
- for(uint8_t t = 4; t > 0; t--) {
- USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
- USE_SERIAL.flush();
- delay(200);
- }
- WiFiMulti.addAP("Peldik", "password");
- }
- void loop() {
- // wait for WiFi connection
- if((WiFiMulti.run() == WL_CONNECTED)) {
- HTTPClient http;
- USE_SERIAL.print("[HTTP] begin...\n");
- // configure traged server and url
- //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
- USE_SERIAL.print("[HTTP] GET...\n");
- // start connection and send HTTP header
- int httpCode = http.GET();
- // httpCode will be negative on error
- if(httpCode > 0) {
- // HTTP header has been send and Server response header has been handled
- USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
- /* buttonstate = digitalRead(buttonpin);
- // file found at server
- if(httpCode == HTTP_CODE_OK) {
- String payload = http.getString();
- USE_SERIAL.println(payload);
- if(payload == "1"){
- digitalWrite(D4, HIGH);
- } else {
- digitalWrite(D4, LOW);
- }*/
- buttonstate= digitalRead(buttonpin);
- if(buttonstate == HIGH){
- http.begin("http://192.168.4.1/help");
- }
- if(buttonstate == LOW){
- http.begin("http://192.168.4.1/page");
- }} else {
- USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- }
- delay(200);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement