Đọc cảm biến và thu thập dữ liệu với Arduino. Sử dụng mạch ESP8266 để kết nối wifi và gửi dữ liệu lên trang Thingspeak. Tại đây, dữ liệu được hiển thị dạng biểu đồ mặc định.
Với công cụ Matlab có sẵn của Thingspeak, ta phân tích và vẽ dạng biểu đồ hoặc hiển thị số liệu như mong muốn.
Sơ đồ cơ sở

Tính tổng dữ liệu theo thời gian
%
readChId = abc;
writeChId = abc;
writeKey = 'klm';
readKey = 'xyz';
%
t1 = datetime('today');
t2 = dateshift(t1, 'start', 'month', 'current');
dateStop = t1;
dateStart = t2;
data = thingSpeakRead(readChId,'ReadKey',readKey,'DateRange',[dateStart,dateStop],...
'Fields',1);
s = sum(data);
thingSpeakWrite(writeChId, s, 'Fields',4,'WriteKey',writeKey);
Hiển thị số liệu
% MATLAB code for visualizing data from a channel as a filled area
% 2D plot using the THINGSPEAKAREA function.currentLit = thingSpeakRead(abc,'ReadKey','xyz','Field',4)
str={[num2str(currentLit) 'L']};
annotation('textbox',[0.01 0.01 0.5 0.5],...
'BackgroundColor','1 1 1',...
'FaceAlpha',0.15,...
'Color','0 0 1',...
'LineWidth',2,...
'HorizontalAlignment','center',...
'VerticalAlignment','middle',...
'LineStyle',':',...
'String',str,...
'FontSize',38);
Video hướng dẫn
Chương trình minh họa
// Khai báo thư viện
// wifi
String ssid = "xxx"; //tên wifi
String pass = "xxx"; //mật khẩu
//
//Thingspeak
String ThingspeakAdd = "api.thingspeak.com"; // trang hiển thị
String APIwrite = "xxx"; // mã API
const int UpdateInterval = 16000; // 16 giây
// Giao tiếp chip wifi
SoftwareSerial esp = SoftwareSerial(rxPin, txPin);
// Khai báo biến cần thiết
void setup()
{
// Cài đặt cần thiết
// Gửi về máy tính
Serial.begin(9600);
//
esp.begin(9600);
//
// LCD
//
// Start up the library
sensors.begin();
delay(5000);
//
if(esp.find("GOT IP"))
{
Serial.println("GOT IP");
lcd.setCursor(0, 0);
lcd.print("Got IP ");
//
wifiStatus = 1;
}
else
{
delay(2000);
// Kết nối lại wifi
connectWifi();
}
}
void loop()
{
// thu thập dữ liệu
// đến thời điểm cập nhật
if(CurrentTime - LastConnection > UpdateInterval)
{
//
LastConnection = millis();
//
if("condition")
{
//
//Dữ liệu chuyển đi
tsData = "&field1=" + Lit + "&field2=" + Pressure + "&field3=" + Temper;
//
connectThingSpeak();
//
if(esp.find("OK") || esp.find("ALREADY"))
{
Serial.println("TCP connection ready");
lcd.setCursor(0, 0);
lcd.print("TCP OK ");
//
updateThingSpeak(tsData);
}
else
{
lcd.setCursor(0, 0);
lcd.print("TCP Error ");
Serial.println("ERROR TCP");
}
}
}
}
//
// Kiểm tra wifi
void reset()
{
esp.print("AT+RST\r\n");
delay(200);
//
if(esp.find("OK") )
{
Serial.println("Module OK");
lcd.setCursor(0, 0);
lcd.print("ESP OK ");
}
else
{
Serial.println("Module…");
lcd.setCursor(0, 0);
lcd.print("ESP… ");
}
//
}
// Kết nối với mạng wifi
void connectWifi()
{
String cmd = "AT+CWJAP=\"" + ssid +"\",\"" + pass + "\"";
cmd += "\r\n";
esp.print(cmd);
delay(3000);
//
if(esp.find("OK"))
{
Serial.println("Connected!");
lcd.setCursor(0, 0);
lcd.print("WIFI OK ");
//
wifiStatus = 1;
}
else
{
wifiStatus = 0;
//
Serial.println("Not Connected…");
lcd.setCursor(0, 0);
lcd.print("NO WIFI ");
}
//
}
//
void updateThingSpeak(String tsData)
{
//
String postRequest = "GET /update?api_key=" + APIwrite + tsData + "\r\n";
String sendCmd = "AT+CIPSEND=";
esp.print(sendCmd);
esp.print(postRequest.length());
esp.print("\r\n");
//
Serial.print(sendCmd);
Serial.println(postRequest.length());
//
delay(500);
//
if(esp.find(">"))
{
Serial.println("Sending..");
lcd.setCursor(0, 0);
lcd.print("Sending ");
//
esp.print(postRequest);
//esp.print("\r\n");
//
Serial.print(postRequest);
delay(1000);
//
if(esp.find("SEND OK"))
{
Serial.println("Packet sent");
lcd.setCursor(0, 0);
lcd.print("Packet OK ");
//
esp.print("AT+CIPCLOSE\r\n");
}
else
{
Serial.println("Packet error");
lcd.setCursor(0, 0);
lcd.print("Packet…");
//
esp.print("AT+CIPCLOSE\r\n");
}
}
else
{
lcd.setCursor(0, 0);
lcd.print("NOT SENT ");
Serial.println("NOT SENT");
}
}
//
void connectThingSpeak()
{
String cmdTCP = "AT+CIPSTART=\"TCP\",\"";
cmdTCP += ThingspeakAdd;
cmdTCP += "\",80";
cmdTCP += "\r\n";
esp.print(cmdTCP);
delay(10);
Serial.println(cmdTCP);
delay(100);
}
