Hirdetés

Új hozzászólás Aktív témák

  • Zoja™

    őstag

    Uraim, megjött a Wemos D1-em, amivel ma tudtam kicsit játszani.
    Egy relé kapcsolgatásával kezdtem az ismerkedést, de valamiért fordítva működik.

    Alapból bekapcsolt helyzetből indul, ON-ra kikapcsol, OFF-ra pedig be. :DDD

    Itt a kód, ha vetnétek rá egy pillantást, hogy mi a hiba azt nagyon megköszönném. :R

    // Import required libraries
    #include <ESP8266WiFi.h>
    #include <ESP8266Ping.h>
    #include "Timer.h"
    #include <aREST.h>
    #include <aREST_UI.h>

    // Create aREST instance
    aREST_UI rest = aREST_UI();

    // WiFi parameters
    const char* ssid = "*********"; //Change the stars with your SSID
    const char* password = "********"; //Change the stars with your password

    Timer t;
    IPAddress ip(192,168,0,109); //The static IP you'd like your lamp to get from the router
    IPAddress gateway(192,168,0,1);
    IPAddress subnet(255,255,255,0);

    // The port to listen for incoming TCP connections
    #define LISTEN_PORT 80

    // Create an instance of the server
    WiFiServer server(LISTEN_PORT);

    // Variables to be exposed to the API

    int ledControl(String command);

    void checkwithPing()
    {
    Serial.print("Pinging ip ");
    Serial.println(gateway);

    if(Ping.ping(gateway)) {
    Serial.println("Success!!");
    Serial.println(Ping.averageTime());
    } else {
    Serial.println("Error :(");
    ESP.reset();
    }
    }

    //SETUP -- SETUP -- SETUP -- SETUP -- SETUP -- SETUP -- SETUP -- SETUP --
    //SETUP -- SETUP -- SETUP -- SETUP -- SETUP -- SETUP -- SETUP -- SETUP --

    void setup()
    {
    // Start Serial
    Serial.begin(115200);

    // Set the title
    rest.title("8/61 ");

    // Create button to control pin 5
    rest.button(5);

    // Function to be exposed
    rest.function("led", ledControl);

    // Give name and ID to device
    rest.set_id("1");
    rest.set_name("esp8266");

    // Connect to WiFi
    WiFi.begin(ssid, password);
    WiFi.config(ip, gateway, subnet);
    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());

    t.every(10000, checkwithPing); //Check if connected every 10 seconds
    }

    void loop()
    {
    t.update();
    // Handle REST calls
    WiFiClient client = server.available();
    if (!client) {
    return;
    }
    while (!client.available()) {
    delay(1);
    }
    rest.handle(client);
    }

    int ledControl(String command) {
    // Print command
    Serial.println(command);

    // Get state from command
    int state = command.toInt();

    digitalWrite(5, state);
    return 1;
    }

    [ Szerkesztve ]

Új hozzászólás Aktív témák