サンプルコード
// https://twelite.net/manuals/twelite-spot/example-sketches/advanced-examples/spot-httpbin/latest.html
// https://twelite.net/start-guides/twelite-spot/receive-from-cue.html
#include <Arduino.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h> //UDP を使う、NTPClient に必要
#include <Arduino_JSON.h> //JSON 文字列を扱う ArduinoJsonとは異なる
#include <HTTPClient.h> //HTTPクライアント JsonのPOSTに必要
#include <MWings.h>// Mono Wireless TWELITE Wings API for 32-bit Arduinosライブラリをインクルード
#include "src/NTPClient/NTPClient.h" //現在時刻を取得するためにNTPを使う
//#include <time.h>
// TWELITE on the SPOT
static const int RST_PIN = 5;
static const int PRG_PIN = 4;
static const int LED_PIN = 18;
// Interfaces
static WiFiClientSecure client;
static WiFiUDP ntpUDP;
static NTPClient timeClient(ntpUDP); // JST(UTC+9)
// TWELITE
void onAppAriaPacket(const ParsedAppAriaPacket& packet);
// System Setup
void setup() {
Serial.begin(115200);
Serial2.begin(115200);
WiFi.begin("WIFIのSSID", "WIFIのPASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
if (Twelite.begin(Serial2, LED_PIN, RST_PIN, PRG_PIN, 18, 0x67720102)) {
Serial.println("Started TWELITE.");
}
Twelite.on([](const ParsedAppCuePacket & packet) {
timeClient.begin();
timeClient.update();
Serial.println("u32SourceSerialId:" + String(packet.u32SourceSerialId, DEC));
Serial.println("u8SourceLogicalId:" + String(packet.u8SourceLogicalId, HEX));
Serial.println("u16SequenceNumber:" + String(packet.u16SequenceNumber, DEC));
Serial.println("u8Lqi:" + String(packet.u8Lqi, DEC));
Serial.println("u16SupplyVoltage:" + String(packet.u16SupplyVoltage, DEC) + " mV");
Serial.println("u8AccelEvent:" + String(packet.u8AccelEvent));
Serial.println("bHasAccelEvent:" + String(packet.bHasAccelEvent));
Serial.println("u8SampleCount:" + String(packet.u8SampleCount));
Serial.println("i16SamplesX:" + String(packet.i16SamplesX[0], DEC) + " mG");
Serial.println("i16SamplesY:" + String(packet.i16SamplesY[0], DEC) + " mG");
Serial.println("i16SamplesZ:" + String(packet.i16SamplesZ[0], DEC) + " mG");
Serial.println("u8MagnetState:" + String(packet.u8MagnetState));
Serial.println("bMagnetStateChanged:" + String(packet.bMagnetStateChanged));
Serial.println("u8MagnetState:" + String(packet.u8MagnetState));
Serial.println("bMagnetStateChanged:" + String(packet.bMagnetStateChanged));
Serial.println("unixtime:" + String(timeClient.getEpochTime()));
JSONVar jsonData;
jsonData["u32SourceSerialId"] = String(packet.u32SourceSerialId, DEC); //送信元のシリアルID
jsonData["u8SourceLogicalId"] = String(packet.u8SourceLogicalId, HEX); // 送信元の論理デバイスID
jsonData["u16SequenceNumber"] = String(packet.u16SequenceNumber, DEC); //シーケンス番号
jsonData["u8Lqi"] = String(packet.u8Lqi, DEC); //電波通信品質 50未満(悪い -80dbm 未満)、50~100(やや悪い)、100~150(良好)、150以上(アンテナの近傍)
jsonData["u16SupplyVoltage"] = String(packet.u16SupplyVoltage, DEC) + "mV"; //電源電圧mV
jsonData["u8AccelEvent"] = packet.u8AccelEvent;
jsonData["bHasAccelEvent"] = packet.bHasAccelEvent;
jsonData["i16SamplesX_0"] = packet.i16SamplesX[0];
jsonData["i16SamplesY_0"] = packet.i16SamplesY[0];
jsonData["i16SamplesZ_0"] = packet.i16SamplesZ[0];
jsonData["u8MagnetState"] = packet.u8MagnetState; //磁気イベントID
jsonData["bMagnetStateChanged"] = packet.bMagnetStateChanged; //磁気イベントID
jsonData["unixtime"] = String(timeClient.getEpochTime());
String json = JSON.stringify(jsonData);
HTTPClient http;
Serial.println(json);
Serial.println("http://hogehoge.com?json=" + json);
http.begin("http://hogehoge.com?json=" + json);
int status_code = http.GET(); //POST
Serial.println("status:" + status_code);
if ( status_code == 200 ) { //200以外ではシリアルにエラーを出力
Serial.println("[POST]Send to server");
} else {
Serial.println("[POST]failed to send to server");
}
http.end();
});
}
void loop() {
Twelite.update();
}