GroveStreams

Assistance reading sensors on system431

lucianosanchez private msg quote post Address this user
Hi all, first post here and first time using Grovestreams. I am working on a school project that involves aquaponics and an Arduino Mega along with various codes. I am using this post (http://www.backyardaquaponics.com/forum/viewtopic.php?f=50&t=22607)on another forum that is also using Grovestrams. I've modified the original code quite a bit since my implementation is a little different but all the sensors are essentially the same.

I've been able to upload the provided sample sketch with success, although incorrect temperatures (due to the difference in calculation for the temperature).

The issues I am having is implementing the sample sketch provided by Grovestrams with my code. Would anyone be able to assist if I posted what I currently have? or help me get the sample code provided by grovestreams and implement a DS18B20 temperature sensor?

Please let me know if there are any questions for me, thank you in advance. Luciano
Post 1 • IP   flag post
MikeMills private msg quote post Address this user
Could be tough to figure out your issue is just from looking at the code. My advice would be to add a lot of tracing and watch for not enough memory or string buffers that are not large enough and are being overrun.

Post the code. Maybe one of us will spot something.
Post 2 • IP   flag post
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", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
 
  return myMac;
}
 
char* getIpReadable(IPAddress ipAddress)
{
  //Convert the ip address to a readable string
  unsigned char octet[4]  = {0,0,0,0};
  for (int i=0; i<4; i++)
  {
    octet[i] = ( ipAddress >> (i*8) ) & 0xFF;
  }
  sprintf(myIPAddress, "%d.%d.%d.%d",octet[0],octet[1],octet[2],octet[3]);
 
  return myIPAddress;
}
 
char* getSamples()
{
  //Get the temperature analog reading and convert it to a string
  float degreesC, degreesF;
 
  degreesC = sensors.getTempC(t1);
  degreesF = sensors.getTempF(t1);
 
  char tempC[15] = {0}; //Initialize buffer to nulls
  dtostrf(degreesC, 12, 3, tempC); //Convert float to string
 
  char tempF[15] = {0}; //Initialize buffer to nulls
  dtostrf(degreesF, 12, 3, tempF); //Convert float to string
 
  //Assemble the samples into URL parameters which are seperated with the "&" character
  //
  // Example: &s1=25.684&s2=78.231
  sprintf(samples, "&%s=%s&%s=%s", gsStreamId1, trim(tempC), gsStreamId2, trim(tempF));
 
  return samples;
}
 
char* trim(char* input)                                         
{
  //Trim leading and ending spaces
  int i,j;
  char *output=input;
  for (i = 0, j = 0; i<strlen(input); i++,j++)          
  {
    if (input[i]!=' ')                           
      output[j]=input[i];                     
    else
      j--;                                     
  }
  output[j]=0;
 
  return output;
}
Post 3 • IP   flag post
MikeMills private msg quote post Address this user
I would investigate the sensors.getTempF(..) call. I saw this thread which says you may need to call isConversionAvailable(..) and setWaitForConversion(..)
Post 4 • IP   flag post
steveb private msg quote post Address this user
look at the last post here:

http://forum.grovestreams.com/topic/161/how-to-ds18b20arduino/

-Steve
Post 5 • IP   flag post
lucianosanchez private msg quote post Address this user
Quote:
Originally Posted by MikeMills
I would investigate the sensors.getTempF(..) call. I saw this thread which says you may need to call isConversionAvailable(..) and setWaitForConversion(..)


Thanks for that Mike. I will check that out and try to implement into my code tonight.

Quote:
Originally Posted by steveb
look at the last post here:

http://forum.grovestreams.com/topic/161/how-to-ds18b20arduino/

-Steve


Steve, thank you for your input. Although I did not understand much of the technical terms in your post, I can probably re-read all of it a couple times and understand exactly what you did. Thank you for the code, I will also be reviewing that and see if I can figure out how to fix this.

You will probably hear back from me soon with additional questions but until then, thank you.
Post 6 • IP   flag post
2572 6 6
Log in or sign up to compose a reply.
destitute