Wio LTE for Arduino で SORACOM Harvest にデータを送るサンプルです。
ダミーの温度と湿度を送っています。
プログラム
soracom_harvest_test/soracom_harvest_test.ino
// ---------------------------------------------------------------
/*
soracom_harvest_test.ino
Aug/26/2024
*/
// ---------------------------------------------------------------
#define PROGRAM "soracom_harvest_test.ino"
#define VERSION "2024-8-26 PM 13:49"
#include <WioLTEforArduino.h>
#include <stdio.h>
#define INTERVAL (6000)
#define RECEIVE_TIMEOUT (15000)
WioLTE Wio;
float tt_aa[] = {20.1, 22.3, 24.8, 25.6, 26.7, 26.1, 25.3, 24.8, 22.6, 21.7};
float hh_aa[] = {50.1, 62.3, 64.8, 75.6, 76.7, 74.1, 72.3, 64.8, 60.6, 56.7};
int icount = 0;
// ---------------------------------------------------------------
void setup() {
delay(200);
SerialUSB.println("");
SerialUSB.println("*** START ***");
setupLTE();
SerialUSB.println(PROGRAM);
SerialUSB.println(VERSION);
SerialUSB.println("### Setup completed. ***");
}
// ---------------------------------------------------------------
void loop() {
char data[100];
float temp;
float humi;
int index = icount % 10;
temp=tt_aa[index];
humi = hh_aa[index];
SerialUSB.println("icount = " + String(icount));
SerialUSB.print("Current humidity = ");
SerialUSB.print(humi);
SerialUSB.print("% ");
SerialUSB.print("temperature = ");
SerialUSB.print(temp);
SerialUSB.println("C");
SerialUSB.println(String(millis()/ 1000));
sprintf(data,"{\"temp\":%.1f,\"humi\":%.1f}", temp, humi);
SerialUSB.println("### Open.");
int connectId;
connectId = Wio.SocketOpen("harvest.soracom.io", 8514, WIOLTE_UDP);
if (connectId < 0) {
SerialUSB.println("### ERROR! SocketOpen ###");
goto err;
}
SerialUSB.println("### Send.");
SerialUSB.print("Send:");
SerialUSB.print(data);
SerialUSB.println("");
if (!Wio.SocketSend(connectId, data)) {
SerialUSB.println("### ERROR! SocketSend ###");
goto err_close;
}
SerialUSB.println("### Receive.");
int length;
length = Wio.SocketReceive(connectId, data, sizeof (data), RECEIVE_TIMEOUT);
if (length < 0) {
SerialUSB.println("### ERROR! SocketReceive ###");
goto err_close;
}
if (length == 0) {
SerialUSB.println("### RECEIVE TIMEOUT! ###");
goto err_close;
}
SerialUSB.print("Receive:");
SerialUSB.print(data);
SerialUSB.println("");
err_close:
SerialUSB.println("### Close.");
if (!Wio.SocketClose(connectId)) {
SerialUSB.println("### ERROR! SocketClose ###");
goto err;
}
err:
delay(INTERVAL);
icount++;
}
// ---------------------------------------------------------------