ahmedibrrahim

Server Board Program

Aug 2nd, 2020
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.30 KB | None | 0 0
  1. /*
  2.    Server Board code V1.0
  3.    192.168.1.4:80
  4. */
  5.  
  6. #include <SoftwareSerial.h>                         //including the SoftwareSerial library will allow you to use the pin no. 2,3 as Rx, Tx.
  7. #include <Adafruit_GFX.h>
  8. #include <Adafruit_SSD1331.h>
  9. #include <SPI.h>
  10.  
  11. SoftwareSerial esp8266(2, 3);                       //set the Rx ==> Pin 2; TX ==> Pin3.
  12.  
  13. #define sclk 13
  14. #define mosi 11
  15. #define cs   10
  16. #define rst  9
  17. #define dc   8
  18.  
  19. // Color definitions
  20. #define  BLACK           0x0000
  21. #define BLUE            0x001F
  22. #define RED             0xF800
  23. #define GREEN           0x07E0
  24. #define CYAN            0x07FF
  25. #define MAGENTA         0xF81F
  26. #define YELLOW          0xFFE0
  27. #define WHITE           0xFFFF
  28.  
  29. #define serialCommunicationSpeed 9600               // <========= define a constant named "serialCommunicationSpeed" with a value 9600. it referes to the Software and hardware serial communication speed(baud rate).
  30. #define DEBUG true                                  //make a constant named "DEBUG" and it's value true. we will use it later.
  31.  
  32. int redLED = 7;                                    //assign a variable named "redLED" with an integer value 5, it refers to the pin which the red LED is connected on.
  33. int blueLED = 6;                                   //assign a variable named "blueLED" with an integer value 6, it refers to the pin which the blue LED is connected on.
  34. int greenLED = 5;                                   //assign a variable named "greenLED" with an integer value 7, it refers to the pin which the green LED is connected on.
  35.  
  36. Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, rst);
  37.  
  38. void setup()
  39.  
  40. {
  41.   display.begin();
  42.  
  43.  
  44.   display.fillScreen(BLACK);
  45.   delay(5);
  46.  
  47.   display.setCursor(5, 15);
  48.   display.setTextColor(WHITE);
  49.   display.setTextSize(1);
  50.   display.print("Ready...");
  51.  
  52.   pinMode(redLED, OUTPUT);                          //set the pin number 5 as an output pin.
  53.   pinMode(blueLED, OUTPUT);                         //set the pin number 6 as an output pin.
  54.   pinMode(greenLED, OUTPUT);                         //set the pin number 7 as an output pin.
  55.  
  56.   digitalWrite(greenLED, HIGH);                        //turn the red LED off at the beginning of the program.
  57.   digitalWrite(redLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  58.   digitalWrite(blueLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  59.  
  60.   Serial.begin(serialCommunicationSpeed);           //begin the Hardware serial communication (0, 1) at speed 9600.
  61.   esp8266.begin(serialCommunicationSpeed);          //begin the software serial communication (2, 3) at speed 9600.
  62.  
  63.   InitWifiModule();                                 //call this user-defined function "InitWifiModule()" to initialize a communication between the ESP8266 and your access point (Home Router or even your mobile hotspot).
  64. }
  65.  
  66. void loop()                                                         //our main program, some fun are about to start)
  67. {
  68.  
  69.   if (esp8266.available())                                          //if there's any data received and stored in the serial receive buffer, go and excute the if-condition body. If not, dont excute the if-condition body at all.
  70.   {
  71.     if (esp8266.find("+IPD,"))                                      //search for the "+IPD," string in the incoming data. if it exists the ".find()" returns true and if not it returns false.
  72.     {
  73.       delay(1000);                                                  //wait 1 second to fill up the buffer with the data.
  74.       int connectionId = esp8266.read() - 48;                       //Subtract 48 because the read() function returns the ASCII decimal value. And 0 (the first decimal number) starts at 48. We use it to convert from ASCI decimal value to a character value.
  75.       esp8266.find("pin=");                                         //Advance the cursor to the "pin=" part in the request header to read the incoming bytes after the "pin=" part which is the pinNumer and it's state.
  76.       int pinNumber = (esp8266.read() - 48); //* 10;                   read the first Byte from the Arduino input buffer(i.e. if the pin 12 then the 1st number is 1) then multiply this number by 10. So, the final value of the "pinNumber" variable will be 10.
  77.       //pinNumber = pinNumber + (esp8266.read() - 48);                //read the second Byte from the Arduino input buffer(i.e. if the pin number is 12 then the 2nd number is 2) then add this number to the first number. So, the final value of the "pinNumber" variable will be 12.
  78.       int statusLed = (esp8266.read() - 48);                        //read the third byte from the Arduino input buffer. then save it inside the "statusLed" variable. At any case, it will be 1 or 0.
  79.  
  80.       display.fillScreen(BLACK);
  81.       delay(5);
  82.       display.setCursor(5, 15);
  83.       display.setTextColor(WHITE);
  84.       display.setTextSize(1);
  85.       display.print(pinNumber);
  86.  
  87.       display.setCursor(5, 30);
  88.       display.setTextColor(WHITE);
  89.       display.setTextSize(1);
  90.       display.print(statusLed);
  91.  
  92.       if (pinNumber == 5 && statusLed == 1) {
  93.         digitalWrite(greenLED, LOW);                        //turn the red LED off at the beginning of the program.
  94.         digitalWrite(redLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  95.         digitalWrite(blueLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  96.  
  97.       }
  98.  
  99.       else if (pinNumber == 6 && statusLed == 1) {
  100.         digitalWrite(greenLED, HIGH);                        //turn the red LED off at the beginning of the program.
  101.         digitalWrite(redLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  102.         digitalWrite(blueLED, LOW);                      //turn the blue LED on at the beginning of the program.
  103.       }
  104.  
  105.       else if (pinNumber == 7 && statusLed == 1) {
  106.         digitalWrite(greenLED, HIGH);                        //turn the red LED off at the beginning of the program.
  107.         digitalWrite(redLED, LOW);                      //turn the blue LED on at the beginning of the program.
  108.         digitalWrite(blueLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  109.  
  110.       } else {
  111.         digitalWrite(greenLED, HIGH);                        //turn the red LED off at the beginning of the program.
  112.         digitalWrite(redLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  113.         digitalWrite(blueLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  114.       }
  115.  
  116.       String closeCommand = "AT+CIPCLOSE=";                         //close the TCP/IP connection.
  117.       closeCommand += connectionId;                                 //append the "connectionId" value to the string.
  118.       closeCommand += "\r\n";                                       //append the "\r\n" to the string. it simulates the keyboard enter press.
  119.       sendData(closeCommand, 1000, DEBUG);                          //then send this command to the ESP8266 module to excute it.
  120.  
  121.     }
  122.   }
  123. }
  124.  
  125. /******************************************************************************************************************************************************************************************
  126.   Name: sendData
  127.   Description: this Function regulates how the AT Commands will ge sent to the ESP8266.
  128.  
  129.   Params: command - the AT Command to send
  130.                   - timeout - the time to wait for a response
  131.                   - debug - print to Serial window?(true = yes, false = no)
  132.  
  133.   Returns: The response from the esp8266 (if there is a reponse)
  134. */
  135. String sendData(String command, const int timeout, boolean debug)
  136. {
  137.   String response = "";                                             //initialize a String variable named "response". we will use it later.
  138.  
  139.   esp8266.print(command);                                           //send the AT command to the esp8266 (from ARDUINO to ESP8266).
  140.   long int time = millis();                                         //get the operating time at this specific moment and save it inside the "time" variable.
  141.   while ( (time + timeout) > millis())                              //excute only whitin 1 second.
  142.   {
  143.     while (esp8266.available())                                     //is there any response came from the ESP8266 and saved in the Arduino input buffer?
  144.     {
  145.       char c = esp8266.read();                                      //if yes, read the next character from the input buffer and save it in the "response" String variable.
  146.       response += c;                                                //append the next character to the response variabl. at the end we will get a string(array of characters) contains the response.
  147.     }
  148.   }
  149.   if (debug)                                                        //if the "debug" variable value is TRUE, print the response on the Serial monitor.
  150.   {
  151.     Serial.print(response);
  152.   }
  153.   return response;                                                  //return the String response.
  154. }
  155.  
  156.  
  157.  
  158. /******************************************************************************************************************************************************************************************
  159.   Name: InitWifiModule
  160.   Description: this Function gives the commands that we need to send to the sendData() function to send it.
  161.  
  162.   Params: Nothing.
  163.  
  164.   Returns: Nothing (void).
  165. */
  166. void InitWifiModule()
  167. {
  168.   sendData("AT+RST\r\n", 2000, DEBUG);                                                  //reset the ESP8266 module.
  169.   delay(1000);
  170.   sendData("AT+CWJAP=\"Ahmed\",\"*10003000#\"\r\n", 2000, DEBUG);        //connect to the WiFi network.
  171.   delay (3000);
  172.   sendData("AT+CWMODE=1\r\n", 1500, DEBUG);                                             //set the ESP8266 WiFi mode to station mode.
  173.   delay (1500);
  174.   //sendData("AT+CIFSR\r\n", 1500, DEBUG);                                                //Show IP Address, and the MAC Address.
  175.   //delay (1500);
  176.   sendData("AT+CIPMUX=1\r\n", 1500, DEBUG);                                             //Multiple conections.
  177.   delay (1500);
  178.   sendData("AT+CIPSERVER=1,80\r\n", 1500, DEBUG);                                       //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
  179. }
Advertisement
Add Comment
Please, Sign In to add comment