-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtask0.ino
137 lines (106 loc) · 4.02 KB
/
task0.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//#include <SPI.h>
//#include <Ethernet.h>
//const char* ssid = "LINKDSL-josephhalim";
//const char* password = "23121973janet";
//const char* ssid = "BME Students Lab 1 (2.4)";
const char* ssid = "BME Students (2.4)";
const char* password = "mybmewifi";
//const char ser[] = "192.168.1.2";
ESP8266WebServer server(80);
WiFiClient client;
MDNSResponder mdns; //multicast Domain Name System
float humidity, temp_f; // Values read from sensor
String webPage = "";
int gpio0_pin = 13;
int gpio2_pin = 14;
int f =0;
// Generally, you should use "unsigned long" for variables that hold time
//unsigned long previousMillis = 0; // will store last temp was read
//const long interval = 2000; // interval at which to read sensor
void setup(void)
{
webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 <a href=\"socket1On\"><button>ON</button></a> <a href=\"socket1Off\"><button>OFF</button></a></p>";
webPage += "<p>Socket #2 <a href=\"socket2On\"><button>ON</button></a> <a href=\"socket2Off\"><button>OFF</button></a></p>";
// preparing GPIOs
pinMode(gpio0_pin, OUTPUT);
digitalWrite(gpio0_pin, LOW);
pinMode(gpio2_pin, OUTPUT);
digitalWrite(gpio2_pin, LOW);
Serial.begin(115200); //baude rate .... Serial connection from ESP-01 via 3.3v console cable
//dht.begin(); // initialize temperature sensor
delay(1000);
Serial.println("Hii ");
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", [](){
server.send(200, "text/html", webPage);
});
server.on("/socket1On", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio0_pin, HIGH);
delay(1000);
});
server.on("/socket1Off", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio0_pin, LOW);
delay(1000);
});
server.on("/socket2On", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio2_pin, HIGH);
delay(1000);
});
server.on("/socket2Off", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio2_pin, LOW);
delay(1000);
});
server.begin();
Serial.println("HTTP server started");
delay(10000);
}
void loop(void)
{
// Wait a few seconds between measurements.
delay(100);
f = f+1 ;
temp_f = analogRead(A0);
Serial.println( String(temp_f)+", 1");
server.handleClient();
if ( f <3 ){Serial.println("\nStarting connection to server..."); }
if (client.connect("192.168.0.106", 80)) {
if ( f <3 ){Serial.println("connected to server");}
client.print("GET /write_data.php?"); // This
client.print("value="); // This
client.print(temp_f); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Host: 127.0.0.1"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here
client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
//delay(10000);
}