As the topics refers to am i in need of help to translate a code so that people who doesn’t know how to program can understand it.
I haven’t used the normal // to write what it means because this is for a seperate document.
The code is the following:
#include <SPI.h>
It’s a Arduino library for communicating with devices using the Serial Peripheral Interface (SPI) Bus.
Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be used for communication between two microcontrollers. (Arduino, n.d.)
#include <Ethernet.h>
With the Arduino Ethernet Shield, this library allows an Arduino board to connect to the internet. It can serve as either a server accepting incoming connections or a client making outgoing ones. The library supports up to four concurrent connection (incoming or outgoing or a combination). (Arduino, n.d.)
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xB7, 0xD9 };
The Media access control (mac) address is setup so that it is possible to lock the Arduino for a certain IP address on the network. The mac address is printed on the back of the Ethernet shield.
IPAddress serverIP(192,168,1,45);
The IP address is setup so that it is possible to establish a TCP connection. The Arduino can obtain its own IP address but when you need to connect to it and forward a port for it on the router you need to be sure which IP address it got therefore is it a good idea to set it up.
byte gateway[] = { 192, 168, 1, 1 };
It’s the internet access via the router
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(5190);
The specified port the server is going to listen on for a connect request.
int led = 8;
Pin 8 on the Arduino Ethernet is called led. When working with a lot of different pins it can be a good idea to give them names so that it’s easier to remember which task they have. For this prototype the researcher started with testing the code with a LED bulb hence the name.
void setup()
The part which will run once every time the Arduino is turned on.
{
This is used to mark blocks of code and it need to be in the beginning and ending of the blocks.
pinMode(led, OUTPUT);
Setting up the pin as output.
Ethernet.begin(mac, serverIP, gateway, subnet);
Initialize the Ethernet device
server.begin();
Start listening for clients
}
Ends the block of code in the void setup
void loop()
The code which is going to run in circuits as long as the Arduino is turned on.
{
This is used to mark blocks of code and it need to be in the beginning and ending of the blocks.
EthernetClient client = server.available();
This piece wait makes it wait for a client to connect and will only run the next part of the code if a client connect.
if (client) {
If a client connect it will continue.
char inBuffer[32];
This code load the data into a buffer called inBuffer. The maximum size of the buffer is set to 32 bytes.
int inCount = 0;
while (client.connected()) {
While the client is connected it will do the following
while (client.available()) {
If there is any data received from the client it will continue.
char ch = client.read();
The Arduino reads the client is sending to it.
if(inCount < 31 && ch != '\r' && ch != '\n') {
inBuffer[inCount] = ch;
inCount++;
inBuffer[inCount] = 0;
}
}
The } above is just the ends of the earlier written code pieces.
if(strcmp(inBuffer,"on") == 0)
digitalWrite(led, HIGH),
It switches the power on.
client.println("The power is ON");
It writes to the client that the power is on.
else if(strcmp(inBuffer,"off") == 0)
digitalWrite(led, LOW),
It switches the power off.
client.println("The power is OFF");
It writes to the client that the power is off.
else client.println("Wrong Commando");
If it receive other characters then on and off it will tell the client that it’s a wrong commando.
if(inCount > 0) {
If the data is bigger than 0 bytes it do the following
client.println("Closing connection");
It tells the client that the connection is going to close. The text inside the “” is just a random message which can be change to anything.
delay(2000);
A delay before it proceed to the next command. The delay is for 2 seconds but because the Arduino calculates in milliseconds it has to be 2000.
client.stop();
It closes the connection to
}
}
}
}
The } above is just the ends of the earlier written code pieces.
And as you can see is there some spots where im not sure about how to explain it.
it is the following code there is troubling me:
byte subnet[] = { 255, 255, 255, 0 };
int inCount = 0;
if(inCount < 31 && ch != '\r' && ch != '\n') {
inBuffer[inCount] = ch;
inCount++;
inBuffer[inCount] = 0;
if(strcmp(inBuffer,"on") == 0)
else if(strcmp(inBuffer,"off") == 0)
I hope someone is up for the challenge and wants to help me with this and if you see some corrections in the translation i have already done don’t hesitate to correct me.
thanks in advance