2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5StampS3、温度を無線で送る。ESP_NOW_Serial_Class、M5StampS3同士

Posted at

x 過去ログを見よ!!
x インサーネットフレームの意味が分かる人
x mac addの意味がわかる人
x mac addの宛先と自分がわかる人
x mac addの宛先を書き換えられる人
x なぜかアナログ入力も起動で失敗する。何度か電源を入れ直す。
x なぜかUSBシリアルの起動が失敗するので起動画面が出るまでシリアルモニタをオンオフする
x 3.0.7
x 温度センサーは、スイッチサイエンスで売っているseeedのGroveのV1.2のやつ

ここを都合よく書き換える


const MacAddress peer_mac({0x48, 0xCA, 0x43, 0x3A, 0x42, 0xEC});
//const MacAddress peer_mac({0x48, 0x27, 0xE2, 0xE3, 0xBD, 0x50});
//const MacAddress peer_mac({0x40, 0x4C, 0xCA, 0x5A, 0xE3, 0x1C});

結果

o_coq611.jpg

o_coq612.jpg

o_coq610.jpg

o_coq613.jpg

プログラム

(ホスト StampS3(温度センサー))



//ESP_NOW_Serial_TMP_M5StampS3_1


//インクルド
#include "ESP32_NOW_Serial.h"
#include "MacAddress.h"
#include "WiFi.h"

#include "esp_wifi.h"

#include <math.h> //logの計算等で使う


//定義
// Channel to be used by the ESP-NOW protocol
#define ESPNOW_WIFI_CHANNEL 1
#define ESPNOW_WIFI_MODE WIFI_STA     // WiFi Mode
#define ESPNOW_WIFI_IF   WIFI_IF_STA  // WiFi Interface

//ここは、相手先のMACアドレスを記述する。
// Set the MAC address of the device that will receive the data
// For example: XX:XX:XX:XX:XX:XX
const MacAddress peer_mac({0x48, 0xCA, 0x43, 0x3A, 0x42, 0xEC});
//const MacAddress peer_mac({0x48, 0x27, 0xE2, 0xE3, 0xBD, 0x50});
//const MacAddress peer_mac({0x40, 0x4C, 0xCA, 0x5A, 0xE3, 0x1C});

ESP_NOW_Serial_Class NowSerial(peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);


//MACを16進に変換
char *mac_add_conv(const uint8_t *a) {

  char h[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  static char b[16 + 1];

  for(int i=0;i<6;i++){
    b[i*2+0] = h[a[i] >> 4];
    b[i*2+1] = h[a[i] & 0x0f];
  }
  b[12] = 0;

  return ( b ); //戻り値
  
}//mac_add_conv


//初期化
void setup() {

  pinMode(15, ANALOG);

  //WiFiモードとWifiチャンネルの設定
  WiFi.mode(ESPNOW_WIFI_MODE);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);

  //シリアルポートの初期化
  Serial.begin(115200);

  //WiFiの起動待ち
  for (int i = 0; i < (6+3); i++) {
    Serial.print(".");
    delay(500); //接続待ち
  }//for
  while (!(WiFi.STA.started())) {
    delay(500); //接続待ち
    Serial.print(".");
  }//while
  Serial.println();

  //WiFiメッセージの表示
  Serial.print("to  MAC Address: " );
  Serial.println( mac_add_conv( peer_mac  ) );
  Serial.print("WiFi Mode: ");
  Serial.println("Station");
  Serial.print("Channel: ");
  Serial.println(ESPNOW_WIFI_CHANNEL);
  Serial.print("MAC Address: ");
  Serial.println(WiFi.macAddress());

  //ESP_NOW_Serialの起動
  // Start the ESP-NOW communication
  Serial.println("ESP-NOW communication starting...");
  NowSerial.begin(115200);
  Serial.println("You can now send data to the peer device using the Serial Monitor.\n");

}//setup


//1文字の送信
void now_ch(char ch) {

  while ( !(NowSerial.availableForWrite()) ) {
    delay(5);//連続送信防止
    Serial.println(".");
  }//while

  if (NowSerial.write(ch) <= 0) {
    Serial.println("Failed to send data");
  }//endif

}


//文字列の表示
void now_print(char *str1)
{
  //文字の中身がゼロか
  while (*str1) {

    //一文字出力
    now_ch(*str1 ++);

  }//while

}//now_print


//メインループ
void loop() {

  //↓温度センサーの処理を開始

  static uint32_t msg_count = 0;

  static char data[256+1];

  const int B = 4275;               // B value of the thermistor
  const int R0 = 100000;            // R0 = 100k

  const int pinTempSensor = 15;     // Grove - Temperature Sensor connect to G1

  int a = analogRead(pinTempSensor);

  //float R = 1023.0/a-1.0;
  //R = R0*R;
  float R,v1,a1,o1; 
  v1 = ((float)a)*(3.3/4096.0); //電圧を求める
  a1 = v1 / 100000.0;           //電流を求める
  o1 = 5.0 / a1;                //全体の抵抗を求める
  R = o1 - 100000.0;    //全体の抵抗から検出抵抗を引く

  float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet

  //↑温度センサーの処理が終了

  a = temperature;
  data[2] = 0;
  data[1] = '0' + a % 10;
  data[0] = '0' + a / 10;
  if( data[0] == '0' ) {data[0] = ' ';}

  
  now_print("\n");
  now_print("\f"); //改ページ
  //         12345678
  now_print("========\n");
  now_print(" Temp.\n");
  now_print("     ");
  now_print(data);now_print("\n");
  now_print("========");

  delay(1000);//1秒待つ
  
}//loop


(スレーブ STampS3 受信)



//ESP_NOW_Serial_SSD1306_M5NanoC6_1


//インクルド
#include "ESP32_NOW_Serial.h"
#include "MacAddress.h"
#include "WiFi.h"
#include "esp_wifi.h"


//定義
// Channel to be used by the ESP-NOW protocol
#define ESPNOW_WIFI_CHANNEL 1
#define ESPNOW_WIFI_MODE WIFI_STA     // WiFi Mode
#define ESPNOW_WIFI_IF   WIFI_IF_STA  // WiFi Interface

//ここは、相手先のMACアドレスを記述する。
// Set the MAC address of the device that will receive the data
// For example: XX:XX:XX:XX:XX:XX
const MacAddress peer_mac({0x48, 0x27, 0xE2, 0xE3, 0xBD, 0x50});
//const MacAddress peer_mac({0x40, 0x4C, 0xCA, 0x5A, 0xE3, 0x1C});
//const MacAddress peer_mac({0x48, 0xCA, 0x43, 0x3A, 0x42, 0xEC});

//無線のシリアル
ESP_NOW_Serial_Class NowSerial(peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);


//MACを16進に変換
char *mac_add_conv(const uint8_t *a) {

  char h[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
               };
  static char b[16 + 1];

  for (int i = 0; i < 6; i++) {
    b[i * 2 + 0] = h[a[i] >> 4];
    b[i * 2 + 1] = h[a[i] & 0x0f];
  }//for
  b[12] = 0;

  return ( b ); //戻り値

}//mac_add_conv


//1文字の送信
void now_ch(char ch) {

  while ( !(NowSerial.availableForWrite()) ) {
    delay(5);//連続送信防止
    Serial.println(".");
  }//while

  //送信
  if (NowSerial.write(ch) <= 0) {
    Serial.println("Failed to send data");
  }//endif

}//now_ch


//文字列の送信
void now_print(char *str1)
{
  //文字の中身がゼロか
  while (*str1) {
    now_ch(*str1 ++); /*一文字出力*/
  }//while
}//now_print


//初期化
void setup() {

  //WiFiモードとWifiチャンネルの設定
  WiFi.mode(ESPNOW_WIFI_MODE);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);

  //シリアルポートの初期化
  Serial.begin(115200);

  //WiFiの起動待ち
  for (int i = 0; i < (6 + 3); i++) {
    Serial.print(".");
    delay(500); //接続待ち
  }//for
  while (!(WiFi.STA.started())) {
    delay(500); //接続待ち
    Serial.print(".");
  }//while
  Serial.println();

  //WiFiメッセージの表示
  Serial.print("to  MAC Address: " );
  Serial.println( mac_add_conv( peer_mac  ) );
  Serial.print("WiFi Mode: ");
  Serial.println("Station");
  Serial.print("Channel: ");
  Serial.println(ESPNOW_WIFI_CHANNEL);
  Serial.print("MAC Address: ");
  Serial.println(WiFi.macAddress());

  //ESP_NOW_Serialの起動
  // Start the ESP-NOW communication
  Serial.println("ESP-NOW communication starting...");
  NowSerial.begin(115200); //無線シリアル
  Serial.println("You can now send data to the peer device using the Serial Monitor.\n");

}//setup


//メインループ
void loop() {

  static char data[4096 + 1]; //文字列の一時領域
  int str_num = 0; //文字列カウンター

  //受信バッファーが空になるまで文字列を読み込む
  str_num = 0; data[0] = 0;
  while (NowSerial.available()) {
    data[str_num] = NowSerial.read(); //読み取り
    str_num++;
    if ( str_num > 4095 ) {str_num = 4095;}
    data[str_num] = 0;
  }//while

  //文字列カウンターが0文字以外の時に受信文字列を出力
  if ( str_num != 0 ) {
    Serial.print(data); //出力
  }//endif

  delay(300); //0.3秒待つ

}//loop


2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?