Controlling Esp12E NodeMCU with LED from website using thingspeak from anywhere in the world(Part-1)
//Controlling Esp12E NodeMCU with LED from website using thingspeak from anywhere in the world(Part-1)
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
//Replace your wifi credentials here
//const char* ssid = "your SSID or hotspot or wifi name";
//const char* password = "password of hotspot or wifi or ssid";
const char* ssid = "PTS-2F";
const char* password = "";
//change your channel number here
unsigned long channel = 515184;
//1,2 and 3 are channel fields. You don't need to change if you are following this tutorial. However, you can modify it according to your application
unsigned int led1 = 1;
unsigned int led2 = 2;
unsigned int led3 = 3;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
for (int i=0; i <= 2; i++){
digitalWrite(D1, 1);
digitalWrite(D2, 1);
digitalWrite(D3, 1);
delay (500);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
delay (500);
}
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
ThingSpeak.begin(client);
}
void loop() {
//get the last data of the fields
int led_1 = ThingSpeak.readFloatField(channel, led1);
int led_2 = ThingSpeak.readFloatField(channel, led2);
int led_3 = ThingSpeak.readFloatField(channel, led3);
if(led_1 == 1){
digitalWrite(D1, 1);
Serial.println("D1 is On..!");
}
else if(led_1 == 0){
digitalWrite(D1, 0);
Serial.println("D1 is Off..!");
}
if(led_2 == 1){
digitalWrite(D2, 1);
Serial.println("D2 is On..!");
}
else if(led_2 == 0){
digitalWrite(D2, 0);
Serial.println("D2 is Off..!");
}
if(led_3 == 1){
digitalWrite(D3, 1);
Serial.println("D3 is On..!");
}
else if(led_3 == 0){
digitalWrite(D3, 0);
Serial.println("D3 is Off..!");
}
Serial.println(led_1);
Serial.println(led_2);
Serial.println(led_3);
delay(5000);
}
沒有留言:
張貼留言