Nyambungake Arduino menyang Internet

Yen sampeyan lagi miwiti nggarap Arduino, nyambung menyang internet bisa dadi tugas sing angel banget. Aku ngelingi perjuanganku dhewe nalika wiwitan coding Arduino. Kanthi kekurangan antarmuka sing tepat lan ora ana alat debugging sing dibangun, bisa dadi angel kanggo nemtokake manawa sampeyan salah. Ing artikel iki, kita bakal nyakup kabeh sing sampeyan kudu ngerti babagan nyambungake papan Arduino menyang internet, kalebu kode.

Nalika pisanan wiwit muter karo Arduino Uno, aku bangga banget nggawe LED kedhip lan sensor bip. Nanging aku langsung nyadari yen potensial sejatine Arduino mung bisa digayuh yen sampeyan nyambungake menyang internet.

Cukup akeh proyek Arduino tingkat menengah nganti tingkat lanjut mbutuhake sampeyan nyambung menyang internet amarga macem-macem alasan, yaiku logging data ing méga kayata data cuaca utawa ngirim printah wektu nyata menyang piranti saka jarak adoh kayata liwat aplikasi ing piranti sampeyan. telpon.

Ing artikel tutorial iki, kita bakal ndeleng carane nyambungake piranti Arduino menyang internet kanthi lengkap. Kabeh-saka hardware kanggo sirkuit lan kode-bakal dijamin.

Nyambung liwat Ethernet

Opsi pisanan kanggo nyambungake Arduino menyang internet yaiku liwat kabel Ethernet. Yen sampeyan nggunakake papan Arduino sing dilengkapi port Ethernet sing dibangun kayata Arduino Yún, sampeyan bisa ngliwati bagean ‘Persyaratan hardware’ lan deskripsi desain sirkuit sing diwenehake ing ngisor iki. Cukup sambungake kabel Ethernet menyang piranti lan miwiti coding. Nanging, yen, kaya umume wong, sampeyan duwe versi Arduino sing luwih gampang kayata Arduino Uno sing ora duwe port Ethernet sing dibangun, mula sampeyan kudu tuku piranti kapisah sing diarani ‘Ethernet Shield’ kanggo dipasang ing Arduino.

Persyaratan hardware

Sampeyan mbutuhake kabel Ethernet karo internet, papan Arduino Uno lan Ethernet Shield. Mangkene katrangan babagan desain sirkuit.

  1. Sambungake Ethernet Shield ing ndhuwur Arduino Uno.

    Gambar 1: Arduino Uno lan Ethernet Shield

  2. Sambungake kabel Ethernet menyang Ethernet Shield.
    Figure 1 nuduhake desain sirkuit Arduino Uno lan Ethernet Shield. Sawise nyambungake loro kasebut, Arduino Uno sampeyan kudu katon kaya ing Gambar 2.

Kode

Sadurunge miwiti kode kanggo nyambungake Arduino menyang internet, kita kudu nyakup perpustakaan eksternal kanggo digunakake ing program kasebut. Pustaka iki bakal mbantu kita nggawe sambungan lan ngirim / nampa data liwat internet.

Cathetan: Pustaka iki kudu wis diinstal kanthi IDE sampeyan. Yen ana sawetara alasan sampeyan ngalami kasalahan, mangga download perpustakaan iki saka gudang GitHub resmi Arduino Project.
#include 

Ing langkah sabanjure, kita bakal nemtokake sawetara konstanta lan variabel sing dibutuhake kanggo program iki. Pisanan, kita butuh alamat MAC. Iki asring dicithak ing stiker ing Ethernet Shield. Sabanjure, kita kudu nemtokake alamat IP statis. Priksa manawa alamat IP sing sampeyan tambahake ora digunakake dening pangguna liyane. Pungkasan, kita bakal nemtokake variabel EthernetClient.

Gambar 2: Ethernet Shield ditempelake ing Arduino Uno
Gambar 2: Ethernet Shield ditempelake ing Arduino Uno
byte mac[] = { oxDE, oxAD, oxBE, oxEF, oxFE, oxED };
IPAddress staticIP(10, 0, 0, 20);

EthernetClient client;

Saiki kita bisa nulis cara kanggo nyambung menyang internet. Ana rong langkah ing kene. Kaping pisanan, kita nyoba njupuk IP liwat DHCP (dynamic host configuration protocol), yaiku, nyoba njupuk IP dinamis sampeyan. Yen langkah kasebut gagal, banjur bali menyang staticIP sing wis ditemtokake ing ndhuwur.

void connectToInternet()
{
// Step 1 - Try connecting with DHCP
If (Ethernet.begin(mac) == 0)
{
Serial.print(“[ERROR] Failed to connect via DHCP”);
Ethernet.begin(mac, staticIP); // Connect via static IP defined earlier
}
// Add a delay for initialization
delay(1000);

Serial.println(“[INFO] Connection Successful”);
Serial.print(“”);
printConnectionInformation(); // Custom method
Serial.println(“---------------------------------”);
Serial.println(“”);
}

Nalika sampeyan bisa ndeleng ing kode ndhuwur, kita digunakake cara adat printConnectionInformation () kanggo informasi sambungan tampilan. Dadi ayo ndhisiki lan nulis kode kasebut.

void printConnectionInformation()
{
// Print IP Address
Serial.print(“[INFO] IP Address: ”);
Serial.println(Ethernet.localIP());

// Print Subnet Mask
Serial.print(“[INFO] Subnet Mask: ”);
Serial.println(Ethernet.subnetMask());

// Print Gateway
Serial.print(“[INFO] Gateway: ”);
Serial.println(Ethernet.gatewayIP());

// Print DNS
Serial.print(“[INFO] DNS: ”);
Serial.println(Ethernet.dnsServerIP());
}

Pungkasan, kita bakal nulis fungsi standar kanggo program kasebut, yaiku, setelan() lan loop().

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

// Connect to the internet
connectToInternet();
}

void loop()
{
// Nothing much to do here.
}

Yen kabeh mriksa metu, sampeyan kudu ndeleng output sing padha karo Gambar 3 ing jendhela Serial Monitor.

Gambar 3: Kasil nyambung menyang internet liwat Ethernet
Gambar 3: Kasil nyambung menyang internet liwat Ethernet

Nyambung liwat Wi-Fi

Opsi kapindho yaiku nyambungake Arduino menyang internet kanthi nirkabel liwat Wi-Fi. Yen, kaya umume wong, sampeyan duwe Arduino Uno utawa papan Arduino liyane sing ora duwe kemampuan Wi-Fi sing dibangun, mula sampeyan bakal duwe. kanggo tuku ‘Wireless Shield’ kanthi kapisah, kaya sing kita tuku Ethernet Shield ing bagean sadurunge.

Yen sampeyan duwe Arduino Yún utawa papan Arduino liyane kanthi kemampuan nirkabel sing dibangun, sampeyan bisa ngliwati ‘Persyaratan Hardware’ lan katrangan babagan desain sirkuit sing diwenehake ing ngisor iki lan langsung miwiti kode kasebut.

Persyaratan hardware

Sampeyan mbutuhake Arduino Uno lan Wireless Shield. Mangkene katrangan babagan desain sirkuit.

1. Sambungake Wireless Shield ing ndhuwur Arduino Uno.
2. Sambungake Arduino Uno menyang komputer liwat port USB.

Gambar 4: Arduino Uno lan Wireless Shield
Gambar 4: Arduino Uno lan Wireless Shield

Figure 4 nuduhake desain sirkuit Arduino Uno lan Wireless Shield. Yen kabeh wis disambungake kanthi bener, papan Arduino Uno sampeyan kudu katon kaya ing Gambar 5.

Gambar 5: Wireless Shield sing dipasang ing Arduino Uno
Gambar 5: Wireless Shield sing dipasang ing Arduino Uno

Kode

Nalika nyambung menyang internet liwat Ethernet, kita wis digunakake perpustakaan external . Kajaba iku, kanggo nyambungake internet kanthi nirkabel, kita kudu nggunakake perpustakaan eksternal .

Cathetan: Pustaka iki kudu wis diinstal kanthi IDE sampeyan. Yen ana sawetara alasan sampeyan ngalami kasalahan, mangga download perpustakaan iki saka gudang GitHub resmi Arduino Project.
#include 
#include 

Sabanjure, kita bakal nemtokake konstanta lan variabel sing dibutuhake kanggo nyambungake kanthi nirkabel. Kanggo nyambungake karo Wi-Fi, kita mbutuhake ssid lan sandhi Wi-Fi sing bakal kita gunakake. Kita uga bakal nggawe variabel WiFiClient kanggo nyambung menyang internet.

char ssid[] = “Write WiFi SSID here”;
char pass[] = “Write WiFi password here”;

int keyIndex - 0;
int status = WL_IDLE_STATUS;

WiFiClient client;

Saiki kita bakal nemtokake sawetara cara khusus kanggo nyambungake lan ngirim / nampa data liwat internet. Pisanan, ayo nggawe cara connectToInternet () kanggo nyambungake karo internet.

void connectToInternet()
{
status = WiFi.status();

if (status == WL_NO_SHIELD)
{
Serial.println(“[ERROR] WiFi Shield Not Present”);
while (true);
}

while ( status != WL_CONNECTED)
{
Serial.print(“[INFO] Attempting Connection - WPA SSID: ”);
Serial.println(ssid);

status = WiFi.begin(ssid, pass);
}

Serial.print(“[INFO] Connection Successful”);
Serial.print(“”);
printConnectionInformation();
Serial.println(“-------------------------”);
Serial.println(“”);
}

Nalika sampeyan bisa ndeleng ing kode ndhuwur, kita wis disebut cara adat printConnectionInformation() kanggo nampilake informasi sambungan Wi-Fi kita. Dadi ayo maju lan nulis metode kasebut:

void printConnectionInformation()
{
Serial.print(“[INFO] SSID: ”);
Serial.println(WiFi.SSID());

// Print Router’s MAC address
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print(“[INFO] BSSID: ”);

Serial.print(bssid[5], HEX);
Serial.print(“:”);

Serial.print(bssid[4], HEX);
Serial.print(“:”);

Serial.print(bssid[3], HEX);
Serial.print(“:”);

Serial.print(bssid[2], HEX);
Serial.print(“:”);

Serial.print(bssid[1], HEX);
Serial.print(“:”);

Serial.println(bssid[0], HEX);

// Print Signal Strength
long rssi = WiFi.RSSI();
Serial.print(“[INFO] Signal Strength (RSSI) : ”);
Serial.println(rssi);

// Print Encryption type
byte encryption = WiFi.encryptionType();
Serial.print(“[INFO] Encryption Type : ”);
Serial.println(encryption, HEX);
// Print WiFi Shield’s IP address
IPAddress ip = WiFi.localIP();
Serial.print(“[INFO] IP Address : ”);
Serial.println(ip);

// Print MAC address
byte mac[6];
WiFi.macAddress(mac);

Serial.print(“[INFO] MAC Address: ”);

Serial.print(mac[5], HEX);
Serial.print(“:”);

Serial.print(mac[4], HEX);
Serial.print(“:”);

Serial.print(mac[3], HEX);
Serial.print(“:”);

Serial.print(mac[2], HEX);
Serial.print(“:”);

Serial.print(mac[1], HEX);
Serial.print(“:”);

Serial.println(mac[0], HEX);
}

Pungkasan, ayo nulis fungsi standar, yaiku, persiyapan () lan loop ():

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

// Connect to the internet
connectToInternet();
}

void loop()
{
// Nothing to do here
}

Hore! Kita wis rampung karo kode Arduino kanggo nyambung menyang internet liwat Wi-Fi. Yen ora ana kesalahan, output ing jendhela Serial Monitor kudu katon kaya output ing Gambar 6.

Gambar 6: Kasil nyambung menyang internet liwat Wi-Fi
Gambar 6: Kasil nyambung menyang internet liwat Wi-Fi

Leave a Comment

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