#include<ArduinoBLE.h> #include<SPI.h> #include<WiFiNINA.h> #include"utility/wifi_drv.h" #define BLE_UUID_NETWORK_CONFIG_SERVICE "343D2964-5ECF-2297-4463-609011571F24" #define BLE_UUID_NETWORK_ENABLE_CHARACTERISTIC "767B22E7-EA6C-B017-286A-55B68310FD9D" BLEService networkConfigService( BLE_UUID_NETWORK_CONFIG_SERVICE ); BLEBoolCharacteristic networkEnableCharacteristic( BLE_UUID_NETWORK_ENABLE_CHARACTERISTIC , BLERead | BLEWrite ); ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char* ssid ="1234"; // WiFi의 SSID 입니다. char* pass = "1234"; // 비번입니다. String apiKey = "NUIMUAV9QL7Y"; // 여기에 API키값을 입력합니다. const char* server ="api.thingspeak.com"; int status = WL_IDLE_STATUS; bool networkInitialized = false; bool wifiModeFlag =false; const int LED_PIN = LED_BUILTIN; const int bat1 = A0; const float referenceVolts =3.20; void setup() { Serial.begin( 9600 ); while ( !Serial ); pinMode( LED_PIN, OUTPUT ); } void loop() { if( !networkInitialized ) { if( !wifiModeFlag ) { Serial.print( "Switch to BLE: " ); if( !switch2BleMode() ) { Serial.println( "failed" ); } else { networkInitialized =true; Serial.println( "success" ); } } else { Serial.print( "Switch to WiFi: " ); if( !switch2WiFiMode() ) { Serial.println( "failed" ); } else { networkInitialized =true; Serial.println( "success" ); } } } else { if( !wifiModeFlag ) { bleMode(); } else { wifiMode(); } } } void bleMode() { BLEDevice central = BLE.central(); if ( central ) { Serial.print( "Connected to central: " ); Serial.println( central.address() ); while ( central.connected() ) { if( networkEnableCharacteristic.written() ) { networkInitialized = false; wifiModeFlag =true; return; } } } } void wifiMode(){ // 와이파이 연결시도 int status = WL_IDLE_STATUS; // the Wifi radio's status WiFiClient client; while (status != WL_CONNECTED) { Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } // you're connected now, so print out the data: Serial.println("You're connected to the network"); int val1 =analogRead(bat1); float volt1 = (referenceVolts/1023.0) * val1; if (isnan(volt1)) { Serial.println("Failed to read from Voltage sensor"); delay(3000); return; } if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com String getStr = "GET /update?api_key="; getStr += apiKey; getStr +="&field1="; getStr += String(volt1); getStr +="\r\n\r\n"; client.print(getStr +" HTTP/1.1\r\n" +"Host: "+ server + "\r\n"+"Connection: close\r\n\r\n"); Serial.print("Voltage : "); Serial.print(volt1); Serial.println("V send to Thingspeak"); } // GET /B switch to Bluetooth networkInitialized = false; wifiModeFlag =false; &bsp;client.stop(); } bool switch2BleMode() { if ( !BLE.begin() ) { returnfalse; } // set advertised local name and service UUID: BLE.setDeviceName( "Arduino Nano 33 IoT" ); BLE.setLocalName( "Arduino Nano 33 IoT" ); BLE.setAdvertisedService( networkConfigService ); // add the characteristic to the service networkConfigService.addCharacteristic( networkEnableCharacteristic ); // add service BLE.addService( networkConfigService ); // set the initial value for the characeristic: networkEnableCharacteristic.writeValue( false ); BLE.advertise(); returntrue; } bool switch2WiFiMode() { BLE.stopAdvertise(); BLE.end(); status = WL_IDLE_STATUS; // Re-initialize the WiFi driver // This is currently necessary to switch from BLE to WiFi wiFiDrv.wifiDriverDeinit(); wiFiDrv.wifiDriverInit(); returntrue; } |