-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniproj.ino
151 lines (130 loc) · 3.14 KB
/
miniproj.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "winnone";
const char* password = "winnone12";
const char* urlroom1 = "https://ecourse.cpe.ku.ac.th/exceed49/api/payload?gid=11";
//const char* urlroom2 = "https://ecourse.cpe.ku.ac.th/exceed49/api/payload?gid=11";
//const char* urlroom3 = "https://ecourse.cpe.ku.ac.th/exceed49/api/payload?gid=11";
const int sizee = 2*JSON_OBJECT_SIZE(4);
int sw=22;
int ldr1=34,ldr2=33,ldr3=32;
int led1=5,led2=18,led3=21;
int used1=0,used2=0,used3=0;
char str[50];
StaticJsonDocument<sizee> JSONPost;
void wificonnect(){
WiFi.disconnect();
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to the WiFi network");
}
void _post (const char* url,int led1){
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type","application/json");
JSONPost["ldr"]=0;
JSONPost["led1"]=led1;
JSONPost["led2"]=0;
serializeJson(JSONPost,str);
int httpCode = http.POST(str);
if(httpCode == HTTP_CODE_OK){
String payload = http.getString();
Serial.print("POST ");
Serial.println(httpCode);
Serial.println(payload);
}
else
wificonnect();
}
delay(100);
}
void setup() {
Serial.begin(115200);
delay(2000);
wificonnect();
pinMode(sw,INPUT_PULLUP);
pinMode(led1,OUTPUT);
digitalWrite(led1,HIGH);
pinMode(led2,OUTPUT);
digitalWrite(led2,LOW);
pinMode(led3,OUTPUT);
digitalWrite(led3,LOW);
pinMode(ldr1,INPUT);
pinMode(ldr2,INPUT);
pinMode(ldr3,INPUT);
xTaskCreatePinnedToCore(Task2,"Task 2",1024*32,NULL,1,NULL,0);
xTaskCreatePinnedToCore(Task3,"Task 3",1024*32,NULL,1,NULL,0);
}
void loop() {
if(analogRead(ldr1)>1000){
if(used1==0){
digitalWrite(led1,LOW);
_post(urlroom1,1);
used1=1;
}
}
else{
if(used1==1){
digitalWrite(led1,HIGH);
_post(urlroom1,0);
used1=0;
}
}
vTaskDelay(30/portTICK_PERIOD_MS);
}
void Task2 (void *parameter){
while(1){
/*if(analogRead(ldr2)>1000){
_post(urlroom1,1);
while(1){
digitalWrite(led2,HIGH);
if(analogRead(ldr2)<1000){
digitalWrite(led2,LOW);
_post(urlroom1,0);
break;
}
}
}
vTaskDelay(30/portTICK_PERIOD_MS);
}*/
if(analogRead(ldr2)>1000){
if(used2==0){
digitalWrite(led2,HIGH);
//_post(urlroom2,1);
used2=1;
}
}
else{
if(used2==1){
digitalWrite(led2,LOW);
// _post(urlroom2,0);
used2=0;
}
}
vTaskDelay(30/portTICK_PERIOD_MS);
}
}
void Task3 (void *parameter){
while(1){
if(analogRead(ldr3)>1000){
if(used3==0){
digitalWrite(led3,HIGH);
//_post(urlroom3,1);
used3=1;
}
}
else{
if(used3==1){
digitalWrite(led3,LOW);
// _post(urlroom2,0);
used3=0;
}
}
vTaskDelay(30/portTICK_PERIOD_MS);
}
}