Timestamp Asksensors Through Ethernet

The EtherTen combines an Uno-equivalent Arduino-compatible board and Wiznet-based Ethernet support, along with a microSD card slot and Power-over-Ethernet support. [Product page]
Joined:Mon Apr 03, 2017 2:00 am
Timestamp Asksensors Through Ethernet

Post by bradhnz » Sun Dec 29, 2019 9:15 pm

Hello, I am trying to update Asksensors with some dummy code and timestamp this using the below code adapted from https://github.com/asksensors/AskSensor ... -Timestamp

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:
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  // timestamp
  while(!timeClient.update()) {
  // get Epoch time
  Serial.print("> NTP Time:");
  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:

  // 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: ");

    //Make a HTTP request:
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + server + "\r\n" +
                 "Connection: close\r\n\r\n");
    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);}}

Post Reply