How to upload data to web using EPS8266 nodemcu with source code

Опубликовано: 16 Октябрь 2024
на канале: Amal Anjula
563
7

#nodemcu #ESP8266 #thingspeak

You can upload your data such like temperature , humidity, Lux level to internet. then you can access that data anywhere if you have internet connection. https://thingspeak.com/ will handle your sensor node data and crate nice live graph.you can customize many of thing in the graph.


#include "ESP8266WiFi.h"
char* ssid = "asd@123ok";
char* password = "mypassword";
char server[] = "api.thingspeak.com";
int valueToUpload = 0;

void setup() {

// put your setup code here, to run once:

Serial.begin(115200);

Serial.printf("WiFi connecting to %s", ssid);

while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Con ok");



}
void loop() {
// put your main code here, to run repeatedly:
valueToUpload = random(10, 99);

Serial.printf("Now uploading %d\n", valueToUpload);

WiFiClient client;

Serial.printf("Now connecting to %s\n", server);

if (client.connect(server, 80)) {

client.print("GET /update?api_key=PYX8K07F9B1D0QOQ&field1=");

client.print(valueToUpload);

client.println(" HTTP/1.1");

client.print("Host: ");

client.print(server);

client.println("Connection: close");

client.println();



}

else {

Serial.println("Connection fail");



}

while (client.connected()) {

if (client.available()) {

char cc = client.read();

Serial.print(cc);

}

}

client.stop();

delay(5000);

}