The use of the ESP8266 in the world of IoT

User avatar
By Ivica Matić
#67833 Hello guys and girls!
First time,first post :)

I am wondering is my code going to work.
I need to create small numbers of weather stations for school project and i need confirmation of my code so they can order all needed parts.The data should be uploaded to ThingSpeak every 60 minutes.

Code: Select all#include "ThingSpeak.h"
#include "ESP8266WiFi.h"

/*------wifi data ------*/
char ssid[] = "Ivica_wifI";
char wifipass[] = "Ivicawifi0968";

/*-------ThingSpeak data-------------*/
char api_write_key[] = "umetni kljuc ovdje";
long api_write_channel = 123456;

  WiFiClient ts_client;

void setup() {

   WiFi.mode(WIFI_STA);
   WiFi.begin(ssid, wifipass);

    while (WiFi.status() != WL_CONNECTED) {
    }

     ThingSpeak.begin(ts_client);
}

void loop() {
     int val1 = random(0,999);
     ThingSpeak.setField(0, val1);
     ThingSpeak.writeFields(api_write_channel, api_write_key);
     ESP.deepSleep(60 * 1000 * 60);
     
}
User avatar
By torntrousers
#67843 DeepSleep is in microseconds so
Code: Select allESP.deepSleep(60 * 1000 * 60);
should probably be
Code: Select allESP.deepSleep(60 * 60 * 1000000);


Also, this wait:

Code: Select allwhile (WiFi.status() != WL_CONNECTED) {
}


should have a delay or yield inside it to give background tasks a chance to run. Its also a good idea to have a timeout on the wait in case there is some problem with the Wifi connection and go back to sleep if it times out and try again later.
User avatar
By Ivica Matić
#67846
torntrousers wrote:DeepSleep is in microseconds so
Code: Select allESP.deepSleep(60 * 1000 * 60);
should probably be
Code: Select allESP.deepSleep(60 * 60 * 1000000);


Also, this wait:

Code: Select allwhile (WiFi.status() != WL_CONNECTED) {
}


should have a delay or yield inside it to give background tasks a chance to run. Its also a good idea to have a timeout on the wait in case there is some problem with the Wifi connection and go back to sleep if it times out and try again later.


Thank you for your response :)
Wow.So many errors in such simple code :lol: .Can you please explain to me what is yield() function all about.Does it provide priority to ESPs other functions before my code.Does it work like that?