c_cpp ESP32-后Data.ino

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp ESP32-后Data.ino相关的知识,希望对你有一定的参考价值。

/**
 * (c) Ark Ecosystem <info@ark.io>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 **/
 
/**
 * ESP32 Post Data Example Sketch
 *
 * This sketch shows how to use Cpp-Crypto and Cpp-Client to post data to the Ark Blockchain.
 */

 /**
 * NOTE: At the time of this writing, the Cpp-Client and Cpp-Crypto libraries require running the 'ARDUINO_IDE.sh' bash script located in the 'extras/' folder.
 * This converts the libraries to be compatible with the Arduino IDE.
 */

/****************************************/

/**
 * This is where you include the 'arkClient.h' & 'arkCrypto.h' headers.
 * This allows your project to use Ark Cpp-Client & Cpp-Crypto.
 */
#include <arkClient.h>
#include <arkCrypto.h>
/**/

/****************************************/
   
/**
 * This is where you include WiFi header for your board.
 * This example is for the ESP32, and uses the 'WiFi.h' header.
 */
#include <WiFi.h>
/**/

/****************************************/

/**
 * Since we'll be creating a signed transaction,
 * we'll need the ability to use Time-related functions.
 */
#include "time.h"
/**/

/****************************************/

/**
 * This is the WiFi network you'd like your board to connect to.
 */
const char* ssid = "yourSSID";
const char* password = "yourPassword";
/**/

/****************************************/

/**
 * This is a Devnet Node IP and Port.
 * You can find more Ark Peers here: https://github.com/ArkEcosystem/peers
 */
const char* darkIp = "167.114.29.49";
int darkPort = 4003;
/**/

/****************************************/

/**
 * This is YOUR Devnet Address, and your super-secret 'Passphrase'.
 * **MAKE SURE TO ERASE YOUR PASSPHRASE HERE WHEN YOU ARE DONE**
 */
const char* recipientId = "yourArkDevnetAddress";
const char* yourSecretPassphrase = "yourSecretPassphrase";
/**/

/****************************************/

/** 
 *  In this project, we will post data to the Ark Blockchain. 
 *  Specifically, we will use your ESP32's Chip ID to create a SHA256 hash.
 *  We will then put that hash into a Transaction which you will send to yourself.
 *  
 *  Step 1: Get the boards Chip ID
 *  Step 2: cast the Chip ID into a byte array
 *  Step 3: Reverse the byte array (convert to little-endian).
 *  Step 4: Get the SHA256 hash of the byte array of the Chip ID.
 *  Step 5: Convert the hash bytes to a HEX string
 *  Step 6: Build the Transaction with your hash in the vendorField using your own Devnet Address and Passphrase.
 *  Step 7: Put the Transaction Json into a 'Transactions' Json Object buffer.
 *  Step 8: Copy the 'Transactions' buffer object into a string for passing to the Ark Client API.
 *  Step 9: Create the connection to an Ark Devnet Node using Cpp-Client API, and a devnet IP and Port.
 *  Step 10: Use the Ark Client API to send the Transaction Json string.
 *  Step 11: Put the board in deep sleep to prevent looping.
 *  Step 12: Erase your ESP32.
 *  Step 13: Delete your passphrase, and WiFi information from this sketch.
 */

/****************************************/

/**
 * This method gets a SHA256 hash of the byte-representation of your ESP32's Chip ID/MAC address.
 * 
 * assuming your chip id is the uint64_t '12345678901234',
 * your little-endian(reversed) bytes array will be '{ 11, 58, 115, 206, 47, 242 }'.
 * The resulting SHA256 hash would be '1f5fee9739d505531bbc2d40537c4387a66e3b495935fed05117c5d5eaf981ff'.
 */
inline void idHashToBuffer(char hashBuffer[64]) {

  // The ESP32's MAC address length is 6-bytes.
  int idByteLen = 6; 
  
  // First, get the ESP32's chipId.
  // This is basically its uint64_t MAC address.
  uint64_t chipId = ESP.getEfuseMac();
    /**/
    // This part is only to show you whats going on under the hood
    // It's the uint64_t DEC value of your Chip ID
    // Uncomment the 'idBuffer' section below to view the output in your serial monitor
//    char idBuffer[16] = { '0' };
//    snprintf(idBuffer, 16, "%llu", chipId);
//    Serial.print("\n\nYour Chips ID: ");
//    Serial.println(idBuffer);
    /**/

  // Well force the cast to a uint8_t array pointer.
  uint8_t *bytArray = *reinterpret_cast<uint8_t(*)[sizeof(uint64_t)]>(&chipId);

  // ESP32 uses little-endian, so lets reverse the array
  std::reverse(&bytArray[0], &bytArray[idByteLen]);
    /**/
    // More to show you whats going on here
    // This is your little-endian/least-significant-byte/reversed byte array
    // Uncomment the 'byteArray' section below to view the output in your serial monitor
//    Serial.print("\nbytArray: ");
//    for(int i = 0; i < idByteLen; ++i) {
//      Serial.print(bytArray[i]);
//      (i < idByteLen - 1) ? Serial.print(", ") : Serial.println();
//    }
    /**/

  // Now that we have the bytes in the correct order and know the byte length,
  // we'll go ahead and perform a SHA256 hash of the bytes of our Chip ID
  const auto shaHash = Sha256::getHash(&bytArray[0], idByteLen);  

  // We now have the raw hash bytes, lets convert that to hex and move them to the hashBuffer we passed in.
  memmove(hashBuffer, BytesToHex(&shaHash.value[0], &shaHash.value[0] + shaHash.HASH_LEN).c_str(), Sha256::BLOCK_LEN);
    /**/
    // This is what the HEX-encoded SHA256 hash of your Chip ID's bytes look like
    // Uncomment the 'nhashBuffer' section below to view the output in your serial monitor
//    Serial.print("\nhashBuffer: ");
//    Serial.println(hashBuffer);
    /**/
}
/**/

/****************************************/

/**
 * This method builds and returns a Transaction object.
 * 
 * It will build the Transaction using your own Ark Devnet address,
 * 0.00000001 DARK, your ChipId hash being passed in via 'hashBuffer[64], and your Passphrase.
 */
Transaction txFromHash(char hashBuffer[64]) {
    Transaction transaction = Ark::Crypto::Transactions::Builder::buildTransfer(recipientId, 1, hashBuffer, yourSecretPassphrase);
    /**/
    // Uncomment the 'transaction.toJson()' section below to view the output in your serial monitor
//    Serial.print("\ntransaction.toJson(): ");
//    Serial.println(transaction.toJson().c_str());
    /**/
    return transaction;
};
/**/

/****************************************/

void setupWiFi() {
  WiFi.begin(ssid, password); // This starts your boards connection to WiFi.
  while (WiFi.status() != WL_CONNECTED) // This will delay your board from continuing until a WiFi connection is established.
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

/****************************************/

void setup() {
  Serial.begin(115200);

  setupWiFi();

  // Configure your boards time server
  configTime(0, 0, "pool.ntp.org");
}

/****************************************/

void loop() {

  // Get a SHA256 Hash of your ESP32's Chip ID/MAC address.
  char hashBuffer[Sha256::BLOCK_LEN + 1] = { '0' };
  idHashToBuffer(hashBuffer);

  // Use your Chip ID's SHA256 as data for the Transactions 'VendorField'
  Transaction transaction = txFromHash(hashBuffer);

  // Now that we have the TX built and signed,
  // we'll put the transactions Json representation into 'transactions' Json for POSTing to the Ark Blockchain.
  char jsonBuffer[576] = { '\0' };
  snprintf(&jsonBuffer[0], 576, "{\"transactions\":[%s]}", transaction.toJson().c_str());
    /**/
    // Uncomment the 'jsonBuffer' section below to view the output in your serial monitor
//  Serial.print("\njsonBuffer: ");
//  Serial.println(jsonBuffer);
    /**/

  // Let's save the jsonBuffer to a 'string' object for passing to Cpp-Client.
  std::string jsonStr = jsonBuffer;

  // Now we create the connection to the Ark API using our Devnet IP and Port.
  Ark::Client::Connection<Ark::Client::Api> connection(darkIp, darkPort);

  // Finally, let's send the transaction we just created
  std::string txSendResponse = connection.api.transactions.send(jsonStr);
    Serial.print("\ntxSendResponse: ");
    Serial.println(txSendResponse.c_str());

  // If the process was successful,
  // you shoul get a response that looks something like this:
  //{
  //  "data": {
  //    "accept": [
  //      "ab018e15ded2918ef307fb388da0fb91aca736bf7d9d05ca14302f10f5d19eb6"
  //    ],
  //    "broadcast": [
  //      "ab018e15ded2918ef307fb388da0fb91aca736bf7d9d05ca14302f10f5d19eb6"
  //    ],
  //    "excess": [
  //      
  //    ],
  //    "invalid": [
  //      
  //    ]
  //  },
  //  "errors": null
  //}

  // Now that we're done, let's put the board to sleep,
  // we don't want to keep running this task.
  esp_deep_sleep_start();
  
  // I'd also recommend erasing your board after using this sketch.
  // The easiest way is to download the 'esptool: https://github.com/espressif/esptool
  // You'll need Python installed on your system to run the 'esptool.py' file.
  // Your boards port can be found at the bottom-right of the Arduino IDE window.
  // From the command line:
  //     python esptool.py --port /dev/cu.SLAB_USBtoUART erase_flash.

  // Don't forget to remove your Passphrase and WiFi info from this sketch!
}

以上是关于c_cpp ESP32-后Data.ino的主要内容,如果未能解决你的问题,请参考以下文章

esp32断电后需要重新烧录程序

esp32蓝牙频偏

c_cpp ESP8266GCodeSender

c_cpp esp8266-webserver.ino

c_cpp esp8266.tv.ino

c_cpp esp8266oled.ino