2021ucs1566 Lab5
2021ucs1566 Lab5
Exercise – 5
Nimish Sikri – 2021UCS1566
AIM:
Transmit any random data from your microcontroller to ThingSpeak Cloud.
COMPONENTS REQUIRED:
1. Arduino Uno R3
2. ESP8266 Wifi Module
3. Connecting wires
4. 10K Ohm resistor
CIRCUIT DIAGRAM:
OUTPUT :
CODE:
String ssid = "Simulator Wifi"; // SSID to connect to
String password = "";
String host = "api.thingspeak.com";
const int httpPort = 80;
String uri = "/update?api_key=M32TGZ8MWUZ9AFGP&field1=";
int setupESP8266(void) {
// Start our ESP8266 Serial Communication
Serial.begin(115200); // Serial connection over USB to computer
Serial.println("AT"); // Serial connection on Tx / Rx port to ESP8266
delay(10); // Wait a little for the ESP to respond
if (!Serial.find("OK")) return 1;
return 0;
}
void anydata(void) {
UNDERSTANDING:
ThingSpeak is an Internet of Things (IoT) platform that allows users to collect, store, analyze,
visualize, and share data from various IoT devices.
1) ESP8266 Setup Function:
The setupESP8266() function initializes communication with the ESP8266 Wi-Fi module. It starts the serial
communication between the Arduino and the ESP8266, sends an "AT" command to check if the ESP8266 is
responding, connects to a Wi-Fi network specified by the ssid and password variables, and establishes a TCP
connection to the host (api.thingspeak.com) on port 80. The function returns 0 if the setup is successful, and
non-zero values indicate potential issues during setup.
2) Data Transmission Function (anydata):
The anydata() function generates a random number (randomNUM) between -40 and 125. It then constructs an
HTTP GET request (httpPacket) with the random number appended to the specified URI (/) and sends it to the
host (api.thingspeak.com). The function uses AT commands to inform the ESP8266 about the length of the
message to be sent (AT+CIPSEND=), sends the HTTP request, and waits for a response indicating successful
transmission (SEND OK). This simulates the transmission of data to a server, potentially for logging or monitoring
purposes.
3) Main Loop:
The setup() function calls the setupESP8266() function to initialize the ESP8266 module. In the loop() function,
the anydata() function is called to simulate sending random data to the specified host at intervals of 10 seconds
(delay(10000)). This loop continues indefinitely, demonstrating a simple periodic data transmission scenario with
the ESP8266.
RESULT: Successfully interfaced ESP8266 Wi-Fi module with an Arduino board and sent
random number to thing speak cloud