Hi, I using esp 32 wroom and mpu6050. I am trying to update the values to thingspeak, but it saying "Problem updating channel. HTTP error code -301"
my code:-
#include <WiFi.h>
#include <Wire.h>
#include <WifiLocation.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
// Replace with your network credentials
const char* ssid = "PRAJNA 6481";
const char* password = "a*8H6689";
unsigned long myChannelNumber = 1740143;
const char * myWriteAPIKey = "1MCRA6GKOZ8W4VR4";
const char* googleApiKey = "AIzaSyAEOhTyIn6Px3dS-9-0pIJL7g_q0B9BxHE";
// Initialize Telegram BOT
#define BOTtoken "5337014441:AAGwQNysuwdCBlJaTcuGq1nQP3_OnVmwK6w" // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "946094893"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
WifiLocation location (googleApiKey);
// Set time via NTP, as required for x.509 validation
void setClock () {
configTime (0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print ("Waiting for NTP time sync: ");
time_t now = time (nullptr);
while (now < 8 * 3600 * 2) {
delay (500);
Serial.print (".");
now = time (nullptr);
}
struct tm timeinfo;
gmtime_r (&now, &timeinfo);
Serial.print ("\n");
Serial.print ("Current time: ");
Serial.print (asctime (&timeinfo));
}
const int ButtonPin = 14; // PIR Motion Sensor
bool PressDetected = false;
const int MPU_addr = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
float ax = 0, ay = 0, az = 0, gx = 0, gy = 0, gz = 0;
boolean fall = false; //stores if a fall has occurred
boolean trigger1 = false; //stores if first trigger (lower threshold) has occurred
boolean trigger2 = false; //stores if second trigger (upper threshold) has occurred
boolean trigger3 = false; //stores if third trigger (orientation change) has occurred
byte trigger1count = 0; //stores the counts past since trigger 1 was set true
byte trigger2count = 0; //stores the counts past since trigger 2 was set true
byte trigger3count = 0; //stores the counts past since trigger 3 was set true
int angleChange = 0;
// Indicates when ButtonPress is detected
void IRAM_ATTR detectsMovement() {
Serial.println("PRESS DETECTED!!!");
PressDetected = true;
}
void setup() {
Serial.begin(115200);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(ButtonPin, INPUT_PULLUP);
// Set ButtonPin pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(ButtonPin), detectsMovement, RISING);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.println("Wrote to IMU");
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
// set filter bandwidth to 21 Hz
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client); // Initialize ThingSpeak
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
uint32_t lastTime = 0;
void loop() {
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
if(millis()-lastTime > 20000)
{
lastTime = millis();
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.print("{\"temp\": ");
Serial.print(temp.temperature);
Serial.print("}");
int x = ThingSpeak.writeField(myChannelNumber, 1, temp.temperature, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
if (PressDetected) {
setClock ();
location_t loc = location.getGeoFromWiFi();
Serial.println("Location request data");
Serial.println(location.getSurroundingWiFiJson() + "\n");
Serial.println ("Location: " + String (loc.lat, 7) + "," + String (loc.lon, 7));
//Serial.println("Longitude: " + String(loc.lon, 7));
Serial.println ("Accuracy: " + String (loc.accuracy));
Serial.println ("Result: " + location.wlStatusStr (location.getStatus ()));
bot.sendMessage(CHAT_ID, "User pushed emergency button\nHelp required\nLocation: " + String (loc.lat, 7) + "," + String (loc.lon, 7), "");
Serial.println("ButtonPress Detected");
PressDetected = false;
}
mpu_read();
ax = (AcX - 2050) / 16384.00;
ay = (AcY - 77) / 16384.00;
az = (AcZ - 1947) / 16384.00;
gx = (GyX + 270) / 131.07;
gy = (GyY - 351) / 131.07;
gz = (GyZ + 136) / 131.07;
// calculating Amplitute vactor for 3 axis
float Raw_Amp = pow(pow(ax, 2) + pow(ay, 2) + pow(az, 2), 0.5);
int Amp = Raw_Amp * 10; // Mulitiplied by 10 bcz values are between 0 to 1
//Serial.println(Amp);
if (Amp <= 2 && trigger2 == false) { //if AM breaks lower threshold (0.4g)
trigger1 = true;
Serial.println("TRIGGER 1 ACTIVATED");
}
if (trigger1 == true) {
trigger1count++;
if (Amp >= 12) { //if AM breaks upper threshold (3g)
trigger2 = true;
Serial.println("TRIGGER 2 ACTIVATED");
trigger1 = false; trigger1count = 0;
}
}
if (trigger2 == true) {
trigger2count++;
angleChange = pow(pow(gx, 2) + pow(gy, 2) + pow(gz, 2), 0.5); Serial.println(angleChange);
if (angleChange >= 30 && angleChange <= 400) { //if orientation changes by between 80-100 degrees
trigger3 = true; trigger2 = false; trigger2count = 0;
Serial.println(angleChange);
Serial.println("TRIGGER 3 ACTIVATED");
}
}
if (trigger3 == true) {
trigger3count++;
if (trigger3count >= 10) {
angleChange = pow(pow(gx, 2) + pow(gy, 2) + pow(gz, 2), 0.5);
//delay(10);
Serial.println(angleChange);
if ((angleChange >= 0) && (angleChange <= 10)) { //if orientation changes remains between 0-10 degrees
fall = true; trigger3 = false; trigger3count = 0;
Serial.println(angleChange);
}
else { //user regained normal orientation
trigger3 = false; trigger3count = 0;
Serial.println("TRIGGER 3 DEACTIVATED");
}
}
}
if (fall == true) { //in event of a fall detection
Serial.println("FALL DETECTED");
setClock ();
location_t loc = location.getGeoFromWiFi();
Serial.println("Location request data");
Serial.println(location.getSurroundingWiFiJson() + "\n");
Serial.println ("Location: " + String (loc.lat, 7) + "," + String (loc.lon, 7));
//Serial.println("Longitude: " + String(loc.lon, 7));
Serial.println ("Accuracy: " + String (loc.accuracy));
Serial.println ("Result: " + location.wlStatusStr (location.getStatus ()));
bot.sendMessage(CHAT_ID, "User Fall \nHelp required\nLocation: " + String (loc.lat, 7) + "," + String (loc.lon, 7), "");
fall = false;
}
if (trigger2count >= 6) { //allow 0.5s for orientation change
trigger2 = false; trigger2count = 0;
Serial.println("TRIGGER 2 DECACTIVATED");
}
if (trigger1count >= 6) { //allow 0.5s for AM to break upper threshold
trigger1 = false; trigger1count = 0;
Serial.println("TRIGGER 1 DECACTIVATED");
}
delay(100);
}
void mpu_read(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}