I2C problem , i2c clock choice is invalid, please check flag and frequency
Samuel
on 26 Jun 2023
Latest activity Edit by Christopher Stapels
on 28 Jun 2023
#include <WiFi.h>
#include <Wire.h>
#include <WireSlave.h>
#include "ThingSpeak.h"
const char* ssid = ; // your network SSID (name)
const char* password = ; // your network password
WiFiClient client;
unsigned long myChannelNumber = ; // Channel ID:
const char* myWriteAPIKey = ; // API kluc
#define SDA_PIN 13
#define SCL_PIN 14
#define I2C_SLAVE_ADDR 0x04
int hodnota;
int MenoVahy;
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
void receiveEvent(int howMany);
void setup() {
Serial.begin(115200); //Initialize serial
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
bool success = WireSlave.begin(SDA_PIN, SCL_PIN, I2C_SLAVE_ADDR);
if (!success) {
Serial.println("I2C slave init failed");
while (1)
delay(30000);
}
WireSlave.onReceive(receiveEvent);
}
void loop()
{
if ((millis() - lastTime) > timerDelay) {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Pripájam sa k sieti...");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nPripojené.");
}
sendDataToThingSpeak();
lastTime = millis();
}
// the slave response time is directly related to how often
// this update() method is called, so avoid using long delays
// inside loop(), and be careful with time-consuming tasks
WireSlave.update();
if (MenoVahy == 2) {
Serial.println(hodnota); // print the integer
}
// let I2C and other ESP32 peripherals interrupts work
delay(1);
}
void receiveEvent(int howMany)
{
while (1 < WireSlave.available()) // loop through all but the last byte
{
MenoVahy = WireSlave.read(); // receive byte as a character
}
hodnota = WireSlave.read(); // receive byte as an integer
}
void sendDataToThingSpeak() {
// Nastavte polia s hodnotami
ThingSpeak.setField(1, 10);
ThingSpeak.setField(2, 20);
ThingSpeak.setField(3, 30);
ThingSpeak.setField(4, 40);
ThingSpeak.setField(5, 50);
ThingSpeak.setField(6, 60);
ThingSpeak.setField(7, 70);
ThingSpeak.setField(8, 80);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Úspešne aktualizovaný kanál.");
}
else {
Serial.println("Problém s aktualizáciou kanála. HTTP chybový kód: " + String(x));
}
}
1 Comment
Time DescendingIf the title is intended to be your question, I would say this is a hardware issue, definitely not anything with ThingSpeak. I would look on a forum for the hardware you are using.
If you have trouble with ThingSpeak after that, please feel free to post here again with your issue. Be sure to describe the problem (what you see, what you have tried) and the hardware when you post.
Sign in to participate