1

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);
  }
}
Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • What happens when you shorten the delay(5000) in your wifi connection setup to 500? – ocrdu Nov 16 '20 at 14:12
  • It only made it fail faster. it didn't even connect as usual (usually i could see it on the mobile hotspot via phone) but it never came. Tried it on 2 different boards (esp8266 NodeMCU v3) same result – Frederik A. Nov 17 '20 at 09:32
  • Hm. Will it connect in one go with a 10s delay? Does it work if you strip away all the code except the WiFi connecting bit plus a Serial.println() to see if the code continues? Just trying to isolate the problem. – ocrdu Nov 17 '20 at 10:47
  • 1
    No sadly, seems like it was my phone not giving it an IP adress. Haven't been able to mark is as solved as i have to wait 21 hours before i can accept my answer – Frederik A. Nov 17 '20 at 12:29

1 Answers1

0

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...

  • Could be; your telephone configured as a WiFi hotspot should give out addresses, though. – ocrdu Nov 17 '20 at 10:49