Skip to content

Commit

Permalink
Merge pull request #4 from smnmsr/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Alwinator authored Nov 20, 2023
2 parents 3fab18c + 23bf764 commit ba9c81e
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 309 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ __pycache__
build
dist
venv
.idea
.idea
Arduino_UNO_WIFI_r2/libraries/
.vscode/arduino.json
Arduino_UNO_WIFI_r2/secrets.h
112 changes: 112 additions & 0 deletions Arduino_UNO_WIFI_r2/Arduino_UNO_WIFI_r2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <WiFiNINA.h>
#include <HCSR04.h>
#include "secrets.h"

// Define variables
const int TRIGGER_PIN = 2; // Pin D2, Ultrasonic sensor
const int ECHO_PIN = 5; // Pin D5, Ultrasonic sensor
const int LED = LED_BUILTIN; // Indicator LED

const int MEASURE_CORRECTION = 9; // Added to measure value since te tabletop is higher that the sensor

// Init web server with port 80
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup(void)
{
// Enable Serial for testing purposes
Serial.begin(115200);

// Setup LED
pinMode(LED, OUTPUT);

// Setup ultrasonic sensor
HCSR04.begin(TRIGGER_PIN, ECHO_PIN);

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
// don't continue
while (true)
;
}

// setup Hostname
WiFi.setHostname(HOSTNAME);

// attempt to connect to WiFi network
Serial.print("Attempting to connect to Network named: ");
Serial.println(WIFI_SSID); // print the network name (SSID);
while (status != WL_CONNECTED)
{
// Connect to WPA/WPA2 network.
status = WiFi.begin(WIFI_SSID, WIFI_PSK);
Serial.print(".");
// wait 1 seconds for connection:
delay(5000);
}

// Print debug info via serial
Serial.println();
Serial.print("Connected - IP: ");
Serial.println(WiFi.localIP());

// start the server
server.begin();
}

void loop()
{
WiFiClient client = server.available(); // listen for incoming clients

if (client)
{ // if you get a client,
// Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected())
{ // loop while the client's connected
if (client.available())
{ // if there's bytes to read from the client,

digitalWrite(LED, HIGH);
char c = client.read(); // read a byte
if (c == '\n')
{ // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0)
{
// measure the height
double *height = HCSR04.measureDistanceCm();
char response[50];
sprintf(response, "{ \"table_height\": %d }", (int)(*height) + MEASURE_CORRECTION);

// send height to client
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/json");
client.println();
client.println(response);
client.println();

// break out of the while loop:
break;
}
else
{ // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r')
{ // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
// Serial.println("client disconnected");
digitalWrite(LED, LOW);
}
}
3 changes: 3 additions & 0 deletions Arduino_UNO_WIFI_r2/secrets.example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define HOSTNAME "<my hostname>"
#define WIFI_SSID "<my ssid>"
#define WIFI_PSK "<my password>"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ When the hardware is finished and powered on, it should be possible to get the t

```
[aw-qt]
autostart_modules = ["aw-server", "aw-watcher-afk", "aw-watcher-window", "aw-table-watcher"]
autostart_modules = ["aw-server", "aw-watcher-afk", "aw-watcher-window", "aw-watcher-table"]
[aw-qt-testing]
autostart_modules = ["aw-server", "aw-watcher-afk", "aw-watcher-window", "aw-table-watcher"]
autostart_modules = ["aw-server", "aw-watcher-afk", "aw-watcher-window", "aw-watcher-table"]
```

### DEV setup
Expand Down
Loading

0 comments on commit ba9c81e

Please sign in to comment.