Code:
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <FastLED.h>
#include <EEPROM.h>
#include <string.h>
#include "secret.h"
#define LED_PIN 6
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define NUM_LEDS 3
CRGB leds[NUM_LEDS];
/* MAIN SERVER CONFIG */
IPAddress destinationIP (10, 168, 1, 100);
unsigned int destinationPort = 9101;
/* END */
/* TABLE VARIABLES */
char *tableID = "string one";
char *situationOne = "string one";
/*
char tableID;
char situationOne = "_available";
*/
int address = 215;
/* END */
ADC_MODE(ADC_VCC);
int status = WL_IDLE_STATUS;
/* BOOLEANS */
bool candleAllowance = false;
bool connectionLive = false;
bool dark = false;
bool light = false;
bool stateIdle = false;
bool stateAvailable = false;
bool stateOrdered = false;
bool stateOff = true;
/* END */
/* LIGHT SENSOR */
int lightSensor;
long makingSureItIsDark = 0;
long makingSureItIsLight = 0;
int sensorState = LOW;
unsigned long last_time = 0L ;
unsigned long intervalSensor = 10000;
/* END */
/* BLINK LED VARIABLES */
const int ledPin = LED_BUILTIN;// the tableID of the LED pin
int ledState = HIGH; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long interval = 5000; // interval at which to blink (milliseconds)
/* END */
/* WiFi TESTER VARIABLES */
unsigned long startTime;
unsigned long comparisonTime = 0;
/* END */
/* STATES */ /*
struct state {
strcpy (state, "_available");
strcpy (state, "_idle");
strcpy (state, "_ordered");
strcpy (state, "_off");
strcpy (state, "_ping");
};
/* END */
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index tableID (needed only for WEP)
unsigned int localPort = 1821; // local port to listen on
char packetBuffer[256]; //buffer to hold incoming packet
//char ReplyBuffer[] = "table 01: acknowledged"; // a string to send back
WiFiUDP Udp;
void setup() {
pinMode(ledPin, OUTPUT);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.clear(true);
// FastLED.setBrightness( BRIGHTNESS );
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);
int compare( char *string1, char *string2 ) {
if ( strlen(string1) != strlen(string2) ) {
return 2;
}
for (int i=0; i<strlen(string1); i++) {
if ( string1[i] != string2[i] ) {
return 0;
}
}
return 1;
}
Serial.println();
delay(500);
Serial.println(" ***** NEW SESSION STARTED ***** ");
delay (500);
Serial.println("THE BULB. terraslamp -- V01 2020");
Serial.println("Setting up network now, hold your drinks high..!");
Serial.println();
delay(100);
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
delay(100);
WiFi.mode(WIFI_STA);
Serial.print("Connecting");
WiFi.begin(ssid, pass);
//WiFi.config(staticIP, gateway, subnet);
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
//status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: **updated later to 1s -- so far no issues to report.
delay(1000);
}
Serial.println("Yes! We are connected to WiFi! Cheers all!");
Serial.println("Connection info:");
connectionLive = true;
printWifiStatus();
Serial.printf("UDP server on port %d\n", localPort);
Serial.println();
Serial.println("Almost there, finalizing boot process.. Setting Table ID...");
//Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
stateOff = false;
stateAvailable = true;
EEPROM.begin(512);
delay(250);
tableID = EEPROM.read(address);
if (tableID <= 001) {
Serial.println("New device, no table ID is set");
Udp.beginPacket(destinationIP, destinationPort);
String ip = WiFi.localIP().toString();
String text = " : No table ID is set.";
String toSend = ip + text;
Udp.print(toSend);
Udp.endPacket();
}
else {
Udp.beginPacket(destinationIP, destinationPort);
String text1 = "Table number ";
String text2 = " is ONLINE";
String toSend = text1 + tableID + text2;
Udp.print(toSend);
Udp.endPacket();
Serial.println(toSend);
}
delay(250);
Serial.println("Device is ready to use!");
}
void loop() {
connectionStatus(); //CHECK CONNECTION EVERY 10S
startTime = millis();
if ((startTime - comparisonTime) > 10000) //every 10s
{
comparisonTime = startTime;
TestWiFiConnection(); //test connection, and reconnect if necessary
}
// END
if (connectionLive == true) {
if (dark == false) {
checkSensorDark();
}
if (dark == true) {
checkSensorLight();
}
// Read packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(destinationPort);
// Place it in buffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.print("Command: ");
Serial.print(packetBuffer);
Serial.println();
Serial.println("--END--");
int cmpResult = compare( tableID, situationOne );}
//if (packetBuffer, strcat(str1, str2)){ // !strcmp(packetBuffer, "101_available"));
//modeAvailable();
//}
//}
else if (strstr(packetBuffer, tableID + "_ordered")) {
modeOrdered();
}
else if (strcmp(packetBuffer, "{0}_idle")) {
if(packetBuffer == tableID + "_idle"){
modeIdle();
}
}
else if (!strcmp(packetBuffer, "all_off")) { //ORIGINAL FUNCTIONING CODE
modeOff();
}
else if (strstr(packetBuffer, tableID + "_off")== packetBuffer) {
modeOff();
}
else if (strstr(packetBuffer, tableID + "_ping")== packetBuffer) {
Udp.beginPacket(Udp.remoteIP(), destinationPort);
Udp.write("01 PING");
Udp.endPacket();
}
else if (strstr(packetBuffer, tableID + "_tableStatus")== packetBuffer) {
tableStatus();
}
else if (strstr(packetBuffer, tableID + "_lightSensor")== packetBuffer) {
lightStatus();
}
else if ( !strcmp(packetBuffer, "tableID")) {
Serial.print("You have requested the table ID: ");
Serial.println(tableID);
Serial.println("If table ID resulted in 0, please assign the device to a table number.");
Udp.beginPacket(Udp.remoteIP(), destinationPort);
String ip = WiFi.localIP().toString();
String text = " : is the IP, the table ID is: ";
String toSend = ip + text + tableID;
Udp.print(toSend);
Udp.endPacket();
}
else {
Serial.println("Captured a message.. Don't know what to do with it. ");
}
/*
HERE SHOULD COME SOME LINES THAT WILL MEASURE THE BATTERY VOLTAGE
IF THE BATTERY IS RUNNING LOW THEN RUN:
also see notes
run this void: lowBattery();
*/
FastLED.show();
if (sensorState == HIGH) {
//lightSensor = analogRead(A0);
}
}
}
}