Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed as3935 ESP8266NodeMCUInterface
main.cpp
00001 /* 00002 Authors: Brian Harden + Stefan Abi-Karam + Larry Kresse 00003 Class: ECE4180 - Fall 2020 00004 Description: The purpose of this program is to set up a single mbed device 00005 00006 BRIAN'S mBED MAC ADDRESS: 00:02:f7:f3:64:9f 00007 BORROWED mBED MAC ADDRESS: 00:02:F7:F3:D2:C5 00008 MAIN ESP8266 IP ADDRESS: 143.215.99.29 00009 BORROWED mBED IP ADDRESS: 143.215.104.115 00010 MAIN ESP8266 MAC ADDRESS: 18-FE-34-05-99-F8 00011 BORROW ESP8266 MAC ADDRESS: 18-FE-34-0D-CB-40 00012 00013 */ 00014 00015 00016 #include <string> 00017 #include "mbed.h" 00018 #include "ESP8266.h" 00019 #include "AS3935.h" 00020 00021 #define LIGHTNINGDETECTORID 1 00022 00023 //DECLARATIONS: STRUCTS 00024 // Struct to send over TCP 00025 struct DATA 00026 { 00027 char detectorID; 00028 char distanceKM; 00029 unsigned long time; 00030 }; 00031 00032 union rawReceivedData 00033 { 00034 struct DATA struc; 00035 char dataString[sizeof(DATA)]; 00036 }; 00037 00038 //using namespace std::chrono 00039 //DECLARATIONS: GLOBAL VARIABLES 00040 DigitalOut led1(LED1); //DEBUGGING: On-board LED used for debugging purposes 00041 DigitalOut led2(LED2); //DEBUGGING: On-board LED used for debugging purposes 00042 DigitalOut led4(LED4); //DEBUGGING: On-board LED used for debugging purposes 00043 char dataTEMP[8] = "5"; //DEBUGGING: A buffer to store the incoming data 00044 Serial pc(USBTX, USBRX); //Set up the mbed USB port for debugging/monitoring 00045 Timer clocky; //A clock used to log the time of each lightning strike 00046 ESP8266 wifi(p28, p27, p26, 9600, 3000); //The WiFi module 00047 AS3935 ld(p11, p12, p13, p14, "ld", 2000000); //MOSI, MISO, SCK, CS, SPI bus freq (hz) 00048 InterruptIn as3935INT(p15); //Interrupt signal that is given by the AS3935 Lightning Detector 00049 DigitalOut wifiRST(p26); //Reset signal to the ESP8266 Device 00050 char ssid[32] = "GTother"; //enter WiFi router ssid inside the quotes 00051 char pwd [32] = "GeorgeP@1927"; //enter WiFi router password inside the quotes 00052 char serverIP [32] = "143.215.99.29"; //The server IP to connect to (the main mBed) 00053 //char serverIP [32] = "143.215.104.115"; //The server IP to connect to (the main mBed) 00054 Timer timeoutTimer; //A timer in case of ESP8266 timeouts 00055 unsigned int timeout; //A maximum time (in seconds) before timeout 00056 int county; //A counter for timeouts in the ESP8266 00057 bool ended; //A boolean letting us know when the ESP8266's role is terminated 00058 union rawReceivedData dataToSend; //A struct to send over TCP to the server 00059 00060 00061 //DECLARATIONS: FUNCTION PROTOTYPES 00062 void LightningDetected(); //Interrupt routine to handle the event of lightning occurring 00063 void SetupTransmitter(); //Sets up the WiFi card for transmitting 00064 void SetupLightningDetector(); //Sets up the AS3935 lightning detector 00065 void dev_recv(); //DEBUGGING: Write out any errors that may occur within the WiFi module 00066 void pc_recv(); //DEBUGGING: Write out any errors that may occur within the WiFi module 00067 00068 /******************************************************************************* 00069 MAIN FUNCTION 00070 *******************************************************************************/ 00071 00072 int main() 00073 { 00074 wifiRST = 0; //Reset the ESP8266 00075 wait(0.5); //Give it a little time to fully reset 00076 wifiRST = 1; //Raise the reset pin 00077 wait(1); //Give it a second to re-initialize 00078 //Set up the transmitter 00079 SetupTransmitter(); 00080 //Initialize the lightning detector 00081 SetupLightningDetector(); 00082 pc.printf("Ready!\r\n"); 00083 clocky.start(); 00084 dataToSend.struc.distanceKM = 5; 00085 dataToSend.struc.detectorID = 1; 00086 dataToSend.struc.time = 0x0F; 00087 while(1) 00088 { 00089 //Blinky 00090 pc.printf("Test packet send!"); 00091 wifi.send((char *)dataToSend.dataString, sizeof(DATA)); //Send the data 00092 wait(3); 00093 led1 = !led1; 00094 //sleep(1); 00095 } 00096 } 00097 00098 /************************** 00099 LIGHTNING DETECTED 00100 ***************************/ 00101 //Summary: The AS3935 breakout board will send the INT signal high when it 00102 // detects lightning. This interrupt function is designed to handle the 00103 // interrupt in the event of lightning being detected 00104 void LightningDetected() 00105 { 00106 clocky.stop(); 00107 led1 = 1; 00108 char OriginInt; 00109 wait_ms(2); 00110 OriginInt = ld.interruptSource(); 00111 if (OriginInt == 1) 00112 { // 00113 pc.printf(" Noise level too high\r\n"); 00114 } 00115 if (OriginInt == 4) 00116 { // 00117 pc.printf(" Disturber\r\n"); 00118 } 00119 if (OriginInt == 8) 00120 { // detection 00121 // pc.printf(" Lightning detection\r\n"); 00122 dataTEMP[0] = ld.lightningDistanceKm(); //Shove the distance into index 0 00123 dataToSend.struc.distanceKM = dataTEMP[0]; //Shove the distance into the struct 00124 pc.printf("Lightning detection, distance=%dm\r\n", dataTEMP[0]); 00125 pc.printf("Energy %d\r\n", ld.getEnergy()); 00126 ld.clearStats(); //Clear the contents and get 00127 } 00128 dataTEMP[1] = (char)LIGHTNINGDETECTORID; //Shove the detector's ID into index 1 00129 dataToSend.struc.detectorID = (char)LIGHTNINGDETECTORID; //Shove thed etector's dsitance into the struct 00130 dataToSend.struc.time = clocky.read(); //Shove the time in milliseconds into the struct 00131 wait(0.5); 00132 //wifi.send((char *)dataToSend.dataString, sizeof(DATA)); //Send the data 00133 clocky.start(); //Start the clock again 00134 00135 //TODO: Send the struct 00136 00137 } 00138 00139 /************************** 00140 SETUP - ESP8266 WIFI MODULE 00141 ***************************/ 00142 //Summary: This function will set up the transmitting ESP8266 module so that 00143 // it will send data to the corresponding ESP8266 server with the interrupt 00144 // data 00145 void SetupTransmitter() 00146 { 00147 wifi.init(); //Initialize the ESP8266 module (using resets contained in the class) 00148 wifi.connect(ssid, pwd); //Connect using the SSID and Password 00149 //Keep re-trying to gain a connection to an access point 00150 while(!(wifi.is_connected())) 00151 { 00152 wait(2); 00153 pc.printf("Connecting to AP...\r\n"); 00154 } 00155 //Keep re-trying to connect to the main device 00156 while(!(wifi.open(true, serverIP, 80, -1))) 00157 { 00158 wait(2); 00159 pc.printf("Connecting to server...\r\n"); 00160 } 00161 } 00162 00163 /************************** 00164 SETUP - AS3935 LIGHTNING DETECTOR 00165 ***************************/ 00166 //Summary: This function sets up the lightning detector so that it may handle 00167 // lightning strikes 00168 void SetupLightningDetector() 00169 { 00170 ld.setTuneCap(1); //500kHz 00171 ld.setOutdoors(); //Scale it for indoors experiments 00172 ld.setMinimumLightnings(1); //Set it so it only needs one lightning strike to trigger 00173 ld.setNoiseFloor(2); 00174 ld.disableDisturbers(); //Stop making it whine about noise 00175 ld.setWatchdogThreshold(2); //2 Second watchdog 00176 as3935INT.rise(&LightningDetected); //Set it so that the interrupt function goes high when lightning is detected 00177 } 00178 00179 /************************** 00180 DEBUGGING - Receiving ESP8266 Data 00181 ***************************/ 00182 //Summary: This function will emplace any characters received by the 00183 // ESP8266 module to the PC terminal 00184 void dev_recv() 00185 { 00186 led1 = !led1; 00187 wait(0.5); 00188 while(wifi.readable()) 00189 { 00190 pc.putc(wifi.getc()); //Emplace characters from the ESP8266 to the PC 00191 } 00192 } 00193 00194 /************************** 00195 DEBUGGING - Placing ESP8266 Data 00196 ***************************/ 00197 //Summary: This function will emplace any characters sent from the PC 00198 // terminal into the ESP8266 module 00199 void pc_recv() 00200 { 00201 led4 = !led4; 00202 while(pc.readable()) 00203 { 00204 wifi.putc(pc.getc()); //Emplace characters from the PC to the ESP8266 00205 } 00206 }
Generated on Tue Jul 12 2022 21:11:43 by
1.7.2