Membaca Sensor Analog pada Intel Galileo menggunakan Agnost

Penjelasan singkat cara membaca sensor analog Intel Galileo menggunakan Agnosthings

IntermediateProtip30 minutes237
Membaca Sensor Analog pada Intel Galileo menggunakan Agnost

Things used in this project

Hardware components

USB-A to B Cable
USB-A to B Cable
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Kabel LAN
×1

Software apps and online services

Arduino IDE
Arduino IDE
Notepad++

Story

Read more

Code

Arduino Program

Arduino
#include <SPI.h>
#include <Ethernet.h>
 
// Update these with values suitable for your network.
byte mac[]      = { 0x98, 0x4F, 0xEE, 0x00, 0x70, 0x32 };
byte ip[]       = { 192, 168, 1, 100 };
byte dns[]      = { 192, 168, 1, 1 };
byte gateway[]  = { 192, 168, 1, 1 };
char server[]   = "agnosthings.com";
EthernetClient ethClient;
 
// set variabel pin & value sensor
const int pinSensorSuhu = A0;
int sensorSuhu = 0;
 
void setup()
{
    // Reset connection
    system("ifdown eth0");
    system("ifup eth0");
    delay(2000);
 
    // connecting network
    Serial.begin(9600);
    Serial.println("connecting...");
    if (Ethernet.begin(mac) != 1) {
      Ethernet.begin(mac, ip, dns, gateway);
    }
    delay(1000);
    Serial.print("Running on: ");
    Serial.println(Ethernet.localIP());
}
 
void loop()
{
    // read the analog in value:
    // map it to the range of the analog out:
    sensorSuhu = map(analogRead(pinSensorSuhu), 0, 1023, 0, 100);
 
    // ganti dengan URL API Push Feed punyamu sendiri
    String url_api  = "/bc0dfdba-f5be-11e5-8001-xxxxxxxxxxxx/feed?push=led={v_led},sensor={v_sensor}";
    url_api.replace("{v_led}", "");
    url_api.replace("{v_sensor}", String(sensorSuhu));
    Serial.println("Sensor Suhu: "+String(sensorSuhu));
 
    boolean requestStatus = httpRequest(url_api);
    if(requestStatus == true)
      Serial.println("Sending data success!");
    else
      Serial.println("Sending data failed!");
    Serial.println();
 
    // close any connection before send a new request.
    // This will free the socket on the Ethernet shield
    ethClient.stop();
    delay(1000);    
}
 
boolean httpRequest(String link) { 
  // if you get a connection, report back via serial:
  Serial.println("Push data to server");
  if (ethClient.connect(server, 80)) {
    // Make a HTTP request:
    ethClient.println("GET " + link + " HTTP/1.0");
    ethClient.println("Host: agnosthings.com");        
    ethClient.println();
    return true;  
  } else {
    // You couldn't make the connection   
    Serial.println("Connection Failed");
    ethClient.stop();
    return false;
  }
}

HTML Program

HTML
<!DOCTYPE html>
<html>
<head>
	<title>Monitoring Data Analog dari Internet</title>
	<script type="text/javascript" src="https:// cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
</head>
<body>
	<h3>Analog Value:</h3>
	<h1 id="analog">27<sup>%</sup></h1>
	<script type="text/javascript">
		var last_feed_api_url = 
			'http:// agnosthings.com/bc0dfdba-f5be-11e5-8001-005056805279/field/last/feed/76/sensor';
 
		setInterval(function(){
	    	$.getJSON(last_feed_api_url, null, function(respon){
	    		var sensor  = respon.value;
	    		$("#analog").html(sensor + "<sup>%</sup>");
	    	});
		}, 1000); // update nilai per 1000 milisecond / 1 detik
	</script>
</body>
</html>

Credits

dirakit community

dirakit community

122 projects • 92 followers
Indonesia IoT Community
Sulis Priyanto

Sulis Priyanto

13 projects • 1 follower
Habibi Mustafa

Habibi Mustafa

13 projects • 1 follower
Dicoding Indonesia

Comments

Add projectSign up / Login