iot
unknown
plain_text
a year ago
6.5 kB
6
Indexable
Never
// Week 6 // ESP8266WiFi for esp8266 - Version: Latest #include <ESP8266WiFi.h> #include <ThingSpeak.h> char ssid[] = "Hotspot Name"; char pass[] = "Hotspot Password"; int keyIndex = 0; WiFiClient client; unsigned long myChannelNumber = 000000; // Channel Number copied From Thing Speak Cloud const char* myWriteApiKey = "Write API Key From Thing Speak"; int number = 0; const int irPin = D5; void setup() { Serial.begin(115200); while (!Serial) { ; } WiFi.mode(WIFI_STA); ThingSpeak.begin(client); } void loop() { if (WiFi.status() != WL_CONNECTED) { Serial.print("Attempting to Connect to SSID: "); Serial.println(ssid); while (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, pass); Serial.print("."); delay(5000); } } Serial.print("\nConnected"); int x = ThingSpeak.writeField(myChannelNumber, 1, number, myWriteApiKey); if (x == 200) { Serial.println("Channel Update Successful."); } else { Serial.println("Problem updating Channel. HTTP error code" + String(x)); } number++; if (numeber > 99) { number = 0; } delay(2000); } ---------------------------------------------------------------------------------------------------------------------------- // Week 7 #include <ESP8266.h> #include "ThingSpeak.h" #include "DHT.h" #define DHTTYPE DHT11 #define DHTPIN D3 char ssid[] = ""; char pass[] = ""; DHT dht(DHTPIN, DHTTYPE); WiFiClient client; int keyIndex = 0; unsigned long myChannelId = 0000000; char* myWriteApiKey = ""; int number1 = 0; int number2 = random(0, 100); int number3 = random(0, 100); int number4 = random(0, 100); void setup() { dht.begin(); Serial.begin(115200); while(!Serial) { ; } WiFi.mode(WIFI_STA); ThingSpeak.begin(client); } void loop() { if (WiFi.status() != WL_CONNECTED) { Serial.print("Attempting to Connect to SSID: "); Serial.println(ssid); while (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, pass); Serial.print("."); delay(5000); } Serial.println("\nConnected"); } ThingSpeak.setField(1, number1); ThingSpeak.setField(2, number2); ThingSpeak.setField(3, number3); ThingSpeak.setField(4, number4); if (number1 > number2) { myStatus = String("Field1 is greater than Field2"); } else { Seral.print("Problem updating channel HTTP error Code" + String(x)); } number1++; if (number1 > 99) { number1 = 0; } number2 = random(0, 100); number3 = random(0, 100); number4 = random(0, 100); delay(2000); } ---------------------------------------------------------------------------------------------------------------------------- // Week 8 #include "ThingSpeak.h" #include <ESP8266WiFi.h> const char ssid[] = ""; const char pass[] = ""; int statusCode = 0; WiFiClient client; unsigned long counterChannelNumber = 000000; const char* myCounterReadApiKey = "key from Thing Speak"; const int fieldNumber1 = 1; const int fieldNumber2 = 2; void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); ThingSpeak.begin(client); } void loop() { // Connecting to Wifi if (WiFi.status() != WL_CONNECTED) { Serial.println("Connecting to: "); Serial.println(ssid); while (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, pass); delay(2000); } Serial.println("Connected!"); } // Reading Temperature From Week 7 long temp = ThingSpeak.readLongField(counterChannelNumber, fieldNumber1, myCounterReadApiKey); statusCode = ThingSpeak.getLastReadStatus(); if (statusCode == 200) { Serial.print("Temperature: "); Serial.println(temp); } else { Serial.println("Unable to Read Channel / No Internet Connection"); } // Reading Humidity From Week 7 long humidity = ThingSpeak.readLongField(counterChannelNumber, fieldNumber2, myCounterReadApiKey); statusCode = ThingSpeak.getLastReadStatus(); if (statusCode == 200) { Serial.print("Humidity: "); Serial.println(humidity); } else { Serial.println("Unable to Read Channel / No Internet Connection"); } delay(1000); } ---------------------------------------------------------------------------------------------------------------------------- // Week 9 #include <ESP8266WiFi.h> #include "DHT.h" const char* ssid = "wifiname" const char* pass = "password" WiFiServer wifiServer(9000); DHT dht(D4, DHT11); void setup() { Serial.begin(115200); dht.begin(); delay(1000); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.print("Connected to Wifi IP: "); Serial.print(WiFi.localIP()); wifiServer.begin(); } void loop() { WifiClient client = wifiServer.available(); if (client) { while(client.connected()) { while(client.available() > 0) { int h dht.readHumidity(); client.print("\nHumidity: "); client.println(h); delay(2000); } } client.stop(); Serial.println("Client Disconnected"); } } ---------------------------------------------------------------------------------------------------------------------------- // Week 10 #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include "DHT.h" #define WIFI_SSID "networkname" #define WIFI_PASS "password" #define UDP_PORT 4210 // D5: Pin && DHT11: DHT Type DHT dht(D5, DHT11); // UDP WiFiUDP UDP; char packet[255]; char reply[] = "Packet Recieved"; void setup() { Serial.begin(115200); dht.begin(); // Initializing DHT WiFi.begin(WIFI_SSID, WIFI_PASS); // Connecting to WIFI Serial.print("Connecting to"); Serial.print(WIFI_SSID); while (WiFi.status() != WL_CONNECTED) { // Waiting for WiFi to be connected delay(2000); Serial.print("."); } Serial.print("\nConnected! IP Address: "); Serial.print(WiFi.localIP()); // Printing the IP UDP.begin(UDP_PORT); // Listening in UDP Port Serial.print("\nListening on UDP Port: "); Serial.print(UDP_PORT); } void loop() { // 1. Reading Temperature and Humidity int h = dht.readHumidity(); int t = dht.readTemperature(); delay(2000); // 2. Writing On UDP Terminal App UDP.beginPacket(UDP.remoteIP(), UDP.remotePort()); UDP.write(reply); UDP.endPacket(); UDP.println(t); UDP.println(h); // 3. Writing on Serial Moniter Serial.print("\nHumidity: "); Serial.println(h); }