Sistem penghitung jumlah banyak orang yang lewat pada suatu gate atau pintu masuk, tes
Project ini adalah suatu sistem penghitung jumlah banyak orang yang yang lewat pada suatu gate atau pintu masuk, sistem ini dibuat dengan menggunakan intel galileo dengan input sebuah photo sensor , yang dimana datanya akan dikirim ke cloud (geeknesia).
Ini adalah sample code pada project ini menggunkan Arduino IDE
#include
#include
#include
#include
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 88 };
// Deklarasi Pin dan flag internal
const int buttonPin = 4;
int buttonState=1;
int flag=0;
int counter=0;
int prev =0;
int flag1=0;
int sendMQTT = 0;
bool conn_ok;
char message_buff[200];
EthernetClient ethClient;
PubSubClient client("geeknesia.com", 1883, callback, ethClient);
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
for(i=0; i message_buff_call[i] = payload[i];
}
message_buff_call[i] = '\0';
String msgString = String(message_buff_call);
Serial.println(msgString + "ui");
}
void setup()
{
system("ifup eth0");
delay(10000); //wait for accessing sensor
counter=EEPROM.read(flag1);
Serial.begin(9600);
pinMode(4, INPUT);
if (Ethernet.begin(mac) != 1) {
Ethernet.begin(mac,ip);
}
delay(1000);
Serial.println("connecting...");
while (client.connect("device-ec93af64ce066e4ad0375e3773a04fca","iot/will",0,0,"device-ec93af64ce066e4ad0375e3773a04fca") != 1)
{
Serial.println("Error connecting to MQTT1");
delay(2000);
}
client.subscribe("topic-ec93af64ce066e4ad0375e3773a04fca");
Serial.println("Setup complete");
counter=counter-1;
}
// fungsi pengiriman data,fungsi ini akan di panggil ketika ada aksi pengiriman attributes
void senddata(char* topic,char* credential, char *var1, int nilai1 ){
String pubString = "{\"code\":\"";
pubString += credential;
pubString +="\",";
pubString +="\"attributes\":{\"";
pubString +=String(var1)+"\":\""+nilai1+"\"";
pubString +="}}";
char message_buff[pubString.length()+1];
pubString.toCharArray(message_buff, pubString.length()+1);
Serial.println(message_buff);
client.publish("iot/data",message_buff);
}
void loop()
{
if (!client.connected()){
client.connect("device-ec93af64ce066e4ad0375e3773a04fca",NULL,NULL,"iot/will",2,64,"device-ec93af64ce066e4ad0375e3773a04fca");
}
buttonState = digitalRead(buttonPin);
if (buttonState!=prev){
prev = buttonState;
if(buttonState==0){ //first time
if (flag == 0){
counter=counter+1;
Serial.println(counter);
senddata("device-ec93af64ce066e4ad0375e3773a04fca","412770eb927e15652f0f83dad2240ed2:f531fb5b08ae272426fdbef0daa3e174","Count", counter); //kirim data counter ke cloud
flag=1;
if(sendMQTT == 1){
conn_ok = client.connected();
}
if (conn_ok == 0){ //no connection, reconnect
client.disconnect();
}
EEPROM.write(flag1,counter);
}
else{
flag=0;
}
}
}
}