Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
wio-lte-sent-to-both-harvest-and-funnel
/* Send to both SORACOM Harvest and SORACOM Funnel */
#include <WioLTEforArduino.h>
#include <stdio.h>
#define INTERVAL (10000)
#define SENSOR_PIN (WIOLTE_D38)
WioLTE Wio;
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(5000);
SerialUSB.println("### Turn on or reset.");
if (!Wio.TurnOnOrReset()) {
SerialUSB.println("### ERROR! ###");
return;
}
SerialUSB.println("### Connecting to \"soracom.io\".");
delay(5000);
if (!Wio.Activate("soracom.io", "sora", "sora")) {
SerialUSB.println("### ERROR! ###");
return;
}
#ifdef SENSOR_PIN
TemperatureAndHumidityBegin(SENSOR_PIN);
#endif
}
void loop() {
char sending_payload[127];
char receive_payload[127];
int harvestConnId;
int funnelConnId;
#ifdef SENSOR_PIN
float temp;
float humi;
if (!TemperatureAndHumidityRead(&temp, &humi)) {
SerialUSB.println("ERROR!");
}
SerialUSB.print("Current humidity = ");
SerialUSB.print(humi);
SerialUSB.print("% ");
SerialUSB.print("temperature = ");
SerialUSB.print(temp);
SerialUSB.println("C");
sprintf(sending_payload,"{\"temp\":%.1f,\"humi\":%.1f}", temp, humi);
#else
sprintf(sending_payload, "{\"uptime\":%lu}", millis() / 1000);
#endif
harvestConnId = Wio.SocketOpen("harvest.soracom.io", 8514, WIOLTE_UDP);
if (harvestConnId < 0) {
SerialUSB.println("### SORACOM Harvest is not available on this connection ###");
}
funnelConnId = Wio.SocketOpen("funnel.soracom.io", 23080, WIOLTE_UDP);
if (funnelConnId < 0) {
SerialUSB.println("### SORACOM Funnel is not available on this connection ###");
}
if ( harvestConnId >= 0 ) {
SerialUSB.println("");
SerialUSB.println("### Send to SORACOM Harvest");
SerialUSB.println(sending_payload);
if (!Wio.SocketSend(harvestConnId, sending_payload)) {
SerialUSB.println("### ERROR! ###");
}
SerialUSB.println("### Receive from SORACOM Harvest");
int length;
do {
length = Wio.SocketReceive(harvestConnId, receive_payload, sizeof (receive_payload));
if (length < 0) {
SerialUSB.println("### ERROR! ###");
}
} while (length == 0);
SerialUSB.print(receive_payload);
SerialUSB.println("### Close on SORACOM Harvest");
if (!Wio.SocketClose(harvestConnId)) {
SerialUSB.println("### ERROR! ###");
}
}
if ( funnelConnId >= 0 ) {
SerialUSB.println("");
SerialUSB.println("### Send to SORACOM Funnel");
SerialUSB.println(sending_payload);
if (!Wio.SocketSend(funnelConnId, sending_payload)) {
SerialUSB.println("### ERROR! ###");
}
SerialUSB.println("### Receive from SORACOM Funnel");
int length;
do {
length = Wio.SocketReceive(funnelConnId, receive_payload, sizeof (receive_payload));
if (length < 0) {
SerialUSB.println("### ERROR! ###");
}
} while (length == 0);
SerialUSB.print(receive_payload);
SerialUSB.println("### Close on SORACOM Funnel");
if (!Wio.SocketClose(funnelConnId)) {
SerialUSB.println("### ERROR! ###");
}
}
delay(INTERVAL);
}
////////////////////////////////////////////////////////////////////////////////////////
//
#ifdef SENSOR_PIN
int TemperatureAndHumidityPin;
void TemperatureAndHumidityBegin(int pin)
{
TemperatureAndHumidityPin = pin;
DHT11Init(TemperatureAndHumidityPin);
}
bool TemperatureAndHumidityRead(float* temperature, float* humidity)
{
byte data[5];
DHT11Start(TemperatureAndHumidityPin);
for (int i = 0; i < 5; i++) data[i] = DHT11ReadByte(TemperatureAndHumidityPin);
DHT11Finish(TemperatureAndHumidityPin);
if(!DHT11Check(data, sizeof (data))) return false;
if (data[1] >= 10) return false;
if (data[3] >= 10) return false;
*humidity = (float)data[0] + (float)data[1] / 10.0f;
*temperature = (float)data[2] + (float)data[3] / 10.0f;
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
//
void DHT11Init(int pin)
{
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);
}
void DHT11Start(int pin)
{
// Host the start of signal
digitalWrite(pin, LOW);
delay(18);
// Pulled up to wait for
pinMode(pin, INPUT);
while (!digitalRead(pin)) ;
// Response signal
while (digitalRead(pin)) ;
// Pulled ready to output
while (!digitalRead(pin)) ;
}
byte DHT11ReadByte(int pin)
{
byte data = 0;
for (int i = 0; i < 8; i++) {
while (digitalRead(pin)) ;
while (!digitalRead(pin)) ;
unsigned long start = micros();
while (digitalRead(pin)) ;
unsigned long finish = micros();
if ((unsigned long)(finish - start) > 50) data |= 1 << (7 - i);
}
return data;
}
void DHT11Finish(int pin)
{
// Releases the bus
while (!digitalRead(pin)) ;
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);
}
bool DHT11Check(const byte* data, int dataSize)
{
if (dataSize != 5) return false;
byte sum = 0;
for (int i = 0; i < dataSize - 1; i++) {
sum += data[i];
}
return data[dataSize - 1] == sum;
}
////////////////////////////////////////////////////////////////////////////////////////
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment