Skip to content

Commit 76c6db5

Browse files
committed
test server
1 parent b359528 commit 76c6db5

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

test-server/test-server.ino

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <SPI.h>
2+
#include <WiFiNINA.h>
3+
#include <WiFiUdp.h>
4+
5+
#include "secrets.h" //supply your own
6+
char ssid[] = SECRET_SSID; // your network SSID (name)
7+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
8+
9+
unsigned int localPort = 2395; // local port to listen for UDP packets
10+
IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
11+
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
12+
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
13+
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
14+
15+
WiFiServer server(80);
16+
17+
18+
void setup() {
19+
Serial.begin(9600);
20+
while(!Serial); //only works on 33 IOT
21+
startWiFi();
22+
}
23+
24+
unsigned long millisLast = 0;
25+
void loop() {
26+
//Every 5 seconds, make a UDP call to NTP
27+
if(millis()-millisLast > 5000) { millisLast+=5000; startNTP(); }
28+
//Every loop, check for a UDP response and a client connection
29+
checkForNTPResponse();
30+
checkForServerClients();
31+
}
32+
33+
34+
void startWiFi(){
35+
if(WiFi.status()==WL_NO_MODULE) Serial.println("Communication with WiFi module failed!");
36+
else if(WiFi.firmwareVersion()<WIFI_FIRMWARE_LATEST_VERSION) Serial.println("Please upgrade the firmware");
37+
else { //hardware is ok
38+
Serial.print(F("Attempting to connect to SSID: ")); Serial.println(ssid);
39+
WiFi.begin(ssid, pass); //hangs while connecting
40+
if(WiFi.status()==WL_CONNECTED){ //did it work?
41+
Serial.println("Connected!");
42+
Serial.print("SSID: "); Serial.println(WiFi.SSID());
43+
Serial.print("IP address: "); Serial.println(WiFi.localIP());
44+
Serial.print("Signal strength (RSSI):"); Serial.print(WiFi.RSSI()); Serial.println(" dBm");
45+
server.begin();
46+
} else { //it didn't work
47+
Serial.println("Wasn't able to connect.");
48+
} //end it didn't work
49+
} //end hardware is ok
50+
} //end fn startWiFi
51+
52+
bool ntpStartLast = 0;
53+
void startNTP(){ //Called at intervals to check for ntp time
54+
if(WiFi.status()!=WL_CONNECTED) return;
55+
if(ntpStartLast!=0 && millis()-ntpStartLast < 3000) return; //do not send more than one request within 3 seconds
56+
Serial.println();
57+
ntpStartLast = millis();
58+
Udp.flush(); //does this do any good?
59+
Udp.stop();
60+
Serial.print(millis()); Serial.println(" Sending UDP packet to NTP server.");
61+
Serial.print("Udp.begin: "); Serial.println(Udp.begin(localPort)); //open connection
62+
memset(packetBuffer, 0, NTP_PACKET_SIZE); // set all bytes in the buffer to 0
63+
// Initialize values needed to form NTP request
64+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
65+
packetBuffer[1] = 0; // Stratum, or type of clock
66+
packetBuffer[2] = 6; // Polling Interval
67+
packetBuffer[3] = 0xEC; // Peer Clock Precision
68+
// 8 bytes of zero for Root Delay & Root Dispersion
69+
packetBuffer[12] = 49;
70+
packetBuffer[13] = 0x4E;
71+
packetBuffer[14] = 49;
72+
packetBuffer[15] = 52;
73+
Serial.print("Udp.beginPacket: "); Serial.println(Udp.beginPacket(timeServer, 123)); //NTP requests are to port 123
74+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
75+
Serial.print("Udp.endPacket: "); Serial.println(Udp.endPacket());
76+
} //end fn startNTP
77+
78+
void checkForNTPResponse(){ //Called on every cycle to see if there is an ntp response to handle
79+
if(!ntpStartLast) return;
80+
if(!Udp.parsePacket()) return;
81+
// We've received a packet, read the data from it
82+
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
83+
unsigned long ntpTime = (packetBuffer[40] << 24) | (packetBuffer[41] << 16) | (packetBuffer[42] << 8) | packetBuffer[43];
84+
Serial.print(millis()); Serial.print(" Received UDP packet from NTP server. Time is "); Serial.println(ntpTime);
85+
Udp.stop();
86+
ntpStartLast = 0;
87+
} //end fn checkNTP
88+
89+
90+
void checkForServerClients(){
91+
if(WiFi.status()!=WL_CONNECTED) return;
92+
WiFiClient client = server.available();
93+
if(client){
94+
Serial.println(); Serial.print(millis()); Serial.println(" Got client.");
95+
client.flush(); //does this do any good?
96+
client.stop();
97+
}
98+
}

0 commit comments

Comments
 (0)