Last active
August 29, 2015 14:06
-
-
Save tzmfreedom/6cccce1918e2c5e49895 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 <SPI.h> | |
#include <Ethernet.h> | |
const int BUTTON = 7; | |
int val = 0; | |
int old_val = 0; | |
//ethernetシールドのmacアドレス | |
byte mac[] = {0x**, 0x**, 0x**, 0x0F, 0x8B, 0x8A}; | |
//Web proxyのIPアドレス | |
IPAddress server(192,168, 1, 2); | |
EthernetClient client; | |
//http接続がclose状態であればtrue | |
boolean isClose = true; | |
void setup(){ | |
pinMode(BUTTON, INPUT); | |
Serial.begin(9600); //シリアル通信はデバッグ用 | |
Ethernet.begin(mac); | |
} | |
void loop(){ | |
val = digitalRead(BUTTON); | |
if (val == HIGH && old_val != val && isClose) { | |
if (client.connect(server, 80)) { | |
Serial.println("connect"); | |
sendHttpRequest(); | |
} | |
delay(10); | |
} | |
old_val = val; | |
if (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
} | |
// if the server's disconnected, stop the client: | |
if (!client.connected() && !isClose) { | |
Serial.println(); | |
Serial.println("disconnecting."); | |
client.stop(); | |
isClose = true; | |
} | |
} | |
//Web Proxyにデータを送信 | |
void sendHttpRequest() { | |
isClose = false; | |
client.println("GET /salesforce/arduino-proxy.php HTTP/1.1"); | |
client.println("Host: 192.168.1.2"); | |
client.println("Connection: close"); | |
client.println(); | |
} |
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
<?php | |
require_once("./phptoolkit/soapclient/SforcePartnerClient.php"); | |
define("SF_USERNAME", "input your username"); | |
define("SF_PASSWORD", "input your password"); | |
$arduino_ip = "192.168.1.7"; | |
//arduinoからの通信のみを許可する。 | |
if ($_SERVER["REMOTE_ADDR"] !== $arduino_ip) { | |
echo $_SERVER["REMOTE_ADDR"]; | |
exit(); | |
} else { | |
echo "success"; | |
} | |
$connection = new SforcePartnerClient(); | |
$soapclient = $connection->createConnection("./phptoolkit/soapclient/partner.wsdl.xml"); | |
$login = $connection->login(SF_USERNAME, SF_PASSWORD); | |
preg_match("/(https:\/\/.+?)\/.+/", $connection->getLocation(), $match); | |
$base_url = $match[1]; | |
$body = array( | |
"body" => array( | |
"messageSegments" => array( | |
array( | |
"type" => "Text", | |
"text" => "hello chatter with arduino!!" | |
) | |
) | |
) | |
); | |
$response = httpRequest($connection->getSessionId(), "POST", $base_url . "/services/data/v30.0/chatter/feeds/news/me/feed-items", json_encode($body)); | |
$connection->logout(); | |
function httpRequest($sessionId, $method, $url, $data) { | |
$ch = curl_init(); | |
if($method == "POST"){ | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
} else { | |
curl_setopt($ch, CURLOPT_URL, $url . "?" . $data); | |
} | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Authorization: Bearer " . $sessionId)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment