เคยศึกษาฐานข้อมูล MySql มาบ้าง จึงใช้ตัวนี้ครับ
การเก็บค่าเซ็นเซอร์บนฐานข้อมูล MySql ของเว็บไซต์ ประกอบด้วย 4 ไฟล์
1.ไฟล์ arduino ส่งเข้า esp8266
2.configab.php ติดต่อฐานข้อมูล
3.tempab.php รับค่าเซ็นเซอร์ลงฐานข้อมูล
4.temp2.sql ไฟล์สร้างตารางในฐานข้อมูล
สามารถสร้างเพจเพื่อดูค่าเซ็นเซอร์หรือดูจาก Blynk
ตัวย่างน้ำที่มีแหนขึ้นสมบูรณ์มากสงสัยว่าในน้ำมันมีอะไร
ผังการต่อขาต่างๆ แต่สามารถแกะจากโค้ดได้
กำลังเขียนแบบ
code สำหรับเชื่อมwifi AP config
/*ESP8266V2 12E+PCF8574+LCD2004+Temp.DS18B20+รีเลย์4ตัว
*
*Blynk WiFiManager เชี่อมผ่านมือถีอ REST แบบง่าย
*/
//-----Blynk-----
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxx";
/*สำหรับ Blynk Server ของ https:eleceasy.com/ ที่แจกให้ เพิ่ม **** แค่ 2 บรรทัดนี
char server[] = "oasiskit.com";
int port = 8080;
*/
//-----Blynk-----
//----- PCF8574-----
#include "Arduino.h"
#include "PCF8574.h"
// Set i2c address
//PCF8574 pcf8574(0x39);
PCF8574 pcf8574(0x20);//address pcf8574
//----- PCF8574-----
//-----DS18B20 TEMP.-----
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS D2 // DS18B20สายDATA
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.
//-----DS18B20 TEMP.-----
//-----20*4 2004 LCD-----
#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 20 chars and 4 line display,D1,D2
int show = -1;
//-----20*4 2004 LCD-----
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//--------------------AP config------------------
Blynk.config(auth);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//--------------------AP config------------------
//-----ESP8266-----
pinMode(D0, OUTPUT); //กำหนดโหมด ว่าเป็น INPUT หรือ OUTPUT
pinMode(D1, OUTPUT); // I2C เชื่อมต่อแบบขนานขา SDL
pinMode(D2, OUTPUT); // I2C เชื่อมต่อแบบขนานขา SDA
// pinMode(D5, OUTPUT);
// digitalWrite(D5, HIGH);//ให้ D5 เป็น 0
// pinMode(D3, INPUT_PULLUP);//กำหนดโหมดใช้งานให้กับขา D3 เป็นขา กดปุ่ม ค้าง เพื่อตั้งค่า AP config
// pinMode(D4, OUTPUT);
// pinMode(D8, OUTPUT);
// pinMode(D6, OUTPUT);
// pinMode(D7, OUTPUT);
// ให้ LED ทั้งหมดดับก่อน
digitalWrite(D0, LOW);//ให้หลอด LED สีฟ้าดับก่อน
// digitalWrite(D1, LOW);
// digitalWrite(D2, LOW);
// digitalWrite(D8, LOW);
// digitalWrite(D4, LOW);
// digitalWrite(D6, LOW);
// digitalWrite(D7, LOW);
//-----ESP8266-----
//----- PCF8574-----
delay(1000);
// Set pinMode to OUTPUT,INPUT
pcf8574.pinMode(P0, OUTPUT);//ตั้งเวลาเปิด ปิดไฟledแสดง
pcf8574.pinMode(P1, OUTPUT);//ตั้งเวลาเปิด ปิดไฟledแสดง
pcf8574.pinMode(P2, OUTPUT);
pcf8574.pinMode(P3, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย์
pcf8574.pinMode(P4, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย
pcf8574.pinMode(P5, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย
pcf8574.pinMode(P6, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย
pcf8574.pinMode(P7, OUTPUT);
Serial.print("Init pcf8574...");
if (pcf8574.begin()){
Serial.println("OK");
}else{
Serial.println("KO");
}
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
//----- PCF8574-----
//-----DS18B20 TEMP.-----
Serial.println("ArduinoAll Test Temperature DS18B20");
sensors.begin();
//-----DS18B20 TEMP.-----
//-----20*4 2004 LCD-----
int error;
Serial.println("LCD...");
// wait on Serial to be available on Leonardo
while (!Serial)
;
Serial.println("Dose: check for LCD");
// See http://playground.arduino.cc/Main/I2cScanner how to test for a I2C device.
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
show = 0;
lcd.begin(20,4); // initialize the lcd
} else {
Serial.println(": LCD not found.");
} // if
//-----20*4 2004 LCD-----
}//End setup()
void loop() {
// put your main code here, to run repeatedly:
//-----Blynk-----
Blynk.run();
//-----Blynk-----
//----- PCF8574-----
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(2000);
// Wait for two seconds (to demonstrate the active low LED)
pcf8574.digitalWrite(P0, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P0, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P1, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P1, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P2, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P2, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P3, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P3, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P4, HIGH);//รีเลย์1เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P4, LOW);//รีเลย์1ปิด
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P5, HIGH);//รีเลย์2เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P5, LOW);//รีเลย์2ปิด
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P6, HIGH);//รีเลย์3เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P6, LOW);//รีเลย์3ปิด
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P7, HIGH);//รีเลย์4เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P7, LOW);//รีเลย์4ปิด
Serial.println(LOW);
delay(2000);
//----- PCF8574-----
//-----DS18B20 TEMP.-----
sensors.requestTemperatures();// Send the command to get temperatures
Serial.println(ONE_WIRE_BUS); // ข้อมูล 4 จะแสดงอุณหภูมิถูกต้อง
Serial.println("DS18B20 Temperature is: ");
Serial.println(sensors.getTempCByIndex(0));// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println(" *C");
delay(2000);
//-----DS18B20 TEMP.-----
//-----20*4 2004 LCD-----
if (show == 0) {
lcd.setBacklight(255);
lcd.home();
lcd.clear();
lcd.print("Hello LCD");
lcd.print("DS18B20 Temperature is: ");
lcd.print(sensors.getTempCByIndex(0));// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
lcd.println(" *C");
delay(1000);
lcd.setBacklight(0);
delay(400);
lcd.setBacklight(255);
} else if (show == 1) {
lcd.clear();
lcd.print("Cursor On");
lcd.cursor();
} else if (show == 2) {
lcd.clear();
lcd.print("Cursor Blink");
lcd.blink();
} else if (show == 3) {
lcd.clear();
lcd.print("Cursor OFF");
lcd.noBlink();
lcd.noCursor();
} else if (show == 4) {
lcd.clear();
lcd.print("Display Off");
lcd.noDisplay();
} else if (show == 5) {
lcd.clear();
lcd.print("Display On");
lcd.display();
} else if (show == 7) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*** first line.");
lcd.setCursor(0, 1);
lcd.print("*** second line.");
} else if (show == 8) {
lcd.scrollDisplayLeft();
} else if (show == 9) {
lcd.scrollDisplayLeft();
} else if (show == 10) {
lcd.scrollDisplayLeft();
} else if (show == 11) {
lcd.scrollDisplayRight();
} else if (show == 12) {
lcd.clear();
lcd.print("write-");
} else if (show > 12) {
lcd.print(show - 13);
} // if
delay(1400);
show = (show + 1) % 16;
//-----20*4 2004 LCD-----
}//End loop()
code สำหรับ เก็บข้อมูล อุณภูมิลงฐานข้อมูล MySql
#include <ESP8266WiFi.h>
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
const char* host = "203.151.59.20"; //ใส่ IP หรือ Host ของเครื่อง Database ก็ได้
const char* message0 = "1"; //ไอดีที่ต้องการจะส่ง
const char* message1 = "b"; //โซนที่ต้องการจะส่ง
const char* message2 = "44"; //อุณหภูมิต้องการจะส่ง
const char* message3 = "55"; //ความชื่นที่ต้องการจะส่ง
const char* message4 = ""; //เวลาบันทึกที่ต้องการจะส่ง
const char* message5 = "manual"; //ระบบที่ต้องการจะส่ง
void setup() {
Serial.begin(9600);
delay(10);
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
//----------------------------------------
//เรียกไฟล์ tempab.php เพื่อ insert update delete ข้อมูล method get()
//insert
//update ตัวแปร zone,temp
Serial.println(message1);
Serial.println(message2);
Serial.println(message3);
Serial.println("แสดงค่าตัวแปรก่อนส่งไปtemp.php");
String url2 = "http://เช่าหูฟัง.com/esp8266ab/tempab.php?zone=";
url2 +=message1; //ส่งค่าตัวแปร
url2 +="&temp="; //ส่งค่าตัวแปร
url2 +=message2; //ส่งค่าตัวแปร
url2 +="&humidity="; //ส่งค่าตัวแปร
url2 +=message3; //ส่งค่าตัวแปร
url2 +="&system="; //ส่งค่าตัวแปร
url2 +=message5; //ส่งค่าตัวแปร
Serial.print("Requesting URL2: ");
Serial.println(url2);
client.print(String("GET ") + url2 + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
//update temp-------------------------------------
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
รวม code ทั้งสองเข้าด้วยกัน เป็นไฟล์ arduino อัปโหลดเข้า esp8266
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxx";
/*สำหรับ Blynk Server ของ https:eleceasy.com/ ที่แจกให้ เพิ่ม **** แค่ 2 บรรทัดนี
char server[] = "oasiskit.com";
int port = 8080;
*/
//-----Blynk-----
const char* host = "203.151.59.20"; //ใส่ IP หรือ Host ของเครื่อง Database ก็ได้
const char* message0 = "1"; //ไอดีที่ต้องการจะส่ง
const char* message1 = "a"; //โซนที่ต้องการจะส่ง
const char* message2 = "34"; //อุณหภูมิต้องการจะส่ง
const char* message3 = "11"; //ความชื่นที่ต้องการจะส่ง
const char* message4 = ""; //เวลาบันทึกที่ต้องการจะส่ง
const char* message5 = "auto"; //ระบบที่ต้องการจะส่ง
//----- PCF8574-----
#include "Arduino.h"
#include "PCF8574.h"
// Set i2c address
//PCF8574 pcf8574(0x39);
PCF8574 pcf8574(0x20);//address pcf8574
//----- PCF8574-----
//-----DS18B20 TEMP.-----
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS D2 // DS18B20สายDATA
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.
//-----DS18B20 TEMP.-----
//-----20*4 2004 LCD-----
#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 20 chars and 4 line display,D1,D2
int show = -1;
//-----20*4 2004 LCD-----
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
/*
//wifi-----database----------
Serial.begin(9600);
delay(10);
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//wifi-----database--------
*/
//--------------------AP config------------------
Blynk.config(auth);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//--------------------AP config------------------
//-----ESP8266-----
pinMode(D0, OUTPUT); //กำหนดโหมด ว่าเป็น INPUT หรือ OUTPUT
pinMode(D1, OUTPUT); // I2C เชื่อมต่อแบบขนานขา SDL
pinMode(D2, OUTPUT); // I2C เชื่อมต่อแบบขนานขา SDA
// pinMode(D5, OUTPUT);
// digitalWrite(D5, HIGH);//ให้ D5 เป็น 0
// pinMode(D3, INPUT_PULLUP);//กำหนดโหมดใช้งานให้กับขา D3 เป็นขา กดปุ่ม ค้าง เพื่อตั้งค่า AP config
// pinMode(D4, OUTPUT);
// pinMode(D8, OUTPUT);
// pinMode(D6, OUTPUT);
// pinMode(D7, OUTPUT);
// ให้ LED ทั้งหมดดับก่อน
digitalWrite(D0, LOW);//ให้หลอด LED สีฟ้าดับก่อน
// digitalWrite(D1, LOW);
// digitalWrite(D2, LOW);
// digitalWrite(D8, LOW);
// digitalWrite(D4, LOW);
// digitalWrite(D6, LOW);
// digitalWrite(D7, LOW);
//-----ESP8266-----
//----- PCF8574-----
delay(1000);
// Set pinMode to OUTPUT,INPUT
pcf8574.pinMode(P0, OUTPUT);//ตั้งเวลาเปิด ปิดไฟledแสดง
pcf8574.pinMode(P1, OUTPUT);//ตั้งเวลาเปิด ปิดไฟledแสดง
pcf8574.pinMode(P2, OUTPUT);
pcf8574.pinMode(P3, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย์
pcf8574.pinMode(P4, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย
pcf8574.pinMode(P5, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย
pcf8574.pinMode(P6, OUTPUT);//ตั้งเวลาเปิด ปิด ดีเลย
pcf8574.pinMode(P7, OUTPUT);
Serial.print("Init pcf8574...");
if (pcf8574.begin()){
Serial.println("OK");
}else{
Serial.println("KO");
}
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
//----- PCF8574-----
//-----DS18B20 TEMP.-----
Serial.println("ArduinoAll Test Temperature DS18B20");
sensors.begin();
//-----DS18B20 TEMP.-----
//-----20*4 2004 LCD-----
int error;
Serial.println("LCD...");
// wait on Serial to be available on Leonardo
while (!Serial)
;
Serial.println("Dose: check for LCD");
// See http://playground.arduino.cc/Main/I2cScanner how to test for a I2C device.
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
show = 0;
lcd.begin(20,4); // initialize the lcd
} else {
Serial.println(": LCD not found.");
} // if
//-----20*4 2004 LCD-----
}//End setup()
int value = 0;
void loop() {
// put your main code here, to run repeatedly:
//-----Blynk-----
Blynk.run();
//-----Blynk-----
//----- PCF8574-----
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(2000);
// Wait for two seconds (to demonstrate the active low LED)
pcf8574.digitalWrite(P0, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P0, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P1, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P1, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P2, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P2, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P3, HIGH);
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P3, LOW);
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P4, HIGH);//รีเลย์1เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P4, LOW);//รีเลย์1ปิด
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P5, HIGH);//รีเลย์2เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P5, LOW);//รีเลย์2ปิด
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P6, HIGH);//รีเลย์3เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P6, LOW);//รีเลย์3ปิด
Serial.println(LOW);
delay(2000);
pcf8574.digitalWrite(P7, HIGH);//รีเลย์4เปิด
Serial.println(HIGH);
delay(1000);
pcf8574.digitalWrite(P7, LOW);//รีเลย์4ปิด
Serial.println(LOW);
delay(2000);
//----- PCF8574-----
//-----DS18B20 TEMP.-----
sensors.requestTemperatures();// Send the command to get temperatures
Serial.println(ONE_WIRE_BUS); // ข้อมูล 4 จะแสดงอุณหภูมิถูกต้อง
Serial.println("DS18B20 Temperature is: ");
Serial.println(sensors.getTempCByIndex(0));// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println(" *C");
delay(2000);
//-----DS18B20 TEMP.-----
//-----20*4 2004 LCD-----
if (show == 0) {
lcd.setBacklight(255);
lcd.home();
lcd.clear();
lcd.print("Hello LCD");
lcd.print("DS18B20 Temperature is: ");
lcd.print(sensors.getTempCByIndex(0));// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
lcd.println(" *C");
delay(1000);
lcd.setBacklight(0);
delay(400);
lcd.setBacklight(255);
} else if (show == 1) {
lcd.clear();
lcd.print("Cursor On");
lcd.cursor();
} else if (show == 2) {
lcd.clear();
lcd.print("Cursor Blink");
lcd.blink();
} else if (show == 3) {
lcd.clear();
lcd.print("Cursor OFF");
lcd.noBlink();
lcd.noCursor();
} else if (show == 4) {
lcd.clear();
lcd.print("Display Off");
lcd.noDisplay();
} else if (show == 5) {
lcd.clear();
lcd.print("Display On");
lcd.display();
} else if (show == 7) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*** first line.");
lcd.setCursor(0, 1);
lcd.print("*** second line.");
} else if (show == 8) {
lcd.scrollDisplayLeft();
} else if (show == 9) {
lcd.scrollDisplayLeft();
} else if (show == 10) {
lcd.scrollDisplayLeft();
} else if (show == 11) {
lcd.scrollDisplayRight();
} else if (show == 12) {
lcd.clear();
lcd.print("write-");
} else if (show > 12) {
lcd.print(show - 13);
} // if
delay(1400);
show = (show + 1) % 16;
//-----20*4 2004 LCD-----
//-----database------------
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
//----------------------------------------
//เรียกไฟล์ tempab.php เพื่อ insert update delete ข้อมูล method get()
//update ตัวแปร zone,temp,humidity
Serial.println(message1);
//Serial.println(message2);
Serial.println(message3);
//-----ข้อมูลจากเซ็นเชอร์ ds18b20
Serial.println(ONE_WIRE_BUS); // ข้อมูล 4 จะแสดงอุณหภูมิถูกต้อง
Serial.println("DS18B20 Temperature is: ");
Serial.println(sensors.getTempCByIndex(0));// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println(" *C");
//-----ข้อมูลจากเซ็นเชอร์ ds18b20
Serial.println("แสดงค่าตัวแปรก่อนส่งไปtemp.php");
//ค้นหาและทดสอบโค้ด2อาทิตย์
String url2 = "http://เช่าหูฟัง.com/esp8266ab/tempab.php?zone=";
url2 +=message1; //ส่งค่าตัวแปร zone
url2 +="&temp="; //ส่งค่าตัวแปร ชื่อ
url2 +=sensors.getTempCByIndex(0); //ส่งค่าตัวแปรแสดงอุณหภูมิ ds18b20
url2 +="&humidity="; //ส่งค่าตัวแปร ชื่อ
url2 +=message3; //ส่งค่าตัวแปร humidity
url2 +="&system="; //ส่งค่าตัวแปร ชื่อ
url2 +=message5; //ส่งค่าตัวแปร system
//ค้นหาและทดสอบโค้ด2อาทิตย์
Serial.print("Requesting URL2: ");
Serial.println(url2);
client.print(String("GET ") + url2 + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
//update temp-------------------------------------
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
//database------end loop------
}//-----end loop----------
ไฟล์รับค่าจากเซ็นเซอร์ arduino esp8266 เพื่อบันทึกลงฐานข้อมูล
tempab.php ใว้ที่ host
<?php
require_once("configab.php");
//UPDATEได้ครั้งละ1ฟิดล์
$id = $_GET['id'];
$zone = $_GET['zone'];
$temp = $_GET['temp'];
$humidity = $_GET['humidity'];
$datetime = $_GET['datetime'];
$system = $_GET['system'];
//echo $id,$zone,$temp,$humidity,$datetime,$system;
echo $zone;
echo $temp;
echo $humidity;
echo $system;
echo "แสดงค่าตัวแปรที่รับจากesp8266ก่อนบันทึกลงฐานข้อมูล";
//update zone,temp,humidity,system
$sql = "UPDATE temp2 SET zone ='$zone',temp ='$temp',humidity ='$humidity',system='$system' WHERE id=1";
$sql_query = mysql_query($sql);
if ($sql_query) {
echo "Complete";
echo $zone;
echo $temp;
echo $humidity;
echo $system;
echo "แสดงค่าตัวแปรที่รับจากesp8266หลังบันทึกลงฐานข้อมูลComplete";
} else {
echo "Error";
echo $zone;
echo $temp;
echo $humidity;
echo $system;
echo "แสดงค่าตัวแปรที่รับจากesp8266หลังบันทึกลงฐานข้อมูลError";
}
?>
ไฟล์ configab.php ใว้ที่ host ใช้เชื่อมต่อฐานข้อมูล
<?php
$mysql_host = 'localhost';
$mysql_user = 'xxxxx';
$mysql_pass='xxxxx';
$mysql_db = 'chaohoofang';
$Conn = mysql_connect ($mysql_host,$mysql_user,$mysql_pass) or die ("ใส่ยูเซอร์เนมและพาสเวอร์ผิด");
$DB = mysql_select_db($mysql_db,$Conn) or die ("เชื่อมต่อไม่ได้");
mysql_query("SET NAMES utf8_general_ci")
?>
ตารางฐานข้อมูล temp2.sql
CREATE TABLE `temp2` (
`id` int(11) NOT NULL,
`zone` varchar(2) DEFAULT NULL,
`temp` int(2) DEFAULT NULL,
`humidity` int(2) DEFAULT NULL,
`datetime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`system` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
รอติดตาม v3 ครับ