In order to connect our Things to Internet, we need select some Internet communication protocol so that different internet device can talk with each other. Some commonly used IoT protocols are HTTP, MQTT etc.
In this lesson, we will learn how to use HTTP protocol and how to make simple HTTP web server with Osoyoo MEGA-IoT shield and MEGA2560 micro-controller and show a “Hello World!” web page to remote browser . The web page also shows the input voltage of A0 pin which is detected by Arduino analogRead() functions.
1).please plug OSOYOO MEGA-IoT Extension Board into MEGA2560 board:
2)Use jumper Cap to connect ESP8266 RX with A8, TX with A9)
|Download Arduino IDE here:
|7 zip is a free zip
utility that un-zips zip files
|Download 7zip here for free
Step 1 Install latest Arduino IDE (If you have Arduino IDE version after 1.1.16, please skip this step).
Download Arduino IDE from https://www.arduino.cc/en/Main/Software?setlang=en, then install the software.
Step 2 OSOYOO MEGA-IoT extension TX/RX pin to Arduino A9/A8 pin by default. So in Arduino sketch code, we need use Software Serial Port to communicate with ESP8266 (set A9 as TX and A8 as RX in softwareserial object).we need download WiFiEsp-master library from following link firstly: https://osoyoo.com/driver/WiFiEsp-master.zip
Step3 Open Arduino IDE ->Sketch ->Include Library ->Add ,Zip library to load above zip files into Arduino IDE.
Step 4 After installing above library, please download the main code from following link.unzip the download zip file lesson3.zip, you will see a folder called smarthome-lesson3.
Step 5 After above operations are completed, connect OSOYOO MEGA2560 Board to PC with USB cable.
Step 6 Open the Arduino IDE and choose corresponding board type and port type for you project.
Step 7 Arduino IDE: Click file -> click Open -> choose code “smarthome-lesson3”, load up the sketch onto your Arduino.
Note: In the sketch, find following lines:
char ssid = "******"; // your network SSID (name) char pass = "******"; // your network password
please replace the ****** with your correct wifi SSID and password, otherwise your project can not connect to Internet.
After loading the sketch to Arduino , open the serial monitor in the upper-right corner of Arduino IDE, you will see following result:
From the serial monitor , you can see the IP address of your MEGA2560 board in the read circle (in above picture, 192.168.50.102).
Then use your browser to visit the website http://mega2560-ip-address (in above case, http://192.168.50.102), you will see following result:
Above result also shows the A0 pin input voltage through analogRead(0) function in line 94. analogRead() can read the input 0-5V voltage and return a value between 0 to 1023 standing for the voltage.
If we connect A0 to GND via jumper wire, Analog input A0: shows 0
If we connect A0 to 5V via jumper wire , Analog input A0: shows 1023.
Sometimes when running lesson3 project, ESP8266 might have initialization problem with MEGA2560 A8,A9 pins and this cause WifiESP initializing error showing in Serial monitor as following:
[WiFiEsp] Initializing ESP module [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] Cannot initialize ESP module
To solve the problem, we need use Stable pin D10,D11 to initialize ESP8266 and then switch back to A8,A9.
To do this, please do as per following steps:
Original line 15 is :
SoftwareSerial softserial(A9, A8); // RX, TXPlease change above line to:
SoftwareSerial softserial(10, 11); // RX, TX
Step 3) Load the sketch to Arduino and the Initializing normally will pass through and show
[WiFiEsp] Initilization successful - 1.5.4 SSI
DownLoad Url osoyoo.com