Some questions about basics of microcontroller

Hello! I recently bought ESP8266 for the first time, together with cheap water sensors, and managed to create a "sketch" for emailing me when there is a water leak. And it works:) However, I don't understand a few things. I read an introductory tutorial, but it only talks about small projects to get started. My questions are:

  1. in the beginning. you are supposed to "flash ESP8266 by ESP8266 community". I understood it as flashing it to ESP8266 like a firmware. But then, as I wanted to set up the second water detector, it says it's already installed. Does it mean that this thing to flash was like a driver for my computer?

  2. It seems that an ESP8266 can take one file at a time. There are a lot of pins, are they all supposed to be for just one logically related project that can be expressed in one sketch, with one void setup and void loop ?

  3. If so, then there is "Basic OTA": I tried it (following random nerd tutorials), and more or less crashed the device, so I reset it. But I don't understand how it should work, because, even if basic OTA worked, as soon as I flash my own code over wifi, "basic OTA" is gone, then it's not going to work anymore?
    It would be convenient, though, if I can modify my sketch over wifi. Could someone tell me how to do it?

  4. The same with firmware: if firmware needs update, how can I do it, without killing my own program?

I appreciate your help very much! I'm pretty confused.

  1. This question is foggy at best. Flashing is simply programing a memory device. That device is a device that is FLASH memory only, or it the FLASH memory can be contained in a chip with other circuitry. Flashing is putting selected data in that device at a selected location. As Wikipedia says Flash memory is an electronic non-volatile computer memory storage medium that can be electrically erased and reprogrammed.

  2. You almost got it. If by file you are talking about the program you are correct but it can contain another program that is not activated. For example you have two hands but putting a gum ball in your mouth you generally use only one hand and some fingers. The other hand is not needed for the task so it is idle. The ESP8266 does not care how many voids,loops or whatever is in the program, it never sees it. What you are doing is making the tool chain happy, mainly the preprocess or and compiler. Eventually all of the statements in your code are converted to machine code, code the processor understands, it is just 1s and 0s in groups depending on the processor ALU (Arithmetic Logic Unit) width. In the case of the Arduino the initial source code is a variation of C++. If you have a COBOL compiler for it it would be in COBOL, the processor only cares about the program data, not how it was derived. In computer world a computer file is a resource for recording data on a computer storage device, primarily identified by its filename.

  3. The OTA (Over The Air) concept is simple but??? The way it works you have enough memory in the processor to hold an additional program. When doing OTA the new code is loaded into memory with code already on the processor. For the new code to work it needs code to allow new code to be uploaded. It then changes the start section so it now runs the new code and forgets about the old. The next time you OTA that old code is overwritten and the process repeats.

  4. That depends on the firmware, if it is variables etc stored in NVM (Non Volatile Memory) with the correct instructions your code will update those values thereby updating your firmware. If it is code that is executing other then doing something like OTA you need to reprogram it.

This is a quick answer, I hope it helps answer your questions. This is basically valid for any processor. Your computer loads new code when you start a program and runs that, it can change at will as the code is on a mas storage device and the memory is read write.

I think that what is meant is to install (instead of 'flash') the board package; this installs the necessary files on your computer. It might got lost in translation.

Thank you @gilshultz @sterretje for your replies !! I realized that I noted a wrong word: yes, as for my question 1, "install" was the right one. I think I wrote it down wrong since I was already confused and had thought that I was setting up ESP8266 by doing it. So it's just an installation on my laptop, that's why it was enough to do it once.

As for 2, @gilshultz so I can send a sketch to be active, and another one to be inactive/idle? How can you make the inactive one active, and inactivate an active one?

And, I can write, say, two void loop, one of them does water checking, and other one, say, temperature reading, both of them in one sketch?

As for 3 and 4, sorry, I don't really understand;;

No. You can write two sketches and combine them and add a mechanism to switch between them (e.g. a button).

They need to have different names. The general way is

void loop()
{
  readWater();
  readTemperature();

  displayWater();
  displayTemperature();
}

void readWater()
{
  ...
}

void displayWater()
{
  ...
}

void readTemperature()
{
  ...
}

void displayTemperature()
{
  ...
}

loop() is a function and in the above it calls other functions (what you referred to as different loops).

1 Like

It is like a function, it will not activate until it is called.
Try this link it explains a lot of what you are asking: ArduinoOTA: Over-the-air (OTA) updates made easy #ESP32

Void loop() is a function called by the code once the setup() is finished. This code is hidden in the background and you do not see it. Part of that background code sets up the Arduino to a known state, initializes and runs mills, etc (interrupt routine) and when it is ready it calls setup() then loop().

Void loop() is your program, it can have as many functions as you like. Void is a predecessor of the function definition telling what it will return. It can only return one item. Void is saying nothing is returned. byte function(){} will return a byte.

You can write two functions, one to check temperature the other to check water. Your main program (void loop()) will call them. You can have a hundred functions, there is no limit other then time constraints and memory.

Hopefully this helps.

Hardware refers to solid elctronics.

Software refers to an application/user program that usually resides in RAM in the case of Computer (the PC) and resides in "Flash Memory" in the case of Microcontroller (the ESP8266).

Firmware is an especial program that resides in a "non-volatile memory (part of flash)". In the case of ESP8266 NodeMCU, it is the Boot Loader Program that accepts users program from the IDE and stores them into the target Flash Memory.

Thank you all for your explanations and useful links/tips!

@gilshultz I looked at your link about OTA and followed, this time it worked! It's totally different from the tutorial I read before.

So from your (@gilshultz + @sterretje ) explanation, it sounds like void loop should be just once in a sketch, and if one wants multiple tasks, they have to be in the loop, then. For example, if I want water detector every 20 sec, and want to check temperature every 50sec, probably I will have to come up with weird trick.... perhaps checking time stamp for 50sec or so? (It's just a thought-example, I'm not going to want to do it!)

Normally your main loop() runs continuously forever but within that loop you can call multiple functions, conditionally with if-statements or switch-case statements if you wish, and you can have multiple for-loops, while() loops and do-while() loops in your main loop, or other loops nested in those loops, etc.

The processor is aways running so it's usually looping. It's possible to make a permanent "infinite loop" inside the main loop that you never get out of until you reset. ...But usually when that happens by accident when you don't want your code stuck in the loop :wink:

Demonstration code for several things at the same time

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.