Created
December 31, 2017 14:57
-
-
Save ma2shita/978a8d382217a6d40d0aad1a3e8b0208 to your computer and use it in GitHub Desktop.
Define setting in Wio LTE by OTA (Tag of SORACOM Air SIM)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Tag on SORACOM Air SIM | |
key=_conf | |
value={"working_mode":"uptime","loop_interval":5000,"send_cloud":true,"endpoint_host":"harvest.soracom.io","endpoint_port":8514} | |
(alt)value={"working_mode":"led","loop_interval":1000} | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <WioLTEforArduino.h> | |
WioLTE Wio; | |
#include <WioLTEClient.h> | |
WioLTEClient WioClient(&Wio); | |
#include <ArduinoJson.h> | |
#define USE_MODEM | |
#define CONF_TAG_NAME "_conf" | |
String working_mode; | |
int loop_interval = 10000; | |
boolean send_cloud = false; | |
const char *endpoint_host; | |
int endpoint_port = 0; | |
void conf_by_metadata(); | |
void udp_send(const char *host, int port, const char *payload, char response[]); | |
void setup() | |
{ | |
delay(1000); | |
SerialUSB.println(""); | |
SerialUSB.println("%% Wio.Init"); | |
Wio.Init(); | |
Wio.PowerSupplyGrove(true); | |
Wio.LedSetRGB(0, 20, 16); | |
#ifdef USE_MODEM | |
Wio.PowerSupplyLTE(true); | |
delay(5000); | |
Wio.LedSetRGB(31, 31, 0); | |
SerialUSB.println("%% Wio.TurnOnOrReset"); | |
if (!Wio.TurnOnOrReset()) | |
{ | |
working_mode = "led"; | |
SerialUSB.println("%% ERROR: Wio.TurnOnOrReset"); | |
return; | |
} | |
Wio.LedSetRGB(31, 31, 31); | |
SerialUSB.println("%% Wio.Activate"); | |
delay(5000); | |
if (!Wio.Activate("soracom.io", "sora", "sora")) | |
{ | |
SerialUSB.println("%% ERROR: Wio.Activate"); | |
return; | |
} | |
Wio.LedSetRGB(0, 0, 31); | |
conf_by_metadata(); | |
#endif | |
Wio.LedSetRGB(0, 0, 0); | |
if (working_mode.equalsIgnoreCase("led")) | |
{ | |
// Setup for parameters in working mode when if you need | |
} | |
else if (working_mode.equalsIgnoreCase("uptime")) | |
{ | |
// Setup for parameters in working mode when if you need | |
} | |
} | |
volatile boolean led_state = false; | |
void loop() | |
{ | |
SerialUSB.println("%% loop"); | |
if (working_mode.equalsIgnoreCase("uptime")) | |
{ | |
char payload[1024]; | |
sprintf(payload, "{\"uptime\":%lu}", millis() / 1000); | |
SerialUSB.println(payload); | |
char response[1024]; | |
if (send_cloud) | |
{ | |
udp_send(endpoint_host, endpoint_port, payload, response); | |
SerialUSB.println(response); | |
} | |
delay(loop_interval); | |
} | |
else | |
{ | |
led_state = !led_state; | |
if (led_state) | |
{ | |
Wio.LedSetRGB(0, 20, 16); | |
} | |
else | |
{ | |
Wio.LedSetRGB(0, 0, 0); | |
} | |
delay(loop_interval); | |
} | |
} | |
void conf_by_metadata() | |
{ | |
#ifdef USE_MODEM | |
char subscriber[2048]; | |
SerialUSB.println("%% Wio.HttpGet"); | |
if (!Wio.HttpGet("http://metadata.soracom.io/v1/subscriber", subscriber, sizeof(subscriber))) | |
{ | |
SerialUSB.println("%% ERROR: HttpGet"); | |
return; | |
} | |
StaticJsonBuffer<2048> jsonBuffer; | |
JsonObject &root = jsonBuffer.parseObject(subscriber); | |
if (!root.success()) | |
{ | |
SerialUSB.println("%% ERROR: JsonObject(root).success"); | |
return; | |
} | |
const char *conf_str = root["tags"][CONF_TAG_NAME]; | |
SerialUSB.println("conf_str:", conf_str); | |
JsonObject &conf = jsonBuffer.parseObject(conf_str); | |
if (!conf.success()) | |
{ | |
SerialUSB.println("%% ERROR: JsonObject(conf).success"); | |
return; | |
} | |
working_mode = conf["working_mode"].as<String>(); | |
loop_interval = conf["loop_interval"].as<int>(); | |
send_cloud = conf["send_cloud"].as<boolean>(); | |
endpoint_host = conf["endpoint_host"]; | |
endpoint_port = conf["endpoint_port"].as<int>(); | |
#else | |
// for development in local | |
working_mode = "uptime"; | |
loop_interval = 3000; | |
send_cloud = false; | |
endpoint_host = "harvest.soracom.io"; | |
endpoint_port = 8514; | |
#endif | |
} | |
void udp_send(const char *host, int port, const char *payload, char response[]) | |
{ | |
int connectId = Wio.SocketOpen(host, port, WIOLTE_UDP); | |
if (connectId < 0) | |
{ | |
SerialUSB.println("%% ERROR: Wio.SocketOpen"); | |
goto err_udp_send; | |
} | |
if (!Wio.SocketSend(connectId, payload)) | |
{ | |
SerialUSB.println("%% ERROR: Wio.SocketSend"); | |
goto err_udp_send; | |
} | |
int length; | |
char res[1024]; | |
do | |
{ | |
length = Wio.SocketReceive(connectId, res, sizeof(res)); | |
if (length < 0) | |
{ | |
SerialUSB.println("%% ERROR: Wio.SocketReceive"); | |
goto err_udp_send; | |
} | |
} while (length == 0); | |
for (uint16_t i = 0; i < sizeof(res); i++) | |
response[i] = res[i]; | |
err_udp_send: | |
if (!Wio.SocketClose(connectId)) | |
{ | |
SerialUSB.println("%% ERROR: Wio.SocketClose"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment