Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
214 lines (163 sloc) 4.42 KB
#include <WioLTEforArduino.h>
#include <stdio.h>
#define INTERVAL (60000)
#define RECEIVE_TIMEOUT (10000)
// uncomment following line to use Temperature & Humidity sensor
// #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(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;
}
#ifdef SENSOR_PIN
TemperatureAndHumidityBegin(SENSOR_PIN);
#endif // SENSOR_PIN
SerialUSB.println("### Setup completed.");
}
void loop() {
char data[100];
#ifdef SENSOR_PIN
float temp;
float humi;
if (!TemperatureAndHumidityRead(&temp, &humi)) {
SerialUSB.println("ERROR!");
goto err;
}
SerialUSB.print("Current humidity = ");
SerialUSB.print(humi);
SerialUSB.print("% ");
SerialUSB.print("temperature = ");
SerialUSB.print(temp);
SerialUSB.println("C");
sprintf(data,"{\"temp\":%.1f,\"humi\":%.1f}", temp, humi);
#else
sprintf(data, "{\"uptime\":%lu}", millis() / 1000);
#endif // SENSOR_PIN
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.print(data);
SerialUSB.println("");
if (!Wio.SocketSend(connectId, data)) {
SerialUSB.println("### ERROR! ###");
goto err_close;
}
SerialUSB.println("### Receive.");
int length;
length = Wio.SocketReceive(connectId, data, sizeof (data), 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.print(data);
SerialUSB.println("");
err_close:
SerialUSB.println("### Close.");
if (!Wio.SocketClose(connectId)) {
SerialUSB.println("### ERROR! ###");
goto err;
}
err:
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;
}
#endif // SENSOR_PIN
////////////////////////////////////////////////////////////////////////////////////////
//
#ifdef SENSOR_PIN
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 // SENSOR_PIN
////////////////////////////////////////////////////////////////////////////////////////
You can’t perform that action at this time.