Help required - error messages

Hello,

I am finishing my project, and code is almost done, I wrote 3 codes (one for sensors, one for Ethernet, and one for ThingSpeak), and now I am combining them. When I am verifying code, I am getting errore messages, and I don't know what I have to do to get rid of them. I hope anyone of You can help me.

Here is the code:

#include <dht.h>
#include <Adafruit_BMP085.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>

#define dht_apin A0 // Analog Pin sensor is connected to

Adafruit_BMP085 bmp;
dht DHT;

float localTemp;
float localPress;
float localHumid;
float FMF;

//------------ETHERNET--------------
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0x73 };

IPAddress ip(193, 2, 68, 140);

// fill in your Domain Name Server address here:
IPAddress myDns(1, 1, 1, 1);

// initialize the library instance:
EthernetClient client;

char server[] = "meteo.fmf.uni-lj.si/";

unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 60L * 1000L;

//-----------THINGSPEAK---------------
byte server[]  = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API -> ERROR MESSAGE

String writeAPIKey = "5NK4YXIIHW0KCLT7";
const unsigned long updateInterval = 60000;       
boolean lastConnected = false;


void setup() {
  Serial.begin(9600);
  bmp.begin();
  delay(1000); // Delay to let system boot
  startEthernetDHCP();
  //-------------ETHERNET
  while (!Serial) {
    ;
  }
  Ethernet.begin(mac, ip, myDns);
}

void loop() {
  
  // BMP180 - temperature and pressure measurement
  Serial.print("Current Temperature is: "); // temperature
  Serial.print(bmp.readTemperature());
  Serial.println(" Celsius");
  localTemp=bmp.readTemperature();
  
  Serial.print("Current Pressure is: "); // pressure
  Serial.print(bmp.readPressure());
  Serial.println(" Pa");
  localPress=bmp.readPressure();
  
  // DHT11 - humidity measurement
  DHT.read11(dht_apin);
  
  Serial.print("Current Humidity is: "); // humidity
  Serial.print(DHT.humidity);
  Serial.println(" %");
  Serial.println(" "); // space between next group measurement
  localHumid=DHT.humidity;    
      
  delay(60000); // Wait 60 seconds before new measurement
 
  //--------------ETHERNET---------------
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
    FMF=client.read();
  }

  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
  
  //----------THINGSPEAK--------------

  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  
  // Odjava s streznika ThingSpeak

  if (!client.connected() && lastConnected)
  {
    Serial.println("...disconnected.");
    client.stop();
  } 

  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))

    updateThingSpeak("field1="+localTemp+"&field2="+localPress+"&field3="+localHumid+"&field4="+FMF); 

    lastConnected = client.connected();

}


void updateThingSpeak(String tsData)
{
  if (client.connect(server,80))
  { 
    Serial.println("Connected to ThingSpeak...");
    Serial.println();
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");
    client.print(tsData);
    lastConnectionTime = millis();
  }

  else
  {
    Serial.println("Connection Failed.");   
    Serial.println();
    lastConnectionTime = millis(); 
  }

}

void startEthernetDHCP()

{
  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }

  Serial.print("Moj naslov IP: ");
  
  for (byte thisByte = 0; thisByte < 4; thisByte++) 
    {
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
    }
  Serial.println();
}

And here are the error messages:

PROGRAM:34: error: conflicting declaration 'byte server []'
PROGRAM:28: error: 'server' has a previous declaration as 'char server [21]'
PROGRAM.ino: In function 'void httpRequest()':
PROGRAM:131: error: invalid operands of types 'const char [8]' and 'float' to binary 'operator+'
conflicting declaration 'byte server []'

This is pretty straightforward :

You are declaring server as a byte array on line 34, but you already declared server as a char array on line 28. You need different names for the two variables

PROGRAM:34: error: conflicting declaration 'byte server []'
PROGRAM:28: error: 'server' has a previous declaration as 'char server [21]'

char server[] = "meteo.fmf.uni-lj.si/";

unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 60L * 1000L;

//-----------THINGSPEAK---------------
byte server[]  = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API -> ERROR

It would appear that the compiler is right (as usual).

   updateThingSpeak("field1="+localTemp+"&field2="+localPress+"&field3="+localHumid+"&field4="+FMF);

Get your calculator out. What does "field1=" PLUS localTemp come to?

PaulS:

char server[] = "meteo.fmf.uni-lj.si/";

unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 60L * 1000L;

//-----------THINGSPEAK---------------
byte server[]  = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API -> ERROR



It would appear that the compiler is right (as usual).



updateThingSpeak("field1="+localTemp+"&field2="+localPress+"&field3="+localHumid+"&field4="+FMF);



Get your calculator out. What does "field1=" PLUS localTemp come to?

When I remove plus I get this error: PROGRAM:131: error: expected ')' before 'localTemp'

mathers:
This is pretty straightforward :

You are declaring server as a byte array on line 34, but you already declared server as a char array on line 28. You need different names for the two variables

Ok, I fixed that, I added number 1 in byte server command.

When I remove plus I get this error: PROGRAM:131: error: expected ')' before 'localTemp'

Well, duh.

You can't just hack at stuff. Sometimes, you actually need to engage your brain.

The function expects a string. You need to make one. Create a char array, big enough to hold the string. Use sprintf() to populate the string. Pass the string to the function.

PaulS:
Well, duh.

You can't just hack at stuff. Sometimes, you actually need to engage your brain.

The function expects a string. You need to make one. Create a char array, big enough to hold the string. Use sprintf() to populate the string. Pass the string to the function.

I've sent You PM.

You need a char array that can hold 32 characters (field1=&field2=&field3=&field4=) for the literal parts, and 4 float values as strings, probably 8 characters each, for a total of 64 characters, plus 1 for the terminating NULL. Bigger is better than smaller.

char buffer[80];

Then, you need 4 smaller buffers, since sprintf() as implemented on the Arduino does not support the %f format.

char tmpStg[10], humStg[10], prsStg[10], fmfStg[10];

You need to populate the smaller strings:

dtostrf(localTemp, 8, 3, tmpStg);

Repeat that for the other three values.

Then, you populate the larger array:

sprintf(buffer, "field1=%s&field2=%s&field3=%s&field4=%s", tmpStg, humStg, prsStg, fmfStg);

Then, call the function with that string:

   updateThingSpeak(buffer);

PaulS:
You need a char array that can hold 32 characters (field1=&field2=&field3=&field4=) for the literal parts, and 4 float values as strings, probably 8 characters each, for a total of 64 characters, plus 1 for the terminating NULL. Bigger is better than smaller.

char buffer[80];

Then, you need 4 smaller buffers, since sprintf() as implemented on the Arduino does not support the %f format.

char tmpStg[10], humStg[10], prsStg[10], fmfStg[10];

You need to populate the smaller strings:

dtostrf(localTemp, 8, 3, tmpStg);

Repeat that for the other three values.

Then, you populate the larger array:

sprintf(buffer, "field1=%s&field2=%s&field3=%s&field4=%s", tmpStg, humStg, prsStg, fmfStg);

Then, call the function with that string:

   updateThingSpeak(buffer);

Where do I have to put this codes?

Where do I have to put this codes?

In this block:

  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))

    updateThingSpeak("field1="+localTemp+"&field2="+localPress+"&field3="+localHumid+"&field4="+FMF); 

    lastConnected = client.connected();

}

in place of the existing call to updateThingSpeak().

PaulS:
In this block:

  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))

updateThingSpeak("field1="+localTemp+"&field2="+localPress+"&field3="+localHumid+"&field4="+FMF);

lastConnected = client.connected();

}



in place of the existing call to updateThingSpeak().

Now , I'm getting this message: error: 'buffer' was not declared in this scope
'buffer' was not declared in this scope

  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))

    char buffer[80];
    char tmpStg[10], humStg[10], prsStg[10], fmfStg[10];
    dtostrf(localTemp, 8, 3, tmpStg);
    dtostrf(localPress, 8, 3, humStg);
    dtostrf(localHumid, 8, 3, prsStg);
    dtostrf(FMF, 8, 3, fmfStg);
    sprintf(buffer, "field1=%s&field2=%s&field3=%s&field4=%s", tmpStg, humStg, prsStg, fmfStg); ->error for this line

    updateThingSpeak(buffer); 

    lastConnected = client.connected();

Now , I'm getting this message: error: 'buffer' was not declared in this scope
'buffer' was not declared in this scope

This is the scope of buffer:

  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))

    char buffer[80];

Where did the { go that was after the if statement?

PaulS:
This is the scope of buffer:

  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))

char buffer[80];




Where did the { go that was after the if statement?

I didn't have {} at all for this if statement. Compiling done, I'm going to test it, and report You results. Thank You for help.

There is problem with updating my ThingSpeak channel. When I open serial monitor I get this:

My IP: 192.168.1.2.
Current Temperature is: 25.90 Celsius
Current Pressure is: 100165 Pa
Current Humidity is: 62.00 %

connection failed
Connection Failed.

Current Temperature is: 25.90 Celsius
Current Pressure is: 100171 Pa
Current Humidity is: 63.00 %

connection failed
Connection Failed.

@PaulS do You maybe know what could be a problem?

Follow the steps here:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Grumpy_Mike:
Follow the steps here:-
Merging Code

I was doing it similar to this, I found another tutorial. But program simply doesn't want to update to ThingSpeak channel.

But program simply doesn't want to update to ThingSpeak channel.

Then I suspect you have not resolved all the conflicts. That is the things that more than one program requires for different uses.

Grumpy_Mike:
Then I suspect you have not resolved all the conflicts. That is the things that more than one program requires for different uses.

Can You help me fix it?

I fixed connection failed error, but still I am not getting results on my ThingSpeak channel. Here is fixed code. Does anyone know what could be a problem?

#include <dht.h>
#include <Adafruit_BMP085.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>

#define dht_apin A0 // Analog Pin sensor is connected to

Adafruit_BMP085 bmp;
dht DHT;

float localTemp;
float localPress;
float localHumid;
float FMF;

//------------ETHERNET--------------
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0x73 };

IPAddress ip(192, 168, 1, 2);

// fill in your Domain Name Server address here:
IPAddress myDns(1, 1, 1, 1);

// initialize the library instance:
EthernetClient client;

char server[] = "meteo.fmf.uni-lj.si";

unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 10L * 1000L;

//-----------THINGSPEAK---------------
byte server1[]  = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API

String writeAPIKey = "5NK4YXIIHW0KCLT7";
const unsigned long updateInterval = 30000;       
boolean lastConnected = false;


void setup() {
  Serial.begin(9600);
  bmp.begin();
  delay(1000); // Delay to let system boot
  startEthernetDHCP();
  //-------------ETHERNET
  while (!Serial) {
    ;
  }
  Ethernet.begin(mac, ip, myDns);
}

void loop() {
  
  // BMP180 - temperature and pressure measurement
  Serial.print("Current Temperature is: "); // temperature
  Serial.print(bmp.readTemperature());
  Serial.println(" Celsius");
  localTemp=bmp.readTemperature();
  
  Serial.print("Current Pressure is: "); // pressure
  Serial.print(bmp.readPressure());
  Serial.println(" Pa");
  localPress=bmp.readPressure();
  
  // DHT11 - humidity measurement
  DHT.read11(dht_apin);
  
  Serial.print("Current Humidity is: "); // humidity
  Serial.print(DHT.humidity);
  Serial.println(" %");
  Serial.println(" "); // space between next group measurement
  localHumid=DHT.humidity;    
      
  delay(60000); // Wait 60 seconds before new measurement
 
  //--------------ETHERNET---------------
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
    FMF=client.read();
  }

  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
  
  
  //----------THINGSPEAK--------------

  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  

  if (!client.connected() && lastConnected)
  {
    Serial.println("...disconnected.");
    client.stop();
  } 

  if(!client.connected() && (millis() - lastConnectionTime > updateInterval)){

    char buffer[80];
    char tmpStg[10], humStg[10], prsStg[10], fmfStg[10];
    dtostrf(localTemp, 8, 3, tmpStg);
    dtostrf(localPress, 8, 3, humStg);
    dtostrf(localHumid, 8, 3, prsStg);
    dtostrf(FMF, 8, 3, fmfStg);
    sprintf(buffer, "field1=%s&field2=%s&field3=%s&field4=%s", tmpStg, humStg, prsStg, fmfStg);

    updateThingSpeak(buffer); 

    lastConnected = client.connected();} 

}


void updateThingSpeak(String tsData)
{
  if (client.connect(server,80))
  { 
    Serial.println("Connected to ThingSpeak...");
    Serial.println();
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");
    client.print(tsData);
    lastConnectionTime = millis();
  }

  else
  {
    Serial.println("Connection Failed.");   
    Serial.println();
    lastConnectionTime = millis(); 
  }

}

void startEthernetDHCP()

{
  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }

  Serial.print("My IP address: ");
  
  for (byte thisByte = 0; thisByte < 4; thisByte++) 
    {
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
    }
  Serial.println();
}
float FMF;


    FMF=client.read();

Why are you storing a byte in a float?

void httpRequest() {
  
  client.stop();

Sopping a client is piss poor practice. Especially when you don't check that there IS a client to stop.

void updateThingSpeak(String tsData)This defeats the whole purpose of making a string using dtostrf() and sprintf().