Created
February 10, 2013 16:12
-
-
Save a-yasui/4750066 to your computer and use it in GitHub Desktop.
サーミスタとEthernetを使ったテスト
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 | |
$form = ""; | |
function write ($temp) { | |
$path = "temp.txt"; | |
$fp = fopen($path, "a"); | |
if ($fp) { | |
$time = time(); | |
fwrite($fp, "$time\t$temp\n"); | |
fclose($fp); | |
} | |
} | |
if (isset($_GET['temp'])) { | |
write($_GET["temp"]); | |
} | |
?>OK | |
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
// 温度をEthernetで喋らせる | |
#include <SPI.h> | |
#include <Ethernet.h> | |
// 各自のEthenetモジュールのMacアドレスを | |
byte mac[] = { | |
0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; | |
IPAddress ip(192, 168, 1, 21); // ここは各自のネットワークのIPに | |
EthernetClient client; | |
const char server[] = "example.com"; // データ投げつけるサーバホスト名を | |
byte serverIp[] = {127,0,0,1}; // サーバIP。名前解決が遅かったので。 | |
int potpin = 1; // analog pin used to connect the potentiometer | |
int val; // variable to read the value from the analog pin | |
float val2, volt, temp; | |
void setup() { | |
Serial.begin(9600); | |
// attempt a DHCP connection: | |
if (!Ethernet.begin(mac)) { | |
// if DHCP fails, start with a hard-coded address: | |
Ethernet.begin(mac, ip); | |
} | |
} | |
void send (float temp) { | |
if (client.connect(serverIp, 80)) { | |
client.print("GET /form.php?temp="); | |
client.print(temp); | |
client.println(" HTTP/1.0"); | |
client.print("HOST: "); | |
client.println(server); | |
client.println(); | |
Serial.println("server send"); | |
} | |
else { | |
Serial.println("Cannot connect Server."); | |
} | |
delay(1000); | |
client.stop(); | |
} | |
void loop () { | |
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) | |
volt = val * 5 / 1023.00 ; | |
val2 = 50000.00 / volt - 10000.00; | |
temp = -18.21 * log(val2) + 223.64; | |
Serial.print("temp="); | |
Serial.println(temp,2); | |
send(temp); | |
// 30 sec | |
delay(30000); // waits for the servo to get there | |
} | |
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
<html> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" charset="utf-8"></script> | |
<script src="http://ccchart.com/js/ccchart.js" charset="utf-8"></script> | |
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/> | |
<script> | |
function unixtimeToDate (ts) { | |
var d = new Date( ts * 1000 ); | |
var year = d.getFullYear(); | |
var month = d.getMonth() + 1; | |
var day = d.getDate(); | |
var hour = ( d.getHours() < 10 ) ? '0' + d.getHours() : d.getHours(); | |
var min = ( d.getMinutes() < 10 ) ? '0' + d.getMinutes() : d.getMinutes(); | |
var sec = ( d.getSeconds() < 10 ) ? '0' + d.getSeconds() : d.getSeconds(); | |
return /*year + '-' + month + '-' + day + ' ' + */hour + ':' + min /*+ ':' + sec*/; | |
} | |
var chartdata3 = { | |
"config": { | |
"title": "サーミスタテスト", | |
"subTitle": "私の部屋の室内温度", | |
"type": "bezi2", | |
"xScaleXOffset": 4, | |
"colorSet": | |
["#ede0ab"], | |
"bgGradient": { | |
"direction":"vertical", | |
"from":"#222", | |
"to":"#4c5e4e" | |
} | |
}, | |
"data": [ | |
] | |
}; | |
$.get("/temp.txt", "", function(data) { | |
var list = data.split("\n"); | |
// 30秒おきのデータなので、5分単位の3時間分にする | |
var newdata = []; | |
var j = 0; | |
var tmp = []; | |
var date=["日付"]; | |
$.each(list, function (idx, _data){ | |
var d = _data.split("\t"); | |
if (j == 10) { | |
date.push(unixtimeToDate(d[0])); | |
_tmp = tmp.reduce(function(previousValue, currentValue, index, array){ return parseFloat(previousValue)+parseFloat(currentValue);})/10; | |
console.log(_tmp); | |
newdata.push(_tmp); | |
j=0; | |
tmp = []; | |
} | |
tmp.push(d[1]); | |
j+=1; | |
}); | |
chartdata3["data"].push(date); | |
chartdata3["data"].push(newdata); | |
console.log(chartdata3); | |
ccchart.init('hoge', chartdata3); | |
}); | |
</script> | |
<body> | |
<canvas id="hoge"></canvas> | |
<div> | |
<span>Use: <a href="http://ccchart.com/#3">ccchart</a></span> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment