forked from aeronixil/uplink-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempmeshrecv.ino
142 lines (114 loc) · 3.85 KB
/
tempmeshrecv.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
#include "painlessMesh.h"
#include <Arduino_JSON.h>
int mydeviceid = 5551;
int device_id = 4567;
String device_name = "sample_name";
String target_building = "sample_building";
String target_floor = "one/two/three";
int target_device = 1234;
int target_payload = 8888;
int secretscode = 123456789;
String mess;
String message_json;
int prevon = 0;
int temp = 0;
#define MESH_PREFIX "uplinkautomesh"
#define MESH_PASSWORD "alphabravocharliedelta"
#define MESH_PORT 5555
#define task_width 3000 // how many times a task occurs given in milli seconds
int counter = 0;
Scheduler uplink_schedule; // to control your personal task
painlessMesh mesh;
// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain
void uplink_execute();
String createjson();
String createjson()
{
JSONVar jsonfile;
jsonfile["device_id"] = device_id;
// jsonfile["device_name"] = device_name;
// jsonfile["target_building"] = target_building;
// jsonfile["target_floor"] = target_floor;
jsonfile["target_device"] = target_device;
jsonfile["target_payload"] = target_payload;
jsonfile["secretscode"] = secretscode;
message_json = JSON.stringify(jsonfile);
return message_json;
}
Task uplink_task_default(task_width, TASK_FOREVER, &uplink_execute );
void uplink_execute()
{
//write code to execute or business code or user code
String str = String(counter);
uplink_send(str);
counter = counter+1;
}
// use the uplink_send command to send messages to the mesh
void uplink_send(String uplstr)
{
//String nodename = "nodenamehere";
//uplstr += mesh.getNodeId();
//uplstr += alpha;
mesh.sendBroadcast(uplstr);
}
void sendMessage() {
String msg = "Hi from node1";
msg += mesh.getNodeId();
mesh.sendBroadcast( msg );
}
// Needed for painless library
void receivedCallback( uint32_t from, String &msg )
{
Serial.println("voidcallback is called");
JSONVar obj = JSON.parse(msg.c_str());
int device_id = obj["device_id"];
// String device_name = obj["device_name"];
// String target_building = obj["target_building"];
// String target_floor = obj["target_floor"];
int target_device = obj["target_device"];
int target_payload = obj["target_payload"];
int secretscode = obj["secretscode"];
if(target_device == mydeviceid)
{
if(target_payload == 1)
{
digitalWrite(2, HIGH);
digitalWrite(13, HIGH);
Serial.println(device_name + "is ON");
}
if(target_payload == 0)
{
digitalWrite(2, LOW);
digitalWrite(13, LOW);
Serial.println(device_name + "is OFF");
}
}
}
void newConnectionCallback(uint32_t nodeId) {
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
void changedConnectionCallback() {
Serial.printf("Changed connections\n");
}
void nodeTimeAdjustedCallback(int32_t offset) {
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(13, OUTPUT);
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages
mesh.init( MESH_PREFIX, MESH_PASSWORD, &uplink_schedule, MESH_PORT );
mesh.onReceive(&receivedCallback);
mesh.onNewConnection(&newConnectionCallback);
mesh.onChangedConnections(&changedConnectionCallback);
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
uplink_schedule.addTask( uplink_task_default );
uplink_task_default.enable();
}
void loop() {
// it will run the user scheduler as well
mesh.update();
}