Hi All,
I have a modified the Thingspeak example code and added the SIM868 Module instead of using WiFi, I had it working until 2 days ago and it stopped transmitting to Thingspeak. I did over 2000 “pushes” – perhaps it worked by accident?, perhaps there is something missing in the code below?, any help would be greatly appreciated.
#include <Arduino.h>
#include <WiFi.h>
#include “ThingSpeak.h”
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <TinyGPS++.h>
#include <Arduino_JSON.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
// Your GPRS credentials (leave empty, if not needed)
const char apn[] = “iphone.vodacom.za”; // use https://wiki.apnchanger.org
const char gprsUser[] = “”; // GPRS User
const char gprsPass[] = “”; // GPRS Password
#define MODEM_PWKEY 33
#define MODEM_TX 16
#define MODEM_RX 17
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS
#include <Wire.h>
#include <TinyGsmClient.h>
#include <HTTPClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
unsigned long myChannelNumber = 3;
const char * myWriteAPIKey = “XXXXXXXXXXXXXXXXX”;
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
#define uS_TO_S_FACTOR 1000000UL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 450 /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */
// Variable to hold temperature readings
float temperatureC;
float humidity;
float pressure;
String gps_Lat;
String gps_Long;
//uncomment if you want to get temperature in Fahrenheit
float latitude , longitude;
String lat_str , lng_str;
float V, P, D;
float voltageThreshold = 4.50;
// Json Variable to Hold Sensor Readings
JSONVar readings;
JSONVar values;
TinyGPSPlus gps;
HardwareSerial SerialGPS(2);
// Get Sensor Readings and return JSON object
String getSensorReadings(){
readings[“voltage”] = String(V, 3);
//readings[“corrosion”] = String(P,1);
String jsonString = JSON.stringify(readings);
return jsonString;
}
void GPS(){
while (SerialGPS.available() > 0) {
if (gps.encode(SerialGPS.read()))
{
if (gps.location.isValid())
{
latitude = gps.location.lat();
lat_str = String(latitude , 6);
longitude = gps.location.lng();
lng_str = String(longitude , 6);
Serial.print(“Latitude = “);
Serial.println(lat_str);
Serial.print(“Longitude = “);
Serial.println(lng_str);
}
}
}
}
void setup() {
Wire.begin(I2C_SDA, I2C_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128×32
Serial.println(F(“SSD1306 allocation failed”));
for(;;); // Don’t proceed, loop forever
}
Serial.begin(115200); //Initialize serial
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Battery”);
display.setCursor(0,20);
display.print(“Monitor”);
display.display();
SerialGPS.begin(9600, SERIAL_8N1, 19, 18);
// Initialize ThingSpeak
ThingSpeak.begin(client);
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
delay (1000);
digitalWrite(MODEM_PWKEY, HIGH);
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println(“Initializing modem…”);
modem.restart();
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Initialise”);
display.setCursor(0,20);
display.print(“GSM Module”);
display.display();
delay (3000);
}
void loop() {
if ((millis() – lastTime) > timerDelay) {
SerialMon.print(“Connecting to APN: “);
SerialMon.print(apn);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Connecting”);
display.setCursor(0,20);
display.print(“To AP….”);
display.display();
delay (3000);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(” fail”);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Failed to”);
display.setCursor(0,20);
display.print(“Connect”);
display.setCursor(0,40);
display.print(“To GSM AP”);
display.display();
delay (3000);
}
else {
SerialMon.println(” OK”);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Connected”);
display.setCursor(0,20);
display.print(“To GSM AP”);
display.display();
delay (3000);
}
getSensorReadings();
//Connect sensor to Analog 34
V = analogRead(34) * 3.3 / 4095; //output voltage
P = V * 2.01;
GPS();
gps_Lat = String(latitude , 6);
gps_Long = String(longitude , 6);
Serial.println (String (latitude , 6));
Serial.println (String (longitude , 6));
Serial.print(“Voltage: “);
Serial.println(P);
Serial.println(V);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“V: “);
display.print((P));
display.display();
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Lat:”);
display.print(gps_Lat);
display.setCursor(0,30);
display.print(“Lng:”);
display.print(gps_Long);
display.display();
delay(2000);
// set the fields with the values
ThingSpeak.setField(1, gps_Lat);
ThingSpeak.setField(2, gps_Long);
ThingSpeak.setField(3, P);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“THINGSPEAK”);
display.setCursor(0,20);
display.print(“PUSH….”);
display.display();
delay (3000);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
// https://api.thingspeak.com/update?api_key=XXXXXXXXXXXX&field1=0
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println(“Channel update successful.”);
// Close client and disconnect
client.stop();
SerialMon.println(F(“Server disconnected”));
modem.gprsDisconnect();
SerialMon.println(F(“GPRS disconnected”));
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Server”);
display.setCursor(0,20);
display.print(“Disconnect”);
display.display();
delay (3000);
}
else{
Serial.println(“Problem updating channel. HTTP error code ” + String(x));
// Close client and disconnect
client.stop();
SerialMon.println(F(“Server disconnected”));
modem.gprsDisconnect();
SerialMon.println(F(“GPRS disconnected”));
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(“Server”);
display.setCursor(0,20);
display.print(“Disconnect”);
display.display();
delay (3000);
}
delay(30000);
}
}
Hi.
If it was working before, it didn’t work “by accident”.
Maybe you exceeded the limit for a free user account?? https://thingspeak.com/pages/license_faq
Or maybe your SIM data plan has reached the limit?
Regards,
Sara
Thanks Sara,
I did buy 2 x Home Units and do have Data, I will try and debug further
Regards,
Gavin