Reading Requirement:
-
Register (Geeknesia)
-
Create Project (Geeknesia)
-
Create Gateway (Geeknesia)
- Create device (Geeknesia)
Pada tutorial ini akan menjelaskan bagaimana cara mengrim Alert Message dari device yang telah terhubung dengan Geeknesia ke alamat email yang dituju.
Siapkan Alat dan bahan:
- Arduino + Ethernet shield.
- Kabel LAN.
- Kabel untuk Upload program.
Rangkailah alat yang sudah disiapkan seperti gambar di bawah ini:
Buatlah code seperti di bawah ini:
Code
#include <SPI.h> #include <Ethernet.h> #include <PubSubClient.h> // Update these with values suitable for your network. byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte ip[] = { Your IP Address }; // ex: 192,168,0,2 byte dns2[] = {8,8,8,8}; byte gateway[] = { IP Access Point }; // ex: 192,168,0,1 char message_buff[200]; int flag; char *deviceId = "device-a3a33635cc9e3eb0ecd2a1e1a91xxxxx"; char *deviceUsername = "7309cb5831af0623a420bf2d3fexxxxx"; char *devicePassword = "a75a605e88b3a8033957b1f1f2dxxxxx"; String deviceCredential = String(deviceUsername) + ":" + String(devicePassword); EthernetClient ethClient; PubSubClient client("geeknesia.com", 1883, callback, ethClient); void callback(char* topic, byte* payload, unsigned int length) { } void setup() { Serial.begin(9600); Serial.println("Get Here"); Ethernet.begin(mac, ip,dns2,gateway); delay(500); } //Send data void senddata(char* topic,String credential, char *var1, unsigned int nilai1){ String pubString ="{\"code\":\""; pubString += credential; pubString +="\","; pubString +="\"attributes\":{\""; pubString +=String(var1)+"\":\""+nilai1+"\""; //pubString +=String(var2)+"\": "+String(nilai2); pubString +="}}"; pubString.toCharArray(message_buff,pubString.length()+1); Serial.println(message_buff); client.publish("iot/data",message_buff); } //Send Alert void sendAlert(String from, String to, String subject, String content){ String pubString = "{\"from\":\""+from+"\",\"to\":\""+to+"\",\"subject\":\""+subject+"\",\"content\":\""+content+"\"}"; pubString.toCharArray(message_buff,pubString.length()+1); client.publish("iot/trigger",message_buff); Serial.println(message_buff); } void loop(){ Serial.println("Loop"); if (!client.connected()){ client.connect(deviceId,NULL,NULL,"iot/will",2,64,deviceId); Serial.println("gagal konek mqtt"); } else{ client.publish("iot/live",deviceId); Serial.println("MQTT Connect"); } flag++; if (flag==10){ sendAlert("support@geeknesia.com","egafebriyana@gmail.com","subject","Perhatian!! Counter=10"); } senddata(deviceId,deviceCredential,"Count",flag); delay(5000); }
Code di atas dibuat hanya sebagai system counter biasa yang dimana counter akan bertambah per 5 detik.
Upload program di atas kemudian buka serial monitor untuk debugging.
Ini adalah gambar pesan yang telah dikirim dari support@geeknesia.com.