Skip to content

Instantly share code, notes, and snippets.

@drcircuit
Last active December 12, 2018 14:28
Show Gist options
  • Select an option

  • Save drcircuit/3f390caf440c6274961e47d64d119ea1 to your computer and use it in GitHub Desktop.

Select an option

Save drcircuit/3f390caf440c6274961e47d64d119ea1 to your computer and use it in GitHub Desktop.
Stuff For Einar
/***********************************************************************
Telenor NB-IoT Hello World
Configures NB-IoT, connects and sends a message every 15 minutes.
See our guide on how to connect the EE-NBIOT-01 to Arduino:
https://docs.nbiot.engineering/tutorials/arduino-basic.html
This example is in the public domain.
Read more on the Exploratory Engineering team at
https://exploratory.engineering/
***********************************************************************/
#include <Udp.h>
#include <TelenorNBIoT.h>
#include "Adafruit_VL53L0X.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
// Example for demonstrating the TSL2591 library - public domain!
// connect SCL to I2C Clock
// connect SDA to I2C Data
// connect Vin to 3.3-5V DC
// connect GROUND to common ground
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
#ifdef SERIAL_PORT_HARDWARE_OPEN
/*
* For Arduino boards with a hardware serial port separate from USB serial.
* This is usually mapped to Serial1. Check which pins are used for Serial1 on
* the board you're using.
*/
#define ublox SERIAL_PORT_HARDWARE_OPEN
#else
/*
* For Arduino boards with only one hardware serial port (like Arduino UNO). It
* is mapped to USB, so we use SoftwareSerial on pin 10 and 11 instead.
*/
#include <SoftwareSerial.h>
SoftwareSerial ublox(10, 11);
#endif
TelenorNBIoT nbiot;
// The remote IP address to send data packets to
// u-blox SARA N2 does not support DNS
IPAddress remoteIP(172, 16, 15, 14);
int REMOTE_PORT = 1234;
unsigned long INTERVAL_MS = (unsigned long) 15 * 60 * 1000;
int moisture = 0;
void configureSensor(void)
{
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
tsl.setGain(TSL2591_GAIN_MED); // 25x gain
//tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
//tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
// tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
/* Display the gain and integration time for reference sake */
Serial.println(F("------------------------------------"));
Serial.print (F("Gain: "));
tsl2591Gain_t gain = tsl.getGain();
switch(gain)
{
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
Serial.print (F("Timing: "));
Serial.print((tsl.getTiming() + 1) * 100, DEC);
Serial.println(F(" ms"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
}
void setup() {
Serial.begin(9600);
Serial.print("Connecting to NB-IoT module...\n");
ublox.begin(9600);
nbiot.begin(ublox);
// if (!lox.begin(0x30 )) {
// Serial.println(F("Failed to boot VL53L0X"));
// while(1);
// }
if (tsl.begin())
{
Serial.println(F("Found a TSL2591 sensor"));
}
else
{
Serial.println(F("No sensor found ... check your wiring?"));
while (1);
}
/*
* You neeed the IMEI and IMSI when setting up a device in our developer
* platform: https://nbiot.engineering
*
* See guide for more details on how to get started:
* https://docs.nbiot.engineering/tutorials/getting-started.html
*/
Serial.print("IMSI: ");
Serial.println(nbiot.imsi());
Serial.print("IMEI: ");
Serial.println(nbiot.imei());
nbiot.createSocket();
}
void loop() {
if (nbiot.isConnected()) {
// Successfully connected to the network
VL53L0X_RangingMeasurementData_t measure;
Serial.print("Reading a measurement... ");
// lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
moisture = analogRead(1);
if (measure.RangeStatus != 4) { // phase failures have incorrect data
Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
} else {
Serial.println(" out of range ");
}
uint16_t v = tsl.getLuminosity(TSL2591_VISIBLE);
uint16_t fs = tsl.getLuminosity(TSL2591_FULLSPECTRUM);
uint16_t ir = tsl.getLuminosity(TSL2591_INFRARED);
if (true == nbiot.sendString(remoteIP, REMOTE_PORT, String(-1)+";m"+String(moisture)+";vl"+v+";ir"+ir+";fs"+fs)) {
Serial.println("Successfully sent data");
} else {
Serial.println("Failed sending data");
}
// Send message to remote server
// Wait 15 minutes before sending again
delay(5000);
} else {
// Not connected yet. Wait 5 seconds before retrying.
Serial.println("Connecting...");
delay(5000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment