Skip to content

Commit bb4ba14

Browse files
2 parents 2da961f + aea4426 commit bb4ba14

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Receive the lora signal to control the on and off of the LED.
3+
Pull the 12th pin high to turn on the LED light.
4+
Pull pin 13 high to turn off the LED light.
5+
by Aaron.Lee from HelTec AutoMation, ChengDu, China
6+
成都惠利特自动化科技有限公司
7+
www.heltec.cn
8+
9+
this project also realess in GitHub:
10+
https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
11+
*/
12+
13+
#include "heltec.h"
14+
#include "string.h"
15+
#include "stdio.h"
16+
#define LED 25
17+
#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
18+
char Readback[50];
19+
void setup() {
20+
//WIFI Kit series V1 not support Vext control
21+
Heltec.begin(false /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
22+
pinMode(LED,OUTPUT);
23+
digitalWrite(LED,LOW);
24+
}
25+
26+
void loop() {
27+
// try to parse packet
28+
int packetSize = LoRa.parsePacket();
29+
if (packetSize) {
30+
// received a packet
31+
Serial.print("Received packet '");
32+
// read packet
33+
while (LoRa.available()) {
34+
sprintf(Readback+strlen(Readback),"%c",(char)LoRa.read());
35+
}
36+
Serial.print(Readback);
37+
if(strncmp(Readback, "OpenLED", strlen(Readback)) == 0) {
38+
digitalWrite(LED, HIGH);
39+
}
40+
else if(strncmp(Readback, "CloseLED", strlen(Readback)) == 0) {
41+
digitalWrite(LED, LOW);
42+
}
43+
memset(Readback,0,50);
44+
// print RSSI of packet
45+
Serial.print(" with RSSI ");
46+
Serial.println(LoRa.packetRssi());
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Send a lora signal to control the LED light switch of another board.
3+
Pull the 12th pin high to turn on the LED light.
4+
Pull pin 13 high to turn off the LED light.
5+
by Aaron.Lee from HelTec AutoMation, ChengDu, China
6+
成都惠利特自动化科技有限公司
7+
www.heltec.cn
8+
9+
this project also realess in GitHub:
10+
https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
11+
*/
12+
#include "heltec.h"
13+
#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
14+
15+
int counter = 0;
16+
#define Open_LED 12
17+
#define Close_LED 13
18+
void setup() {
19+
20+
//WIFI Kit series V1 not support Vext control
21+
Heltec.begin(false /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
22+
pinMode(Open_LED,INPUT);
23+
digitalWrite(Open_LED,LOW);
24+
pinMode(Close_LED,INPUT);
25+
digitalWrite(Close_LED,LOW);
26+
LoRa.setTxPower(14,RF_PACONFIG_PASELECT_PABOOST);
27+
28+
}
29+
30+
void loop() {
31+
if(digitalRead(Open_LED)){
32+
Serial.print("Sending packet: OpenLED\r\n");
33+
// send packet
34+
LoRa.beginPacket();
35+
LoRa.print("OpenLED");
36+
LoRa.endPacket();
37+
digitalWrite(Open_LED,LOW);
38+
}
39+
if(digitalRead(Close_LED)){
40+
Serial.print("Sending packet: CloseLED\r\n");
41+
// send packet
42+
LoRa.beginPacket();
43+
LoRa.print("CloseLED");
44+
LoRa.endPacket();
45+
digitalWrite(Close_LED,LOW);
46+
}
47+
delay(1000);
48+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* TimeNTP_ESP32WiFi.ino
3+
* Example showing time sync to NTP time source
4+
*
5+
* This sketch uses the ESP32WiFi library
6+
* This sketch uses the Time library https://github.com/PaulStoffregen/Time
7+
*/
8+
9+
#include <TimeLib.h>
10+
#include <WiFi.h>
11+
#include <WiFiUdp.h>
12+
13+
const char ssid[] = "ssid"; // your network SSID (name)
14+
const char pass[] = "password"; // your network password
15+
16+
// NTP Servers:
17+
static const char ntpServerName[] = "pool.ntp.org";
18+
19+
const int timeZone = 0; // UTC
20+
21+
WiFiUDP Udp;
22+
unsigned int localPort = 8888; // local port to listen for UDP packets
23+
24+
time_t getNtpTime();
25+
void digitalClockDisplay();
26+
void printDigits(int digits);
27+
void sendNTPpacket(IPAddress &address);
28+
29+
void setup()
30+
{
31+
Serial.begin(115200);
32+
while (!Serial) ; // Needed for Leonardo only
33+
delay(250);
34+
Serial.println("TimeNTP Example");
35+
Serial.print("Connecting to ");
36+
Serial.println(ssid);
37+
WiFi.disconnect();
38+
WiFi.mode(WIFI_MODE_STA);
39+
WiFi.begin(ssid, pass);
40+
41+
while (WiFi.status() != WL_CONNECTED) {
42+
delay(500);
43+
Serial.print(".");
44+
}
45+
46+
Serial.print("IP number assigned by DHCP is ");
47+
Serial.println(WiFi.localIP());
48+
Serial.println("Starting UDP");
49+
Udp.begin(localPort);
50+
Serial.print("Local port: ");
51+
// Serial.println(Udp.localPort());
52+
Serial.println("waiting for sync");
53+
setSyncProvider(getNtpTime);
54+
setSyncInterval(300);
55+
}
56+
57+
time_t prevDisplay = 0; // when the digital clock was displayed
58+
59+
void loop()
60+
{
61+
if (timeStatus() != timeNotSet) {
62+
if (now() != prevDisplay) { //update the display only if time has changed
63+
prevDisplay = now();
64+
digitalClockDisplay();
65+
}
66+
}
67+
}
68+
69+
void digitalClockDisplay()
70+
{
71+
// digital clock display of the time
72+
Serial.print(hour());
73+
printDigits(minute());
74+
printDigits(second());
75+
Serial.print(" ");
76+
Serial.print(day());
77+
Serial.print(".");
78+
Serial.print(month());
79+
Serial.print(".");
80+
Serial.print(year());
81+
Serial.println();
82+
}
83+
84+
void printDigits(int digits)
85+
{
86+
// utility for digital clock display: prints preceding colon and leading 0
87+
Serial.print(":");
88+
if (digits < 10)
89+
Serial.print('0');
90+
Serial.print(digits);
91+
}
92+
93+
/*-------- NTP code ----------*/
94+
95+
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
96+
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
97+
98+
time_t getNtpTime()
99+
{
100+
IPAddress ntpServerIP; // NTP server's ip address
101+
102+
while (Udp.parsePacket() > 0) ; // discard any previously received packets
103+
Serial.println("Transmit NTP Request");
104+
// get a random server from the pool
105+
WiFi.hostByName(ntpServerName, ntpServerIP);
106+
Serial.print(ntpServerName);
107+
Serial.print(": ");
108+
Serial.println(ntpServerIP);
109+
sendNTPpacket(ntpServerIP);
110+
uint32_t beginWait = millis();
111+
while (millis() - beginWait < 1500) {
112+
int size = Udp.parsePacket();
113+
if (size >= NTP_PACKET_SIZE) {
114+
Serial.println("Receive NTP Response");
115+
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
116+
unsigned long secsSince1900;
117+
// convert four bytes starting at location 40 to a long integer
118+
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
119+
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
120+
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
121+
secsSince1900 |= (unsigned long)packetBuffer[43];
122+
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
123+
}
124+
}
125+
Serial.println("No NTP Response :-(");
126+
return 0; // return 0 if unable to get the time
127+
}
128+
129+
// send an NTP request to the time server at the given address
130+
void sendNTPpacket(IPAddress &address)
131+
{
132+
// set all bytes in the buffer to 0
133+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
134+
// Initialize values needed to form NTP request
135+
// (see URL above for details on the packets)
136+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
137+
packetBuffer[1] = 0; // Stratum, or type of clock
138+
packetBuffer[2] = 6; // Polling Interval
139+
packetBuffer[3] = 0xEC; // Peer Clock Precision
140+
// 8 bytes of zero for Root Delay & Root Dispersion
141+
packetBuffer[12] = 49;
142+
packetBuffer[13] = 0x4E;
143+
packetBuffer[14] = 49;
144+
packetBuffer[15] = 52;
145+
// all NTP fields have been given values, now
146+
// you can send a packet requesting a timestamp:
147+
Udp.beginPacket(address, 123); //NTP requests are to port 123
148+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
149+
Udp.endPacket();
150+
}

0 commit comments

Comments
 (0)