-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud.cpp
More file actions
81 lines (67 loc) · 2.05 KB
/
Copy pathcloud.cpp
File metadata and controls
81 lines (67 loc) · 2.05 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 <MakestroCloudClient32.h>
#include "WiFi.h"
#define use_ESPectro32 1
#if use_ESPectro32
#include <ESPectro32_Board.h>
#endif
const int ledPin = 15;
const int sendPeriod = 1000;
unsigned long oldTime = 0;
const char* SSID = "Private";
const char* Password = "SleepingBag95";
const char* username = "bintangthunder";
const char* token = "2G2AigJyv3wUhXwqYQ8CzZoXfeYcYYL4QC66kSwVGpUMee3CucJ39D1yLuWWLowV";
const char* project = "Testing";
const char* deviceId = "bintang-device";
const char* topic = "bintangthunder/Testing/data";
const char* subsTopic = "bintangthunder/Testing/control";
MakestroCloudClient32 makestro;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Message arrived");
char msg = (char)payload[9];
if (msg == '1') {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
}
void setup(){
Serial.begin(115200);
#if use_ESPectro32
ESPectro32.begin();
#endif
// If too long to connect, uncomment line below, then reupload
// WiFi.disconnect();
delay(100);
pinMode(ledPin, OUTPUT);
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());
delay(3000);
// MQTT initializing..
makestro.connect(username, token, project, deviceId);
makestro.subscribe(subsTopic);
makestro.data()->setCallback(callback);
delay(1000);
}
void loop(){
if(!makestro.connected()){
makestro.connect(username, token, project, deviceId);
makestro.subscribe(subsTopic);
}
makestro.loop();
if(millis() - oldTime > sendPeriod){
oldTime = millis();
int phototr = ESPectro32.readPhotoTransistorValue();
Serial.print("phototr: ");
Serial.println(phototr);
makestro.publishKeyValue(topic, "sensor", phototr);
Serial.println("publish to cloud");
}
}