4

I am trying to send the temperatures from an DS18B20 to ThingSpeak, using an ESP8266. You can see the code I am using for that.

When I run the code I do not get any errors. In serial monitor it just shows that it is not connected to WiFi. If I change the WiFi name in code to something not even existing I still do not get any errors. I do not think that the ESP even finds the WiFi.

I think that the only problem to get everything work is just to get the WiFi connection to work properly. I have also checked the ESP with another simple code just to check that it is not broken and it actually get the code from my computer.

Here is the code I am using. (I have removed my password etc... )

//nodeMCU v1.0 (black) with Arduino IDE
//stream temperature data DS18B20 with 1wire on ESP8266 ESP12-E (nodeMCU v1.0)
//shin-ajaran.blogspot.com
//nodemcu pinout https://github.com/esp8266/Arduino/issues/584
#include <ESP8266WiFi.h>
#include <DallasTemperature.h>
#include <OneWire.h>

//Def
#define myPeriodic 15 //in sec | Thingspeak pub is 15sec
#define ONE_WIRE_BUS D4  // DS18B20 on arduino pin2 corresponds to D4 on physical board
//#define mySSR 0  // Solid State Relay on pin 0

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float prevTemp = 0;
const char* server = "api.thingspeak.com";
String apiKey ="******";
const char* MY_SSID = "******";
const char* MY_PWD = "******";
int sent = 0;

void setup() {
    Serial.begin(115200);
    connectWifi();
}


void loop() {
    float temp;
    //char buffer[10];
    //DS18B20.requestTemperatures();
    //temp = DS18B20.getTempCByIndex(0);
    temp=1111;
    //String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
    Serial.print(String(sent)+" Temperature: ");
    Serial.println(temp);

    //if (temp != prevTemp)
    //{
    //  sendTeperatureTS(temp);
    //  prevTemp = temp;
    //}

    sendTeperatureTS(temp);
    int count = myPeriodic;
    while(count--)
    delay(1000);
}

void connectWifi()
{
    Serial.print("Connecting to "+*MY_SSID);
    WiFi.begin(MY_SSID, MY_PWD);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("Connected");
    Serial.println("");
}//end connect

void sendTeperatureTS(float temp)
{
    WiFiClient client;

    if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
        Serial.println("WiFi Client connected ");

        String postStr = apiKey;
        postStr += "&field1=";
        postStr += String(temp);
        postStr += "\r\n\r\n";

        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.print(postStr);
        delay(1000);

    }//end if
    sent++;
    client.stop();
}//end send
5
  • 3
    it prints a dot every second in Serial Monitor? how is the esp8266 powered? can the power source 300 mA?
    – Juraj
    Commented Feb 1, 2018 at 19:46
  • 1
    You should upload the "WiFiScan.ino" example sketch to verify what WiFi stations (and with what signal strength) your ESP can see, if you think you cannot connect to your network. Commented Feb 1, 2018 at 20:17
  • yes it just shows the dot all the time. The ESP is right now connected to my computer by a micro USB-cable so no external power source if that is what you mean ? I just run the scaning and yes it finds the wifi witch I am trying to connect to.
    – user42391
    Commented Feb 2, 2018 at 8:39
  • 1
    For me i just changed the AP Band to 2.4Ghz from 5.0Ghz and it's working now Commented Sep 2, 2020 at 6:20
  • I have the same problem and I was struggled many days. The solution for my issue was because SSID is case sensitive and I was trying to type all small letter. Commented Dec 22, 2020 at 7:21

4 Answers 4

5

Maybe you have a space in your WIFI SSID so you should check this in your router. Also maybe your WIFI is on 5Ghz and the ESP can't see it because it is only for 2.4 Ghz. Also if your WIFI AP is on a channel from 13 - 14 it also couldn't be seen by the ESP. And to test your ESP you can run the scan example and if it works you should try the web server example to connect to your network. If that all will work ok than the problem is in your code.

4

I had the same problem, when i tried to connect to my home wifi i got stuck with just printing dots and never could connected. First of all i though that my ssid or password had a typo, but it hadn't. Then i changed the nodemcu and the problem persisted. But when i tried to connect to other wifi it worked. So the problem was in my modem/router.So i changed my modem/router configuration to: Operation mode: 802.11g+n to Automatic Channel: 11 to Automatic Channel Bandwich: 20 to Automatic

then the problem was solved.

1
  • This worked for me too, thanks for sharing. Commented Dec 20, 2021 at 16:12
2

In order to connect to Wifi from ESP32 and other boards, the WiFi connection needs to be 2.4GHz, wireless mode must be legacy. It cannot be set to Auto or N only. Go to your router box web page, and change this setting under Advanced Settings > Wireless.

The only code required to connect is.

void setup() {
  ...
  WiFi.begin(MY_SSID, MY_PWD);
}

void loop() {
  while (WiFi.status() != WL_CONNECTED) {
     delay(1000);
     Serial.print(".");
  }
  ...
}
1
  • In my experience a NodeMCU / ESP32 works perfectly fine with “N-only” Wifi networks.
    – StarCat
    Commented Aug 31, 2020 at 20:43
0

Try to add WiFi.hostname("ESP-host"); before WiFi.begin(ssid, password); - it works in my case.

Not the answer you're looking for? Browse other questions tagged or ask your own question.