So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By flagtrax
#60277 Hi all,
After working with the 8266 for a year or so(off and on), I still consider myself a newbie because there are quite a few "holes" in my understanding. For one thing, way back in my early days(before the term "PC" was coined), we worked with routines, subroutines calls returns, jumps, etc. The way I understand the philosophy used by Arduino's C we talk about functions (not sure that's totally correct). That being said, void setup() would be a function, as is void loop(). I get that (I think :lol: ).
One of the things I find confusing is that in examples of servers being presented much work is done in the setup function as such:
Code: Select allvoid setup(void){
  webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 <a href=\"socket1On\"><button>ON</button></a>&nbsp;<a href=\"socket1Off\"><button>OFF</button></a></p>";
  webPage += "<p>Socket #2 <a href=\"socket2On\"><button>ON</button></a>&nbsp;<a href=\"socket2Off\"><button>OFF</button></a></p>";
 
  // preparing GPIOs
  pinMode(gpio4_pin, OUTPUT);
  digitalWrite(gpio4_pin, LOW);
  pinMode(gpio5_pin, OUTPUT);
  digitalWrite(gpio5_pin, LOW);
 
  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }
 
  server.on("/", [](){
    server.send(200, "text/html", webPage);
  });
  server.on("/socket1On", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio4_pin, HIGH);
    delay(1000);
  });
  server.on("/socket1Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio4_pin, LOW);
    delay(1000);
  });
  server.on("/socket2On", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio5_pin, HIGH);
    delay(1000);
  });
  server.on("/socket2Off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio5_pin, LOW);
    delay(1000);
  });
  server.begin();
  Serial.println("HTTP server started");
}
 
void loop(void){
  server.handleClient();
}


and the loop (being only one line of code) I find quite confusing. My brain tells me is should be the other way around :lol:

I have successfully followed others provided examples to turn on/off a gpio pin for several different projects. And have been vasilating between the Arduino IDE and Nodemcu platforms. Rumor now has it that Nodemcu may not see much more development so I am leaning more toward the Arduino IDE. I want learn to do more than just turn gpio's on and off, and move on to being able to control other "operations" (so as not to use the term subroutine) within the sketch. For example from a browser, turn on a gpio to "fade" mode in a continuous manor until a request to turn it off is received, or change viewings of various sensors. I'm lost at tying these two simple things together, I think because of my confusion explained above . Finding tutorials to aid in educating myself becomes frustrating as well with the speed of changes coming forth. So the question is: Is there a repository of examples WITH explanations above and beyond "do this", more "like do this because.." I do realize that my knowledge of "today's" languages (like C and scripting in general) is in it's infancy :oops: so I think that explanations would be most helpful. Any steering is greatly appreciated.
User avatar
By jeffas
#60500 I am very much an Arduino (and ESP8266) newbie. I bought The Arduino Cookbook by Michael Margolis. I sent it back in exchange for Exploring Arduino by Jeremy Blum, because I found the Cookbook went into far too much detail about programming for me. But I'm a professional programmer. You might well find that the Cookbook matches your needs. It's well written, and explains the programming side of things very well.
User avatar
By flagtrax
#60518 Thanks for that jeffas. Indeed I've found lots of information out there, in bits and peaces. Every little bit helps, this forum for example is a great help. I have resolved a lot of issues through the resources and good people on the forum. Neil Kolban's book is another great source.
My early stumbles involved the usual setup issues, plus the fact that I was constantly working with "apples vs. oranges and for a while didn't realize it. Apples being Nodemcu/lua using ESPlorer as the IDE and oranges being the Arduino IDE. As one gains experience it could be argued that the differences are not too overly drastic (aside from syntax), but for a beginner trying to get started, beyond AT commands, it can be an excruciating experience. Now I can say my challenges would be more on the order of learning and understanding everything from C+, Lua, HTML, CSS, Python, and the limitations of the 8266 soc. I envy those who have been in the recent world of technology who have been associated with these marvelous devices enough to have a working understanding. Coming from a world of tubes to transistors, then on to IC's, and early MPU's like the 8080 and 6502. Coming back into the technology after a hiatus has not been as easy as I thought. Terminology changes, jargon changes, even programming philosophies change. Still it's a wonderful rewarding challenge to work with a module that would have been the size of my desk in what seems a few years ago. For other newbies, my advice again, read as much as you can, be mindful of outdated information, when you ask a question, provide enough information for others to understand your problem. I have been guilty of the last point myself, thinking everyone is on the same page I am. The more I play with the 8266 the more I realize there are multiple platforms to work with and all are different.
User avatar
By martinayotte
#60541 But what is you original question ?
If your sketch doesn't work as expected, maybe it is the way you are defining your buttons.
Personally, I would rather use microajax from https://code.google.com/archive/p/microajax/ and define the button as follow :
Code: Select all<input type="button" value="On" onClick="microAjax('/socket1On', function(res) { } );" />