/*
Client Board code V1.0
192.168.1.6:80
*/
#include <SoftwareSerial.h> //including the SoftwareSerial library will allow you to use the pin no. 2,3 as Rx, Tx.
SoftwareSerial esp8266(2, 3); //set the Rx ==> Pin 2; TX ==> Pin3.
#define serialCommunicationSpeed 9600 // <========= define a constant named "serialCommunicationSpeed" with a value 9600. it referes to the Software and hardware serial communication speed(baud rate).
#define DEBUG true //make a constant named "DEBUG" and it's value true. we will use it later.
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.
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.
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.
#define temperatureInput1 A1
#define temperatureInput2 A2
int temperatureResult = 0;
void setup()
{
delay(20000);
pinMode(redLED, OUTPUT); //set the pin number 5 as an output pin.
pinMode(blueLED, OUTPUT); //set the pin number 6 as an output pin.
pinMode(greenLED, OUTPUT); //set the pin number 7 as an output pin.
pinMode(temperatureInput1, INPUT); //set the pin number A1 as an output pin.
pinMode(temperatureInput2, INPUT); //set the pin number A2 as an output pin.
Serial.begin(serialCommunicationSpeed); //begin the Hardware serial communication (0, 1) at speed 9600.
esp8266.begin(serialCommunicationSpeed); //begin the software serial communication (2, 3) at speed 9600.
LEDBlinking(200);
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).
LEDBlinking(200);
LEDBlinking(200);
LEDBlinking(200);
}
void loop() //our main program, some fun are about to start)
{
temperatureResult = analogRead(temperatureInput2) - analogRead(temperatureInput1); //read the A1 input, A2 input. then subtract A1 reading from A2 reading. that gives you the temperature value.
LEDBlinking(200);
sendCommands(String(temperatureResult)); //convert from Int ro String. Then send the temperature value to the Server board.
delay(10000); //wait 5 seconds between each data being sent.
/*sendCommands("61");
delay(5000);
sendCommands("61");
delay(5000);
*/
}
/******************************************************************************************************************************************************************************************
Name: sendData
Description: this Function regulates how the AT Commands will ge sent to the ESP8266.
Params: command - the AT Command to send
- timeout - the time to wait for a response
- debug - print to Serial window?(true = yes, false = no)
Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = ""; //initialize a String variable named "response". we will use it later.
esp8266.print(command); //send the AT command to the esp8266 (from ARDUINO to ESP8266).
long int time = millis(); //get the operating time at this specific moment and save it inside the "time" variable.
while ( (time + timeout) > millis()) //excute only whitin 1 second.
{
while (esp8266.available()) //is there any response came from the ESP8266 and saved in the Arduino input buffer?
{
char c = esp8266.read(); //if yes, read the next character from the input buffer and save it in the "response" String variable.
response += c; //append the next character to the response variabl. at the end we will get a string(array of characters) contains the response.
}
}
if (debug) //if the "debug" variable value is TRUE, print the response on the Serial monitor.
{
Serial.print(response);
}
return response; //return the String response.
}
/******************************************************************************************************************************************************************************************
Name: InitWifiModule
Description: this Function gives the commands that we need to send to the sendData() function to send it.
Params: Nothing.
Returns: Nothing (void).
*/
void InitWifiModule()
{
sendData("AT+RST\r\n", 2000, DEBUG); //reset the ESP8266 module.
delay(1000);
sendData("AT+CWJAP=\"Ahmed\",\"*10003000#\"\r\n", 2000, DEBUG); //connect to the WiFi network.
delay (4000);
sendData("AT+CWMODE=3\r\n", 1500, DEBUG); //set the ESP8266 WiFi mode to station mode.
delay (2000);
sendData("AT+CIFSR\r\n", 1500, DEBUG); //Show IP Address, and the MAC Address.
delay (1500);
}
void sendCommands(String reading) {
sendData("AT+CIPSTART=\"TCP\",\"192.168.1.6\",80\r\n", 1500, DEBUG); //Multiple conections.
delay (1500);
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.
delay (1500);
sendData("pin=", 1500, DEBUG); //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
sendData(reading, 1500, DEBUG); //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
sendData("\r", 1500, DEBUG); //start the communication at port 80, port 80 used to communicate with the web servers through the http requests.
}
void LEDBlinking(int delayTime) {
digitalWrite(greenLED, LOW); //turn the red LED off at the beginning of the program.
digitalWrite(redLED, HIGH); //turn the blue LED on at the beginning of the program.
digitalWrite(blueLED, HIGH); //turn the blue LED on at the beginning of the program.
delay(delayTime);
digitalWrite(greenLED, HIGH); //turn the red LED off at the beginning of the program.
digitalWrite(redLED, HIGH); //turn the blue LED on at the beginning of the program.
digitalWrite(blueLED, HIGH); //turn the blue LED on at the beginning of the program.
delay(delayTime);
}