-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
81 lines (66 loc) · 1.6 KB
/
Code
File metadata and controls
81 lines (66 loc) · 1.6 KB
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
#include <Wire.h>
#include <BH1750.h>
#include <WiFiNINA.h>
//WiFi credentials
char ssid[] = "LIAM";
char pass[] = "HOTSPOTHOTSHOT";
//IFTTT Webhooks configuration
char HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME = "https://maker.ifttt.com/trigger/light_sensor/with/key/b13Vx0B-E3zQxq32yDgVQ_";
//Updated Webhooks URL
String query = "?value1=";
//Query parameters for light readings
WiFiClient client;
BH1750 lightMeter;
void setup()
{
Serial.begin(9600);
while (!Serial);
//Initialize the I2C bus and BH1750 sensor
Wire.begin();
lightMeter.begin();
//Connect to WiFi
WiFi.begin(ssid, pass);
while(WiFi.status()!= WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("The Wifi has connected");
//Connect to IFTTT server
if(client.connect(HOST_NAME,80))
{
Serial.println("Connected to the IFTTT server");
}
else
{
Serial.println("The connection to the IFTTT server failed");
}
}
void loop()
{
//Read light level from BH1750 sensor
float reading = lightMeter.readLightLevel();
Serial.print("Light reading: ");
Serial.print(reading);
Serial.println("lx");
//Send HTTP request to IFTTT with light readings
sendIFTTT(PATH_NAME + query + String(reading));
//Send readings every second
delay(1000);
}
void sendIFTTT(String path)
{
//Send HTTP request to IFTTT Webhooks
if(client.connected())
{
client.println("GET " + path + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println();
Serial.println("Data has been transmitted.");
}
else
{
Serial.println("The data has not been sent.");
}
}