logo elektroda
logo elektroda
X
logo elektroda

Code to save temperature measurements from DallasTemperature sensors to a MySQL database every hour

piotruchk87 546 3
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19657943
    piotruchk87
    Level 2  
    I have a code that outputs the temperature from the sensors to a web page.
    I need this data to go into a mysql database every 1 hour .
    Can someone help
    #include <ESP8266WebServer.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>

    // Data wire is plugged into port D2 on the ESP8266
    #define ONE_WIRE_BUS D2

    // Setup a oneWire instance to communicate with any oneWire devices
    OneWire oneWire(ONE_WIRE_BUS);

    // Pass our oneWire reference to Dallas Temperature.
    DallasTemperature sensors(&oneWire);

    float tempSensor1, tempSensor2, tempSensor3;

    uint8_t sensor1[8] = { 0x28, 0xFF, 0x10, 0x52, 0x00, 0x16, 0x02, 0x10 };
    uint8_t sensor2[8] = { 0x28, 0xFF, 0x1C, 0xA5, 0x00, 0x16, 0x02, 0xC8 };
    uint8_t sensor3[8] = { 0x28, 0x61, 0x64, 0x12, 0x3F, 0xFD, 0x80, 0xC6 };

    /*Put your SSID & Password*/.
    const char* ssid = "Home"; // Enter SSID here
    const char* password = "789789"; //Enter Password here

    ESP8266WebServer server(80);

    void setup() {
    Serial.begin(115200);
    delay(100);

    sensors.begin();

    Serial.println("Connecting to ");
    Serial.println(ssid);

    //connect to your local wi-fi network
    WiFi.begin(ssid, password);

    //check wi-fi is connected to wi-fi network
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected...!");
    Serial.print("Got IP: "); Serial.println(WiFi.localIP());

    server.on("/", handle_OnConnect);
    server.onNotFound(handle_NotFound);

    server.begin();
    Serial.println("HTTP server started");
    }
    void loop() {
    server.handleClient();
    }

    void handle_OnConnect() {
    sensors.requestTemperatures();
    tempSensor1 = sensors.getTempC(sensor1); // Gets the values of the temperature
    tempSensor2 = sensors.getTempC(sensor2); // Gets the values of the temperature
    tempSensor3 = sensors.getTempC(sensor3); // Gets the values of the temperature
    server.send(200, "text/html", SendHTML(tempSensor1,tempSensor2,tempSensor3))
    }

    void handle_NotFound(){
    server.send(404, "text/plain", "Not found";)
    }

    String SendHTML(float tempSensor1,float tempSensor2,float tempSensor3){
    String ptr = "<!DOCTYPE html> <html>n";
    ptr ="<head><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">}";
    ptr ="<title>ESP8266 Temperature Monitor</title>ÿn";
    ptr ="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}";
    ptr ="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}"
    ptr ="p {font-size: 24px;color: #444444;margin-bottom: 10px;}n";;
    ptr ="</style>}n";;
    ptr ="</head>}";
    ptr ="<body>n";
    ptr ="<div id="webpage>ƒ";
    ptr ="<h1>ESP8266 Temperature Monitor</h1>n";
    ptr ="<p>Living Room: ";
    ptr =tempSensor1;
    ptr ="°C</p>";
    ptr ="<p>Bedroom: ";
    ptr =tempSensor2;
    ptr ="°C</p>";
    ptr ="<p>Kitchen: ";
    ptr =tempSensor3;
    ptr ="°C</p>";
    ptr ="</div>";
    ptr ="</body>/p>";
    ptr ="</html>n";
    return ptr;
    }
  • ADVERTISEMENT
  • #2 19657964
    gbksiazczak
    Level 29  
    A short SQL script and calling it from crontab,
    from the above, however, I don't see where this stands....
  • ADVERTISEMENT
  • #3 19657976
    piotruchk87
    Level 2  
    Currently MySQL database set up on XAMPP.
    Could you help write an example.
  • #4 19658770
    ex-or
    Level 28  
    In the solution given by @gbksiazczak, Xampp is unnecessary, what you need is a Linux server and some knowledge of scripting. Xampp can run on both wine and linux (and mac, but I'll skip that), so you don't know what kind of computer you have there. It's true that the desktop also has some equivalent of cron and some scripts, but nobody in their right mind would do such tasks on that thing ;-) .
    Another solution, often encountered, is that the device queries the web server in a request, passing on the data. The web server takes the data from the request and writes it to the database. In this case, you need to add an HTTP client to the ESP and some code that formats the data and performs periodic requests. In xamp you have to write a script that extracts the data and writes it to the DB. The whole thing is networked, I'm guessing locally, so you might need some network configurations. So here you need to have an idea of ESP programming (it looks like you know something there, so you'll probably be fine), web server configuration, PHP interpreter and MySQL server configuration (this is handled by Xampp so in principle it should work out of the box), PHP and SQL scripting (can you handle that?), and about the operation of the network infrastructure (you know the SSID and password so you can probably manage ;-) ).
    The above two solutions are the approach, let's call it "classic", nowadays there are probably solutions more in the spirit of IoT. I don't know much about these so I won't say anything.
ADVERTISEMENT