Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.    Client Board code V1.0
  3.    192.168.1.6: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. SoftwareSerial esp8266(2, 3);                       //set the Rx ==> Pin 2; TX ==> Pin3.
  8.  
  9. #define serialCommunicationSpeed 9600               // <========= define a constant named "serialCommunicationSpeed" with a value 9600. it referes to the Software and hardware serial communication speed(baud rate).
  10. #define DEBUG true                                  //make a constant named "DEBUG" and it's value true. we will use it later.
  11.  
  12. 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.
  13. 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.
  14. 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.
  15.  
  16. #define temperatureInput1 A1
  17. #define temperatureInput2 A2
  18. int temperatureResult = 0;
  19.  
  20. void setup()
  21.  
  22. {
  23.   delay(20000);
  24.  
  25.   pinMode(redLED, OUTPUT);                          //set the pin number 5 as an output pin.
  26.   pinMode(blueLED, OUTPUT);                         //set the pin number 6 as an output pin.
  27.   pinMode(greenLED, OUTPUT);                         //set the pin number 7 as an output pin.
  28.  
  29.   pinMode(temperatureInput1, INPUT);                //set the pin number A1 as an output pin.
  30.   pinMode(temperatureInput2, INPUT);                //set the pin number A2 as an output pin.
  31.  
  32.   Serial.begin(serialCommunicationSpeed);           //begin the Hardware serial communication (0, 1) at speed 9600.
  33.   esp8266.begin(serialCommunicationSpeed);          //begin the software serial communication (2, 3) at speed 9600.
  34.  
  35.   LEDBlinking(200);
  36.  
  37.   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).
  38.  
  39.   LEDBlinking(200);
  40.   LEDBlinking(200);
  41.   LEDBlinking(200);
  42. }
  43.  
  44. void loop()                                                         //our main program, some fun are about to start)
  45. {
  46.   temperatureResult = analogRead(temperatureInput2) - analogRead(temperatureInput1);      //read the A1 input, A2 input. then subtract A1 reading from A2 reading. that gives you the temperature value.
  47.  
  48.   LEDBlinking(200);
  49.   sendCommands(String(temperatureResult));          //convert from Int ro String. Then send the temperature value to the Server board.
  50.   delay(10000);                                      //wait 5 seconds between each data being sent.
  51.  
  52.   /*sendCommands("61");
  53.     delay(5000);
  54.     sendCommands("61");
  55.     delay(5000);
  56.   */
  57. }
  58.  
  59. /******************************************************************************************************************************************************************************************
  60.   Name: sendData
  61.   Description: this Function regulates how the AT Commands will ge sent to the ESP8266.
  62.  
  63.   Params: command - the AT Command to send
  64.                   - timeout - the time to wait for a response
  65.                   - debug - print to Serial window?(true = yes, false = no)
  66.  
  67.   Returns: The response from the esp8266 (if there is a reponse)
  68. */
  69. String sendData(String command, const int timeout, boolean debug)
  70. {
  71.   String response = "";                                             //initialize a String variable named "response". we will use it later.
  72.  
  73.   esp8266.print(command);                                           //send the AT command to the esp8266 (from ARDUINO to ESP8266).
  74.   long int time = millis();                                         //get the operating time at this specific moment and save it inside the "time" variable.
  75.   while ( (time + timeout) > millis())                              //excute only whitin 1 second.
  76.   {
  77.     while (esp8266.available())                                     //is there any response came from the ESP8266 and saved in the Arduino input buffer?
  78.     {
  79.       char c = esp8266.read();                                      //if yes, read the next character from the input buffer and save it in the "response" String variable.
  80.       response += c;                                                //append the next character to the response variabl. at the end we will get a string(array of characters) contains the response.
  81.     }
  82.   }
  83.   if (debug)                                                        //if the "debug" variable value is TRUE, print the response on the Serial monitor.
  84.   {
  85.     Serial.print(response);
  86.   }
  87.   return response;                                                  //return the String response.
  88. }
  89.  
  90. /******************************************************************************************************************************************************************************************
  91.   Name: InitWifiModule
  92.   Description: this Function gives the commands that we need to send to the sendData() function to send it.
  93.  
  94.   Params: Nothing.
  95.  
  96.   Returns: Nothing (void).
  97. */
  98. void InitWifiModule()
  99. {
  100.   sendData("AT+RST\r\n", 2000, DEBUG);                                                  //reset the ESP8266 module.
  101.   delay(1000);
  102.   sendData("AT+CWJAP=\"Ahmed\",\"*10003000#\"\r\n", 2000, DEBUG);        //connect to the WiFi network.
  103.   delay (4000);
  104.   sendData("AT+CWMODE=3\r\n", 1500, DEBUG);                                             //set the ESP8266 WiFi mode to station mode.
  105.   delay (2000);
  106.   sendData("AT+CIFSR\r\n", 1500, DEBUG);                                                //Show IP Address, and the MAC Address.
  107.   delay (1500);
  108. }
  109.  
  110. void sendCommands(String reading) {
  111.   sendData("AT+CIPSTART=\"TCP\",\"192.168.1.6\",80\r\n", 1500, DEBUG);                                             //Multiple conections.
  112.   delay (1500);
  113.   sendData("AT+CIPSEND=6\r\n", 1500, DEBUG);                                       //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
  114.   delay (1500);
  115.  
  116.   sendData("pin=", 1500, DEBUG);                                       //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
  117.   sendData(reading, 1500, DEBUG);                                       //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
  118.   sendData("\r", 1500, DEBUG);                                       //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
  119. }
  120.  
  121. void LEDBlinking(int delayTime) {
  122.   digitalWrite(greenLED, LOW);                        //turn the red LED off at the beginning of the program.
  123.   digitalWrite(redLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  124.   digitalWrite(blueLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  125.   delay(delayTime);
  126.   digitalWrite(greenLED, HIGH);                        //turn the red LED off at the beginning of the program.
  127.   digitalWrite(redLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  128.   digitalWrite(blueLED, HIGH);                      //turn the blue LED on at the beginning of the program.
  129.   delay(delayTime);
  130. }