Kamis, 19 Oktober 2017

Sim Aplikasi IOT 3 Sensor



1.Registrasi di https://thingspeak.com/
2.Membuat Channel


3.Dapatkan API KEY nya



CATATAN:

Kita memiliki 2 api key, api pertama u menulis /mengirim data ke server IOT, API kedua untuk membaca data IOT .

API Keys Settings

  • Write API Key: Use this key to write data to a channel. If you feel your key has been compromised, click Generate New Write API Key.
  • Read API Keys: Use this key to allow other people to view your private channel feeds and charts. Click Generate New Read API Key to generate an additional read key for the channel.
  • Note: Use this field to enter information about channel read keys. For example, add notes to keep track of users with access to your channel.

API Requests

Update a Channel Feed
GET https://api.thingspeak.com/update?api_key=RX25Q5K1OKO6XXXXX&field1=0
Get a Channel Feed
GET https://api.thingspeak.com/channels/349194/feeds.json?api_key=NRCQKD7BXADXXXXX&results=2
Get a Channel Field
GET https://api.thingspeak.com/channels/349194/fields/1.json?api_key=NRCQKD7BXADXXXXX&results=2
Get Channel Status Updates
GET https://api.thingspeak.com/channels/349194/status.json?api_key=NRCQKD7BXADXXXXX

API Endpoints



HTTP API Address

For nonsecure communication to ThingSpeak using HTTP, use the address:
http://api.thingspeak.com
For secure communication to ThingSpeak using HTTPS, use the address:
https://api.thingspeak.com

HTTP API Static IP Address

To communicate with the ThingSpeak HTTP server IP, use the address:
http://184.106.153.149

MQTT API Address

To communicate with the ThingSpeak MQTT broker at the port 1883, use the address:
mqtt.thingspeak.com

Cross-Domain XML

To post using cross-domain XML, use the address:
http://api.thingspeak.com/crossdomain.xml

Channel Access in MATLAB

To read data from a private channel in MATLAB, use the thingSpeakRead function:
thingSpeakRead(channelID,'ReadKey','Your.Read.API.Key.String');
To write data from MATLAB, use the thingSpeakWrite function:
thingSpeakWrite(channelId,data,thingSpeakWrite(17504,[2.3,1.2,3.2],'WriteKey','Your.Write.API.Key.String');

Adapun contoh code pada INO adalah sbb

KIta baca dahulu IP server  https://thingspeak.com/




#include <SoftwareSerial.h>
SoftwareSerial ESP8266(10, 11); //RX,TX

#define IP "34.230.96.113" // thingspeak.com IP address
String GET = "GET /update?api_key=RX25Q5K1OKO6XXXXX"; // API key

void setup() {
  Serial.begin(9600); // Hardware serial (serial monitor)
  ESP8266.begin(9600); // Software serial (komunikasi dengan ESP8266)
  ESP8266.println("AT+RST"); // Reset ESP8266
  delay(2000);
  ESP8266.println("AT");
  delay(5000);
  if (ESP8266.find("OK")) {
    Serial.println("Connected to ESP8266");
  }
  else {
    Serial.println("Not connected to ESP8266");
  }
}

void loop() {
  delay(2000);
  ESP8266.println("AT+RST"); // Reset setiap akan mengirim data
  delay(7000);

  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";

  ESP8266.println(cmd);
  delay(5000);

  if (ESP8266.find("Error")) {
    Serial.println("AT+CIPSTART Error");
    return;
  }
  else {
    Serial.println("AT+CIPSTART Success");
  }
  cmd = GET;
  cmd += "&c1=";
  cmd += random(300);
  cmd += "&c2=";
  cmd += random(300);
  cmd += "&c3=";
  cmd += random(300);
  cmd += "\r\n\r\n";
  ESP8266.print("AT+CIPSEND=");
  ESP8266.println(cmd.length());
  delay(10000);

  if (ESP8266.find(">")) { // Perintah AT+CIPSEND akan menampilkan prompt ">"
    ESP8266.print(cmd);
    Serial.println("Data sent");
  }
  else
  {
    Serial.println("AT+CIPSEND error");
  }
}



HASIL:




NB Teman2 sebelum praktek menggunakan sensor bisa juga kita uji IOT kita ini menggunakan code php sbb:

<?php
//https://thingspeak.com/channels/349194/api_keys

//RX25Q5K1OK212411
//https://api.thingspeak.com/update?api_key=RX25Q5K1OK212411&field1=0

//NRCQKD7BXAD81ID7
//https://api.thingspeak.com/channels/349194/feeds.json?api_key=NRCQKD7BXAD81ID7&results=2
//https://api.thingspeak.com/channels/349194/fields/1.json?api_key=NRCQKD7BXAD81ID7&results=2
//https://api.thingspeak.com/channels/349194/status.json?api_key=NRCQKD7BXAD81ID7


//https://api.thingspeak.com/update?api_key=RX25Q5K1OK212411&field1=122&field2=126&field3=129

$r1=rand(200,300);
$r2=rand(200,300);
$r3=rand(200,300);

?>

<form action="https://api.thingspeak.com/update?" method="get">

SUHU1:<input type="text" name="field1" value="<?php echo $r1;?>"><br>
SUHU2:<input type="text" name="field2" value="<?php echo $r2;?>"><br>
SUHU3:<input type="text" name="field3" value="<?php echo $r3;?>"><br>
<input type="Submit" value="Kirim">
<input type="hidden" name="api_key" value="RX25Q5K1OK212411">
</form>

<?php

$homepage = file_get_contents('https://api.thingspeak.com/channels/349194/feeds.json?api_key=NRCQKD7BXAD81ID7&results=2');
echo $homepage;
echo"<hr>";


$homepage = file_get_contents('https://api.thingspeak.com/channels/349194/fields/1.json?api_key=NRCQKD7BXAD81ID7&results=2');
echo $homepage;
echo"<hr>";


$homepage = file_get_contents('https://api.thingspeak.com/channels/349194/status.json?api_key=NRCQKD7BXAD81ID7');
echo $homepage;
echo"<hr>";

?>

NB API KEY DIGANTI SESUAI API KEY MASING2 YAAA>...Lalu simpan di C:\XAMPP\HTDOCS\Thingspeak\

DAN JALANKAN:



HASILNYA:

OK MUDAH KAN...........
jika sudah selesai baru mainkan menggunakan arduino +sensor2








Tidak ada komentar:

Posting Komentar