Kirim data dari arduino ke webserver
#include <EEPROM.h>
//#include <Ethernet.h> //Uno Mega
#include <UIPEthernet.h> //Nano
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int address = 0;
byte value;
int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = HIGH; // variable to store the read value
int valOld = HIGH;
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
int incomingByte = 0; // for incoming serial data
int LANcon = 0;
int LANPrint = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 169, 254, 222, 40 }; //assigned arduino LAN IP address
byte server[] = { 169, 254, 222, 37 }; // laptop running apache LAN IP address
EthernetClient client; //apache web server running on port 80
void setup()
{
pinMode(inPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
inputString.reserve(200);
Ethernet.begin(mac, ip);
Serial.begin(9600);
Serial.println("starting simple arduino client test");
Serial.println();
value = EEPROM.read(address);
Serial.println("");
Serial.print("Hardware ID: ");
Serial.print(value);
Serial.println();
Serial.println("999 -> Hardware ID");
Serial.println(" 0 -> test RESET");
Serial.println(" 1 -> test SET");
Serial.println(" >1 -> set Hardware ID");
Serial.println("");
Serial.println();
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
Serial.println("");
client.println("GET /ardu/data.php?id=" + String(value) + "&st=2 HTTP/1.0"); //php page invoking my web service
client.println();
LANcon = 1;
} else {
Serial.println("connection failed");
}
}
void TombolEvent(int st) {
Serial.print("Send to server st= ");
Serial.println(st);
if (client.connect(server, 80)) {
Serial.println("connected");
Serial.println("");
client.println("GET /ardu/data.php?id=" + String(value) + "&st=" + String(st) + " HTTP/1.0"); //php page invoking my web service
client.println();
LANcon = 1;
} else {
Serial.println("connection failed");
}
}
void loop()
{
val = digitalRead(inPin);
digitalWrite(ledPin, val);
if (valOld != val) {
if (val == LOW) {
Serial.print("Tombol ");
Serial.print(value);
Serial.println(" -> SET!");
TombolEvent(1);
valOld = val;
} else if (val == HIGH) {
Serial.print("Tombol ");
Serial.print(value);
Serial.println(" -> RESET!");
TombolEvent(0);
valOld = val;
}
delay(500);
}
if (stringComplete) {
Serial.print("inputString: ");
Serial.print(inputString.toInt());
if (inputString.toInt() == 999) {
Serial.println(" Mem Read");
value = EEPROM.read(address);
Serial.print("Hardware ID: ");
Serial.println(value);
Serial.println("");
} else if (inputString.toInt() > 1) {
Serial.println(" Mem Update");
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
delay(100);
Serial.println("Mem clear");
int val = inputString.toInt();
EEPROM.write(address, val);
delay (100);
value = EEPROM.read(address);
Serial.print("Update Hardware ID to: ");
Serial.println(value);
Serial.println("");
}
if (inputString.toInt() == 1) {
Serial.println("");
Serial.println("Serial -> SET!");
TombolEvent(1);
} else if (inputString.toInt() == 0) {
Serial.println("");
Serial.println("Serial -> RESET!");
TombolEvent(0);
}
stringComplete = false;
inputString = "";
}
if (client.available() && LANcon == 1) {
char c = client.read();
}
if (!client.connected() && LANcon == 1) {
Serial.println("disconnecting.");
Serial.println("");
client.stop();
LANcon = 0;
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
Libraries
#include <SPI.h>
#include <EEPROM.h>
#include <UIPEthernet.h>
SPI.h
: Digunakan untuk komunikasi SPI.EEPROM.h
: Digunakan untuk membaca dan menulis pada EEPROM.UIPEthernet.h
: Digunakan untuk menghubungkan dan berkomunikasi melalui Ethernet untuk Arduino Nano.
Variables
int address = 0;
byte value;
int ledPin = 13;
int inPin = 7;
int val = HIGH;
int valOld = HIGH;
String inputString = "";
bool stringComplete = false;
int incomingByte = 0;
int LANcon = 0;
int LANPrint = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 169, 254, 222, 40 };
byte server[] = { 169, 254, 222, 37 };
EthernetClient client;
address
: Alamat EEPROM yang sedang diakses.value
: Nilai yang dibaca dari atau ditulis ke EEPROM.ledPin
: Pin untuk LED.inPin
: Pin untuk tombol input.val
dan valOld
: Nilai saat ini dan sebelumnya dari tombol input.inputString
: String untuk menyimpan data serial yang masuk.stringComplete
: Menandakan apakah string yang masuk telah lengkap.incomingByte
, LANcon
, LANPrint
: Variabel tambahan untuk pengelolaan data.mac
, ip
, server
: Alamat MAC dan IP untuk koneksi Ethernet.client
: Objek untuk mengelola koneksi Ethernet.
Setup Function
void setup() {
pinMode(inPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
inputString.reserve(200);
Ethernet.begin(mac, ip);
Serial.begin(9600);
Serial.println("starting simple arduino client test");
value = EEPROM.read(address);
Serial.println("");
Serial.print("Hardware ID: ");
Serial.print(value);
Serial.println();
Serial.println("999 -> Hardware ID");
Serial.println(" 0 -> test RESET");
Serial.println(" 1 -> test SET");
Serial.println(" >1 -> set Hardware ID");
Serial.println("");
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
Serial.println("");
client.println("GET /ardu/data.php?id=" + String(value) + "&st=2 HTTP/1.0");
client.println();
LANcon = 1;
} else {
Serial.println("connection failed");
}
}
- Mengatur pin mode dan memulai koneksi Ethernet dan Serial.
- Membaca nilai dari EEPROM dan menampilkannya.
- Mencoba menghubungkan ke server dan mengirimkan permintaan HTTP GET.
TombolEvent Function
void TombolEvent(int st) {
Serial.print("Send to server st= ");
Serial.println(st);
if (client.connect(server, 80)) {
Serial.println("connected");
Serial.println("");
client.println("GET /ardu/data.php?id=" + String(value) + "&st=" + String(st) + " HTTP/1.0");
client.println();
LANcon = 1;
} else {
Serial.println("connection failed");
}
}
- Fungsi untuk mengirimkan status tombol ke server.
Loop Function
void loop() {
val = digitalRead(inPin);
digitalWrite(ledPin, val);
if (valOld != val) {
if (val == LOW) {
Serial.print("Tombol ");
Serial.print(value);
Serial.println(" -> SET!");
TombolEvent(1);
valOld = val;
} else if (val == HIGH) {
Serial.print("Tombol ");
Serial.print(value);
Serial.println(" -> RESET!");
TombolEvent(0);
valOld = val;
}
delay(500);
}
if (stringComplete) {
Serial.print("inputString: ");
Serial.print(inputString.toInt());
if (inputString.toInt() == 999) {
Serial.println(" Mem Read");
value = EEPROM.read(address);
Serial.print("Hardware ID: ");
Serial.println(value);
Serial.println("");
} else if (inputString.toInt() > 1) {
Serial.println(" Mem Update");
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
delay(100);
Serial.println("Mem clear");
int val = inputString.toInt();
EEPROM.write(address, val);
delay (100);
value = EEPROM.read(address);
Serial.print("Update Hardware ID to: ");
Serial.println(value);
Serial.println("");
}
if (inputString.toInt() == 1) {
Serial.println("");
Serial.println("Serial -> SET!");
TombolEvent(1);
} else if (inputString.toInt() == 0) {
Serial.println("");
Serial.println("Serial -> RESET!");
TombolEvent(0);
}
stringComplete = false;
inputString = "";
}
if (client.available() && LANcon == 1) {
char c = client.read();
}
if (!client.connected() && LANcon == 1) {
Serial.println("disconnecting.");
Serial.println("");
client.stop();
LANcon = 0;
}
}
- Memeriksa status tombol dan mengirimkan status ke server jika berubah.
- Menangani input serial untuk membaca atau memperbarui nilai EEPROM.
- Memeriksa koneksi ke server dan menutup koneksi jika tidak terhubung.
SerialEvent Function
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
- Mengumpulkan data dari Serial hingga menemukan karakter newline (
'\n'
), lalu menandai string lengkap.
Kesimpulan
Skrip ini berfungsi untuk:
- Membaca dan menulis nilai pada EEPROM.
- Menghubungkan ke server melalui Ethernet dan mengirimkan data berdasarkan status tombol.
- Menangani input serial untuk mengontrol perangkat secara manual.
Terima kasih