Created
August 4, 2017 01:20
-
-
Save ma2shita/4be51957502acfd23cc72b362ac1988f to your computer and use it in GitHub Desktop.
Wio Tracker LTE and SORACOM Harvest w/ Grove touch sensor
This file contains hidden or 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
/* LED status | |
* bootup = Purple | |
* connect sucessful = Green | |
* Sensor Open = White | |
* Sensor Close = Blue | |
*/ | |
/* | |
* D38: "Grove Touch sensor" or "Magnetic door Switch w/ Grove connector made in max" | |
*/ | |
#define GROVE_PIN 38 | |
#define LED_PIN 10 | |
#define HOST "harvest.soracom.io" | |
#define PORT 8514 | |
#define USE_CELLULAR | |
#include <wiolte-driver.h> | |
WioLTE wio; | |
#include <LoopbackStream.h> | |
LoopbackStream msgpack_buffer; | |
LoopbackStream json_buffer; | |
#include "msgpck.h" | |
void error_led() { wio.LedSetRGB(127, 0, 0); } | |
void bootup_led() { wio.LedSetRGB(127, 0, 127); } | |
void connected_led() { wio.LedSetRGB(0, 127, 0); } | |
void sensor_off() { wio.LedSetRGB(63, 63, 63); } | |
void sensor_on() { wio.LedSetRGB(0, 0, 127); } | |
void setup() { | |
delay(500); | |
SerialUSB.println("board> bootup WioLTE w/ LTE module"); | |
wio.Init(); | |
bootup_led(); | |
SerialUSB.println("board> Waiting 15 seconds for Init/PowerOn"); | |
wio.PowerSupplyLTE(true); | |
delay(3000); /* waiting for bootup */ | |
if (wio.TurnOnOrReset()) { | |
SerialUSB.println("modem> TurnOnOrReset successful"); | |
} else { | |
error_led(); | |
SerialUSB.println("modem> TurnOnOrReset failed"); | |
return; | |
} | |
#ifdef USE_CELLULAR | |
if (wio.Activate("soracom.io", "sora", "sora")) { | |
connected_led(); | |
SerialUSB.println("modem> Activate(connect) successful"); | |
delay(1000); | |
} else { | |
error_led(); | |
SerialUSB.println("modem> Activate(connect) failed"); | |
return; | |
} | |
#endif | |
pinMode(GROVE_PIN, INPUT); | |
} | |
uint8_t latest_state = 0; | |
void loop() { | |
uint8_t val = digitalRead(GROVE_PIN); | |
SerialUSB.println(val); | |
if (val == 0) { sensor_off(); } else { sensor_on(); } | |
if (latest_state == val) { return; } else { latest_state = val; } | |
msgpck_write_map_header(&msgpack_buffer, 1); | |
msgpck_write_string(&msgpack_buffer, "val"); /* write key name */ | |
msgpck_write_integer(&msgpack_buffer, val); /* write value */ | |
msgpck_to_json(&json_buffer, &msgpack_buffer); | |
String json = json_buffer.readString(); | |
char payload[json.length()+1]; | |
json.toCharArray(payload, json.length()+1); | |
SerialUSB.println(payload); | |
#ifdef USE_CELLULAR | |
int connectId = wio.SocketOpen(HOST, PORT, WIOLTE_UDP); | |
SerialUSB.print("modem> ConnectId: "); | |
SerialUSB.println(connectId); | |
if (connectId < 0) { | |
SerialUSB.println("modem> SocketOpen failed"); | |
} | |
if (wio.SocketSend(connectId, payload)) { | |
SerialUSB.println("modem> SocketSend Successful"); | |
char res[127]; | |
if (wio.SocketReceive(connectId, res, sizeof (res)) < 0) { | |
SerialUSB.println("modem> SocketReceive failed"); | |
error_led(); | |
} | |
SerialUSB.println(res); | |
} else { | |
SerialUSB.println("modem> SocketSend failed"); | |
error_led(); | |
} | |
if (wio.SocketClose(connectId)) { | |
SerialUSB.println("modem> SocketClose Successful"); | |
} else { | |
SerialUSB.println("modem> SocketClose failed"); | |
error_led(); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment