#include <GSM.h>
//#include <SoftwareSerial.h>
#define APIKEY "3kOj1mwZCZpurCzBdNqjYzIXn4OypY4WpiRuY9NtVTycg2HG"
#define FEEDID 1235963552
#define USERAGENT "DustBin"
#define PINNUMBER ""
#define GPRS_APN "www"
#define GPRS_LOGIN ""
#define GPRS_PASSWORD ""
GSMClient client;
GPRS gprs;
GSM gsmAccess;
long microsecondsToInches();
long microsecondsToCentimeters();
const int trigPin =12;
const int echoPin =13;
float n;
float sensorReading;
long duration,inches,cm;
char server[] = "api.xively.com";
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10*1000;
void setup(){
//mySerial.begin(9600);
Serial.begin(9600);
boolean notConnected = true;
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(100);
}
}
}
void sendData(int thisData)
{
if (client.connect(server, 80))
{
Serial.println("connecting...");
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.print("Host: api.xively.com\n");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
int thisLength = 8 + getLength(thisData);
client.println(thisLength);
client.print("Content-Type: text/csv\n");
client.println("Connection: close");
client.println();
client.print("sensor1,");
client.println(thisData);
Serial.print(thisData);
}
else
{
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
lastConnectionTime = millis();
}
int getLength(int someValue)
{
int digits = 1;
int dividend = someValue /10;
while (dividend > 0)
{
dividend = dividend /10;
digits++;
}
return digits;
}
void loop(){
n= StatusCheck();
int sensorReading = n;
if (client.available())
{
char c = client.read();
Serial.print(c);
}
if (!client.connected() && lastConnected)
{
client.stop();
}
if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
{
sendData(sensorReading);
}
lastConnected = client.connected();
}
float StatusCheck()
{
pinMode(trigPin,OUTPUT);
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
pinMode(echoPin,INPUT);
duration=pulseIn(echoPin,HIGH);
inches= microsecondsToInches(duration);
cm= microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
return cm;
}
long microsecondsToInches(long microseconds)
{
return microseconds/74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds/29/2;
}
//#include <SoftwareSerial.h>
#define APIKEY "3kOj1mwZCZpurCzBdNqjYzIXn4OypY4WpiRuY9NtVTycg2HG"
#define FEEDID 1235963552
#define USERAGENT "DustBin"
#define PINNUMBER ""
#define GPRS_APN "www"
#define GPRS_LOGIN ""
#define GPRS_PASSWORD ""
GSMClient client;
GPRS gprs;
GSM gsmAccess;
long microsecondsToInches();
long microsecondsToCentimeters();
const int trigPin =12;
const int echoPin =13;
float n;
float sensorReading;
long duration,inches,cm;
char server[] = "api.xively.com";
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10*1000;
void setup(){
//mySerial.begin(9600);
Serial.begin(9600);
boolean notConnected = true;
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(100);
}
}
}
void sendData(int thisData)
{
if (client.connect(server, 80))
{
Serial.println("connecting...");
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.print("Host: api.xively.com\n");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
int thisLength = 8 + getLength(thisData);
client.println(thisLength);
client.print("Content-Type: text/csv\n");
client.println("Connection: close");
client.println();
client.print("sensor1,");
client.println(thisData);
Serial.print(thisData);
}
else
{
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
lastConnectionTime = millis();
}
int getLength(int someValue)
{
int digits = 1;
int dividend = someValue /10;
while (dividend > 0)
{
dividend = dividend /10;
digits++;
}
return digits;
}
void loop(){
n= StatusCheck();
int sensorReading = n;
if (client.available())
{
char c = client.read();
Serial.print(c);
}
if (!client.connected() && lastConnected)
{
client.stop();
}
if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
{
sendData(sensorReading);
}
lastConnected = client.connected();
}
float StatusCheck()
{
pinMode(trigPin,OUTPUT);
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
pinMode(echoPin,INPUT);
duration=pulseIn(echoPin,HIGH);
inches= microsecondsToInches(duration);
cm= microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
return cm;
}
long microsecondsToInches(long microseconds)
{
return microseconds/74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds/29/2;
}
No comments:
Post a Comment