// ------style guard ----

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

uint8_t temprature_sens_read();

#ifdef __cplusplus
}
#endif


// ------header files----
#include <WiFi.h>
#include <OneWire.h>
#include <ThingSpeak.h>
const int tempin=4;
#include <DallasTemperature.h>

// -----network credentials
const char* ssid = "Wokwi-GUEST";      // Replace with your network SSID (name)
const char* password = "";  // Replace with your network password

WiFiClient client;

// -----ThingSpeak channel details
unsigned long myChannelNumber = 1;
const char * myWriteAPIKey = "4CDAAAHGYZQ9YE6T";

// ----- Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 1000;

//-------------------------------
const int ONE_WIRE_BUS =4; // Example GPIO pin for OneWire communication

OneWire oneWire(ONE_WIRE_BUS); // Initialize the OneWire communication on the specified pin
DallasTemperature sensors(&oneWire);



void setup() {
  Serial.begin(115200); // Initialize serial
  WiFi.mode(WIFI_STA);
pinMode(tempin, INPUT);

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Attempting to connect");

    while (WiFi.status() != WL_CONNECTED) {
      WiFi.begin(ssid, password);
      delay(1000);
    }

    Serial.println("Connected.");

    
  }
sensors.begin();
  ThingSpeak.begin(client); // Initialize ThingSpeak
}

void loop() {

  if ((millis() - lastTime) > timerDelay) {
    int hall_value = 0;
    float temperature = 0;

    hall_value = hallRead();  // Assuming you have a function named hallRead()

    // Get a new temperature reading
  sensors.requestTemperatures();
  float temperatureC = sensors.getTempCByIndex(0); // Assuming you have a function named temprature_sens_read()

    Serial.print("Temperature (ºC): ");
    Serial.print(temperatureC);
    Serial.println("ºC");

    Serial.print("Hall value: ");
    Serial.println(hall_value);

    ThingSpeak.setField(1, temperatureC);
    ThingSpeak.setField(2, hall_value);

    // Write to ThingSpeak
    int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

    if (x == 200) {
      Serial.println("Channel update successful.");
    } else {
      Serial.println("Problem updating channel. HTTP error code " + String(x));
    }

    lastTime = millis();
  }
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
temp1:GND
temp1:DQ
temp1:VCC