Home › Forums › Mayfly Data Logger › Using Xbee Cellular Modem with ThingSpeak › Reply To: Using Xbee Cellular Modem with ThingSpeak
Hi there @neilh20. Thanks for your response, sorry I couldn’t get to it until now. I spent some time on the code, and I tried the code with the 0.28.5 version of the EnviroDIY ModularSensors library just to see if anything changed. It still isn’t uploading to ThingSpeak though. I tried the 0.30.0 version of the ModularSensors library, alongside the StreamDebugger as well, here’s the output of the code (excluding the ThingSpeak data). It doesn’t look that much different.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\waked\OneDrive – East Carolina University\Documents\PlatformIO\Projects\LearnEnviroDIYcode-master> pio device monitor –port COM10 –baud 115200
— Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
— More details at http://bit.ly/pio-monitor-filters
— Miniterm on COM10 115200,8,N,1 —
— Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H —
Now running logging_to_thingspeak5.ino on Logger logger
Using ModularSensors Library version TinyGSM Library version 0.11.4
Current RTC time is: 2021-08-02T12:16:53-05:00
CTDcond has a non-unique UUID!
CTDtemp has a non-unique UUID!
CTDdepth has a non-unique UUID!
Battery has a non-unique UUID!
12345678-abcd-1234-ef00-1234567890ab -> CTDcond
12345678-abcd-1234-ef00-1234567890ab -> CTDtemp
12345678-abcd-1234-ef00-1234567890ab -> CTDdepth
12345678-abcd-1234-ef00-1234567890ab -> Battery
12345678-abcd-1234-ef00-1234567890ab -> decibelMiliWatt
This logger has a variable array with 5 variables, of which 4 come from 2 sensors and 1 are calculated.
Setting up sensors…
Attempting to connect to the internet and synchronize RTC with NIST
This may take up to two minutes!
OKCN163.97.697.6
OKCN163.97.6
OKCN163.97.6
OKCN163.97.6
OKCN163.97.6
Clock already within 5 seconds of time.
Putting modem to sleep
Setting up file on SD card
Data will be saved as logger_2021-08-02.csv
Putting processor to sleep
——————————————
\/—- Line Saved to SD Card —-\/
2021-08-02 12:20:00,0.0,22.40,-3.2,4.897,-81
OKCN3.23.121
Sending data to [ 0 ] mqtt.thingspeak.com
MQTT connection failed with state: -4: MQTT_CONNECTION_TIMEOUT
——————————————
——————————————
\/—- Line Saved to SD Card —-\/
2021-08-02 12:21:00,0.0,22.40,-2.7,4.897,0
OKCN3.23.121
Sending data to [ 0 ] mqtt.thingspeak.com
MQTT connection failed with state: -4: MQTT_CONNECTION_TIMEOUT
Additionally, here’s the updated version of the code with the StreamDebugger stuff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
/** ========================================================================= * @file logging_to_ThingSpeak.ino * @brief Example logging data and publishing to ThingSpeak. * * @author Sara Geleskie Damiano <sdamiano@stroudcenter.org> * @copyright (c) 2017-2020 Stroud Water Research Center (SWRC) * and the EnviroDIY Development Team * This example is published under the BSD-3 license. * * Build Environment: Visual Studios Code with PlatformIO * Hardware Platform: EnviroDIY Mayfly Arduino Datalogger * * DISCLAIMER: * THIS CODE IS PROVIDED "AS IS" - NO WARRANTY IS GIVEN. * ======================================================================= */ // ========================================================================== // Defines for the Arduino IDE // NOTE: These are ONLY needed to compile with the Arduino IDE. // If you use PlatformIO, you should set these build flags in your // platformio.ini // ========================================================================== /** Start [defines] */ #ifndef TINY_GSM_RX_BUFFER #define TINY_GSM_RX_BUFFER 64 #endif #ifndef TINY_GSM_YIELD_MS #define TINY_GSM_YIELD_MS 2 #endif #ifndef MQTT_MAX_PACKET_SIZE #define MQTT_MAX_PACKET_SIZE 240 #endif #define TINY_GSM_MODEM_XBEE /** End [defines] */ // ========================================================================== // Include the libraries required for any data logger // ========================================================================== /** Start [includes] */ // The Arduino library is needed for every Arduino program. #include <Arduino.h> // EnableInterrupt is used by ModularSensors for external and pin change // interrupts and must be explicitly included in the main program. #include <EnableInterrupt.h> // To get all of the base classes for ModularSensors, include LoggerBase. // NOTE: Individual sensor definitions must be included separately. #include <LoggerBase.h> #include <StreamDebugger.h> /** End [includes] */ // ========================================================================== // Data Logging Options // ========================================================================== /** Start [logging_options] */ // The name of this program file const char* sketchName = "logging_to_thingspeak5.ino"; // Logger ID, also becomes the prefix for the name of the data file on SD card const char* LoggerID = "logger"; // How frequently (in minutes) to log data const uint8_t loggingInterval = 1; // Your logger's timezone. const int8_t timeZone = -5; // Eastern Standard Time // NOTE: Daylight savings time will not be applied! Please use standard time! // Set the input and output pins for the logger // NOTE: Use -1 for pins that do not apply const int32_t serialBaud = 115200; // Baud rate for debugging const int8_t greenLED = 8; // Pin for the green LED const int8_t redLED = 9; // Pin for the red LED const int8_t buttonPin = 21; // Pin for debugging mode (ie, button pin) const int8_t wakePin = A7; // MCU interrupt/alarm pin to wake from sleep // Set the wake pin to -1 if you do not want the main processor to sleep. // In a SAMD system where you are using the built-in rtc, set wakePin to 1 const int8_t sdCardPwrPin = -1; // MCU SD card power pin const int8_t sdCardSSPin = 12; // SD card chip select/slave select pin const int8_t sensorPowerPin = 22; // MCU pin controlling main sensor power /** End [logging_options] */ // ========================================================================== // Wifi/Cellular Modem Options // ========================================================================== /** Start [xbee_cell_transparent] */ // For any Digi Cellular XBee's // NOTE: The u-blox based Digi XBee's (3G global and LTE-M global) can be used // in either bypass or transparent mode, each with pros and cons // The Telit based Digi XBees (LTE Cat1) can only use this mode. #if defined STREAMDEBUGGER_DBG #include <StreamDebugger.h> #include <modems/DigiXBeeCellularTransparent.h> // NOTE: Extra hardware and software serial ports are created in the "Settings // for Additional Serial Ports" section HardwareSerial& modemSerial = Serial1; // Use hardware serial if possible const int32_t modemBaud = 9600; // All XBee's use 9600 by default StreamDebugger modemDebugger(modemSerial, STANDARD_SERIAL_OUTPUT); #define modemSerHw modemDebugger #else #define modemSerHw modemSerial #endif // STREAMDEBUGGER_DBG // Modem Pins - Describe the physical pin connection of your modem to your board // NOTE: Use -1 for pins that do not apply // The pin numbers here are for a Digi XBee with a Mayfly and LTE adapter // For options https://github.com/EnviroDIY/LTEbee-Adapter/edit/master/README.md const int8_t modemVccPin = -1; // MCU pin controlling modem power // Option: modemVccPin = A5, if Mayfly SJ7 is // connected to the ASSOC pin const int8_t modemStatusPin = 13; // MCU pin used to read modem status // NOTE: If possible, use the <code>STATUS/SLEEP_not</code> (XBee pin 13) for status, but // the CTS pin can also be used if necessary const bool useCTSforStatus = false; // Flag to use the CTS pin for status const int8_t modemResetPin = 20; // MCU pin connected to modem reset pin const int8_t modemSleepRqPin = 23; // MCU pin for modem sleep/wake request const int8_t modemLEDPin = redLED; // MCU pin connected an LED to show modem // status // Network connection information const char* apn = "hologram"; // APN for GPRS connection // Create the modem object DigiXBeeCellularTransparent modemXBCT(&modemSerHw, modemVccPin, modemStatusPin, useCTSforStatus, modemResetPin, modemSleepRqPin, apn); // Create an extra reference to the modem by a generic name DigiXBeeCellularTransparent modem = modemXBCT; /** End [xbee_cell_transparent] */ // ========================================================================== // ========================================================================== // Using the Processor as a Sensor // ========================================================================== /** Start [processor_sensor] */ #include <sensors/ProcessorStats.h> // Create the main processor chip "sensor" - for general metadata const char* mcuBoardVersion = "v0.5b"; ProcessorStats mcuBoard(mcuBoardVersion); /** End [processor_sensor] */ // ========================================================================== // Maxim DS3231 RTC (Real Time Clock) // ========================================================================== /** Start [ds3231] */ #include <sensors/MaximDS3231.h> // Create a DS3231 sensor object MaximDS3231 ds3231(1); /** End [ds3231] */ // ========================================================================== // Meter Hydros 21 Conductivity, Temperature, and Depth Sensor // ========================================================================== /** Start [decagon_ctd] */ #include <sensors/DecagonCTD.h> const char* CTDSDI12address = "1"; // The SDI-12 Address of the CTD const uint8_t CTDNumberReadings = 6; // The number of readings to average const int8_t SDI12Power = sensorPowerPin; // Power pin (-1 if unconnected) const int8_t SDI12Data = 7; // The SDI12 data pin // Create a Decagon CTD sensor object DecagonCTD ctd(*CTDSDI12address, SDI12Power, SDI12Data, CTDNumberReadings); /** End [decagon_ctd] */ // ========================================================================== // Creating the Variable Array[s] and Filling with Variable Objects // ========================================================================== /** Start [variable_arrays] */ Variable* variableList[] = { new DecagonCTD_Cond(&ctd, "12345678-abcd-1234-ef00-1234567890ab"), new DecagonCTD_Temp(&ctd, "12345678-abcd-1234-ef00-1234567890ab"), new DecagonCTD_Depth(&ctd, "12345678-abcd-1234-ef00-1234567890ab"), new ProcessorStats_Battery(&mcuBoard, "12345678-abcd-1234-ef00-1234567890ab"), new Modem_RSSI(&modem, "12345678-abcd-1234-ef00-1234567890ab")}; // Count up the number of pointers in the array int variableCount = sizeof(variableList) / sizeof(variableList[0]); // Create the VariableArray object VariableArray varArray; /** End [variable_arrays] */ // ========================================================================== // The Logger Object[s] // ========================================================================== /** Start [loggers] */ // Create a logger instance Logger dataLogger; /** End [loggers] */ // ========================================================================== // Creating Data Publisher[s] // ========================================================================== // Create a channel with fields on ThingSpeak in advance // The fields will be sent in exactly the order they are in the variable array. // Any custom name or identifier given to the field on ThingSpeak is irrelevant. // No more than 8 fields of data can go to any one channel. Any fields beyond // the eighth in the array will be ignored. const char* thingSpeakMQTTKey = "thingSpeakMQTTKey"; // Your MQTT API Key from Account > MyProfile. const char* thingSpeakChannelID = "thingSpeakChannelID"; // The numeric channel id for your channel const char* thingSpeakChannelKey = "thingSpeakChannelKey"; // The Write API Key for your channel // Create a data publisher for ThingSpeak #include <publishers/ThingSpeakPublisher.h> ThingSpeakPublisher TsMqtt; /** End [loggers] */ // ========================================================================== // Working Functions // ========================================================================== /** Start [working_functions] */ // Flashes the LED's on the primary board void greenredflash(uint8_t numFlash = 4, uint8_t rate = 75) { for (uint8_t i = 0; i < numFlash; i++) { digitalWrite(greenLED, HIGH); digitalWrite(redLED, LOW); delay(rate); digitalWrite(greenLED, LOW); digitalWrite(redLED, HIGH); delay(rate); } digitalWrite(redLED, LOW); } // Reads the battery voltage // NOTE: This will actually return the battery level from the previous update! float getBatteryVoltage() { if (mcuBoard.sensorValues[0] == -9999) mcuBoard.update(); return mcuBoard.sensorValues[0]; } /** End [working_functions] */ // ========================================================================== // Arduino Setup Function // ========================================================================== /** Start [setup] */ void setup() { // Start the primary serial connection Serial.begin(serialBaud); // Print a start-up note to the first serial port Serial.print(F("Now running ")); Serial.print(sketchName); Serial.print(F(" on Logger ")); Serial.println(LoggerID); Serial.println(); Serial.print(F("Using ModularSensors Library version ")); //Serial.println(MODULAR_SENSORS_VERSION); Serial.print(F("TinyGSM Library version ")); Serial.println(TINYGSM_VERSION); Serial.println(); // Start the serial connection with the modem modemSerial.begin(modemBaud); // Set up pins for the LED's pinMode(greenLED, OUTPUT); digitalWrite(greenLED, LOW); pinMode(redLED, OUTPUT); digitalWrite(redLED, LOW); // Blink the LEDs to show the board is on and starting up greenredflash(); // Set the timezones for the logger/data and the RTC // Logging in the given time zone Logger::setLoggerTimeZone(timeZone); // It is STRONGLY RECOMMENDED that you set the RTC to be in UTC (UTC+0) Logger::setRTCTimeZone(0); // Attach the modem and information pins to the logger dataLogger.attachModem(modem); modem.setModemLED(modemLEDPin); dataLogger.setLoggerPins(wakePin, sdCardSSPin, sdCardPwrPin, buttonPin, greenLED); // Begin the variable array[s], logger[s], and publisher[s] varArray.begin(variableCount, variableList); dataLogger.begin(LoggerID, loggingInterval, &varArray); TsMqtt.begin(dataLogger, &modem.gsmClient, thingSpeakMQTTKey, thingSpeakChannelID, thingSpeakChannelKey); // Note: Please change these battery voltages to match your battery // Set up the sensors, except at lowest battery level if (getBatteryVoltage() > 3.4) { Serial.println(F("Setting up sensors...")); varArray.setupSensors(); } // Sync the clock if it isn't valid or we have battery to spare if (getBatteryVoltage() > 3.55 || !dataLogger.isRTCSane()) { // Synchronize the RTC with NIST // This will also set up the modem dataLogger.syncRTC(); } // Create the log file, adding the default header to it // Do this last so we have the best chance of getting the time correct and // all sensor names correct // Writing to the SD card can be power intensive, so if we're skipping // the sensor setup we'll skip this too. if (getBatteryVoltage() > 3.4) { Serial.println(F("Setting up file on SD card")); dataLogger.turnOnSDcard( true); // true = wait for card to settle after power up dataLogger.createLogFile(true); // true = write a new header dataLogger.turnOffSDcard( true); // true = wait for internal housekeeping after write } // Call the processor sleep Serial.println(F("Putting processor to sleep")); dataLogger.systemSleep(); } /** End [setup] */ // ========================================================================== // Arduino Loop Function // ========================================================================== /** Start [loop] */ // Use this short loop for simple data logging and sending void loop() { // Note: Please change these battery voltages to match your battery // At very low battery, just go back to sleep if (getBatteryVoltage() < 3.4) { dataLogger.systemSleep(); } // At moderate voltage, log data but don't send it over the modem else if (getBatteryVoltage() < 3.55) { dataLogger.logData(); } // If the battery is good, send the data to the world else { dataLogger.logDataAndPublish(); } } /** End [loop] */ |
That’s very strange that the code worked with the Digi Wifi module. Any other suggestions are greatly appreciated! And if you see anything that I did wrong with the code, please let me know! I’m still rather new to this.