Skip to content

Instantly share code, notes, and snippets.

@okhiroyuki
Created June 21, 2011 10:21
Show Gist options
  • Save okhiroyuki/1037571 to your computer and use it in GitHub Desktop.
Save okhiroyuki/1037571 to your computer and use it in GitHub Desktop.
xbee経由で受けとった温度データをarduinoが読みとり、Railsに上げる
#include <Ethernet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
byte mac[] = { 0x00, 0x50, 0xC2, 0x97, 0x21, 0xBD };
byte ip[] = { 192, 168, 0, 112 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 192, 168, 0, 111 };
byte buf[30];
//SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int flg=0;
int strn=0;
Client client(server, 3000);
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
// pinMode(13,OUTPUT);
// digitalWrite(13,LOW);
delay(1000);
Serial.flush();
Serial.println("connecting...");
}
void loop(){
int c,buf[200];
int i,j;
int imax=20;
long data;
if(Serial.available()>imax-1){
i=0;
do{
c = Serial.read();
buf[i] = c;
i++;
}while(c != -1);
i--;
data = 0;
for(j=0;j<3;j++){
data += 256*(3-j) * buf[15+j];
}
data += buf[18];
Serial.println(data);
data_post(data);
// Serial.flush();
}
delay(1000);
}
void data_post(long buf)
{
char str[100];
if(client.connect()){
Serial.println("connected");
sprintf(str,"%d",buf);
client.print("GET /push/push?id=");
client.print(str);
client.println(" HTTP/1.0");
client.stop();
}
else{
Serial.println("connection failed");
client.stop();
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment