Search code examples
arduinoesp8266arduino-esp8266esp8266wifi

ESP8266 connects to wifi, but doesnt registrer it


I have a ESP8266 which can connect to the wifi just fine (can see it on the mobile hotspot) but it does not registrer it itself (as in it does not continue the code)

sometimes it can registrer it and sometime it doesn't.

I could not find any solutions for it.

some of the code is in danish (mostly just comments)

#include "ThingSpeak.h"
#include "navnkode.h"
#include <ESP8266WiFi.h>
//inkluderet biblioteker og ekstra filer for at få koden til at virke efter ønske

char ssid[] = SECRET_SSID;   
char pass[] = SECRET_PASS;   
WiFiClient client;
unsigned long kodekanal = SECRET_CODECHANNEL;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
const char * myReadAPIKey = SECRET_READ_APIKEY;
int kanal = 1; //her vælges den kanal inde på thingspeak som skal modtage data
int TO_SLAVE = 15;
int FROM_SLAVE = 13; 

void setup() {
  Serial.begin(115200);  // Initialize serial
  pinMode(TO_SLAVE,OUTPUT);
  WiFi.mode(WIFI_STA); 
  ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void loop() {
  // Connect or reconnect to WiFi
  if (WiFi.status() != WL_CONNECTED) {
    digitalWrite(TO_SLAVE,LOW);
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while (WiFi.status() != WL_CONNECTED) {
      WiFi.begin(ssid, pass);  
      Serial.print(".");
      delay(5000);     
      digitalWrite(TO_SLAVE,LOW);
    } 

    digitalWrite(TO_SLAVE,HIGH);
    Serial.println("\nConnected.");
  }

  int x = ThingSpeak.writeField(myChannelNumber, kanal, 23, myWriteAPIKey); 
  if (x == 200) {
    Serial.println("kanal 1 blev opdateret uden problemer.");
  } else {
    Serial.println("Problem med at opdatere. HTTP fejl kode " + String(x));
  }

  delay(5000); // Venter 20 sekunder før den opdatere kanalen (SKAL FJERNES!)
}

void hotspot() {
  if (ThingSpeak.readIntField(myChannelNumber, kanal, myReadAPIKey) >= 1) {
    digitalWrite(TO_SLAVE,HIGH);
  } else {
    digitalWrite(TO_SLAVE,LOW);
  }
}

Solution

  • The most likely fix was to change the hotspot from my phone to a pc.

    My phone most likely did not give it an IP adress...