LAMPIRAN (6)
#include <Adafruit_CC3000.h> #include <ccspi.h>
#define Wido_IRQ 7 #define Wido_VBAT 5 #define Wido_CS 10 #include "utility/debug.h"
#include <LiquidCrystal.h> LiquidCrystal lcd(8,9,4,5,6,7);
int sensorPin = A1; // inisiasi Pin input nilai analog sensor int sensorValue = 0; // variabel mulai sensor
// Tipe Security : WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define WLAN_SSID "LG" // Nama SSID Wifi tidak boleh lebih dari 32 karakter
#define WLAN_PASS "lglglglglg" // Password WiFi #define TCP_TIMEOUT 3000 // TCP Timeout
#define TOKEN "oFtshN0z3TIjQwWxH3eTmhV43Hj9dE" // TOKEN Ubidots
#define VARIABLE_ID "5730aa3476254206d2899ede" // ID Variabel pada source yang akan diupload di UBidots
void setup(){
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n")); /* Initialise the module */
Serial.println(F("\nInitialising the CC3000 ...")); if (!Wido.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
By default connectToAP will retry indefinitely, however you can pass an
optional maximum number of retries (greater than zero) as the fourth parameter.
*/
if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { Serial.println(F("Failed!"));
while(1); }
/* Wait for DHCP to complete */ Serial.println(F("Request DHCP")); while (!Wido.checkDHCP()) Adafruit_CC3000_Client WidoClient;
void loop(){
static unsigned long RetryMillis = 0; static unsigned long uploadtStamp = 0; static unsigned long sensortStamp = 0;
if(!WidoClient.connected() && millis() - RetryMillis > TCP_TIMEOUT){
// Update the time stamp for reconnecting the ip RetryMillis = millis();
Serial.println(F("Trying to connect to Ubidots...")); // Connect to Ubidots
ip = Wido.IP2U32(50,23,124,68); WidoClient = Wido.connectTCP(ip, 80);
Serial.println(F("Successfully connected to Ubidots.")); }
if(WidoClient.connected() && millis() - uploadtStamp > 1000){ // If the device is connected to the cloud server, upload the data every 1000ms.
uploadtStamp = millis(); moisture();
saklar2Ubidots(String variable, String value); if (WidoClient.available())
// mengirim http data stream ke Ubidots bacaan sensor sendstream2Ubidots(String variable, String value); /********** Get the http page feedback and print the response ***********/
unsigned long rTimer = millis();
Serial.println(F("Reading Cloud Response...\r\n")); while (millis() - rTimer < 2000) {
while (WidoClient.connected() && WidoClient.available()) { applying the connection with the service
} }
void moisture() {
sensorValue = analogRead(sensorPin); // membaca nilai keluaran sensor
if(sensorValue<=300 ){ digitalWrite(13, HIGH); lcd.print(“KERING”); }
if(sensorValue>700 ){ lcd.print(“BASAH”); }
else if(sensorValue >=300 &&sensorValue <=700){ digitalWrite(13, LOW);
lcd.print(“LEMBAB”) }
}
void sendstream2Ubidots(String variable, String value){
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
// Variables for storing the length of http package body int length = 0;
char lengthstr[5];
Serial.println(httpBodyPackage); // Debug the http body stream
//Make an HTTP request to the Ubidots server Serial.print(F("Sending Http Request..."));
WidoClient.fastrprint(F("POST /api/v1.6/variables/")); WidoClient.fastrprint(VARIABLE_ID);
WidoClient.fastrprintln(F("/values HTTP/1.1"));
WidoClient.fastrprintln(F("Host: things.ubidots.com")); WidoClient.fastrprint(F("X-Auth-Token: "));
WidoClient.fastrprintln(TOKEN);
WidoClient.fastrprintln(F("Content-Type: application/json"));
WidoClient.fastrprint(F("Content-Length: "));
WidoClient.println(String(httpBodyPackage.length())); WidoClient.fastrprintln(F(""));
WidoClient.println(httpBodyPackage); Serial.println(F("Done...")); }
void saklar2Ubidots(String variable, String value){
String httpBodyPackage = "{\"value\":" + value + "}"; Serial.println(httpBodyPackage); // Debug the http body stream
//Make an HTTP request to the Ubidots server Serial.print(F("Sending Http Request...")); Serial.print(F("Sending Http Request..."));
WidoClient.fastrprint(F("GET /api/v1.6/variables/")); WidoClient.fastrprint(VARIABLE_ID);
WidoClient.fastrprint(F("/values/?page_size=1&token=")); WidoClient.fastrprint(TOKEN);
WidoClient.fastrprintln(F(" HTTP/1.1")); WidoClient.fastrprintln(F("Content-Type: application/json"));
WidoClient.fastrprintln(F("Host: things.ubidots.com")); WidoClient.fastrprintln(F("Connection: close"));