I'm a newbie to programing and 1st timer here posting. I'm trying to write a code to have a light turn on for xtime and turn off for xtime automatically based on a schedule set on a dashboard in a webpage. The device is small and can only fit an esp8266-12E in it so, I'm programing for it using Arduino IDE and testing on an arduino uno board.
I'm getting an error in line 133.
"Compilation error: expected initializer before 'if' "
Below is the code I got thus far.
/*In this code, the ESP8266 creates a web server and listens for incoming
requests. When it receives a request, it parses the request and updates
the light times or the light status accordingly. The server also sends
a response to the client with the current status of the light and a form
for setting the light times for each day of the week.
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DateTime.h>
#define LED_BUILTIN 2
// Replace these with your WiFi credentials
const char* ssid = "xxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxx";
// Create an object for the ESP8266 WiFi connection
WiFiServer server(80);
// Variables for the timer
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const long interval = 60000; // 1 minute
bool lightOn = false;
// Days of the week
const char* daysOfTheWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Array to store the times when the light should be turned on and off
int lightTimes[7][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
void setup() {
// Initialize the serial connection for debugging
Serial.begin(115200);
// Connect to the WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
// Print the local IP address
Serial.println(WiFi.localIP());
// Start the web server
server.begin();
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait for the client to send a request
while(!client.available()) {
delay(1);
}
// Read the request and parse it
String request = client.readStringUntil('\r');
client.flush();
// Check if the request is to turn the light on or off
if (request.indexOf("/lightOn") != -1) {
lightOn = true;
} else if (request.indexOf("/lightOff") != -1) {
lightOn = false;
}
// Check if the request is to set the light times
if (request.indexOf("/setLightTimes") != -1) {
// Parse the request to get the light times
int startIndex = request.indexOf("?") + 1;
int endIndex = request.indexOf(" HTTP/1.1");
String timesString = request.substring(startIndex, endIndex);
// Split the times string into individual times
int times[7][2];
int day = 0;
int time = 0;
String currentTime = "";
for (int i = 0; i < timesString.length(); i++) {
char c = timesString.charAt(i);
if (c == '&') {
times[day][time] = currentTime.toInt();
currentTime= "";
if (time == 0) {
time = 1;
} else {
time = 0;
day++;
}
} else {
currentTime += c;
}
}
// Set the light times
for (int i = 0; i < 7; i++) {
lightTimes[i][0] = times[i][0];
lightTimes[i][1] = times[i][1];
}
}
// Send the response to the client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// Send the current status of the light as a response
if (lightOn) {
client.println("Light is currently ON");
} else {
client.println("Light is currently OFF");
}
// Create the form for setting the light times
client.println("<form action='/setLightTimes' method='get'>");
for (int i = 0; i < 7; i++) {
client.println("<p>");
client.println(daysOfTheWeek[i]);
client.println(": <input type='text' name='" + String(i) + "on' value='" + String(lightTimes[i][0]) + "'> to <input type='text' name='" + String(i) + "off' value='" + String(lightTimes[i][1]) + "'>");
client.println("</p>");
}
client.println("<br>");
client.println("<input type='submit' value='Set times'>");
client.println("</form>");
client.println("</html>");
// Close the connection
client.stop();
// Update the timer variables
previousMillis = currentMillis;
currentMillis = millis();
// Check if it's time to turn the light on or off
if (currentMillis - previousMillis >= interval) {
// Get the current day of the week
time_t currentTime = time(nullptr);
// Convert the time to a tm struct
tm* timeInfo = localtime(¤tTime);
struct tm *localtime(const time_t *_timer)
// Check if the current time is between the on and off times for the current day
if (hour() >= lightTimes[day][0] && hour() < lightTimes[day][1]) {
// Check if the light is currently on or off
if (!lightOn) {
// Turn the light on
digitalWrite(LED_BUILTIN, HIGH);
lightOn = true;
}
} else {
// Turn the light off
digitalWrite(LED_BUILTIN, LOW);
lightOn = false;*/
}
Thanks for your reply. I have not being able to load the code into anything yet as the errors stated happen when I verify the sketch code. This is why I'm asking for help. I can load the code into a NodeMcu once the code to get the date is good. Is this a bad approach?
BTW commenting these lines to "get the current day of the week and "convert the time to a tm struct" the code loads into an esp8266 and I can see the dashboard but since I cannot get the current day of the week I cannot change the time to compare and turn the light on/off
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.
Here is the version with auto correction. Sorry I'm not a programmer so proper or standard code format is unknown to me.
/*In this code, the ESP8266 creates a web server and listens for incoming
requests. When it receives a request, it parses the request and updates
the light times or the light status accordingly. The server also sends
a response to the client with the current status of the light and a form
for setting the light times for each day of the week.
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DateTime.h>
#define LED_BUILTIN 2
// Replace these with your WiFi credentials
const char* ssid = "xxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxx";
// Create an object for the ESP8266 WiFi connection
WiFiServer server(80);
// Variables for the timer
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const long interval = 60000; // 1 minute
bool lightOn = false;
// Days of the week
const char* daysOfTheWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
// Array to store the times when the light should be turned on and off
int lightTimes[7][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
void setup() {
// Initialize the serial connection for debugging
Serial.begin(115200);
// Connect to the WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
// Print the local IP address
Serial.println(WiFi.localIP());
// Start the web server
server.begin();
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait for the client to send a request
while (!client.available()) {
delay(1);
}
// Read the request and parse it
String request = client.readStringUntil('\r');
client.flush();
// Check if the request is to turn the light on or off
if (request.indexOf("/lightOn") != -1) {
lightOn = true;
} else if (request.indexOf("/lightOff") != -1) {
lightOn = false;
}
// Check if the request is to set the light times
if (request.indexOf("/setLightTimes") != -1) {
// Parse the request to get the light times
int startIndex = request.indexOf("?") + 1;
int endIndex = request.indexOf(" HTTP/1.1");
String timesString = request.substring(startIndex, endIndex);
// Split the times string into individual times
int times[7][2];
int day = 0;
int time = 0;
String currentTime = "";
for (int i = 0; i < timesString.length(); i++) {
char c = timesString.charAt(i);
if (c == '&') {
times[day][time] = currentTime.toInt();
currentTime = "";
if (time == 0) {
time = 1;
} else {
time = 0;
day++;
}
} else {
currentTime += c;
}
}
// Set the light times
for (int i = 0; i < 7; i++) {
lightTimes[i][0] = times[i][0];
lightTimes[i][1] = times[i][1];
}
}
// Send the response to the client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// Send the current status of the light as a response
if (lightOn) {
client.println("Light is currently ON");
} else {
client.println("Light is currently OFF");
}
// Create the form for setting the light times
client.println("<form action='/setLightTimes' method='get'>");
for (int i = 0; i < 7; i++) {
client.println("<p>");
client.println(daysOfTheWeek[i]);
client.println(": <input type='text' name='" + String(i) + "on' value='" + String(lightTimes[i][0]) + "'> to <input type='text' name='" + String(i) + "off' value='" + String(lightTimes[i][1]) + "'>");
client.println("</p>");
}
client.println("<br>");
client.println("<input type='submit' value='Set times'>");
client.println("</form>");
client.println("</html>");
// Close the connection
client.stop();
// Update the timer variables
previousMillis = currentMillis;
currentMillis = millis();
// Check if it's time to turn the light on or off
if (currentMillis - previousMillis >= interval) {
// Get the current day of the week
time_t currentTime = time(nullptr);
// Convert the time to a tm struct
tm* timeInfo = localtime(¤tTime);
struct tm* localtime(const time_t* _timer)
// Check if the current time is between the on and off times for the current day
if (hour() >= lightTimes[day][0] && hour() < lightTimes[day][1]) {
// Check if the light is currently on or off
if (!lightOn) {
// Turn the light on
digitalWrite(LED_BUILTIN, HIGH);
lightOn = true;
}
}
else {
// Turn the light off
digitalWrite(LED_BUILTIN, LOW);
lightOn = false;
* /
}
aarg... thanks for your responses but it seems to me that you are more interested to read my comments than helping me with the problem. perhaps you are traying to rack up posts?
I don't know programing nor have use the Arduino IDE as much as most of you guys have. That being said things that are to you natural is not for me... Anyways thanks for the comments.
As far as "selecting the correct board..." I'm not sure why you keep bringing this up. I already did. I selected the ESP8266 and loaded the code to the chip eliminating the lines to "Get the current day of the week" & "Convert the time to a tm struct". See my post with the image showing the Dashboard. I got it the first time . NOT using the Arduino Uno and going directly into the chip. If doing this or being a newbie is a problem here then let me know and I'll move on to somewhere else for help.
No, I am more interested in helping you get to the bottom of your problem. You said you were "testing on an arduino uno board" and I was not the only reader to remark on the strangeness of that - see reply #3. In fact, I have read the entire thread and you never clarified that until now. So I can hardly take blame for continuing to suggest it.
I make my time and thoughts available to people here for free. I don't need to "rack up posts" as I have about 50,000 here by now.
I make a productive suggestion on how to make your code more presentable, and this is what I get. Honestly, I think anyone on any forum at all would find it unfair. So I am blocking you permanently.
alto777 thanks for the response...
Sorry for the /* I forgot to remove it before posting.
I was using that to test my code and comment big sections of it. example /* "code lines" */
Thanks for the tip... I'm using it every time before I verify the code... Thanks for your useful feedback!
I try avoid forums as sometimes I get frustrated when I get comments that do not provide any value to the problem. Example. You already posted the need for auto format. Hate when people just leave a quick post stating what you already mention vs looking at the code and provide their potential solutions or suggestions. I call that wracking posts. you do couple K's of those useless and you get prestige?.. I think if you don't have anything to contribute don't post.