|
lucianosanchez private msg quote post Address this user | |
I will try and add some print statements to see what the data is doing and maybe add a memory function that prints the available memory. I'm new to Arduino so I think it is very possible I've coded something wrong. Attached is my current code modifying the Grovestreams sample sketch to read the DS18B20 temperature sensor.
Currently it prints -169 F, meaning it is not a valid output. I am able to get the -169 to display in GS.
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h> // For Temp sensor
const int temperaturePin = 53;
// Local Network Settings
byte mac[] = {
0x00, 0xAA, 0xBB, 0x0CC, 0xDE, 0x02 }; // Change this!!! Must be unique on local network.
// Look for a sticker on the back of your Ethernet shield.
// GroveStreams Settings
char gsApiKey[] = "Clearing this out for sample purposes"; //Change This!!!
char gsComponentName[] = "Temperature"; //Optionally change. Set this to give your component a name when it initially registers.
char gsDomain[] = "grovestreams.com"; //Don't change. The GroveStreams domain.
char gsComponentTemplateId[] = "temp"; //Don't change. Tells GS what template to use when the feed initially arrives and a new component needs to be created.
// The blueprint is expecting "temp".
//GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to.
//Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these.
char gsStreamId1[] = "s1"; //Don't change. Temp C.
char gsStreamId2[] = "s2"; //Don't change. Temp F.
// Other Settings
const unsigned long updateFrequency = 20000UL; // Update frequency in milliseconds (20000 = 20 seconds). Change this to change your sample frequency.
char samples[35]; // Change this buffer size only if you increase or decrease the size of samples being uploaded.
char myIPAddress[20]; //Don't Change. Set below from DHCP. Needed by GroveStreams to verify that a device is not uploading more than once every 10s.
char myMac[20]; //Don't Change. Set below from the above mac variable. The readable Mac is used by GS to determine which component the
// feeds are uploading into. It must match an existing GroveStreams component's ID
unsigned long lastSuccessfulUploadTime = 0; //Don't change. Used to determine if samples need to be uploaded.
int failedCounter = 0; //Don't change. Used for Internet Connection Reset logic
DeviceAddress t1 = { 0x28, 0xFF, 0xB5, 0xB6, 0x70, 0x16, 0x05, 0xDB }; //device address for temp sensors
DeviceAddress t2 = { 0x28, 0xFF, 0x4F, 0x83, 0x73, 0x16, 0x05, 0xED };
// Initialize Arduino Ethernet Client
OneWire oneWire(temperaturePin);
DallasTemperature sensors(&oneWire);
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
sensors.begin(); //For temperature sensors
sensors.requestTemperatures();
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
float temp = calculateTempF(t1);
Serial.println(temp);
// Update sensor data to GroveStreams
if(millis() - lastSuccessfulUploadTime > updateFrequency)
{
updateGroveStreams();
}
}
float calculateTempF(const unsigned char *tempSensor) {
sensors.requestTemperatures();
float temp = sensors.getTempF(tempSensor);
if (temp < -100 || temp >= 185.00) {
Serial.println("Error reading temp on temp sensor ");
} else return temp;
}
void updateGroveStreams()
{
//Assemble the url that is used to pass the temperature readings to GroveStreams and call it
unsigned long connectAttemptTime = millis();
if (client.connect(gsDomain, 80))
{
//You may need to increase the size of urlBuf if any other char array sizes have increased
char urlBuf[175];
sprintf(urlBuf, "PUT /api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
gsComponentTemplateId, myMac, gsComponentName, gsApiKey, getSamples());
//Uncomment the next three lines for debugging purposes
Serial.println(urlBuf);
//Serial.print(F("urlBuf length = ");
//Serial.println(strlen(urlBuf));
client.println(urlBuf); //Send the url with temp readings in one println(..) to decrease the chance of dropped packets
client.print(F("Host: "));
client.println();
client.println(F("Connection: close"));
client.print(F("X-Forwarded-For: ")); //Include this line and the next line if you have more than one device uploading behind
client.println(myIPAddress); // your outward facing router (avoids the GS 10 second upload rule)
client.println(F("Content-Type: application/json"));
client.println();
if (client.connected())
{
//Begin Report Response
while(!client.available())
{
delay(1);
}
while(client.available())
{
char c = client.read();
Serial.print(c);
}
//End Report Response
//Client is now disconnected; stop it to cleannup.
client.stop();
lastSuccessfulUploadTime = connectAttemptTime;
failedCounter = 0;
}
else
{
handleConnectionFailure();
}
}
else
{
handleConnectionFailure();
}
}
void handleConnectionFailure() {
//Connection failed. Increase failed counter
failedCounter++;
Serial.print(F("Connection to GroveStreams Failed "));
Serial.print(failedCounter);
Serial.println(F(" times"));
delay(1000);
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 )
{
//Too many failures. Restart Ethernet.
startEthernet();
}
}
void startEthernet()
{
//Start or restart the Ethernet connection.
client.stop();
Serial.println(F("Connecting Arduino to network..."));
Serial.println();
//Wait for the connection to finish stopping
delay(2000);
//Connect to the network and obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println(F("DHCP Failed, reset your Arduino and try again"));
Serial.println();
}
else
{
Serial.println(F("Arduino connected to network using DHCP"));
//Set the mac and ip variables so that they can be used during sensor uploads later
Serial.print(F(" MAC: "));
Serial.println(getMacReadable());
Serial.print(F(" IP address: "));
Serial.println(getIpReadable(Ethernet.localIP()));
Serial.println();
}
}
char* getMacReadable()
{
//Convert the mac address to a readable string
sprintf(myMac, "%02x:%02x:%02x:%02x:%02x:%02x
|