Skip to content

Instantly share code, notes, and snippets.

@yoronneko
Created February 22, 2020 01:55
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 yoronneko/f12a9f51ec9543cad6f4a6f352d8bc03 to your computer and use it in GitHub Desktop.
Save yoronneko/f12a9f51ec9543cad6f4a6f352d8bc03 to your computer and use it in GitHub Desktop.
Wio LTE M1/NB1 sample program
#include <WioCellLibforArduino.h>
#include <math.h>
#define L_EQUIP 100
#define L_MSG 100
#define T_SEND 1800
#define APN "soracom.io"
#define USERNAME "sora"
#define PASSWORD "sora"
#define WEBHOOK_URL "http://beam.soracom.io:8888/lte-m/"
WioCellular Wio;
void ColorScale(double value, int *rr, int *gg, int *bb)
{ // https://qiita.com/krsak/items/94fad1d3fffa997cb651
int r, g, b;
double tmp_val = cos( 4 * 3.14159 * value );
int col_val = (int)( ( -tmp_val / 2 + 0.5 ) * 255 );
if ( value >= ( 4.0 / 4.0 ) ) { r = 255; g = 0; b = 0; }
else if ( value >= ( 3.0 / 4.0 ) ) { r = 255; g = col_val; b = 0; }
else if ( value >= ( 2.0 / 4.0 ) ) { r = col_val; g = 255; b = 0; }
else if ( value >= ( 1.0 / 4.0 ) ) { r = 0; g = 255; b = col_val; }
else if ( value >= ( 0.0 / 4.0 ) ) { r = 0; g = col_val; b = 255; }
else { r = 0; g = 0; b = 255; }
*rr = r; *gg = g; *bb = b;
return;
}
void setup() {
delay(200);
SerialUSB.begin(115200);
SerialUSB.println("Initialize.");
Wio.Init();
SerialUSB.println("Cellular ON.");
Wio.PowerSupplyCellular(true);
delay(500);
SerialUSB.println("LED ON.");
Wio.PowerSupplyLed(true);
delay(500);
Wio.LedSetRGB(1, 1, 1);
SerialUSB.println("Turn on or reset.");
if (!Wio.TurnOnOrReset()) {
Wio.LedSetRGB(1, 0, 0);
SerialUSB.println("failed.");
}
delay(500);
SerialUSB.println("Connecting to \"" APN "\".");
Wio.SetSelectNetwork(WioCellular::SELECT_NETWORK_MODE_MANUAL_IMSI);
if (!Wio.Activate(APN, USERNAME, PASSWORD)) {
Wio.LedSetRGB(1, 0, 0);
SerialUSB.println("failed.");
}
char imei[L_EQUIP];
SerialUSB.print("IMEI: ");
if (Wio.GetIMEI(imei, L_EQUIP)) SerialUSB.println(imei);
else SerialUSB.println("IMEI read error");
char imsi[L_EQUIP];
SerialUSB.print("IMSI: ");
if (Wio.GetIMSI(imsi, L_EQUIP)) SerialUSB.println(imsi);
else SerialUSB.println("IMSI read error");
}
void loop() {
static int count=T_SEND;
int rssi = Wio.GetReceivedSignalStrength();
char msg[L_MSG];
sprintf(msg, "Time left: %4d RSSI: %d dBm", count, rssi);
SerialUSB.println(msg);
if (rssi == -999) Wio.LedSetRGB(0, 0, 1);
else {
int rr, gg, bb;
ColorScale((rssi+109)/(109.-53.), &rr, &gg, &bb);
int r = int (rr/25.5+0.5);
int g = int (gg/25.5+0.5);
int b = int (bb/25.5+0.5);
Wio.LedSetRGB(r, g, b);
}
count --;
if (count == 0) {
Wio.LedSetRGB(0, 0, 0);
SerialUSB.println(WEBHOOK_URL);
char data[150];
sprintf(data, "{\"rssi\":\"%d\",\"uptime\":\"%lu\"}", rssi, millis() / 1000);
SerialUSB.print("Post: "); SerialUSB.print(data); SerialUSB.println("");
int status=0;
if (!Wio.HttpPost(WEBHOOK_URL, data, &status)) {
SerialUSB.println("Post failed.");
} else {
SerialUSB.print("Status:"); SerialUSB.println(status);
}
count = T_SEND;
}
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment