Created
May 15, 2016 16:20
-
-
Save fajarlabs/89d00a5fead1281a39d0a51be208d380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ArduinoJson.h> | |
#include <vector> | |
// WIfi Multi | |
ESP8266WiFiMulti WiFiMulti; | |
// GPIO init | |
const int pinOutput[] = { D2,D3,D4,D5,D6,D7,D8,D9,D10 }; | |
typedef std::vector<int> data_signal; | |
int countPin = 0; | |
void setup() | |
{ | |
Serial.begin(115200); | |
// set Wifi SSID dan passwordnya | |
WiFiMulti.addAP("SSID_1", "password_1"); | |
WiFiMulti.addAP("SSID_2", "password_2"); | |
// GPIO set OUTPUT | |
countPin = (sizeof(pinOutput)/sizeof(*pinOutput)); | |
for(int p = 0; p < countPin; p++) | |
pinMode(pinOutput[p], OUTPUT); | |
} | |
/** | |
* Untuk parsing data string | |
*/ | |
void parseAndExecData(char * data) { | |
// Vector | |
data_signal ds; | |
const char s[2] = "-"; | |
char *token = strtok(data, s); | |
Serial.print("Debug TOKEN => "); | |
/* walk through other tokens */ | |
while( token != NULL ) | |
{ | |
// Konversi char ke integer | |
int j = atoi(token); | |
// Add to vector | |
ds.push_back(j); | |
token = strtok(NULL, s); | |
} | |
Serial.print("Debug size token => "); | |
Serial.print(ds.size()); | |
printf("\n"); | |
Serial.print("Debug Data => "); | |
// Iterasi | |
data_signal::iterator iter; | |
// Periksa apakah total data sudah sama | |
// dengan total pin ? | |
if(countPin == ds.size()) { | |
int index = 0; | |
for (iter = ds.begin(); iter != ds.end(); ++iter) { | |
// Debug iterasi | |
printf("%d ", *iter); | |
// GPIO = 1 (hidup) | |
if(*iter == 0) | |
digitalWrite(pinOutput[index],LOW); | |
// GPIO = 1 (mati) | |
if(*iter == 1) | |
digitalWrite(pinOutput[index],HIGH); | |
index++; | |
} | |
} | |
printf("\n"); | |
} | |
/** | |
* Untuk parsing JSON | |
**/ | |
char * parseJson (String json) { | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(json); | |
if (!root.success()){ | |
Serial.printf("parseObject() failed\n"); | |
return NULL; | |
} | |
// Get value | |
const char * value = root["value"]; | |
char * data = const_cast<char*>(value); | |
// Not use | |
// Get error code | |
int code = root["code"]; | |
return data; | |
} | |
/** | |
* Untuk periksa web service agnosthing | |
**/ | |
void HTTPSwitchCheck(String url, const int gate[]) { | |
HTTPClient http; | |
// Storage anti drop | |
static String json; | |
// ganti dengan URL API Last Feed punyamu sendiri | |
http.begin(url); | |
// mulai koneksi dan ambil HTTP Header | |
int httpCode = http.GET(); | |
// httpCode akan bernilai negatif bila error | |
if(httpCode > 0) | |
{ | |
// cetak httpCode ke Serial | |
Serial.printf("[HTTP] GET... code: %d\n", httpCode); | |
// bila nilai dari server diterima | |
if(httpCode == HTTP_CODE_OK) | |
{ | |
// cetak string json dari server | |
json = http.getString(); | |
} | |
} else { | |
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
// tutup koneksi HTTP | |
http.end(); | |
// Debug | |
Serial.print("Debug JSON => "); | |
Serial.println(json); | |
char* data = parseJson(json); | |
parseAndExecData(data); | |
} | |
void loop() | |
{ | |
// tunggu koneksi Wifi | |
if((WiFiMulti.run() == WL_CONNECTED)) | |
{ | |
Serial.printf("Succesfully Connected Wifi\n"); | |
HTTPSwitchCheck("http://agnosthings.com/88bXXXXX-117c-XXXX-XXXX-0050568XXXXX/field/last/feed/255/switch",pinOutput); | |
} else { | |
Serial.printf("Failed Connected Wifi\n"); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment