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 []'
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
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.
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.
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.
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.
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();
}