-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature.ino
109 lines (94 loc) · 3.35 KB
/
temperature.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
//************************************************************
// this is a simple example that uses the painlessMesh library to
// setup a node that logs to a central logging node
// The logServer example shows how to configure the central logging nodes
//************************************************************
#include "painlessMesh.h"
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
Scheduler userScheduler; // to control your personal task
painlessMesh mesh;
float vref = 3.3;
float resolution = vref/1023;
float moisture_percentage;
const int sensor_pin = A0; /* Connect Soil moisture analog sensor pin to A0 of NodeMCU */
// Prototype
void receivedCallback( uint32_t from, String &msg );
void Calculatetemperature();
size_t logServerId = 0;
Task taskCalculatetemperature( TASK_SECOND * 1 , TASK_FOREVER, &Calculatetemperature );
float temperature;
void Calculatetemperature(){
temperature = analogRead(A0);
temperature = (temperature*resolution);
temperature = temperature*100;
}
// Send message to the logServer every 10 seconds
Task myLoggingTask(10000, TASK_FOREVER, []() {
#if ARDUINOJSON_VERSION_MAJOR==6
DynamicJsonDocument jsonBuffer(1024);
JsonObject msg = jsonBuffer.to<JsonObject>();
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& msg = jsonBuffer.createObject();
#endif
msg["topic"] = "temperature";
msg["value"] = temperature;//random(0, 180);
String str;
#if ARDUINOJSON_VERSION_MAJOR==6
serializeJson(msg, str);
#else
msg.printTo(str);
#endif
if (logServerId == 0) // If we don't know the logServer yet
mesh.sendBroadcast(str);
else
mesh.sendSingle(logServerId, str);
// log to serial
#if ARDUINOJSON_VERSION_MAJOR==6
serializeJson(msg, Serial);
#else
msg.printTo(Serial);
#endif
Serial.printf("\n");
});
void setup() {
Serial.begin(115200);
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages
mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 );
mesh.onReceive(&receivedCallback);
// Add the task to the your scheduler
userScheduler.addTask(myLoggingTask);
userScheduler.addTask(taskCalculatetemperature);
myLoggingTask.enable();
taskCalculatetemperature.enable();
}
void loop() {
// it will run the user scheduler as well
mesh.update();
}
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("logClient: Received from %u msg=%s\n", from, msg.c_str());
// Saving logServer
#if ARDUINOJSON_VERSION_MAJOR==6
DynamicJsonDocument jsonBuffer(1024 + msg.length());
DeserializationError error = deserializeJson(jsonBuffer, msg);
if (error) {
Serial.printf("DeserializationError\n");
return;
}
JsonObject root = jsonBuffer.as<JsonObject>();
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(msg);
#endif
if (root.containsKey("topic")) {
if (String("logServer").equals(root["topic"].as<String>())) {
// check for on: true or false
logServerId = root["nodeId"];
Serial.printf("logServer detected!!!\n");
}
Serial.printf("Handled from %u msg=%s\n", from, msg.c_str());
}
}