I2C beginner request
Posted: Wed May 17, 2023 5:33 pm
Hello all,
I am looking for help to connect ESP32-C3-DevKitC-02 as slave to another arduino controller via 3v3 I2C. Thanks in advance.
The reason for this requirement is that I do not have any available hardware serials and the main controller does not support software serial easily or reliably. The purpose of the module is to scan for local hotspots and then send wifi survey to I2C master on request (it is a component of a GPS redundant location system).
This is the quick test code I am failing to compile using the arduino IDE, note that I can compile and run happily without the I2C code, just scanning for hotspots. Also note that I can compile and run the I2C code on another controller type and it works fine. Also the test code is only sending some of the wifi survey string at the moment, it's not a mistake, just a test.
I believe that I have the latest libraries/drivers and I have seen other code examples using wire.h. That said, I think I need to use a different library now that I'm working with an ESP32. Please can I ask for help getting this code to compile and work via arduino IDE?
...and this is the compiler error:
C:\Users\bench\AppData\Local\Temp\arduino\sketches\973B34183760F62A07B4CE4E535A6136\sketch\WiFiScan.ino.cpp.o:(.literal._Z5setupv+0x1c): undefined reference to `TwoWire::onRequest(void (*)())'
C:\Users\bench\AppData\Local\Temp\arduino\sketches\973B34183760F62A07B4CE4E535A6136\sketch\WiFiScan.ino.cpp.o: In function `setup()':
C:\Users\bench\OneDrive\Documents\Arduino\WiFiScan/WiFiScan.ino:15: undefined reference to `TwoWire::onRequest(void (*)())'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
Thanks.
Bw,
Ben
I am looking for help to connect ESP32-C3-DevKitC-02 as slave to another arduino controller via 3v3 I2C. Thanks in advance.
The reason for this requirement is that I do not have any available hardware serials and the main controller does not support software serial easily or reliably. The purpose of the module is to scan for local hotspots and then send wifi survey to I2C master on request (it is a component of a GPS redundant location system).
This is the quick test code I am failing to compile using the arduino IDE, note that I can compile and run happily without the I2C code, just scanning for hotspots. Also note that I can compile and run the I2C code on another controller type and it works fine. Also the test code is only sending some of the wifi survey string at the moment, it's not a mistake, just a test.
I believe that I have the latest libraries/drivers and I have seen other code examples using wire.h. That said, I think I need to use a different library now that I'm working with an ESP32. Please can I ask for help getting this code to compile and work via arduino IDE?
- /*
- * This sketch demonstrates how to scan WiFi networks.
- * The API is almost the same as with the WiFi Shield library,
- * the most obvious difference being the different file you need to include:
- */
- #include "WiFi.h"
- #include <Wire.h>
- String wifiSurvey = "";
- void setup()
- {
- Serial.begin(115200);
- // Set WiFi to station mode and disconnect from an AP if it was previously connected
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- Wire.onRequest(requestEvent);
- Wire.begin(6);
- delay(100);
- }
- void loop()
- {
- String wifi1 = "";
- String wifi2 = "";
- String wifi3 = "";
- String wifi4 = "";
- String wifi5 = "";
- String wifi6 = "";
- String wifi7 = "";
- // WiFi.scanNetworks will return the number of networks found
- int n = WiFi.scanNetworks();
- if (n == 0) {
- Serial.println("no networks found");
- } else {
- Serial.print(n);
- Serial.println(" networks found");
- for (int i = 0; i < n; ++i) {
- // Print SSID and RSSI for each network found
- if(i == 0){
- wifi1 += WiFi.BSSIDstr(i);
- wifi1 += "|";
- wifi1 += WiFi.channel(i);
- wifi1 += "|2400|";
- wifi1 += WiFi.RSSI(i);
- wifi1 += "|";
- }
- if(i == 1){
- wifi2 += WiFi.BSSIDstr(i);
- wifi2 += "|";
- wifi2 += WiFi.channel(i);
- wifi2 += "|2400|";
- wifi2 += WiFi.RSSI(i);
- wifi2 += "|";
- }
- if(i == 2){
- wifi3 += WiFi.BSSIDstr(i);
- wifi3 += "|";
- wifi3 += WiFi.channel(i);
- wifi3 += "|2400|";
- wifi3 += WiFi.RSSI(i);
- wifi3 += "|";
- }
- if(i == 3){
- wifi4 += WiFi.BSSIDstr(i);
- wifi4 += "|";
- wifi4 += WiFi.channel(i);
- wifi4 += "|2400|";
- wifi4 += WiFi.RSSI(i);
- wifi4 += "|";
- }
- if(i == 4){
- wifi5 += WiFi.BSSIDstr(i);
- wifi5 += "|";
- wifi5 += WiFi.channel(i);
- wifi5 += "|2400|";
- wifi5 += WiFi.RSSI(i);
- wifi5 += "|";
- }
- if(i == 5){
- wifi6 += WiFi.BSSIDstr(i);
- wifi6 += "|";
- wifi6 += WiFi.channel(i);
- wifi6 += "|2400|";
- wifi6 += WiFi.RSSI(i);
- wifi6 += "|";
- }
- if(i == 6){
- wifi7 += WiFi.BSSIDstr(i);
- wifi7 += "|";
- wifi7 += WiFi.channel(i);
- wifi7 += "|2400|";
- wifi7 += WiFi.RSSI(i);
- wifi7 += "|";
- }
- // Serial.print(WiFi.BSSIDstr(i));
- // Serial.print(",");
- // Serial.print(WiFi.channel(i));
- // Serial.print(",");
- // Serial.print("2400,");
- // Serial.print(WiFi.RSSI(i));
- // Serial.println("");
- // Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
- delay(10);
- }
- }
- wifiSurvey = "";
- wifiSurvey = wifi1 + wifi2 + wifi3 + wifi4 + wifi5 + wifi6 + wifi7;
- Serial.println("\n\n");
- Serial.println(wifiSurvey.length());
- Serial.println(wifiSurvey);
- // Wait a bit before scanning again
- delay(5000);
- }
- void requestEvent() {
- for (int i = 0; i < 26; i++) { Wire.write(wifiSurvey[i]); }
- // (wifiSurvey.length()+1)
- }
C:\Users\bench\AppData\Local\Temp\arduino\sketches\973B34183760F62A07B4CE4E535A6136\sketch\WiFiScan.ino.cpp.o:(.literal._Z5setupv+0x1c): undefined reference to `TwoWire::onRequest(void (*)())'
C:\Users\bench\AppData\Local\Temp\arduino\sketches\973B34183760F62A07B4CE4E535A6136\sketch\WiFiScan.ino.cpp.o: In function `setup()':
C:\Users\bench\OneDrive\Documents\Arduino\WiFiScan/WiFiScan.ino:15: undefined reference to `TwoWire::onRequest(void (*)())'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
Thanks.
Bw,
Ben