@@ -13,13 +13,35 @@ unsigned long getTime() {
13
13
return WiFi.getTime ();
14
14
}
15
15
16
+ // Thing properties
16
17
int position;
18
+ bool valid = true ;
19
+ float ratio = 2.47 ;
20
+ String welcome = " ciao" ;
21
+
22
+ // Last time when the WiFi connection was checked
23
+ unsigned long lastMillis = 0 ;
17
24
18
25
void onPositionUpdate () {
19
26
Serial.print (" New position value: " );
20
27
Serial.println (position);
21
28
}
22
29
30
+ void onWelcomeUpdate () {
31
+ Serial.print (" New state value: " );
32
+ Serial.println (welcome);
33
+ }
34
+
35
+ void onValidUpdate () {
36
+ Serial.print (" New valid value: " );
37
+ Serial.println (valid);
38
+ }
39
+
40
+ void onRatioUpdate () {
41
+ Serial.print (" New ratio value: " );
42
+ Serial.println (ratio);
43
+ }
44
+
23
45
void setup () {
24
46
// Initialize serial and wait for port to open:
25
47
Serial.begin (9600 );
@@ -47,7 +69,7 @@ void setup() {
47
69
status = WiFi.begin (ssid, pass);
48
70
49
71
// wait 10 seconds for connection:
50
- delay (10000 );
72
+ delay (5000 );
51
73
}
52
74
53
75
// you're connected now, so print out the data:
@@ -63,21 +85,45 @@ void setup() {
63
85
}
64
86
65
87
Serial.println (" Successfully connected to Arduino Cloud :)" );
66
-
67
- ArduinoCloud.addProperty (position, READ, 10 *SECONDS, onPositionUpdate);
88
+ ArduinoCloud.addProperty (welcome, READWRITE, ON_CHANGE, onWelcomeUpdate);
89
+ ArduinoCloud.addProperty (position, READWRITE, ON_CHANGE, onPositionUpdate);
90
+ ArduinoCloud.addProperty (valid, READWRITE, ON_CHANGE, onValidUpdate);
91
+ ArduinoCloud.addProperty (ratio, READWRITE, ON_CHANGE, onRatioUpdate);
68
92
69
93
CloudSerial.begin (9600 );
94
+ lastMillis = millis ();
70
95
}
71
96
72
97
void loop () {
73
98
ArduinoCloud.poll ();
74
-
75
- if (CloudSerial.available ()) {
76
- Serial.write (CloudSerial.read ());
99
+ Serial.println (" loop updated" );
100
+ /*
101
+ Serial.println(".");
102
+ welcome += "!";
103
+ ratio += 0.4355;
104
+ valid = !valid;
105
+ position += 1;
106
+ */
107
+ if (millis () - lastMillis > 20000 ) {
108
+ Serial.println (" ..Check WiFi status.." );
109
+ bool error = false ;
110
+ // Check Wifi status
111
+ while (WiFi.status () != WL_CONNECTED) {
112
+ error = true ;
113
+ Serial.print (" ..Reconnection to connect to WPA SSID: " );
114
+ Serial.println (ssid);
115
+ status = WiFi.begin (ssid, pass);
116
+ // wait 10 seconds for connection:
117
+ delay (2000 );
118
+ }
119
+ if (error) {
120
+ Serial.println (" ..Reconnected to the Nework!" );
121
+ // Call the reconnect method to clean up the ArduinoCloud connection
122
+ ArduinoCloud.reconnect (wifiClient);
123
+ }
124
+ delay (500 );
125
+ lastMillis = millis ();
77
126
}
78
127
79
- if (Serial.available ()) {
80
- CloudSerial.write (Serial.read ());
81
- }
128
+ delay (2000 );
82
129
}
83
-
0 commit comments