├── Manual.md ├── README.md ├── examples ├── Server │ └── Server.ino ├── ADT7410Self │ └── ADT7410Self.ino └── Client │ └── Client.ino ├── library.properties ├── keywords.txt ├── src ├── WiFiWire.h └── WiFiWire.cpp └── LICENSE /Manual.md: -------------------------------------------------------------------------------- 1 | https://qiita-com.translate.goog/chrmlinux03/items/e944b58189d92a654a74?_x_tr_sl=ja&_x_tr_tl=en&_x_tr_hl=ja&_x_tr_pto=wapp 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WiFiWire 2 | WiFiWire is equipped with Wire (i2c) protocol in AsyncUDP 3 | You can wirelessly make it look like there is a Wire (i2c) sensor there. 4 | An epoch-making library 5 | -------------------------------------------------------------------------------- /examples/Server/Server.ino: -------------------------------------------------------------------------------- 1 | #include "WiFiWire.h" 2 | #include "AsyncUDP.h" 3 | 4 | WiFiWire WWire(WIFIWIRE_SERVER); 5 | 6 | void setup() 7 | { 8 | Serial.begin( 115200 ); 9 | while (!Serial); 10 | WWire.begin(); 11 | } 12 | 13 | void loop() 14 | { 15 | WWire.update(); 16 | } 17 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=WiFiWire 2 | version=0.0.2 3 | author=@chrmlinux03 4 | maintainer=@chrmlinux03 5 | sentence=Arduino library for Wire on WiFi. 6 | paragraph=Arduino library for Wire on Wifi library. 7 | category=Signal Input/Output 8 | url=https://github.com/chrmlinux/WiFiWire 9 | architectures=* 10 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | WiFiWire KEYWORD1 WiFiWire 2 | WWire KEYWORD1 WiFiWire 3 | 4 | begin KEYWORD2 5 | beginTransmission KEYWORD2 6 | requestFrom KEYWORD2 7 | endTransmission KEYWORD2 8 | available KEYWORD2 9 | read KEYWORD2 10 | write KEYWORD2 11 | update KEYWORD2 12 | setupUDP KEYWORD2 13 | 14 | -------------------------------------------------------------------------------- /examples/ADT7410Self/ADT7410Self.ino: -------------------------------------------------------------------------------- 1 | // 2 | // ADT7410.ino 3 | // THX. by https://hatakekara.com/adt7410/ 4 | // 5 | 6 | #include 7 | #define MYSDA (25) 8 | #define MYSCL (21) 9 | #define SENS_ADRS (0x49) 10 | #define TEMP_REGS ( 0) 11 | #define RECV_BYTE ( 2) 12 | 13 | void setup() 14 | { 15 | Serial.begin( 115200 ); 16 | while (!Serial); 17 | Wire.begin(MYSDA, MYSCL, 400); 18 | 19 | Wire.beginTransmission(SENS_ADRS); 20 | Wire.write(TEMP_REGS); 21 | Wire.endTransmission(false); 22 | Wire.requestFrom(SENS_ADRS, RECV_BYTE); 23 | while(1) { 24 | if (Wire.available() >= RECV_BYTE) break; 25 | } 26 | 27 | uint16_t dt = 0x0000; 28 | float tmp = 0.0; 29 | dt = Wire.read() << 8; 30 | dt |= Wire.read(); 31 | 32 | Serial.printf("dt = 0x%04x\n", dt); 33 | Serial.printf("Temp = %.2f'C\n",(float)(dt >> 3)/16.0); 34 | } 35 | 36 | void loop() 37 | { 38 | } 39 | -------------------------------------------------------------------------------- /examples/Client/Client.ino: -------------------------------------------------------------------------------- 1 | #include "WiFiWire.h" 2 | #include "AsyncUDP.h" 3 | 4 | WiFiWire WWire(WIFIWIRE_CLIENT); 5 | 6 | #define SENS_ADRS (0x49) 7 | #define TEMP_REGS ( 0) 8 | #define RECV_BYTE ( 2) 9 | 10 | float tempRead(void) 11 | { 12 | float rtn = 0.0; 13 | 14 | WWire.beginTransmission(SENS_ADRS); 15 | WWire.write(TEMP_REGS); 16 | WWire.endTransmission(false); 17 | WWire.requestFrom(SENS_ADRS, RECV_BYTE); 18 | while(1) { 19 | if (WWire.available() >= RECV_BYTE) break; 20 | } 21 | 22 | uint16_t dt = 0x0000; 23 | dt = WWire.read() << 8; 24 | dt |= WWire.read(); 25 | 26 | Serial.printf("dt = 0x%04x\n", dt); 27 | rtn = (float)(dt >> 3) / 16.0; 28 | return rtn; 29 | } 30 | 31 | void setup(void) 32 | { 33 | Serial.begin( 115200 ); 34 | while (!Serial); 35 | WWire.begin(); 36 | 37 | Serial.printf("%.2f'C\n", tempRead()); 38 | } 39 | 40 | void loop(void) 41 | { 42 | WWire.update(); 43 | } 44 | -------------------------------------------------------------------------------- /src/WiFiWire.h: -------------------------------------------------------------------------------- 1 | #ifndef _WIFIWIRE_H_ 2 | #define _WIFIWIRE_H_ 3 | 4 | #include "arduino.h" 5 | #include "WiFi.h" 6 | #include "AsyncUDP.h" 7 | 8 | #define RECV_BUF_MAX (16) 9 | #define SEND_BUF_MAX (16) 10 | 11 | #include 12 | #define MYSDA (25) 13 | #define MYSCL (21) 14 | 15 | enum {NOP = 0, SOH, STX, ETX}; 16 | enum {WIFIWIRE_SERVER = 0, WIFIWIRE_CLIENT}; 17 | 18 | #define UDP_SEND_DELAY (1) 19 | 20 | class WiFiWire { 21 | 22 | public: 23 | 24 | WiFiWire(int); 25 | int16_t begin(void); 26 | int16_t beginTransmission(int); 27 | int16_t requestFrom(int, int); 28 | int16_t endTransmission(bool); 29 | int16_t available(void); 30 | int16_t read(void); 31 | int16_t write(uint8_t); 32 | int16_t update(void); 33 | 34 | private: 35 | 36 | AsyncUDP _udp; 37 | const char *_ssid = "WifiWire"; 38 | const char *_pass = "#WifiWire#"; 39 | int _serverKind = WIFIWIRE_SERVER; 40 | int16_t sendUDP(void); 41 | 42 | }; 43 | #endif 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 chrmlinux 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/WiFiWire.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WiFiWire.h" 3 | 4 | uint8_t _send[SEND_BUF_MAX] = {0}; 5 | uint8_t _recv[RECV_BUF_MAX] = {0}; 6 | uint16_t _sendCnt = 0; 7 | uint16_t _recvCnt = 0; 8 | uint16_t _readPos = 0; 9 | 10 | //=========================================== 11 | // 12 | //=========================================== 13 | void setLocal(uint8_t *dt, uint16_t cnt) 14 | { 15 | memcpy(_recv, dt, cnt); 16 | _recvCnt = cnt; 17 | _readPos = 0; 18 | } 19 | 20 | uint8_t getLocal(void) 21 | { 22 | uint8_t rtn = _recv[_readPos]; 23 | _readPos ++; 24 | return rtn; 25 | } 26 | 27 | 28 | //=========================================== 29 | // 30 | // 31 | // 32 | // 33 | //=========================================== 34 | WiFiWire::WiFiWire(int serverKind) 35 | { 36 | _serverKind = serverKind; 37 | } 38 | 39 | //=========================================== 40 | // begin 41 | //=========================================== 42 | int16_t WiFiWire::begin(void) 43 | { 44 | int16_t rtn = 0; 45 | WiFi.disconnect(true, true); 46 | 47 | //=========================================== 48 | // IPAddress set 49 | //=========================================== 50 | IPAddress serverIp = IPAddress(192, 168, 4, 2); 51 | IPAddress gwIp = IPAddress(192, 168, 4, 1); 52 | IPAddress subnet = IPAddress(255, 255, 255, 0); 53 | uint16_t serverPort = 50002; 54 | 55 | if (_serverKind == WIFIWIRE_SERVER) { 56 | 57 | //=========================================== 58 | // Setver 59 | //=========================================== 60 | WiFi.softAPConfig(serverIp, gwIp, subnet); 61 | WiFi.softAP(_ssid, _pass); 62 | _udp.listen(serverPort); 63 | Wire.begin(MYSDA, MYSCL, 400); 64 | memset(_send, 0x0, sizeof(_send)); 65 | _sendCnt = 0; 66 | Serial.print("UDP Listening on IP = "); Serial.print(WiFi.softAPIP()); 67 | Serial.print(":"); Serial.println(serverPort); 68 | 69 | } else { 70 | 71 | //=========================================== 72 | // Client 73 | //=========================================== 74 | WiFi.mode(WIFI_STA); 75 | WiFi.begin(_ssid, _pass); 76 | if (WiFi.waitForConnectResult() != WL_CONNECTED) { 77 | Serial.println("WiFi Failed"); 78 | while (1) { 79 | delay(1000); 80 | } 81 | } 82 | _udp.connect(serverIp, serverPort); 83 | Serial.print("UDP Connect IP = "); Serial.print(serverIp); 84 | Serial.print(":"); Serial.println(serverPort); 85 | } 86 | 87 | //=========================================== 88 | // data on recv set 89 | //=========================================== 90 | _udp.onPacket([](AsyncUDPPacket packet) { 91 | 92 | if (packet.length()) { 93 | 94 | //=========================================== 95 | // on packet ? 96 | //=========================================== 97 | uint8_t send[SEND_BUF_MAX] = {0}; 98 | uint16_t sendCnt = 0; 99 | uint8_t *recv = NULL; 100 | uint8_t cmd = 0; 101 | 102 | recv = packet.data(); 103 | cmd = recv[0]; 104 | 105 | switch (cmd) { 106 | 107 | //=========================================== 108 | // requestFrom 109 | //------------------------------------------- 110 | // Client -> local recv 111 | //=========================================== 112 | case 'F': 113 | { 114 | int adrs = recv[1]; 115 | int cnt = recv[2]; 116 | setLocal(&recv[3], cnt); 117 | } 118 | break; 119 | 120 | //=========================================== 121 | // beginTransmission 122 | //------------------------------------------- 123 | // Server -> Wire 124 | //=========================================== 125 | case 'b': 126 | Wire.beginTransmission(recv[1]); 127 | break; 128 | 129 | //=========================================== 130 | // requestFrom 131 | //------------------------------------------- 132 | // Server -> Wire -> Client 133 | //=========================================== 134 | case 'f': 135 | { 136 | int adrs = recv[1]; 137 | int len = recv[2]; 138 | int rcnt = 0; 139 | sendCnt = 0; 140 | 141 | //=========================================== 142 | // Server -> Wire 143 | //=========================================== 144 | Wire.requestFrom(adrs, len); 145 | send[sendCnt] = 'F'; sendCnt ++; 146 | send[sendCnt] = adrs; sendCnt ++; 147 | send[sendCnt] = len; sendCnt ++; 148 | 149 | //=========================================== 150 | // is Wire.available ? 151 | //=========================================== 152 | while (1) { 153 | if (Wire.available() >= len) break; 154 | rcnt++; 155 | } 156 | 157 | //=========================================== 158 | // Wire.read 159 | //=========================================== 160 | for (int i = 0; i < len; i++) { 161 | send[sendCnt] = Wire.read(); 162 | sendCnt ++; 163 | } 164 | 165 | //=========================================== 166 | // Server -> Client 167 | //=========================================== 168 | // 'F',adrs,cnt,high,low 169 | packet.write(send, sendCnt); 170 | sendCnt = 0; 171 | } 172 | break; 173 | 174 | //=========================================== 175 | // endTransmission 176 | //------------------------------------------- 177 | // Server -> Wire 178 | //=========================================== 179 | case 'e': 180 | Wire.endTransmission(recv[1] ? true : false); 181 | break; 182 | 183 | //=========================================== 184 | // write 185 | //------------------------------------------- 186 | // Server -> Wire 187 | //=========================================== 188 | case 'w': 189 | Wire.write(recv[1]); 190 | break; 191 | 192 | } 193 | } 194 | }); 195 | 196 | } 197 | 198 | //=========================================== 199 | // update 200 | //------------------------------------------- 201 | // delay :) 202 | //=========================================== 203 | int16_t WiFiWire::update(void) 204 | { 205 | uint16_t rtn = 0; 206 | delay(1); 207 | return rtn; 208 | } 209 | 210 | //=========================================== 211 | // beginTransmission 212 | //------------------------------------------- 213 | // Client -> Server 214 | //=========================================== 215 | int16_t WiFiWire::beginTransmission(int adrs) 216 | { 217 | uint16_t rtn = 0; 218 | _sendCnt = 0; 219 | _send[_sendCnt] = 'b'; _sendCnt ++; 220 | _send[_sendCnt] = adrs; _sendCnt ++; 221 | sendUDP(); 222 | return rtn; 223 | } 224 | 225 | //=========================================== 226 | // requestFrom 227 | //------------------------------------------- 228 | // Client -> Server 229 | //=========================================== 230 | int16_t WiFiWire::requestFrom(int adrs, int cnt) 231 | { 232 | uint16_t rtn = 0; 233 | _sendCnt = 0; 234 | _send[_sendCnt] = 'f'; _sendCnt ++; 235 | _send[_sendCnt] = adrs; _sendCnt ++; 236 | _send[_sendCnt] = cnt; _sendCnt ++; 237 | sendUDP(); 238 | return rtn; 239 | } 240 | 241 | //=========================================== 242 | // endTransmission 243 | //------------------------------------------- 244 | // Client -> Server 245 | //=========================================== 246 | int16_t WiFiWire::endTransmission(bool flag) 247 | { 248 | uint16_t rtn = 0; 249 | _sendCnt = 0; 250 | _send[_sendCnt] = 'e'; _sendCnt ++; 251 | _send[_sendCnt] = flag ? true : false; _sendCnt ++; 252 | sendUDP(); 253 | return rtn; 254 | } 255 | 256 | //=========================================== 257 | // available 258 | //------------------------------------------- 259 | // Server -> Client 260 | //=========================================== 261 | int16_t WiFiWire::available(void) 262 | { 263 | uint16_t rtn = _recvCnt; 264 | return rtn; 265 | } 266 | 267 | //=========================================== 268 | // read 269 | //------------------------------------------- 270 | // local recv -> Client 271 | //=========================================== 272 | int16_t WiFiWire::read(void) 273 | { 274 | uint16_t rtn = getLocal(); 275 | _recvCnt --; 276 | return rtn; 277 | } 278 | 279 | //=========================================== 280 | // write 281 | //------------------------------------------- 282 | // Client -> Server 283 | //=========================================== 284 | int16_t WiFiWire::write(uint8_t dt) 285 | { 286 | uint16_t rtn = 0; 287 | _sendCnt = 0; 288 | _send[_sendCnt] = 'w'; _sendCnt ++; 289 | sendUDP(); 290 | return rtn; 291 | } 292 | 293 | //=========================================== 294 | // sendUDP 295 | //------------------------------------------- 296 | // Client -> Server 297 | //=========================================== 298 | int16_t WiFiWire::sendUDP(void) 299 | { 300 | uint16_t rtn = 0; 301 | _udp.write(_send, _sendCnt); 302 | _sendCnt = 0; 303 | delay(UDP_SEND_DELAY); 304 | return rtn; 305 | } 306 | --------------------------------------------------------------------------------