|
| 1 | +/*......................................................................................... |
| 2 | + * HelTec Automation(TM) Wireless Stick Lite factory test code, witch include |
| 3 | + * follow functions: |
| 4 | + * |
| 5 | + * - Node A search WiFi and send WiFi AP number to another node B via LoRa, Node B |
| 6 | + * will send a string "abc" back to Node A, if Node A received "abc", test pass. |
| 7 | + * |
| 8 | + * - Basic serial port test(in baud rate 115200); |
| 9 | + * |
| 10 | + * - LED blink test; |
| 11 | + * |
| 12 | + * - WIFI join and scan test; |
| 13 | + * |
| 14 | + * - LoRa Ping-Pong test(DIO0 -- GPIO26 interrup check the new incoming messages; |
| 15 | + * |
| 16 | + * - Timer test and some other Arduino basic functions. |
| 17 | + * |
| 18 | + *by Aaron.Lee from HelTec AutoMation, ChengDu, China |
| 19 | + *成都惠利特自动化科技有限公司 |
| 20 | + *www.heltec.cn |
| 21 | + * |
| 22 | + *this project also realess in GitHub: |
| 23 | + *https://github.com/HelTecAutomation/Heltec_ESP32 |
| 24 | +*/ |
| 25 | +#include "Arduino.h" |
| 26 | +#include "heltec.h" |
| 27 | +#include "WiFi.h" |
| 28 | + |
| 29 | +#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6 |
| 30 | +#define LoRa_LED 22 |
| 31 | +#define WIFI_LED 23 |
| 32 | +#define BLE_LED 25 |
| 33 | + |
| 34 | +uint64_t chipid; |
| 35 | +bool IsACKRecvied = false; |
| 36 | + |
| 37 | +String rssi = "RSSI --"; |
| 38 | +String packSize = "--"; |
| 39 | +String packet; |
| 40 | + |
| 41 | +unsigned int counter = 0; |
| 42 | + |
| 43 | +void WIFISetUp(void) |
| 44 | +{ |
| 45 | + //WIFI初始化 + 扫描演示 |
| 46 | + // Set WiFi to station mode and disconnect from an AP if it was previously connected |
| 47 | + WiFi.disconnect(true); |
| 48 | + delay(1000); |
| 49 | + WiFi.mode(WIFI_STA); |
| 50 | + WiFi.setAutoConnect(true); |
| 51 | + WiFi.begin("HelTec_AutoMation","hunter_3120");//fill in "Your WiFi SSID","Your Password" |
| 52 | + delay(100); |
| 53 | + |
| 54 | + byte count = 0; |
| 55 | + Serial.print("Connecting."); |
| 56 | + while(WiFi.status() != WL_CONNECTED && count < 5) |
| 57 | + { |
| 58 | + count ++; |
| 59 | + Serial.print("."); |
| 60 | + delay(500); |
| 61 | + } |
| 62 | + |
| 63 | + if(WiFi.status() == WL_CONNECTED) |
| 64 | + { |
| 65 | + Serial.println("\r\nConnecting...OK."); |
| 66 | + digitalWrite(WIFI_LED,HIGH); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + Serial.println("Connecting...Failed"); |
| 71 | + //while(1); |
| 72 | + } |
| 73 | + Serial.println("WIFI Setup done"); |
| 74 | +} |
| 75 | + |
| 76 | +void WIFIScan(unsigned int value) |
| 77 | +{ |
| 78 | + unsigned int i; |
| 79 | + if(WiFi.status() != WL_CONNECTED) |
| 80 | + { |
| 81 | + WiFi.mode(WIFI_MODE_NULL); |
| 82 | + } |
| 83 | + |
| 84 | + for(i=0;i<value;i++) |
| 85 | + { |
| 86 | + Serial.println("Scan start..."); |
| 87 | + |
| 88 | + int n = WiFi.scanNetworks(); |
| 89 | + Serial.println("Scan done"); |
| 90 | + delay(500); |
| 91 | + |
| 92 | + if (n == 0) |
| 93 | + { |
| 94 | + Serial.println("no network found"); |
| 95 | + //while(1); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + Serial.print(n); |
| 100 | + Serial.println("networks found:"); |
| 101 | + delay(500); |
| 102 | + |
| 103 | + for (int i = 0; i < n; ++i) { |
| 104 | + // Print SSID and RSSI for each network found |
| 105 | + Serial.print((i + 1)); |
| 106 | + Serial.print(":"); |
| 107 | + Serial.print((String)(WiFi.SSID(i))); |
| 108 | + Serial.print(" ("); |
| 109 | + Serial.print((String)(WiFi.RSSI(i))); |
| 110 | + Serial.println(")");; |
| 111 | + delay(10); |
| 112 | + } |
| 113 | + } |
| 114 | + delay(800); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +bool receiveflag = false; |
| 119 | +bool resendflag=false; |
| 120 | +bool deepsleepflag=false; |
| 121 | +void interrupt_GPIO0() |
| 122 | +{ |
| 123 | + delay(10); |
| 124 | + if(digitalRead(0)==0) |
| 125 | + { |
| 126 | + if(digitalRead(LED)==LOW) |
| 127 | + {resendflag=true;} |
| 128 | + else |
| 129 | + { |
| 130 | + deepsleepflag=true; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +void setup() |
| 136 | +{ |
| 137 | + pinMode(LoRa_LED,OUTPUT); |
| 138 | + pinMode(WIFI_LED,OUTPUT); |
| 139 | + pinMode(BLE_LED,OUTPUT); |
| 140 | + |
| 141 | + Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /**/); |
| 142 | + |
| 143 | + PSRAM_TEST(); |
| 144 | + WIFISetUp(); |
| 145 | + |
| 146 | + WiFi.disconnect(true); //重新初始化WIFI |
| 147 | + delay(1000); |
| 148 | + WiFi.mode(WIFI_STA); |
| 149 | + WiFi.setAutoConnect(true); |
| 150 | + |
| 151 | + WIFIScan(1); |
| 152 | + |
| 153 | + attachInterrupt(0,interrupt_GPIO0,FALLING); |
| 154 | + LoRa.onReceive(onReceive); |
| 155 | + send(); |
| 156 | + LoRa.receive(); |
| 157 | +} |
| 158 | + |
| 159 | +void loop() |
| 160 | +{ |
| 161 | + if(deepsleepflag) |
| 162 | + { |
| 163 | + delay(1000); |
| 164 | + LoRa.end(); |
| 165 | + LoRa.sleep(); |
| 166 | + pinMode(4,INPUT); |
| 167 | + pinMode(5,INPUT); |
| 168 | + pinMode(14,INPUT); |
| 169 | + pinMode(15,INPUT); |
| 170 | + pinMode(16,INPUT); |
| 171 | + pinMode(17,INPUT); |
| 172 | + pinMode(18,INPUT); |
| 173 | + pinMode(19,INPUT); |
| 174 | + pinMode(26,INPUT); |
| 175 | + pinMode(27,INPUT); |
| 176 | + digitalWrite(Vext,HIGH); |
| 177 | + delay(2); |
| 178 | + esp_deep_sleep_start(); |
| 179 | + } |
| 180 | + if(resendflag) |
| 181 | + { |
| 182 | + resendflag=false; |
| 183 | + send(); |
| 184 | + LoRa.receive(); |
| 185 | + } |
| 186 | + if(receiveflag) |
| 187 | + { |
| 188 | + digitalWrite(LoRa_LED,HIGH); |
| 189 | + Serial.print("Received Size "); |
| 190 | + Serial.print(packSize); |
| 191 | + Serial.print(" pakeges: "); |
| 192 | + Serial.print(packet); |
| 193 | + Serial.print(" With "); |
| 194 | + Serial.println(rssi); |
| 195 | + receiveflag = false; |
| 196 | + delay(1000); |
| 197 | + send(); |
| 198 | + LoRa.receive(); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +void send() |
| 203 | +{ |
| 204 | + LoRa.beginPacket(); |
| 205 | + LoRa.print("hello "); |
| 206 | + LoRa.print(counter++); |
| 207 | + LoRa.endPacket(); |
| 208 | + Serial.print("Packet "); |
| 209 | + Serial.print(counter-1); |
| 210 | + Serial.println(" sent done"); |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +void onReceive(int packetSize)//LoRa receiver interrupt service |
| 215 | +{ |
| 216 | + //if (packetSize == 0) return; |
| 217 | + |
| 218 | + packet = ""; |
| 219 | + packSize = String(packetSize,DEC); |
| 220 | + |
| 221 | + while (LoRa.available()) |
| 222 | + { |
| 223 | + packet += (char) LoRa.read(); |
| 224 | + } |
| 225 | + |
| 226 | + rssi = "RSSI: " + String(LoRa.packetRssi(), DEC); |
| 227 | + |
| 228 | + receiveflag = true; |
| 229 | +} |
| 230 | + |
| 231 | +void PSRAM_TEST() |
| 232 | +{ |
| 233 | + Serial.printf("Total heap: %d\r\n", ESP.getHeapSize()); |
| 234 | + Serial.printf("Free heap: %d\r\n", ESP.getFreeHeap()); |
| 235 | + Serial.printf("Total PSRAM: %d\r\n", ESP.getPsramSize()); |
| 236 | + byte* psdRamBuffer = (byte*)ps_malloc(4000000); |
| 237 | + Serial.printf("Free PSRAM: %d\r\n", ESP.getFreePsram()); |
| 238 | +} |
0 commit comments