Вы находитесь на странице: 1из 8

READ WRITE DATA THINGSPEAK MENGGUNAKAN LIBRARY

ThingSpeak.h dan WifiEsp.h


(Mikrokontroller: ARDUINO+ESP8266)

Oleh: Rustam A.

Mengirim data ke server thingspeak menggunakan modul wifi ESP8266 versi 01 dengan mikrokontroller
Arduino dapat dilakukan menggunakan library WifiEsp.h. Dengan adanya fungsi-fungsi yang ada di library
ini read dan write data dari dank e server thingspeak tidak memerlukan AT-Command lagi. Untuk
menggunakan library ini harus diinstal terlebih dahulu. Caranya masuk ke menu Sketch | Include Library |
Manage Libraries… Ketikkan “wifiesp”, pilih library seperti tampilan dalam gambar berikut.

Setelah terinstall, klik tombol Close. Kemudian ketikan program berikut. Kemudian upload.

/*===== Arduino Codes for ESP8266 module using Library ===== */

#include "WiFiEsp.h"
#include "SoftwareSerial.h"

SoftwareSerial Esp8266(2, 3); // RX, TX

char ssid[] = "Amagabar Wifi"; // your network SSID (name)


char pass[] = "pengenm4suk"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

char server[] = "api.thingspeak.com";

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 15000L; // delay between updates, in milliseconds

WiFiEspClient client;

void setup()
{
Serial.begin(9600);
Esp8266.begin(9600); // initialize serial for ESP module
WiFi.init(&Esp8266); // initialize ESP module

if (WiFi.status() == WL_NO_SHIELD) // check for the presence of the shield


{
Serial.println("WiFi shield not present");
while (true); // don't continue
}

while ( status != WL_CONNECTED) // attempt to connect to WiFi network


{
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
}

Serial.println("You're connected to the network");


printWifiStatus();
delay(100);
}

void loop()
{

// if 15 seconds have passed since your last connection,


// then connect again and send data
if (millis() - lastConnectionTime > postingInterval) {
WriteData2TS(1);
}
}

// kirim data ke thingspeak


void WriteData2TS(int stat)
{
String datastr = String(analogRead(A0));
Serial.print("Data = "); Serial.println(datastr);
Serial.println("Starting Writing data to thingspeak");
Serial.println("Connecting to server...");

// close any connection before send a new request


// this will free the socket on the WiFi shield
client.stop();
if (client.connect(server, 80)) // if there's a successful connection
{
String strget = "GET
/update?api_key=3D6QMA0DYX9736GC&field1="+datastr+" HTTP/1.1";
client.println(strget);
client.println(F("Host: api.thingspeak.com"));
client.println("Connection: close");
client.println();
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection
Serial.println("Connection failed");
}
}

void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: "); Serial.println(WiFi.SSID());

// print your WiFi shield's IP address


IPAddress ip = WiFi.localIP();
Serial.print("IP Address: "); Serial.println(ip);

// print the received signal strength


long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):"); Serial.print(rssi);
Serial.println(" dBm");
}

Sementara itu untuk membaca data dari Thingspeak menggunakan library ini programnya sbb.

/*===== Arduino Code for ESP8266 module using Libary ===== */

#include "WiFiEsp.h"
#include "SoftwareSerial.h"

SoftwareSerial Esp8266(2, 3); // RX, TX

char ssid[] = "Amagabar Wifi"; // your network SSID (name)


char pass[] = "pengenm4suk"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

char server[] = "api.thingspeak.com";

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 3000L; // delay between updates, in milliseconds

WiFiEspClient client;

void setup()
{
Serial.begin(9600);
Esp8266.begin(9600); // initialize serial for ESP module
WiFi.init(&Esp8266); // initialize ESP module

if (WiFi.status() == WL_NO_SHIELD) // check for the presence of the shield


{
Serial.println("WiFi shield not present");
while (true); // don't continue
}

while ( status != WL_CONNECTED) // attempt to connect to WiFi network


{
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
}

Serial.println("You're connected to the network");


printWifiStatus();
delay(100);
}

void loop()
{
int c=0;
String dataStr="";

// if there's incoming data from the net connection send it out the serial port
while (client.available())
{
int a = client.read();
Serial.write(a); // this is for debugging purposes only
if((a>=32) && (a<=127))
{
dataStr = dataStr + char(a);
c++;
}
}

if (c>0) // ada data


{
Serial.println("original Data read: "); Serial.println(dataStr);
Serial.print("panjang: "); Serial.print(dataStr.length());
Serial.print(" (counter panjang c = "); Serial.println(c);
}

// if 10 seconds have passed since your last connection,


// then connect again and send data
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}

//====== this method makes a HTTP connection to the server


void httpRequest()
{
Serial.println();

// close any connection before send a new request


// this will free the socket on the WiFi shield
client.stop();

// if there's a successful connection


if (client.connect(server, 80)) {
Serial.println("Connecting to server...");

// send the HTTP PUT request


client.println(F("GET /channels/200777/fields/1/last.json
HTTP/1.1"));
client.println(F("Host: api.thingspeak.com"));
client.println("Connection: close");
client.println();

// note the time that the connection was made


lastConnectionTime = millis();
}
else {
// if you couldn't make a connection
Serial.println("Connection failed");
}
}

void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address


IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength


long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

LIBRARY ThingSpeak.h
Read dan Write data dari dank e Thingspeak bisa lebih mudah dan praktis dalam codingnya menggunakan
library ThingSpeak.h. Namun seperti sebelumnya, anda harus menginstal library ini terlebih dahulu
sebelum menggunakannya.

Setelah terinstal, kemudian ketikkan program berikut.


//====== uploading (writing)& Reading a data to thingspeak ======
// using Thingspeak library and WifiEsp Lib
// Hardware: Arduino UNO and ESP8266-01 module
// by Rustam, 10 Maret 2019 (fixed)
// instruksi utk write ke 1 field:
// ThingSpeak.writeField(200777,1,val,writeAPI);
//===========================================================

#include "WiFiEsp.h"
#include "SoftwareSerial.h"
#include "ThingSpeak.h"

SoftwareSerial Esp8266(2, 3); // RX, TX

char ssid[] = "Amagabar Wifi"; // your network SSID (name)


char pass[] = "pengenm4suk"; // your network password
const char *writeAPI = "3D6QMA0DYX9736GC"; // replace with yours
const char *readAPI = "DEFK5WEKOM7JU2PK"; // replace with yours
unsigned long int channelID=200777; // replace with your channel id

int status = WL_IDLE_STATUS; // the Wifi radio's status

char server[] = "api.thingspeak.com";

unsigned long waktuterakhir = 0; // last time you connected to the server, in milliseconds
const unsigned long interval = 20000L; // delay between updates, in milliseconds
unsigned long waktuterakhirbaca = 0; // last time you connected to the server, in milliseconds
const unsigned long intervalbaca = 5000L;

WiFiEspClient client;

void setup()
{
Serial.begin(9600);
Esp8266.begin(9600); // initialize serial for ESP module
WiFi.init(&Esp8266); // initialize ESP module

if (WiFi.status() == WL_NO_SHIELD)
{
Serial.println("WiFi shield not present");
while (true); // don't continue
}

while ( status != WL_CONNECTED) // attempt to connect to WiFi network


{
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
}

Serial.println("You're connected to the network");


// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi); Serial.println(" dBm");
delay(100);
ThingSpeak.begin(client); // inisialisasi ThingSpeak
}

void loop()
{
//==== start WRITING ....
if (millis() - waktuterakhir > interval)
{
int val = analogRead(A0);
Serial.print(val);
// === starting writing multiple data into multiple fields of thingspeak
ThingSpeak.setField(1,val);
ThingSpeak.setField(2,22);
ThingSpeak.setField(3,33);
ThingSpeak.setField(4,44);
int stat = ThingSpeak.writeFields (channelID, writeAPI);
Serial.println("Uploading/Writing 4 data to thingspeak.....");
Serial.print("status writing/sending data to thingspeak (200:
success): ");
Serial.println(stat);
waktuterakhir = millis();
}

if (millis() - waktuterakhirbaca > intervalbaca)


{
//======start reading field 1 from thingspeak
int hasilbacaf1 = ThingSpeak.readIntField(channelID, 1, readAPI);
Serial.print("Hasil baca field 1: "); Serial.println(hasilbacaf1);
waktuterakhirbaca = millis();
}
}

Вам также может понравиться