Go Down

Topic: Need explanation on what im doing wrong (Read 897 times) previous topic - next topic

Prinny

Okay so my problem is that my sketch suddenly gives me a ' this ' was not declared in this scope, it was working fine before up until i made one small change and reverted it back to the original form. Can someone point out to me what im doing wrong?

This sketch is meant to work with the Grovestream API to stream data there.

My sketch is as follow

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
 
int logValue1=0;
int logValue2=0;
int logValue3=0;
int a,b,c;


byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0E, 0x60, 0xA3 };  
 

char gsApiKey[] = "f7a57ded-fcc4-30d3-b09e-03038d52c0c0";  
char gsComponentName1[] = "Station1";
char gsComponentName2[] = "Station2";
char gsComponentName3[] = "Station3";
char gsComponentName4[] = "Station4";
 
char gsDomain[] = "grovestreams.com";  
char gsComponentTemplateId1[] = "Station1";  
char gsComponentTemplateId2[] = "Station2";
char gsComponentTemplateId3[] = "Station3";
char gsComponentTemplateId4[] = "Station4";                                    
 
char gsStreamId1[] = "set1";  
char gsStreamId2[] = "set2";  
char gsStreamId3[] = "set3";
char gsStreamId4[] = "set4";
char gsStreamId5[] = "set5";
char gsStreamId6[] = "set6";
char gsStreamId7[] = "set7";
char gsStreamId8[] = "set8";  
 

const unsigned long updateFrequency = 60000UL;  

char samples[35];                      

char myIPAddress[20];  
char myMac[20];        
                      
 
unsigned long lastSuccessfulUploadTime = 0;
int failedCounter = 0;                      
 
EthernetClient client;
 
 
void setup()
{
  pinMode (13 ,OUTPUT); //Red
  pinMode (12 ,OUTPUT); //Yellow
  pinMode (11 ,OUTPUT); //Green
  pinMode (9 ,OUTPUT);  //Red
  pinMode (8 ,OUTPUT);  //Yellow
  pinMode (7 ,OUTPUT);  //Green
  //pinMode (6, INPUT); // For switch
  pinMode(A13, INPUT); //analog
  pinMode(A12, INPUT); //analog
  pinMode(A11, INPUT); //analog
  Serial.begin(9600);
 
  
  startEthernet();
}
 
void loop()
{
  logValue1= analogRead(A13);
  logValue2= analogRead(A12);
  
  a = logValue1;
  b = logValue2;
 
  if(millis() - lastSuccessfulUploadTime > updateFrequency)
  {
    updateGroveStreams();
  }
 
}
 
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("proxy.tp.edu.sg", 80))
  {        
  
    char urlBuf[175];
 
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId1, myMac, gsComponentName1, gsApiKey, getSamples1());
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId2, myMac, gsComponentName2, gsApiKey, getSamples2());
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId3, myMac, gsComponentName3, gsApiKey, getSamples3());
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId4, myMac, gsComponentName4, gsApiKey, getSamples4());

    Serial.println(urlBuf);    
    //Serial.print(F("urlBuf length = "));
    //Serial.println(strlen(urlBuf));
    
    client.println(urlBuf);  
    client.print(F("Host:"));
    client.println();
    client.println(F("Connection: close"));
    client.print(F("X-Forwarded-For: "));    
    client.println(myIPAddress);              
    client.println(F("Content-Type: application/json"));
  
    client.println();
 
 
    if (client.connected())
    {
          
 
      while(!client.available())
      {
        delay(1);
      }
 
      while(client.available())
      {
        char c = client.read();
        Serial.print(c);
      }

      client.stop();
  
      lastSuccessfulUploadTime = connectAttemptTime;
      failedCounter = 0;
    }
    else
    {
      handleConnectionFailure();
    }
 
  }
  else
  {
     handleConnectionFailure();
  }
 
}
 
void handleConnectionFailure() {
 
  failedCounter++;
 
  Serial.print(F("Connection to GroveStreams Failed "));
  Serial.print(failedCounter);  
  Serial.println(F(" times"));
  delay(1000);
    
  if (failedCounter > 3 )
  {
  
    startEthernet();
  }
 
 }
 
void startEthernet()
{

  client.stop();
 
  Serial.println(F("Connecting Arduino to network..."));
  Serial.println();  
 
  delay(2000);
 
  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"));
 
    Serial.print(F(" MAC: "));
    Serial.println(getMacReadable());
    Serial.print(F(" IP address: "));
    Serial.println(getIpReadable(Ethernet.localIP()));
    Serial.println();
  }
 
}
 
char* getMacReadable()
  
  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)
 
  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* getSamples1()    
{
 
  float PCV1,MFCV1;
  int val;
  
   PCV1 = logValue1;
   MFCV1 = logValue2;

  char PC[15] = {0};
  dtostrf(PCV1, 12, 3,trim(PC));

  char MFC[15] = {0};
  dtostrf(MFCV1, 12, 3,trim(MFC));
 
 
  sprintf(samples, "&%s=%s&%s=%s", gsStreamId1, trim(PC),gsStreamId2, trim(MFC));

  return samples;
}

char* getSamples2()
{
 
  float PCV2,MFCV2;
  int val;
  
   PCV2 = logValue1;
   MFCV2 = logValue2;

  char PC[15] = {0};
  dtostrf(PCV2, 12, 3,trim(PC));
  
  char MFC[15] = {0};
  dtostrf(MFCV2, 12, 3,trim(MFC));
 
 
  sprintf(samples, "&%s=%s&%s=%s", gsStreamId3, trim(PC),gsStreamId4, trim(MFC)); // change to set 3, 4, 5, 6, 7, 8

  return samples;
}

char* getSamples3()  

  
  float PCV3,MFCV3;
  int val;
  
   PCV3 = logValue1;
   MFCV3 = logValue2;
  
  char PC[15] = {0};
  dtostrf(PCV3, 12, 3,trim(PC));

  char MFC[15] = {0};
  dtostrf(MFCV3, 12, 3,trim(MFC));
 
 

  sprintf(samples, "&%s=%s&%s=%s", gsStreamId5, trim(PC),gsStreamId6, trim(MFC)); // change to set 3, 4, 5, 6, 7, 8
 
  return samples;
}

char* getSamples4() //change no 2 ,3 ,4  
{
  
  float PCV4,MFCV4;
  int val;
  
   PCV4 = logValue1;
   MFCV4 = logValue2;

  char PC[15] = {0};
  dtostrf(PCV4, 12, 3,trim(PC));

  char MFC[15] = {0};
  dtostrf(MFCV4, 12, 3,trim(MFC));
  
  sprintf(samples, "&%s=%s&%s=%s", gsStreamId7, trim(PC),gsStreamId8, trim(MFC));

  return samples;
}
 
char* trim(char* input)                        
{
  
  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;

    }




Anatroll

You call your fonctions before declaring them, just add their prototype before the setup() loop :

Something like this :

Code: [Select]
void handleConnectionFailure();
void startEthernet();
char* getMacReadable();
char* getIpReadable(IPAddress ipAddress);
char* getSamples1();
char* getSamples2();
char* getSamples3();
char* getSamples4();
char* trim(char* input);


ps : Also be carefull with the use of 'this' when refering to cpp programming ;)

Prinny

@Anatroll Thanks alot! Your suggestion worked! The solution i could find for this was to position my function higher up if the error, and the more i did that the more errors i had...
 
PS: Oh okay. Didnt know that existed (o.o)...  Still quite bad at cpp :>

Anatroll

Glad I could help.

Indeed declaring your fonctions before the main loop (or the loop() loop) allows you not to write their prototype. But this is ugly :p
If you declare them bellow your main loop, you first need to tell the compiler (who's building your code) that they exist before you can use them. That's what prototype are made for ;)

AWOL

You call your fonctions before declaring them, just add their prototype before the setup() loop
Two things:
1) setup() is a function, not a loop
2) the IDE creates function prototypes for you

PaulS

Code: [Select]
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId1, myMac, gsComponentName1, gsApiKey, getSamples1());
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId2, myMac, gsComponentName2, gsApiKey, getSamples2());
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId3, myMac, gsComponentName3, gsApiKey, getSamples3());
    sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
           gsComponentTemplateId4, myMac, gsComponentName4, gsApiKey, getSamples4());

The end result of this is that the 1st three calls to sprintf() are useless.
The art of getting good answers lies in asking good questions.

Prinny

#6
Jul 01, 2016, 01:12 pm Last Edit: Jul 01, 2016, 01:17 pm by Prinny
@PaulS Yeah i know that the first 3 sprintf() are useless, was trying to make each of them return a different value, couldnt make it happen cuz of the "something" is not declared issue so i just posted it like that. But any tips on making each of those lines return a different value each?

Edit:
Was planning to make a new post for it but ill ask here and move it if needed, also ill be only able to test your suggestion in two days because currently my Arduino board is with my project partner and she's busy for the next two days... So yeah...

PaulS

Quote
But any tips on making each of those lines return a different value each?
The sprintf() function returns the number of characters written to the specified array. I can't see why you would want sprintf() to return different values.

Writing to different arrays makes sense.

Calling the sprintf() function at the appropriate time makes sense.

Wanting to make the functions return different values does not.
The art of getting good answers lies in asking good questions.

aarg

2) the IDE creates function prototypes for you
But seems to have become lazy lately.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Prinny

@PaulS Ill try to do that when i can.

sailorgreg

I would also suggest getting up to speed on structures and arrays. Having separated independent variables for data that is related and having separate functions for item 1, 2, 3, and 4 (func1(), func2(), func3(), func4()) instead of func(int iGS) would be a start. A structure groups related data into modular units so once you have access to the structured data, you can use it generally instead of knowing it's for data set 3. Then, when you have the structure defined, you can make an array out of that so then you just pass the index of the array element to the functions for them to access the specific data for the desired item.

Programming is fun! :)

econjack

If you feel you need a quick brush up on scope, in Nick Gammon's Tutorials post at the top of this Forum is a section on C++ programming with a subsection on scope.

Prinny

Thanks for the suggestions guys!

Did try to do some of them. But I found a easier solution that works for me, which was to make 3 more loops for the sprintf() lines individually beacuse appearently the sprintf() line the PaulS pointed out was sending out a url line appearently.(Yes ik what im doing is really bad but this was the one that worked for (T.T), will try to improve on my programming skills :D)

Go Up