Skip to content

Commit e61f22e

Browse files
committed
add arduino metadata files and samples
1 parent fbecd92 commit e61f22e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+426
-31
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ add_custom_target(
5151
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
5252
)
5353

54-
add_executable(${PROJECT_NAME} ${SRC_LIST} src/example.cpp src/Conf.h src/web3/Web3.cpp src/web3/Web3.h src/web3/Log.cpp src/web3/Log.h src/web3/Contract.cpp src/web3/Contract.h)
54+
add_executable(${PROJECT_NAME} ${SRC_LIST} src/example/example.cpp src/Conf.h src/Web3.cpp src/Web3.h src/Log.cpp src/Log.h src/web3/Contract.cpp src/web3/Contract.h)

KEYWORD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
########################################################
2+
# Class
3+
###################################################################
4+
5+
Web3 KEYWORD1
6+
Contract KEYWORD1
7+
8+
###################################################################
9+
# Methods and Functions
10+
###################################################################
11+
12+
Web3ClientVersion KEYWORD2
13+
Web3Sha3 KEYWORD2
14+
NetVersion KEYWORD2
15+
NetListening KEYWORD2
16+
NetPeerCount KEYWORD2
17+
EthProtocolVersion KEYWORD2
18+
EthSyncing KEYWORD2
19+
EthMining KEYWORD2
20+
EthHashrate KEYWORD2
21+
EthGasPrice KEYWORD2
22+
EthAccounts KEYWORD2
23+
EthBlockNumber KEYWORD2
24+
EthGetTransactionCount KEYWORD2
25+
EthCall KEYWORD2
26+
EthSendSignedTransaction KEYWORD2
27+
SetPrivateKey KEYWORD2
28+
SetupContractData KEYWORD2
29+
Call KEYWORD2
30+
SendTransaction KEYWORD2
31+
32+
###################################################################
33+
# Constants
34+
###################################################################
35+

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@
4040
- [secp256k1](https://github.com/bitcoin-core/secp256k1)
4141
- [ESP32-Arduino](https://github.com/espressif/arduino-esp32)
4242

43-

example/basic_eth/basic_eth.ino

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <WiFi.h>
2+
#include <Web3.h>
3+
4+
#define USE_SERIAL Serial
5+
6+
#define ENV_SSID "<YOUR_SSID>"
7+
#define ENV_WIFI_KEY "<YOUR_PASSWORD>"
8+
#define INFURA_HOST "rinkeby.infura.io"
9+
#define INFURA_PATH "/<YOUR_INFURA_ID>"
10+
#define ADDRESS "0x<YOUR_ETH_ADDRESS>"
11+
12+
Web3 web3(INFURA_HOST, INFURA_PATH);
13+
14+
void eth_example();
15+
16+
void setup() {
17+
USE_SERIAL.begin(115200);
18+
19+
for(uint8_t t = 4; t > 0; t--) {
20+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
21+
USE_SERIAL.flush();
22+
delay(1000);
23+
}
24+
25+
WiFi.begin(ENV_SSID, ENV_WIFI_KEY);
26+
27+
// attempt to connect to Wifi network:
28+
while (WiFi.status() != WL_CONNECTED) {
29+
Serial.print(".");
30+
// wait 1 second for re-trying
31+
delay(1000);
32+
}
33+
34+
USE_SERIAL.println("Connected");
35+
36+
eth_example();
37+
}
38+
39+
void loop() {
40+
// put your main code here, to run repeatedly:
41+
}
42+
43+
void eth_example() {
44+
char tmp[32];
45+
46+
double version = web3.EthProtocolVersion();
47+
USE_SERIAL.println("eth_protocolVersion");
48+
USE_SERIAL.println(version);
49+
50+
bool listening = web3.EthSyncing();
51+
USE_SERIAL.println("eth_syncing");
52+
if (listening) {
53+
USE_SERIAL.println("syncing");
54+
} else{
55+
USE_SERIAL.println("not syncing");
56+
}
57+
58+
bool mining = web3.EthMining();
59+
USE_SERIAL.println("eth_mining");
60+
if (mining) {
61+
USE_SERIAL.println("mining");
62+
} else{
63+
USE_SERIAL.println("not mining");
64+
}
65+
66+
double hashrate = web3.EthHashrate();
67+
USE_SERIAL.println("eth_hashrate");
68+
USE_SERIAL.println(hashrate);
69+
70+
long long int gasPrice = web3.EthGasPrice();
71+
USE_SERIAL.println("eth_gasPrice");
72+
memset(tmp, 0, 32);
73+
sprintf(tmp, "%lld", gasPrice);
74+
USE_SERIAL.println(tmp);
75+
76+
int blockNumber = web3.EthBlockNumber();
77+
USE_SERIAL.println("eth_blockNumber");
78+
memset(tmp, 0, 32);
79+
sprintf(tmp, "%d", blockNumber);
80+
USE_SERIAL.println(tmp);
81+
82+
long long int balance = web3.EthGetBalance(ADDRESS);
83+
USE_SERIAL.println("eth_getBalance");
84+
memset(tmp, 0, 32);
85+
sprintf(tmp, "%lld", balance);
86+
USE_SERIAL.println(tmp);
87+
88+
int txcount = web3.EthGetTransactionCount(ADDRESS);
89+
USE_SERIAL.println("eth_getBalance");
90+
memset(tmp, 0, 32);
91+
sprintf(tmp, "%d", txcount);
92+
USE_SERIAL.println(tmp);
93+
94+
}

example/basic_net/basic_net.ino

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <WiFi.h>
2+
#include <Web3.h>
3+
4+
#define USE_SERIAL Serial
5+
6+
#define ENV_SSID "<YOUR_SSID>"
7+
#define ENV_WIFI_KEY "<YOUR_PASSWORD>"
8+
#define INFURA_HOST "rinkeby.infura.io"
9+
#define INFURA_PATH "/<YOUR_INFURA_ID>"
10+
11+
Web3 web3(INFURA_HOST, INFURA_PATH);
12+
13+
void net_example();
14+
15+
void setup() {
16+
USE_SERIAL.begin(115200);
17+
18+
for(uint8_t t = 4; t > 0; t--) {
19+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
20+
USE_SERIAL.flush();
21+
delay(1000);
22+
}
23+
24+
WiFi.begin(ENV_SSID, ENV_WIFI_KEY);
25+
26+
// attempt to connect to Wifi network:
27+
while (WiFi.status() != WL_CONNECTED) {
28+
Serial.print(".");
29+
// wait 1 second for re-trying
30+
delay(1000);
31+
}
32+
33+
USE_SERIAL.println("Connected");
34+
35+
net_example();
36+
}
37+
38+
void loop() {
39+
// put your main code here, to run repeatedly:
40+
}
41+
42+
void net_example() {
43+
char result[128];
44+
45+
int version = web3.NetVersion();
46+
USE_SERIAL.println("net_version");
47+
USE_SERIAL.println(version); // 4
48+
49+
bool listening = web3.NetListening();
50+
USE_SERIAL.println("net_listening");
51+
if (listening) {
52+
USE_SERIAL.println("listening");
53+
} else{
54+
USE_SERIAL.println("not listening");
55+
}
56+
57+
memset(result, 0, 128);
58+
int peerCount = web3.NetPeerCount();
59+
USE_SERIAL.println("net_peerCount");
60+
USE_SERIAL.println(peerCount); // 100
61+
}
62+

example/basic_web3/basic_web3.ino

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <WiFi.h>
2+
#include <Web3.h>
3+
4+
#define USE_SERIAL Serial
5+
6+
#define ENV_SSID "<YOUR_SSID>"
7+
#define ENV_WIFI_KEY "<YOUR_PASSWORD>"
8+
#define INFURA_HOST "rinkeby.infura.io"
9+
#define INFURA_PATH "/<YOUR_INFURA_ID>"
10+
11+
Web3 web3(INFURA_HOST, INFURA_PATH);
12+
13+
void web3_example();
14+
15+
void setup() {
16+
USE_SERIAL.begin(115200);
17+
18+
for(uint8_t t = 4; t > 0; t--) {
19+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
20+
USE_SERIAL.flush();
21+
delay(1000);
22+
}
23+
24+
WiFi.begin(ENV_SSID, ENV_WIFI_KEY);
25+
26+
// attempt to connect to Wifi network:
27+
while (WiFi.status() != WL_CONNECTED) {
28+
Serial.print(".");
29+
// wait 1 second for re-trying
30+
delay(1000);
31+
}
32+
33+
USE_SERIAL.println("Connected");
34+
35+
web3_example();
36+
}
37+
38+
void loop() {
39+
// put your main code here, to run repeatedly:
40+
}
41+
42+
void web3_example() {
43+
char result[128];
44+
45+
memset(result, 0, 128);
46+
web3.Web3ClientVersion(result);
47+
USE_SERIAL.println("web3_ClientVersion");
48+
USE_SERIAL.println(result); // Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9
49+
50+
memset(result, 0, 128);
51+
web3.Web3Sha3("0x68656c6c6f20776f726c64", result);
52+
USE_SERIAL.println("web3_sha3");
53+
USE_SERIAL.println(result); // 0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad
54+
}

example/eth_call/eth_call.ino

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <WiFi.h>
2+
#include <Web3.h>
3+
#include <Contract.h>
4+
5+
#define USE_SERIAL Serial
6+
7+
#define ENV_SSID "<YOUR_SSID>"
8+
#define ENV_WIFI_KEY "<YOUR_PASSWORD>"
9+
#define MY_ADDRESS "0x<MY_ADDRESS>"
10+
#define CONTRACT_ADDRESS "0x<CONTRACT_ADDRESS>"
11+
#define INFURA_HOST "rinkeby.infura.io"
12+
#define INFURA_PATH "/<YOUR_INFURA_ID>"
13+
14+
Web3 web3(INFURA_HOST, INFURA_PATH);
15+
16+
void eth_call_example();
17+
18+
void setup() {
19+
USE_SERIAL.begin(115200);
20+
21+
for(uint8_t t = 4; t > 0; t--) {
22+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
23+
USE_SERIAL.flush();
24+
delay(1000);
25+
}
26+
27+
WiFi.begin(ENV_SSID, ENV_WIFI_KEY);
28+
29+
// attempt to connect to Wifi network:
30+
while (WiFi.status() != WL_CONNECTED) {
31+
Serial.print(".");
32+
// wait 1 second for re-trying
33+
delay(1000);
34+
}
35+
36+
USE_SERIAL.println("Connected");
37+
38+
eth_call_example();
39+
}
40+
41+
void loop() {
42+
// put your main code here, to run repeatedly:
43+
}
44+
45+
void eth_call_example() {
46+
char result[512];
47+
memset(result, 0, 512);
48+
Contract contract(&web3, CONTRACT_ADDRESS);
49+
strcpy(contract.options.from, MY_ADDRESS);
50+
strcpy(contract.options.gasPrice,"2000000000000");
51+
contract.options.gas = 5000000;
52+
contract.SetupContractData(result, "get()");
53+
contract.Call(result);
54+
USE_SERIAL.println(result);
55+
}

example/eth_send/eth_send.ino

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <WiFi.h>
2+
#include <Web3.h>
3+
#include <Contract.h>
4+
5+
#define USE_SERIAL Serial
6+
7+
#define ENV_SSID "<YOUR_SSID>"
8+
#define ENV_WIFI_KEY "<YOUR_PASSWORD>"
9+
#define MY_ADDRESS "0x<MY_ADDRESS>"
10+
#define CONTRACT_ADDRESS "0x<CONTRACT_ADDRESS>"
11+
#define INFURA_HOST "rinkeby.infura.io"
12+
#define INFURA_PATH "/<YOUR_INFURA_ID>"
13+
14+
const char PRIVATE_KEY[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
15+
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
16+
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
17+
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
18+
Web3 web3(INFURA_HOST, INFURA_PATH);
19+
20+
void eth_send_example();
21+
22+
void setup() {
23+
USE_SERIAL.begin(115200);
24+
25+
for(uint8_t t = 4; t > 0; t--) {
26+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
27+
USE_SERIAL.flush();
28+
delay(1000);
29+
}
30+
31+
WiFi.begin(ENV_SSID, ENV_WIFI_KEY);
32+
33+
// attempt to connect to Wifi network:
34+
while (WiFi.status() != WL_CONNECTED) {
35+
Serial.print(".");
36+
// wait 1 second for re-trying
37+
delay(1000);
38+
}
39+
40+
USE_SERIAL.println("Connected");
41+
42+
eth_send_example();
43+
}
44+
45+
void loop() {
46+
// put your main code here, to run repeatedly:
47+
}
48+
49+
void eth_send_example() {
50+
char result[32];
51+
memset(result, 0, 32);
52+
53+
Contract contract(&web3, CONTRACT_ADDRESS);
54+
contract.SetPrivateKey((uint8_t*)PRIVATE_KEY);
55+
uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount((char *)MY_ADDRESS);
56+
57+
uint32_t gasPriceVal = 141006540;
58+
uint32_t gasLimitVal = 3000000;
59+
uint8_t toStr[] = CONTRACT_ADDRESS;
60+
uint8_t valueStr[] = "0x00";
61+
uint8_t dataStr[100];
62+
memset(dataStr, 0, 100);
63+
contract.SetupContractData((char*)dataStr, "set(uint256)", 123);
64+
contract.SendTransaction((uint8_t *) result,
65+
nonceVal, gasPriceVal, gasLimitVal, toStr, valueStr, dataStr);
66+
67+
USE_SERIAL.println(result);
68+
}

0 commit comments

Comments
 (0)