dirakit communityHabibi Mustafa
Published © CC BY

Monitoring Analog dengan Potensiometer Berbasis Geeknesia

Artikel series membahas dengan singkat tentang cara membuat monitoring analog dengan potensiometer

IntermediateFull instructions provided30 minutes30
Monitoring Analog dengan Potensiometer Berbasis Geeknesia

Things used in this project

Hardware components

Intel Galileo Gen 1
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×3

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

sketch

Arduino
#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[]          = { 192, 168, 100, 2 };
byte dns[]         = {8,8,8,8};
byte gateway[]     = { 192, 168, 100, 1 };
 
const int      analogInPin = A0;
int            sensorValue = 0;        // value read from the pot
int            sendMQTT = 0;
bool           conn_ok;
char           message_buff[200]; 
char*          geeknesia_deviceid   = "DEVICE-ID";
char*          geeknesia_credential = "USERNAME:PASSWORD";
 
EthernetClient ethClient;
void callback(char* topic, byte* payload, unsigned int length) {}
PubSubClient   client("geeknesia.com", 1883, callback, ethClient);  //Address geeknesia
 
void setup() {
    // reset network
    system("ifdown eth0");
    system("ifup eth0");
 
    // wait for accessing sensor
    delay(2000);
    Serial.begin(9600);
 
  	// Koneksi DHCP
  	if (Ethernet.begin(mac) != 1) {
    	// Jika gagal, maka koneksi manual
    	Ethernet.begin(mac, ip, dns, gateway);
  	}
 
	Serial.print("Running on: ");
	Serial.println(Ethernet.localIP());  
 
	Serial.println("connecting...");
	while (client.connect(geeknesia_deviceid,"iot/live",0,0,geeknesia_deviceid) != 1) 
	{
		Serial.println("Error connecting to MQTT");
		delay(2000);
	}
}
 
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(geeknesia_deviceid,NULL,NULL,"iot/live",2,64,geeknesia_deviceid);
  }
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
 
  // Sending data to Geeknesia
  senddata(geeknesia_deviceid, geeknesia_credential,"Analog",sensorValue);
 
  // print the results to the serial monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
 
  if(sendMQTT == 1){
      conn_ok = client.connected();
  }
  if (conn_ok == 0){            //no connection, reconnect
      client.disconnect();
  }
 
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);  
}

Credits

dirakit community

dirakit community

122 projects • 90 followers
Indonesia IoT Community
Habibi Mustafa

Habibi Mustafa

13 projects • 1 follower
Dicoding Indonesia

Comments

Add projectSign up / Login