Almost there. IT WORKS! Wheee.
Right - well it does work - but something has appeared that I didn't notice it before. I've cleaned up the code a little as I realised there was another silly mistake..
On power up I now get the time as expected and the IP address.
14:14:29
server is at 192.168.0.123
So now if I fire this from a browser.. http://192.168.0.123:1000/?OUT1ON
I get this..
GET /?OUT1ON HTTP/1.1
%d
GET /favicon.ico HTTP/1.1
%d
I get the favicon line - that's fine - but where is the "%d" coming from - never seen that in previous versions??? Could this be a bug perhaps? I've started at my code over and over and I don't see anything wrong.. I've tried another browser... made no difference- I get %d after every line coming back into the server???
#include <UIPEthernet.h>
#include <UIPUdp.h>
#include <Time.h>
IPAddress timeServer(80, 96, 196, 58);
#define OUT1 7
#define OUT2 2
#define OUT3 16
EthernetServer server = EthernetServer(1000);
EthernetUDP Udp;
unsigned int localPort = 2390; // local port to listen for UDP packets
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
time_t x;
void setup()
{
Serial.begin(57600);
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x04};
IPAddress myIP(192,168,0,123);
Ethernet.begin(mac,myIP);
x=getNtpTime(timeServer);
setTime(x);
Serial.print(hour()); Serial.print(":"); Serial.print(minute()); Serial.print(":"); Serial.println(second());
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(OUT1, OUTPUT); digitalWrite(OUT1,HIGH);
pinMode(OUT2, OUTPUT); digitalWrite(OUT2,HIGH);
pinMode(OUT3, OUTPUT); digitalWrite(OUT3,HIGH);
}
char buffer[128];
int bp;
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& 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();
}
time_t getNtpTime(IPAddress& address)
{
Udp.begin(localPort);
while (Udp.parsePacket() > 0) ; // discard any previously received packets
sendNTPpacket(address);;
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
Udp.stop(); return secsSince1900 - 2208988800UL;
}
}
Udp.stop(); return 0; // return 0 if unable to get the time
}
void loop()
{
byte gotsome=0;
EthernetClient client = server.available();
if (client)
{
if (client.connected())
{
bp=0;
while(client.available())
{
buffer[bp]=client.read(); gotsome=1;
if ((buffer[bp]<' ')||(bp>=126)) { buffer[bp]=0; client.flush(); } else bp++;
}
if (gotsome)
{
gotsome=0;
client.println("ok");
if (strstr(buffer,"HTTP")) client.stop(); // differentiate browser or SOCKET
if (strstr(buffer,"OUT1ON")) digitalWrite(OUT1,HIGH);
if (strstr(buffer,"OUT1OFF")) digitalWrite(OUT1,LOW);
if (strstr(buffer,"OUT2ON")) digitalWrite(OUT2,HIGH);
if (strstr(buffer,"OUT2OFF")) digitalWrite(OUT2,LOW);
if (strstr(buffer,"OUT3ON")) digitalWrite(OUT3,HIGH);
if (strstr(buffer,"OUT3OFF")) digitalWrite(OUT3,LOW);
Serial.println(buffer);
}
}
}
}