{ On to Some Coding }

In the last tutorial we made a scenario by using an E-mail service in PushingBox. In this tutorial we will be merging the Arduino with the scenario created.

The device ID that I mentioned as important will be used here within the Arduino code. 

Let's get Started!

You will need to gather the following components.

  • Arduino UNO
  • ESP8266 Wi-Fi Module ESP-01
  • Breadboard
  • Male/Female jumper wires

First, you will have to visit my GitHub repository and download it. 



Extract the “Adafruit_ESP8266.h” library folder to the Arduino library and open up the file “code.ino” with the Arduino IDE and keep it ready. We’ll look into the main parts of the code after getting the wiring right.

Connect the pins marked in the diagram with the Arduino as given in the below diagram.




             RX to Digital Pin 9
             TX to Digital Pin 10
             RST to Digital Pin 8
             CH_ and VCC to 3.3V (DO NOT use 5V)
             GPIO0 and Ground to Ground

Alright. We’re done with the set up. 

Now let’s get into some coding. Now that you have my code ready as a sketch, you have to change only a few things in it.Here we go!




You will have to change the pin numbers 9, 10 and 8 if you have done the connections differently. If you have followed the same Arduino setup as mine you can leave it as it is.



You will have to change ESP_SSID and ESP_PASS values given within double quotations to your Wi-Fi network name and password respectively.

Our Host is PushingBox and Standard HTTP port is 80. You can leave it as it is.

Next the value we noticed in the PushingBox tutorial gets used in the code.




In the first line of the above piece of code, the value which is highlighted has to be changed to the value which you copied and saved in a text file in earlier tutorial.

Verify the code by clicking the compile icon and make sure that there are no compilation errors.


Connect the Arduino to your PC via the USB cable, Open up the Arduino IDE, Go to Tools, Select your board and the correct Port (Eg:COM4 ). 

After you’ve compiled without errors and you’ve gotten the values right on the Serial Monitor, upload the code!


(When you have compiled the code and started running it on the serial monitor if you see some weird values instead of what you’ve coded to be printed on the SM, Try changing the baud rate until the expected output is recieved. Make Sure you've selected Both NL & CR)



You can see the progress of the code through the serial monitor. When the Code is executed you will receive an email as expected.

Got confused with the Code?


These simple stuff might be helpful in figuring out complex parts of the code. :D

                            
  • In the first line, an Instance of SoftwareSerial is made and to enable communication, we call softser.begin() in our code.
  • For character and binary based systems, the base class is Stream. It is only called whenever a function that relies on it is used.
  • Here, Stream is the pointer’s base type and esp is the name of the pointer variable.
  • Asterisk mark (*) given after the class name, is the C++ notation for a pointer.
  • Object of type Adafruit_ESP8266 named “wifi” is made. This will be used later to call different methods of Adafruit_Esp8266 class. 
         Eg:  wifi.connectToAP(F(ESP_SSID), F(ESP_PASS))

 Forward declarations


A Forward Declaration can be defined as a declaration of an identifier such as a variable or a function which has no complete definition given by the programmer yet.

It is previously declared. Implementation will happen later.

Forward Declarations doesn’t seem to be very important right now, but it makes the compiler perform a better validation of the code and makes it a more sophisticated code.


When it comes to complex programs, these might be helpful in reducing build time as well.

Anonymous Enum


If an Enum is denfined in a class, it will have a scope and abilities of the specific class. It can be called by the class’ name.

AT commands

Following will be the AT commands used by us in our code. But there are many other AT commands introduced in order to perform certain functions.

room-15.github.io/blog/2015/03/26/esp8266-at-command-reference/

toCharArray

This method will copy the given String’s characters (in the given example àcmd’s characters) to the buffer.

Parameters passed will be,

  • The buffer which we copy the characters into (char []) : char replybuffer[REPLYBUFFSIZ]
  • Size of the buffer (unsigned int) : #define REPLYBUFFSIZ 255

     Eg: cmd.toCharArray(replybuffer, REPLYBUFFSIZ)

Other syntaxes

uint32_t (Unsigned): Integer type with a width of exactly 32 bits.

 uint16_t (Unsigned), int16_t (Signed): Integer type with a width of exactly 16 bits.




In the next tutorial we will make small changes in this code, so that instead of sending an email via PushingBox we will be updating a Google form with a text response. 


Comments

Popular Posts