ในการทดลองนี้ ต้องการทดสอบส่งข้อมูลจาก ESP32 ไปยัง ThingSpeak ซึ่ง จะต้องมีอุปกรณ์ดังนี้
1. NodeMCU ESP32
2. IDE ที่มีการติดตั้ง Library ของ Board ESP
3. ต้องมี Account ของ Matlab ซึ่งเป็น account เดี่ยวกับ ThingSpeak เพราะเป็นบริษัทเดียวกันคือ MathWork
แหล่งข้อมูลที่มีประโยชน์
[2] https://ton.packetlove.com/blog/iot/esp32-node32-lite-pm25-temperature-humidity-sensors-node.html
[3] https://www.mathworks.com/help/thingspeak/writedata.html#d117e32226
[4] https://www.mathworks.com/help/thingspeak/MoistureMonitor.html
[5] https://blogs.mathworks.com/iot/2019/02/12/thingspeak-library-for-arduino/
ติดตั้ง ESP32 เข้ากับ Arduino IDE
https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/
เป้าหมายของโปรเจ็ค
1. หา solution ในการสื่อสารจาก ESP32 ไปยัง ThingSpeak
2. ทดลองนำข้อมูลจาก Think Speak ไปทำประโยคทางด้าน DATA Science
ความคืบหน้า
1. สามารถทำการสือสารจาก EPS32 ไปยัง ThingSpeak โดยการใช้ Library Standard ที่ทาง Mathwork มีให้ []
2. สามารถสือสารโดยวิธีการ POST ของ [1]และ[4] ได้ ตัวอย่าง code อยู่ด้านล่าง
To Do List
1. Application ที่คิดว่าจะนำไปใช้งานเช่น การวัดอุหณภูมิ ความชื่น ความแข้มแสง ค่า pH
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
#define TIMEOUT 5000 // Timeout for server response.
#include <WiFi.h>
String apiKey = "MWI5CXIIVFH6ZHR0"; // Enter your Write API key from ThingSpeak
const char *ssid = "MM2_2.4G"; // replace with your wifi ssid and wpa2 key
const char *pass = "aaabbbccc";
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
int h = 0;
float t =0;
h = random(0,100);
t = random(0,100);
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(h);
postStr += "&field2=";
postStr += String(t);
postStr += "\r\n\r\n";
/* client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Hall: ");
Serial.println(h);
Serial.print("Temperature:");
Serial.print(t);
Serial.println(" C");
Serial.println(postStr);
Serial.println("%. Send to Thingspeak.");
*/
// POST data via HTTP.
Serial.println( "Connecting to ThingSpeak for update..." );
Serial.println();
client.println( "POST /update HTTP/1.1" );
client.println( "Host: api.thingspeak.com" );
client.println( "Connection: close" );
client.println("X-THINGSPEAKAPIKEY: "+apiKey);
client.println( "Content-Type: application/x-www-form-urlencoded" );
client.println( "Content-Length: " + String( postStr.length() ) );
client.println();
client.println( postStr );
Serial.println( postStr );
String answer=getResponse();
Serial.println( answer );
}
client.stop();
Serial.println("Waiting...");
delay(10000);
}
// Wait for a response from the server indicating availability,
// and then collect the response and build it into a string.
String getResponse(){
String response;
long startTime = millis();
delay( 200 );
while ( client.available() < 1 && (( millis() - startTime ) < TIMEOUT ) ){
delay( 5 );
}
if( client.available() > 0 ){ // Get response from server.
char charIn;
do {
charIn = client.read(); // Read a char from the buffer.
response += charIn; // Append the char to the string response.
} while ( client.available() > 0 );
}
client.stop();
return response;
}
/*
WriteMultipleFields
Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
Hardware: ESP32 based boards
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires installation of EPS32 core. See https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/boards_manager.md for details.
- Select the target hardware from the Tools->Board menu
- This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
For licensing information, see the accompanying license file.
Copyright 2018, The MathWorks, Inc.
*/
#include "ThingSpeak.h"
#include "secrets.h"
#include <WiFi.h>
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";
void setup() {
Serial.begin(115200); //Initialize serial
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// set the fields with the values
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
ThingSpeak.setField(3, number3);
ThingSpeak.setField(4, number4);
// figure out the status message
if(number1 > number2){
myStatus = String("field1 is greater than field2");
}
else if(number1 < number2){
myStatus = String("field1 is less than field2");
}
else{
myStatus = String("field1 equals field2");
}
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
// change the values
number1++;
if(number1 > 99){
number1 = 0;
}
number2 = random(0,100);
number3 = random(0,100);
number4 = random(0,100);
delay(20000); // Wait 20 seconds to update the channel again
}