Home › Forums › Mayfly Data Logger › Connecting SIM7080 to New Thingspeak › Reply To: Connecting SIM7080 to New Thingspeak
Hi all! Thank you for all of your feedback and tips! I spent some time this week looking at the mqtt3.thingspeak.com server and I was able to get a channel up and running on it.
Overall, it’s not drastically different than mqtt.thingspeak.com. The main aspect being removed in the legacy Thingspeak server is the API keys. Thingspeak now relies on the Channel ID, the client ID, the username, and the password to send data to a specific channel.
There are also a few formatting changes for sending the data, which can be found on this site, under July 2021:
I did end up editing the ThingSpeakPublisher library in order to get the data to upload correctly, but I don’t think I had to change the data type.
Here’s the Arduino code:
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
/** ========================================================================= * @file DRWI_sim7080LTE.ino * @brief Example for DRWI CitSci LTE sites. * * This example shows proper settings for the following configuration: * * Mayfly v1.0 board * EnviroDIY sim7080 LTE module (with Hologram SIM card) * Hydros21 CTD sensor * Campbell Scientific OBS3+ Turbidity sensor * * @author Sara Geleskie Damiano <sdamiano@stroudcenter.org> * @copyright (c) 2017-2021 Stroud Water Research Center (SWRC) * and the EnviroDIY Development Team * This example is published under the BSD-3 license. * * 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 256 #endif /** 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> // Include the main header for ModularSensors #include <ModularSensors.h> /** End [includes] */ // ========================================================================== // Data Logging Options // ========================================================================== /** Start [logging_options] */ // The name of this program file const char* sketchName = "thingspeaksingular_mayfly_1point0_GW1.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 = 31; // MCU interrupt/alarm pin to wake from sleep // Mayfly 0.x D31 = A7 // 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 [sim7080] */ // For almost anything based on the SIMCom SIM7080G #include <modems/SIMComSIM7080.h> #define modemSerial Serial1 // NOTE: Extra hardware and software serial ports are created in the "Settings // for Additional Serial Ports" section const int32_t modemBaud = 9600; // SIM7080 does auto-bauding by default, but for simplicity we set to 9600 // Modem Pins - Describe the physical pin connection of your modem to your board // NOTE: Use -1 for pins that do not apply const int8_t modemVccPin = 18; // MCU pin controlling modem power --- Pin 18 is the power enable pin for the bee socket on Mayfly v1.0, // use -1 if using Mayfly 0.5b or if the bee socket is constantly powered (ie you changed SJ18 on Mayfly1.0 to 3.3v) const int8_t modemStatusPin = 19; // MCU pin used to read modem status 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 connection name, typically Hologram unless you have a different provider's SIM card. Change as needed // Create the modem object SIMComSIM7080 modem7080(&modemSerial, modemVccPin, modemStatusPin, modemSleepRqPin, apn); // Create an extra reference to the modem by a generic name SIMComSIM7080 modem = modem7080; /** End [sim7080] */ // ========================================================================== // ========================================================================== // 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"; // is only used for onboard battery voltage calculation, // "v0.5b" is safe to use on Mayfly v1.0 boards because the formula is the same 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 [hydros21] */ #include <sensors/MeterHydros21.h> const char* hydrosSDI12address = "1"; // The SDI-12 Address of the Hydros 21 const uint8_t hydrosNumberReadings = 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 Meter Hydros 21 sensor object MeterHydros21 hydros(*hydrosSDI12address, SDI12Power, SDI12Data, hydrosNumberReadings); /** End [hydros21] */ Variable *Hydros21_TempF = new MeterHydros21_Temp(&hydros, "12345678-abcd-1234-ef00-1234567890ab"); //Hydros temperature in degrees Fahrenheit float calculateHydros21degF(void) { float MeterHydros21_TempC = Hydros21_TempF->getValue(); float MeterHydros21_TempF = MeterHydros21_TempC*(1.8)+32; if (MeterHydros21_TempC == -9999) { MeterHydros21_TempF = -9999; } return MeterHydros21_TempF; } const char* Hydros21TempFVarName = "Temperature"; const char *Hydros21TempVarFUnit = "Degree"; int Hydros21TempFVarResolution = 2; const char *Hydros21TempFUUID = "12345678-abcd-1234-ef00-1234567890ab"; const char *Hydros21TempFVarCode = "Hydros21TempF"; Variable *HydrosCalcTempF = new Variable(calculateHydros21degF, Hydros21TempFVarResolution, Hydros21TempFVarName, Hydros21TempVarFUnit, Hydros21TempFVarCode, Hydros21TempFUUID); Variable *Hydros21_Depthft = new MeterHydros21_Depth(&hydros, "12345678-abcd-1234-ef00-1234567890ab"); //Hydros temperature in degrees Fahrenheit float calculateHydros21feet(void) { float MeterHydros21_Depth_mm = Hydros21_Depthft->getValue(); float MeterHydros21_Depth_ft = MeterHydros21_Depth_mm/304.8; if (MeterHydros21_Depth_mm == -9999) { MeterHydros21_Depth_ft = -9999; } return MeterHydros21_Depth_ft; } const char* Hydros21DepthftVarName = "Depth"; const char *Hydros21DepthftVarUnit = "Feet"; int Hydros21DepthftVarResolution = 2; const char *Hydros21DepthftUUID = "12345678-abcd-1234-ef00-1234567890ab"; const char *Hydros21DepthftVarCode = "Hydros21Depthft"; Variable *HydrosCalcDepthft = new Variable(calculateHydros21feet, Hydros21DepthftVarResolution, Hydros21DepthftVarName, Hydros21DepthftVarUnit, Hydros21DepthftVarCode, Hydros21DepthftUUID); // ========================================================================== // Campbell OBS 3 / OBS 3+ Analog Turbidity Sensor // ========================================================================== /** Start [obs3] */ // ========================================================================== // Creating the Variable Array[s] and Filling with Variable Objects // ========================================================================== /** Start [variable_arrays] */ Variable* variableList[] = { new MeterHydros21_Cond(&hydros), HydrosCalcTempF, HydrosCalcDepthft, new ProcessorStats_Battery(&mcuBoard), }; // All UUID's, device registration, and sampling feature information can be pasted directly from Monitor My Watershed. //To get the list, click the "View token UUID list" button on the upper right of the site page. // *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION *** // Check the order of your variables in the variable list!!! // Be VERY certain that they match the order of your UUID's! // Rearrange the variables in the variable list ABOVE if necessary to match! // Do not change the order of the variables in the section below. // *** CAUTION --- CAUTION --- CAUTION --- CAUTION --- CAUTION *** // Replace all of the text in the following section with the UUID array from MonitorMyWatershed // --------------------- Beginning of Token UUID List --------------------------------------- // 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(LoggerID, loggingInterval, &varArray); /** 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. //Will be removed in a future iteration of thingspeak const char* thingSpeakChannelID = "INPUT_CHANNEL_ID_HERE"; // The numeric channel id for your channel const char* thingSpeakMQTTusername = "INPUT_USERNAME_HERE"; //Your MQTT username const char* thingSpeakMQTTpassword = "INPUT_PASSWORD_HERE"; //Your MQTT password const char* thingSpeakMQTTclientID = "INPUT_CLIENT_ID_HERE"; //Your MQTT client ID // 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]; } // ========================================================================== // 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(); pinMode(20, OUTPUT); //for proper operation of the onboard flash memory chip's ChipSelect (Mayfly v1.0 and later) // 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, thingSpeakChannelID, thingSpeakMQTTusername, thingSpeakMQTTpassword, thingSpeakMQTTclientID); // 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(); } /** Start [setup_sim7080] */ modem.setModemWakeLevel(HIGH); // ModuleFun Bee inverts the signal modem.setModemResetLevel(HIGH); // ModuleFun Bee inverts the signal Serial.println(F("Waking modem and setting Cellular Carrier Options...")); modem.modemWake(); // NOTE: This will also set up the modem modem.gsmModem.setBaud(modemBaud); // Make sure we're *NOT* auto-bauding! modem.gsmModem.setNetworkMode(38); // set to LTE only // 2 Automatic // 13 GSM only // 38 LTE only // 51 GSM and LTE only modem.gsmModem.setPreferredMode(1); // set to CAT-M // 1 CAT-M // 2 NB-IoT // 3 CAT-M and NB-IoT /** End [setup_sim7080] */ // 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\n")); 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] */ |