Adding webclient to existing project

Hi there,

I have written a code to get temperature and controlling my (RGBWW) LED's, everything is working fine.
Only now I would like to add a webclient like the default one. Only I have a NTP also which is using DHCP.

I have no idea how I can merge these 2 that I can use the following things;

  • serial output (to control a 3rd part hardware device)
  • RBGWW LED Control
  • Temperature
  • NTP Time sync (for the LED's)

and the Web interface.

Below my code in a link, this is without a webclient.h and sorry at all but I have rename it to dutch :blush:
Keep in mind, everything is working fine, just would like to add a webclient

Code Project

Thanks in advance!

Why not use the web client example from the Ethernet library as a guide? You didn't say what the client would do...

aarg:
Why not use the web client example from the Ethernet library as a guide? You didn't say what the client would do...

Hiya, I have tried that, but the example use a static IP, and my NTP Sync is a DHCP one, I can't find how to merge that.

Issue now is when I do the things what I think I need to do, I am still able to ping my arduino.. but he isn't show any website.

Is there a reason why you can not initialize Ethernet with a static IP instead of DHCP?

aarg:
Is there a reason why you can not initialize Ethernet with a static IP instead of DHCP?

Nope, if i knew that reason it was easier to solve :-s

The last time I looked at the Ethernet library, there were very simple provisions for static IP initialization. There are other people that are also reading this, they are probably looking for a more detailed description of the actual obstacle that is blocking you.

“Below my code in a link”

Why not post your code using </> code tags if you want people to take a look?

Okay I have merge the 2 examples UdpNtpClient & WebClient, so as I mention how it needs to be.

Whatever I do I can’t reach it via IP on a browser. This is exactly the same issue that I have in my own project.

Hopefully it is clear now what my issue is.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Vars UdpNtpClient
byte mac[] = {  0xA8,0x61,0x0A,0xAE,0x15,0xCB };
unsigned int localPort = 8888;       
IPAddress timeServer(213, 154, 236, 182);
const int timeZone = +2;     // Central European Time, Amsterdam
const int NTP_PACKET_SIZE = 48; 
byte packetBuffer[NTP_PACKET_SIZE]; 
EthernetUDP Udp;

// Vars WebClient
char server[] = "www.google.com";
IPAddress ip(192, 168, 2, 6);
IPAddress myDns(192, 168, 2, 254);
EthernetClient client;
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; 

void setup() {
// Setup UdpNtpClient
 Serial.begin(9600);
 while (!Serial) {
   ; 
 }

 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
     Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
   } else if (Ethernet.linkStatus() == LinkOFF) {
     Serial.println("Ethernet cable is not connected.");
   }
   while (true) {
     delay(1);
   }
 }
 Udp.begin(localPort);

// Setup WebClient
 Serial.begin(9600);
 while (!Serial) {
   ; 
 }

 Serial.println("Initialize Ethernet with DHCP:");
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
     Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
     while (true) {
       delay(1);
     }
   }
   if (Ethernet.linkStatus() == LinkOFF) {
     Serial.println("Ethernet cable is not connected.");
   }
   Ethernet.begin(mac, ip, myDns);
 } else {
   Serial.print("  DHCP assigned IP ");
   Serial.println(Ethernet.localIP());
 }
 delay(1000);
 Serial.print("connecting to ");
 Serial.print(server);
 Serial.println("...");

 if (client.connect(server, 80)) {
   Serial.print("connected to ");
   Serial.println(client.remoteIP());
   // Make a HTTP request:
   client.println("GET /search?q=arduino HTTP/1.1");
   client.println("Host: www.google.com");
   client.println("Connection: close");
   client.println();
 } else {
   // if you didn't get a connection to the server:
   Serial.println("connection failed");
 }
 beginMicros = micros();

}

void loop() {
// Loop UdpNtpClient
sendNTPpacket(timeServer); // send an NTP packet to a time server

 // wait to see if a reply is available
 delay(1000);
 if (Udp.parsePacket()) {
   // We've received a packet, read the data from it
   Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

   // the timestamp starts at byte 40 of the received packet and is four bytes,
   // or two words, long. First, extract the two words:

   unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
   unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
   // combine the four bytes (two words) into a long integer
   // this is NTP time (seconds since Jan 1 1900):
   unsigned long secsSince1900 = highWord << 16 | lowWord;
   Serial.print("Seconds since Jan 1 1900 = ");
   Serial.println(secsSince1900);

   // now convert NTP time into everyday time:
   Serial.print("Unix time = ");
   // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
   const unsigned long seventyYears = 2208988800UL;
   // subtract seventy years:
   unsigned long epoch = secsSince1900 - seventyYears;
   // print Unix time:
   Serial.println(epoch);


   // print the hour, minute and second:
   Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
   Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
   Serial.print(':');
   if (((epoch % 3600) / 60) < 10) {
     // In the first 10 minutes of each hour, we'll want a leading '0'
     Serial.print('0');
   }
   Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
   Serial.print(':');
   if ((epoch % 60) < 10) {
     // In the first 10 seconds of each minute, we'll want a leading '0'
     Serial.print('0');
   }
   Serial.println(epoch % 60); // print the second
 }
 // wait ten seconds before asking for the time again
 //delay(10000);
 Ethernet.maintain();

// Loop WebClient
// if there are incoming bytes available
 // from the server, read them and print them:
 int len = client.available();
 if (len > 0) {
   byte buffer[80];
   if (len > 80) len = 80;
   client.read(buffer, len);
   if (printWebData) {
     Serial.write(buffer, len); // show in the serial monitor (slows some boards)
   }
   byteCount = byteCount + len;
 }

 // if the server's disconnected, stop the client:
 if (!client.connected()) {
   endMicros = micros();
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   Serial.print("Received ");
   Serial.print(byteCount);
   Serial.print(" bytes in ");
   float seconds = (float)(endMicros - beginMicros) / 1000000.0;
   Serial.print(seconds, 4);
   float rate = (float)byteCount / seconds / 1000.0;
   Serial.print(", rate = ");
   Serial.print(rate);
   Serial.print(" kbytes/second");
   Serial.println();

   // do nothing forevermore:
   while (true) {
    // delay(1);
   }
 }

}

void sendNTPpacket(const char * address) {
 // set all bytes in the buffer to 0
 memset(packetBuffer, 0, NTP_PACKET_SIZE);
 // Initialize values needed to form NTP request
 // (see URL above for details on the packets)
 packetBuffer[0] = 0b11100011;   // LI, Version, Mode
 packetBuffer[1] = 0;     // Stratum, or type of clock
 packetBuffer[2] = 6;     // Polling Interval
 packetBuffer[3] = 0xEC;  // Peer Clock Precision
 // 8 bytes of zero for Root Delay & Root Dispersion
 packetBuffer[12]  = 49;
 packetBuffer[13]  = 0x4E;
 packetBuffer[14]  = 49;
 packetBuffer[15]  = 52;

 // all NTP fields have been given values, now
 // you can send a packet requesting a timestamp:
 Udp.beginPacket(address, 123); // NTP requests are to port 123
 Udp.write(packetBuffer, NTP_PACKET_SIZE);
 Udp.endPacket();
}