I2C beginner request

benorange
Posts: 2
Joined: Wed May 17, 2023 5:16 pm

I2C beginner request

Postby benorange » 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?
  1. /*
  2.  *  This sketch demonstrates how to scan WiFi networks.
  3.  *  The API is almost the same as with the WiFi Shield library,
  4.  *  the most obvious difference being the different file you need to include:
  5.  */
  6. #include "WiFi.h"
  7. #include <Wire.h>
  8. String wifiSurvey = "";
  9.  
  10. void setup()
  11. {
  12.     Serial.begin(115200);
  13.     // Set WiFi to station mode and disconnect from an AP if it was previously connected
  14.     WiFi.mode(WIFI_STA);
  15.     WiFi.disconnect();
  16.  
  17.    
  18.     Wire.onRequest(requestEvent);
  19.     Wire.begin(6);              
  20.     delay(100);
  21. }
  22.  
  23. void loop()
  24. {
  25.  
  26.     String wifi1 = "";
  27.     String wifi2 = "";
  28.     String wifi3 = "";
  29.     String wifi4 = "";
  30.     String wifi5 = "";
  31.     String wifi6 = "";
  32.     String wifi7 = "";
  33.     // WiFi.scanNetworks will return the number of networks found
  34.     int n = WiFi.scanNetworks();
  35.     if (n == 0) {
  36.         Serial.println("no networks found");
  37.     } else {
  38.         Serial.print(n);
  39.         Serial.println(" networks found");
  40.         for (int i = 0; i < n; ++i) {
  41.             // Print SSID and RSSI for each network found
  42.  
  43.             if(i == 0){
  44.               wifi1 += WiFi.BSSIDstr(i);
  45.               wifi1 += "|";
  46.               wifi1 += WiFi.channel(i);
  47.               wifi1 += "|2400|";
  48.               wifi1 += WiFi.RSSI(i);
  49.               wifi1 += "|";
  50.             }
  51.             if(i == 1){
  52.               wifi2 += WiFi.BSSIDstr(i);
  53.               wifi2 += "|";
  54.               wifi2 += WiFi.channel(i);
  55.               wifi2 += "|2400|";
  56.               wifi2 += WiFi.RSSI(i);
  57.               wifi2 += "|";
  58.             }
  59.             if(i == 2){
  60.               wifi3 += WiFi.BSSIDstr(i);
  61.               wifi3 += "|";
  62.               wifi3 += WiFi.channel(i);
  63.               wifi3 += "|2400|";
  64.               wifi3 += WiFi.RSSI(i);
  65.               wifi3 += "|";
  66.             }
  67.             if(i == 3){
  68.               wifi4 += WiFi.BSSIDstr(i);
  69.               wifi4 += "|";
  70.               wifi4 += WiFi.channel(i);
  71.               wifi4 += "|2400|";
  72.               wifi4 += WiFi.RSSI(i);
  73.               wifi4 += "|";
  74.             }
  75.             if(i == 4){
  76.               wifi5 += WiFi.BSSIDstr(i);
  77.               wifi5 += "|";
  78.               wifi5 += WiFi.channel(i);
  79.               wifi5 += "|2400|";
  80.               wifi5 += WiFi.RSSI(i);
  81.               wifi5 += "|";
  82.             }
  83.             if(i == 5){
  84.               wifi6 += WiFi.BSSIDstr(i);
  85.               wifi6 += "|";
  86.               wifi6 += WiFi.channel(i);
  87.               wifi6 += "|2400|";
  88.               wifi6 += WiFi.RSSI(i);
  89.               wifi6 += "|";
  90.             }
  91.             if(i == 6){
  92.               wifi7 += WiFi.BSSIDstr(i);
  93.               wifi7 += "|";
  94.               wifi7 += WiFi.channel(i);
  95.               wifi7 += "|2400|";
  96.               wifi7 += WiFi.RSSI(i);
  97.               wifi7 += "|";
  98.             }
  99.  
  100.             // Serial.print(WiFi.BSSIDstr(i));
  101.             // Serial.print(",");
  102.             // Serial.print(WiFi.channel(i));
  103.             // Serial.print(",");
  104.             // Serial.print("2400,");
  105.             // Serial.print(WiFi.RSSI(i));
  106.             // Serial.println("");
  107.             // Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
  108.             delay(10);
  109.         }
  110.     }
  111.     wifiSurvey = "";
  112.     wifiSurvey = wifi1 + wifi2 + wifi3 + wifi4 + wifi5 + wifi6 + wifi7;
  113.     Serial.println("\n\n");
  114.     Serial.println(wifiSurvey.length());
  115.     Serial.println(wifiSurvey);
  116.  
  117.     // Wait a bit before scanning again
  118.     delay(5000);
  119. }
  120.  
  121. void requestEvent() {
  122.   for (int i = 0; i < 26; i++) { Wire.write(wifiSurvey[i]); }
  123.   // (wifiSurvey.length()+1)
  124. }
...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

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: I2C beginner request

Postby lbernstone » Thu May 18, 2023 5:43 pm

Compiles for me. Seems like you have a version of Wire.h outside of the board libraries. Set your preferences to show the compilation output, and watch what version of Wire.h it is using.

benorange
Posts: 2
Joined: Wed May 17, 2023 5:16 pm

Re: I2C beginner request

Postby benorange » Fri May 19, 2023 11:18 am

Hi @lbernstone,

Thanks and sounds reasonable. I haven't been able to fix it yet but am on the case with verbose output for complier and happy to have a clue where to look.

Bw,
Ben.

Who is online

Users browsing this forum: No registered users and 54 guests