Simple arduino control over the Internet



    Good day to all.

    About a year ago, I wrote a short review article to control Arduino over the Internet using a NinjaBlocks server. It was a pretty good and convenient solution, and it worked fine until, at one point, connection problems started. Attempts to persuade developers to solve problems through the forum were in vain - they simply ignored my requests and did not even bother to answer, which was very sad.

    From that moment, the entire Internet was scanned in search of a replacement - and many very interesting projects were found, but they were either too complicated to implement and required significant programming knowledge, or were simply uncomfortable. And here the thought came, why not do everything yourself.

    Of course, I really wanted to use modern data transfer protocols websockets or mqtt, which would allow to control all processes in real time, but if things were good with the client for arduino - the presence of good libraries was good, then things were worse with the server side - we needed servers with support for the necessary protocols that the usual hoster did not have. And I did not want to start my own server for the sake of lighting the LED. And the choice fell on the good and old http.

    1. How it all works.

    We have:
    - a server on php located on a hosting that is tied to a domain name
    - a client in the form of arduino
    - the

    Arduino control panel connects to the server and sends a GET request that contains the values ​​of the temperature sensors.

    The server accepts the request and writes the temperature values ​​to text files. At the same time, it reads from the text file the value of the installed output for arduino and sends it in response to a controller request.

    Arduino receives a response from the server and, according to it, sets the status of its output.

    The control panel, using Ajax, reads the temperature value from text files and updates the sensors. It also reads their text file output status and updates it on the page. Using the same Ajax, the controller output value is written to the text file through the form, from where the server will then take the value and send it to the controller.

    2. Client on Arduino

    A sketch is quite simple, all it does is collect the sensors and send them to the server, receive a response, enable or disable the output.

    Sketch Arduino
    #include
    #include
    #include
    #include

    #define ONE_WIRE_BUS 2
    OneWire oneWire (ONE_WIRE_BUS);
    DallasTemperature sensors (& oneWire);

    byte mac [] = {0x54, 0x34, 0x41, 0x30, 0x30, 0x31};

    EthernetClient client;
    char server [] = "*************"; // your server name
    int buff = 0;
    const int led = 5;

    void setup ()
    {
    Ethernet.begin (mac);
    sensors.begin ();
    pinMode (led, OUTPUT);
    digitalWrite (led, LOW);
    }

    void loop ()
    {

    sensors.requestTemperatures ();

    if (client.connect (server, 80))
    {

    client.print ("GET /add_data.php?");
    client.print ("temperature =");
    client.print (sensors.getTempCByIndex (0));
    client.print ("&");
    client.print ("&");
    client.print ("temperature1 =");
    client.print (sensors.getTempCByIndex (1));
    client.println ("HTTP / 1.1");
    client.print ("Host:");
    client.println (server);
    client.println ("Connection: close");
    client.println ();
    client.println ();

    delay (200);

    while (client.available ())
    {
    char c = client.read ();
    if (c == '1')
    {
    buff = 1;
    }
    if (c == '0')
    {
    buff = 0;
    }
    }
    client.stop ();
    client.flush ();
    delay (100);



    client.stop ();
    delay (1000);
    client.connect (server, 80);
    }

    if (buff == 1)
    {
    digitalWrite (led, HIGH);
    }
    else
    {
    digitalWrite (led, LOW);
    }
    delay (500);
    }


    3. Server and control panel

    The server consists of only a few files:

    index.php -
    add_data.php control panel - a file that processes requests from the controller and sends a response back to arduino
    style.css - determines the appearance of the panel
    Transfer folder - contains files using which reads and writes values ​​from text files.
    led.php - writes the output status to the out-1.txt file sent via the form in the control panel
    ledstate.php - reads the status from the out-1.txt text file and displays it on the panel as “ON” or “OFF”
    temp- 1.php and temp-2.php- read the temperature values ​​from the in-1.txt and in-2.txt files and send them to the control panel.
    The txt folder is a kind of database for storing information.

    The server is actually very simple and it can be installed by any person with minimal knowledge, for example, like me. Before working on this project, I had experience working only with arduino, so php, ajax, html and css had to be studied literally from scratch.

    Installation is very simple. Just copy the files to the server and upload the sketch to the controller, while editing the domain name in the sketch, connect the sensors and LED, and everything should work for you.

    I am sure that seasoned programmers will kick me and poke my nose in those places where it would be possible to write code more succinctly and correctly. I only welcome this !!!

    If you saw that some things can be done easier and faster, then let me know.

    What do we have in the end?

    Pros:

    - everything is simple and clear
    - you can configure it to your needs and tasks
    - good stability
    - the server can be deployed on any free hosting

    Cons:

    - a large number of requests to the server (some hosting providers may not like this, in this case you need to increase the pause between requests in a sketch)
    - eats a lot of traffic from the client (with 1 request per second, about 300 MB per day comes out)
    - there is a slight delay in turning on the outputs (this may be critical for some cases)

    Future plans:

    - add a button on the controller to turn the relay on and off with a state change to the server
    - add authorization
    - add identification keys in requests
    - organize the work of several cards simultaneously with one control panel
    - add confirmation from the controller to turn on the output
    - I would very much like to use websockets or mqtt, but still inclined to use websockets using socket.io.

    Perhaps, if it’s interesting, I’ll write an article about managing wifi module esp8266 via the Internet. I already managed to successfully test it and made sure that everything works, however, there are some nuances in the work.

    And if there are enough people who want to write, there will be a detailed article where we will consider adding new units with sensors and controlling additional outputs to the control panel.

    I ask for advice on how best to implement authorization in the control panel so that there is a relatively simple and clear installation and an acceptable level of security.

    Everyone can visit my server page themselves and check in action arduino.zhodinovel.com
    !!! To change the controller output, put the marker on the desired value and press "SEND" !!!

    Watch the video


    Download files
    1. Server
    2. Sketch for arduino

    Thank you all for your attention!

    UPD Added feedback to the panel in the form of a photoresistor. When the light is off, the reading is about 130, when it is on - 900.

    Also popular now: