From 73f10ace6fd4d1cc8b864de91fdbdcb3a35711e2 Mon Sep 17 00:00:00 2001 From: tigoe Date: Fri, 22 Mar 2019 09:00:39 -0400 Subject: [PATCH] added Web server with time string --- .gitignore | 2 + RubeGoldberg/RubeGoldberg.ino | 97 --------------- .../WebServerClientDisplay.ino | 117 ++++++++++++++++++ 3 files changed, 119 insertions(+), 97 deletions(-) delete mode 100644 RubeGoldberg/RubeGoldberg.ino create mode 100644 WebServerClientDisplay/WebServerClientDisplay.ino diff --git a/.gitignore b/.gitignore index 53deab1..0e1f551 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ *arduino_secrets.h ConnDevClient/arduino_secrets.h *keys.txt +ConnectedDevices/ips-2019.txt +ConnectedDevices/sql_queries.txt diff --git a/RubeGoldberg/RubeGoldberg.ino b/RubeGoldberg/RubeGoldberg.ino deleted file mode 100644 index 7170505..0000000 --- a/RubeGoldberg/RubeGoldberg.ino +++ /dev/null @@ -1,97 +0,0 @@ -/* - Shows how to make an MQTT client using SSL - to connect to . - - IMPORTANT: You need to install/update the SSL certificates first: - https://github.com/arduino-libraries/WiFi101-FirmwareUpdater#to-update-ssl-certificates - Add the domain shiftr.io:443 - You can check on your device after a successful - connection here: https://shiftr.io/try. - - modified from an example by Gilberto Conti - https://github.com/256dpi/arduino-mqtt - updated 4 Mar 2018 - by Tom Igoe -*/ - -//#include -#include -#include -// put your network SSID and password in -// a tab called arduino_secrets.h: -#include "arduino_secrets.h" - -const int ledPin = 5; -WiFiSSLClient netConnection; -MQTTClient client; -char server[] = "broker.shiftr.io"; -int port = 8883; -char device[] = "mkr1000"; -char username[] = "try"; -char password[] = "try"; -String mainTopic = "rubeGoldberg"; -unsigned long timestamp = 0; - -void setup() { - Serial.begin(9600); - Serial.setTimeout(10); - pinMode(ledPin, OUTPUT); - while (!Serial); - // try to connect to the network: - while ( WiFi.status() != WL_CONNECTED) { - Serial.print("Attempting to connect to Network named: "); - Serial.println(SECRET_SSID); // print the network name (SSID); - - // Connect to WPA/WPA2 network: - WiFi.begin(SECRET_SSID, SECRET_PASS); - delay(2000); - } - - // print the SSID of the network you're attached to: - Serial.print("Connected to: "); - Serial.println(WiFi.SSID()); - - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - client.begin(server, port, netConnection); - client.onMessage(messageReceived); - connect(); -} - -void loop() { - client.loop(); - - if (!client.connected()) { - connect(); - } - - if (Serial.available()) { - String who = Serial.readString(); - Serial.println(who); - if (client.connected()) { - client.publish(mainTopic + "/" + who, String("Action!")); - } - } -} - -void connect() { - Serial.println("connecting..."); - while (!client.connect(device, username, password)) { - delay(500); - } - Serial.println("connected"); - client.subscribe(mainTopic + "/yourName/#"); -} - - -void messageReceived(String & topic, String & payload) { - digitalWrite(ledPin, HIGH); - Serial.println("received: " + topic + "/" + payload); - delay(1000); - client.publish(mainTopic + "/yanlin", String("Next!")); - digitalWrite(ledPin, LOW); - -} diff --git a/WebServerClientDisplay/WebServerClientDisplay.ino b/WebServerClientDisplay/WebServerClientDisplay.ino new file mode 100644 index 0000000..4f59b09 --- /dev/null +++ b/WebServerClientDisplay/WebServerClientDisplay.ino @@ -0,0 +1,117 @@ +/* + Web Server with client display + Shows the client address in the Serial Monitor and returns + the time UTC. Checks the client request path, and + if the request is for a particular string, responds accordingly + + created 22 Mar 2019 + by Tom Igoe +*/ + +#include +#include +#include +#include "arduino_secrets.h" + +WiFiServer server(80); +RTCZero rtc; + +void setup() { + rtc.begin(); + Serial.begin(9600);// initialize serial communications + // while you're not connected to a WiFi AP: + while ( WiFi.status() != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(SECRET_SSID); + WiFi.begin(SECRET_SSID, SECRET_PASS); // try to connect + delay(2000); // wait 2 seconds before trying again + } + + // print out the device's network status + Serial.print("To see this device's web interface, go to http://"); + IPAddress ip = WiFi.localIP(); + Serial.println(ip); + + // get the time from the network: + unsigned long epoch; + do { + Serial.println("Trying to get time..."); + epoch = WiFi.getTime(); + delay(500); + } while (epoch == 0); + + // print the epoch once you have it: + Serial.print("Epoch received: "); + Serial.println(epoch); + rtc.setEpoch(epoch); + + // print the time: + Serial.println(getTimeString()); + + server.begin(); // When you're connected, start the server +} + +void loop() { + String response; + + // listen for incoming clients + WiFiClient client = server.available(); + + // if there's a client, get the IP address and set up a response string: + if (client) { + response = getTimeString(); + Serial.print("request from: "); + Serial.println(client.remoteIP()); + } + + while (client.connected()) { // while the client is connected, + if (client.available()) { // and there are incoming bytes to read, + // read the incoming line by line: + String request = client.readStringUntil('\n'); + Serial.println(request); // print the line + + // if it's a GET request for rubeGoldberg: + if (request.startsWith("GET /rubeGoldberg")) { + response += "\nHi Rube"; + } + // if the request is a blank line (\n or \r\n) + // then you're in the body of the request: + if (request.length() <= 2) { + client.println("HTTP 200 OK\n"); // send an HTTP response + client.println("\n"); // send an HTTP response + client.println(response); + + delay(10); + // give the client time to get the data + if (client.connected()) { // if the client's still connected + client.stop(); // disconnect the client + } + } + } + } +} + + +// get the time as an ISO8601 string e.g. 2019-03-22T12:25:16+0000): +String getTimeString() { + String now = "20"; + if (rtc.getYear() < 9) now += "0"; + now += rtc.getYear(); + now += "-"; + if (rtc.getMonth() < 9) now += "0"; + now += rtc.getMonth(); + now += "-"; + if (rtc.getDay() < 9) now += "0"; + now += rtc.getDay(); + now += "T"; + if (rtc.getHours() < 9) now += "0"; + now += rtc.getHours(); + now += ":"; + if (rtc.getMinutes() < 9) now += "0"; + now += rtc.getMinutes(); + now += ":"; + if (rtc.getSeconds() < 9) now += "0"; + now += rtc.getSeconds(); + now += "+0000"; + return now; +}