ESP8266 + Database
Posted: Tue Jul 26, 2016 11:28 am
Hello,
I would like to send my data to databases with my esp8266. Firstly I create database with 3-column table with xampp in localhost, I want save date when the measurement was done,temperature and humidity. In other database I want save date when it was switched and state of device1,device2 etc.
How can I do that?
What should I add to my code?
I partly know MySQL and PHP, but I don't know how I can use them to this project.
It's my code, I can show temperature, humidity and switch on/switch off two leds.
I would like to send my data to databases with my esp8266. Firstly I create database with 3-column table with xampp in localhost, I want save date when the measurement was done,temperature and humidity. In other database I want save date when it was switched and state of device1,device2 etc.
How can I do that?
What should I add to my code?
I partly know MySQL and PHP, but I don't know how I can use them to this project.
It's my code, I can show temperature, humidity and switch on/switch off two leds.
Code: Select all
#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTTYPE DHT22 // DHT 21 (AM2301)
// WiFi parameters
const char* ssid = "xxx";
const char* password = "xxx";
// Create an instance of the server
WiFiServer server(80);
// Pin
int device1pin = 5; //d1
int device2pin = 4; //d2
//DHT sensor
const int DHTPin = 14; //d5
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
void setup() {
// Start Serial
Serial.begin(115200);
delay(10);
//DHT begin
dht.begin();
// Prepare GPIO5
pinMode(device1pin, OUTPUT);
pinMode(device2pin, OUTPUT);
digitalWrite(device1pin, 0);
digitalWrite(device2pin, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
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());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
float h = dht.readHumidity();
float t = dht.readTemperature();
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
if (req.indexOf("/on2") != -1) {
digitalWrite(device2pin, 1);
}
else if (req.indexOf("/off2") != -1) {
digitalWrite(device2pin, 0);
}
if (req.indexOf("/on1") != -1){
digitalWrite(device1pin, 1);
}
else if (req.indexOf("/off1") != -1) {
digitalWrite(device1pin, 0);
}
client.flush();
// Prepare the response
client.println( "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
client.println( "<head>");
client.println( "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println( "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>");
client.println( "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");
client.println( "</head>");
client.println( "<div class=\"container\">");
client.println("<h1>Temperature: ");
client.println(t);
client.println( "</h1>");
client.println( "<h1>Humidity: ");
client.println(h);
client.println("</h1>");
String s = "<h1>Relay Control</h1>";
s += "<div class=\"row\">";
s += "<div class=\"col-md-2\"><input id=\"divinput1\" class=\"btn btn-block btn-lg btn-primary\" type=\"button\" value=\"On1\" onclick=\"on1()\"></div>";
s += "<div class=\"col-md-2\"><input class=\"btn btn-block btn-lg btn-danger\" type=\"button\" value=\"Off1\" onclick=\"off1()\"></div>";
s += "<div class='col-md-2'><div id='value1'>Brak inf</div></div>";
s += "</div>";
s += "<br />";
s += "<div class=\"row\">";
s += "<div class=\"col-md-2\"><input class=\"btn btn-block btn-lg btn-primary\" type=\"button\" value=\"On2\" onclick=\"on2()\"></div>";
s += "<div class=\"col-md-2\"><input class=\"btn btn-block btn-lg btn-danger\" type=\"button\" value=\"Off2\" onclick=\"off2()\"></div>";
s += "<div class=\"col-md-2\"><div id=\"value2\">Brak inf</div></div>";
s += "</div></div>";
s += "<script>function on1() {$.get('on1');$(\"#value1\").text(\"Device1 is on\");}</script>";
s += "<script>function off1() {$.get(\"/off1\");$(\"#value1\").text(\"Device1 is off\");}</script>";
s += "<script>function on2() {$.get('on2');$(\"#value2\").text(\"Device2 is on\");}</script>";
s += "<script>function off2() {$.get(\"/off2\");$(\"#value2\").text(\"Device2 is off\");}</script>";
client.print(s);
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}