-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHANGE OVER ESP V2
137 lines (107 loc) · 3.16 KB
/
CHANGE OVER ESP V2
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
#include <ESP8266WiFi.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
int fmr;
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x65};
String message = "";
//String message = "";
uint8_t broadcastAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};
unsigned long initTime; //some global variables available anywhere in the program
unsigned long inits = 0; //some global variables available anywhere in the program
unsigned long timeNow;
const unsigned long sTime = 3500; //the value is a number of milliseconds
// Define variables to store incoming readings
int gen = 0;
float bat = 0;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message
{
int genStatus; //// send 1 0r zero
float batlvl; ///0-100
} struct_message;
// Create a struct_message called DHTReadings to hold sensor readings
struct_message valuesToSend;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus)
{
// Serial.print(("Last Packet Send Status: "));
if (sendStatus == 0)
{fmr= valuesToSend.genStatus;
//Serial.println(("Delivery success"));
}
else
{
//Serial.println(("Delivery fail"));
}
}
// Callback when data is received
void OnDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len)
{
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
// Serial.println(F(len));
gen = incomingReadings.genStatus;
bat = incomingReadings.batlvl;
Serial.print(("Bytes received="));
Serial.print(bat);
Serial.println(",");
}
void setup()
{
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
wifi_set_macaddr(STATION_IF, &newMACAddress[0]);
// Init ESP-NOW
if (esp_now_init() != 0)
{
//Serial.println(F("Error initializing ESP-NOW"));
return;
}
// Set ESP-NOW Role
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop()
{
timeNow = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (Serial.available())
{
delay(10);
while (Serial.available())
{
message += (char)Serial.read();
}
if (message.indexOf("1") != -1)
{
valuesToSend.genStatus = 1;
valuesToSend.batlvl = 1;
Serial.println("gen on");
}
else if (message.indexOf("00000") != -1)
{
valuesToSend.genStatus = 0;
valuesToSend.batlvl = 0;
Serial.println("gen off");
}
message="";
}
if (timeNow - inits >= sTime) //test whether the period has elapsed
{
if (fmr!= valuesToSend.genStatus){
esp_now_send(broadcastAddress, (uint8_t *)&valuesToSend, sizeof(valuesToSend));
inits = timeNow;
}
}
// Serial.flush();
}