#include <EthernetClient.h>
#include <Ethernet.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <SPI.h>
// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
byte ip[] = { 192, 168, 0, 249 }; // Must be unique on local network
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
// ThingSpeak Settings
byte server[] = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API
String writeAPIKey = "XXXXXXXXXXX"; // Write API Key for a ThingSpeak Channel
const int updateInterval = 30000; // Time interval in milliseconds to update ThingSpeak
Client client(server, 80);
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int resetCounter = 0;
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
delay(1000);
}
void loop()
{
String analogPin0 = String(analogRead(A0), DEC);
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println();
Serial.println("...disconnected.");
Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
{
updateThingSpeak("field1="+analogPin0);
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect())
{
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();
resetCounter = 0;
}
else
{
Serial.println("Connection Failed.");
Serial.println();
resetCounter++;
if (resetCounter >=5 ) {resetEthernetShield();}
lastConnectionTime = millis();
}
}
void resetEthernetShield()
{
Serial.println("Resetting Ethernet Shield.");
Serial.println();
client.stop();
delay(1000);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
}
and i got this error:
[/opt/energia-0101E0013/hardware/tools/lm4f/bin/arm-none-eabi-g++, -c, -O0, -w, -fno-rtti, -fno-exceptions, -ffunction-sections, -fdata-sections, -mthumb, -mcpu=cortex-m4, -mfloat-abi=hard, -mfpu=fpv4-sp-d16, -fsingle-precision-constant, -DF_CPU=120000000L, -MMD, -DARDUINO=101, -DENERGIA=13, -I/opt/energia-0101E0013/hardware/lm4f/cores/lm4f, -I/opt/energia-0101E0013/hardware/lm4f/variants/launchpad_129, -I/opt/energia-0101E0013/hardware/lm4f/libraries/Ethernet, -I/opt/energia-0101E0013/hardware/lm4f/libraries/SPI, /tmp/build7390494062982619478.tmp/sketch_nov17a.cpp, -o, /tmp/build7390494062982619478.tmp/sketch_nov17a.cpp.o]
In file included from sketch_nov17a.ino:2:0:
/opt/energia-0101E0013/hardware/lm4f/libraries/Ethernet/Ethernet.h:12:28: error: redefinition of 'const IPAddress INADDR_NONE'
In file included from /opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:5:0,
from /opt/energia-0101E0013/hardware/lm4f/libraries/Ethernet/EthernetClient.h:5,
from sketch_nov17a.ino:1:
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/IPAddress.h:73:17: error: 'const IPAddress INADDR_NONE' previously declared here
sketch_nov17a.ino:21:25: error: no matching function for call to 'Client::Client(byte [4], int)'
sketch_nov17a.ino:21:25: note: candidates are:
In file included from /opt/energia-0101E0013/hardware/lm4f/libraries/Ethernet/EthernetClient.h:5:0,
from sketch_nov17a.ino:1:
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:7:7: note: Client::Client()
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:7:7: note: candidate expects 0 arguments, 2 provided
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:7:7: note: Client::Client(const Client&)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:7:7: note: candidate expects 1 argument, 2 provided
sketch_nov17a.ino:21:8: error: cannot declare variable 'client' to be of abstract type 'Client'
In file included from /opt/energia-0101E0013/hardware/lm4f/libraries/Ethernet/EthernetClient.h:5:0,
from sketch_nov17a.ino:1:
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:7:7: note: because the following virtual functions are pure within 'Client':
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:12:18: note: virtual size_t Client::write(uint8_t)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:13:18: note: virtual size_t Client::write(const uint8t, size_t)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:14:15: note: virtual int Client::available()
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:15:15: note: virtual int Client::read()
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:17:15: note: virtual int Client::peek()
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:18:16: note: virtual void Client::flush()
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:10:15: note: virtual int Client::connect(IPAddress, uint16t)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:11:15: note: virtual int Client::connect(const char, uint16_t)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:16:15: note: virtual int Client::read(uint8t, size_t)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:19:16: note: virtual void Client::stop()
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:20:19: note: virtual uint8_t Client::connected()
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:21:11: note: virtual Client::operator bool()
sketch_nov17a.ino: In function 'void updateThingSpeak(String)':
sketch_nov17a.ino:67:22: error: no matching function for call to 'Client::connect()'
sketch_nov17a.ino:67:22: note: candidates are:
In file included from /opt/energia-0101E0013/hardware/lm4f/libraries/Ethernet/EthernetClient.h:5:0,
from sketch_nov17a.ino:1:
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:10:15: note: virtual int Client::connect(IPAddress, uint16t)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:10:15: note: candidate expects 2 arguments, 0 provided
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:11:15: note: virtual int Client::connect(const char, uint16_t)
/opt/energia-0101E0013/hardware/lm4f/cores/lm4f/Client.h:11:15: note: candidate expects 2 arguments, 0 provided
Where did this Sketch come from? There is no Client client(server, 80); You will need to specify the server and port in the ::connect() API. See the ThingSpeakClient example in the Ethernet library
I tried using this script
and i got this error: