Mengirim data dari Arduino ke Web Server

Bagaimana mengirim data dari Arduino ke web (website)? Tulisan ini sebagai catatan untuk belajar terutama arduini dan IoT. Contoh kasusnya adalah bagaimana secara rutin mengirim informasi (nilai) perubahan suhu dan kelembaban udata ke webserver. Salah satu topologi yang digunakan disini adalah data dari sernsor di arduino di dikirm ke server melalui jaringan (ethernet).

Kebutuhan utama adalah sebagai berikut :

  1. Client : Arduino Uno (support Ethernet) dan DHT 11 sensor
  2. Server : Web server Support php dan MySql database
  3. Progrma di Client
  4. Program di Server

Contoh code program di Client :

#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;

#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT11 // SENSOR TYPE – THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL

int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
String data;

void setup() {
Serial.begin(115200);

if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
}

dht.begin();
delay(10000); // GIVE THE SENSOR SOME TIME TO START

h = (int) dht.readHumidity();
t = (int) dht.readTemperature();

data = “”;
}

void loop(){

currentMillis = millis();
if(currentMillis – previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
}

data = “temp1=” + t + “&hum1=” + h;

if (client.connect(“www.*****.*************.com”,80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println(“POST /add.php HTTP/1.1”);
client.println(“Host: *****.*************.com”); // SERVER ADDRESS HERE TOO
client.println(“Content-Type: application/x-www-form-urlencoded”);
client.print(“Content-Length: “);
client.println(data.length());
client.println();
client.print(data);
}

if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}

delay(300000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}

Metode pengiriman ke webserver menggunakan metode post ke alamat web (php) yang sudah disiapkan. Seperti posting menggunakan web browser, hanya saja disini yang browsing dan posting adalah mesin arduino dengan mambawa data (suhu dan kelembaban udara). Data kirima kana tersimpan di database MySQL dan ditampilkan dengan menggunakan php.

Untuk kode selengkapnya bisa kunjungi Bahan Bacaan yang dijadikan sebagai rujukan :

2 comments

  1. Needed to compose you the very little word so as to give thanks as before on the nice things you’ve shared on this website. It has been certainly shockingly open-handed of you to offer publicly all some people would’ve marketed as an e-book to earn some cash for themselves, primarily now that you might well have tried it in the event you desired. Those principles additionally worked to be the easy way to realize that someone else have a similar dreams much like mine to know significantly more when considering this condition. I think there are numerous more enjoyable opportunities in the future for people who view your blog.

  2. I would like to point out my admiration for your kindness supporting individuals who absolutely need guidance on your area of interest. Your real commitment to getting the solution throughout came to be wonderfully important and has regularly permitted girls just like me to attain their aims. Your new interesting guideline denotes so much a person like me and extremely more to my peers. Thanks a ton; from each one of us.

Leave a Reply

Your email address will not be published. Required fields are marked *