Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Created February 7, 2018 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ma2shita/c566da46d363426fc61d286d4a18f1f4 to your computer and use it in GitHub Desktop.
Save ma2shita/c566da46d363426fc61d286d4a18f1f4 to your computer and use it in GitHub Desktop.
Grove UltrasonicRanger plot to SORACOM Harvest (for CodeZine)
/* CodeZine: Grove IoT Starter Kit for SORACOM <grove-ultrasonic-ranger_soracom-harvest.ino> */
#include <stdio.h>
#include <WioLTEforArduino.h>
WioLTE Wio;
#include <Ultrasonic.h>
#define ULTRASONIC_PIN (WIOLTE_D38)
#define INTERVAL (3000)
#define RECEIVE_TIMEOUT (10000)
Ultrasonic UltrasonicRanger(ULTRASONIC_PIN);
void setup()
{
delay(200);
SerialUSB.println("");
SerialUSB.println("--- START ---------------------------------------------------");
SerialUSB.println("### I/O Initialize.");
Wio.Init();
SerialUSB.println("### Power supply ON.");
Wio.PowerSupplyLTE(true);
delay(500);
SerialUSB.println("### Turn on or reset.");
if (!Wio.TurnOnOrReset()) {
SerialUSB.println("### ERROR! ###");
return;
}
SerialUSB.println("### Connecting to \"soracom.io\".");
if (!Wio.Activate("soracom.io", "sora", "sora")) {
SerialUSB.println("### ERROR! ###");
return;
}
}
void loop()
{
long distance;
distance = UltrasonicRanger.MeasureInCentimeters();
char data[128];
sprintf(data,"{\"distance_cm\":%lu}", distance);
SerialUSB.println("### Open.");
int connectId;
connectId = Wio.SocketOpen("harvest.soracom.io", 8514, WIOLTE_UDP);
if (connectId < 0) {
SerialUSB.println("### ERROR! ###");
goto err;
}
SerialUSB.println("### Send.");
SerialUSB.print("Send:");
SerialUSB.println(data);
if (!Wio.SocketSend(connectId, data)) {
SerialUSB.println("### ERROR! ###");
goto err_close;
}
SerialUSB.println("### Receive.");
int length;
char res[128];
length = Wio.SocketReceive(connectId, res, sizeof(res), RECEIVE_TIMEOUT);
if (length < 0) {
SerialUSB.println("### ERROR! ###");
goto err_close;
}
if (length == 0) {
SerialUSB.println("### RECEIVE TIMEOUT! ###");
goto err_close;
}
SerialUSB.print("Receive:");
SerialUSB.println(res);
err_close:
SerialUSB.println("### Close.");
if (!Wio.SocketClose(connectId)) {
SerialUSB.println("### ERROR! ###");
goto err;
}
err:
delay(INTERVAL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment