I have done this successfully without the timestamp.
I cannot seem to get this to work and am thinking it is something to do with the include <EthernetUdp.h> line.
Any help appreciate
Code: Select all
#include <Ethernet.h>
#include <NTPClient.h>
//#include <EthernetUdp.h>
// ETHERNET config.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177); // Set the static IP address to use if the DHCP fails to assign
// ASKSENSORS config.
char server[] = "api.asksensors.com"; // ASKSENSORS host name
const int port = 80; // Port: HTTP=80
const char* apiKeyIn = "vMdxIBAIzbWnuAvr4Y8kxg7FDoG3i8ts"; // API KEY IN (change it with your API KEY IN)
EthernetClient client;// Initialize the Ethernet client library
//EthernetUDP Udp; // UDP client
NTPClient timeClient(ntpUDP); // NTP client
timeClient.begin(); // init NTP
timeClient.setTimeOffset(0); // 0= GMT, 3600 = GMT+1
// dummy data
int dumData = 560; // set your data
//-----------------------------------------------------------------
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// timestamp
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// get Epoch time
Serial.print("> NTP Time:");
Serial.println(timeClient.getFormattedTime());
long unsigned int timestamp = timeClient.getEpochTime();
// start the Ethernet connection
//Try to congifure using IP address instead of DHCP::
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(1000);// give the Ethernet shield a second to initialize:
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
Serial.print("connected asksensors.com");
//Create a URL for the request
String url = "http://api.asksensors.com/write/";
url += apiKeyIn;
url += "?module1=";
url += dumData;
url += " ";
url += timestamp;
Serial.print("********** requesting URL: ");
Serial.println(url);
//Make a HTTP request:
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Connection: close\r\n\r\n");
client.println();
Serial.println("> Request sent to ASKSENSORS");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}}
//-----------------------------------------------------------------
void loop() {
// if there are incoming bytes available from the server, read them and print them:
if (client.available()) {char c = client.read(); Serial.print(c);}
// if the server's disconnected, stop the client:
if (!client.connected()) {Serial.println(); Serial.println("disconnecting."); client.stop();
// do nothing forevermore:
while (true);}}
//-----------------------------------------------------------------