0% found this document useful (0 votes)
22 views3 pages

2021ucs1566 Lab5

The document describes transmitting random data from an Arduino to the ThingSpeak cloud platform using an ESP8266 WiFi module. It includes: 1) A setupESP8266() function to initialize the ESP8266 module and connect to WiFi. 2) An anydata() function that generates a random number and sends an HTTP request with the number to ThingSpeak. 3) A main loop that calls anydata() every 10 seconds to periodically transmit data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

2021ucs1566 Lab5

The document describes transmitting random data from an Arduino to the ThingSpeak cloud platform using an ESP8266 WiFi module. It includes: 1) A setupESP8266() function to initialize the ESP8266 module and connect to WiFi. 2) An anydata() function that generates a random number and sends an HTTP request with the number to ThingSpeak. 3) A main loop that calls anydata() every 10 seconds to periodically transmit data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COCSC20: Internet of Things

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;

// Connect to 123D Circuits Simulator Wifi


Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
delay(10); // Wait a little for the ESP to respond
if (!Serial.find("OK")) return 2;

// Open TCP connection to the host:


Serial.println("AT+CIPSTART=\"TCP\",\"" + host + "\"," + httpPort);
delay(50); // Wait a little for the ESP to respond
if (!Serial.find("OK")) return 3;

return 0;
}

void anydata(void) {

// Generate a random number between -40 and 125


int randomNUM = random(-40, 126);

// Construct our HTTP call


String httpPacket = "GET " + uri + String(randomNUM) + " HTTP/1.1\r\nHost: "
+ host + "\r\n\r\n";
int length = httpPacket.length();

// Send our message length


Serial.print("AT+CIPSEND=");
Serial.println(length);
delay(10); // Wait a little for the ESP to respond if (!Serial.find(">"))
return -1;
// Send our http request
Serial.print(httpPacket);
delay(10); // Wait a little for the ESP to respond
if (!Serial.find("SEND OK\r\n")) return;
}
void setup() {
setupESP8266();
}
void loop() {
anydata();
delay(10000);
}

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

You might also like