6  First programs

If for some reason you are having any difficulties with your ESP32, you can simulate any code using this simulator:
https://wokwi.com/projects/new/esp32

6.2 Printing increasing numbers

Upload the following code to your ESP32, then click on the on the top right corner. This is called “Serial Monitor”.

Download code

6.3 Printing random numbers, then plotting!

  1. Upload the following code to your ESP32.
  2. Open the Serial Monitor (). Now close it, and
  3. Go to Tools > Serial Plotter.
Download code

6.4 Floats, functions, conditionals

We will now learn how to deal with floats, how to define our own functions, and how to write conditionals (if/else).

Download code
// initialize 4 floats (decimals)
// time and 3 more variables to be plotted
float t = 0, x1 = 0, x2 = 0, x3 = 0;

void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
}

/*
 * This is how you define a function.
 * In this case, the function returns a float (decimal)
 */
float my_sine(float time) {
  return sin(time);
}

float my_cosine(float time) {
  float double_time;              // you can define internal variables,
  double_time = 2.0 * time;       // write as many commands as you want
  return 2.0 * cos(double_time);  // in the end, return float
}

void loop() {
  x1 = my_sine(t);
  x2 = my_cosine(t);
  // now let's make a square wave
  if (x1 > 0) {
    x3 = -1.0;
  } else {
    x3 = 1.0;
  }
  Serial.print("sin:"); Serial.print(x1); Serial.print("\t");
  Serial.print("cos:"); Serial.print(x2); Serial.print("\t");
  Serial.print("square:"); Serial.print(x3);
  Serial.println();
  delay(50);        // 1000 ms delay
  t += 0.05;
}

6.5 WiFi, read time

Download code
//-----------------------------------------------------------------------------------
//  WIFI setup:

#include <WiFi.h>

const char* ssid = "HUJI-guest"; // your wifi SSID name
const char* password = "" ;// wifi pasword
String formattedTime;
float rand_int = 0, cumulative = 0;

WiFiClient client;

//-------------------------------------------------------------------------------------
// Set time
#include <NTPClient.h>
#include <WiFiUdp.h>

const long utcOffsetInSeconds = 3600 * 2; // For UTC +2.00 : 2 * 60 * 60

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

int time_s ; // variable for the second


void setup() {
  Serial.begin(9600);
  //---------------------------------------------------------------------------
  //wifi conection:
  WiFi.disconnect();
  delay(10);
  WiFi.begin(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("NodeMcu connected to wifi...");
  Serial.println(ssid);
  Serial.println();
  //-----------------------------------------------------------------------
  // stings for time:
  timeClient.begin();

}

void loop() {
  timeClient.update();
  time_s = timeClient.getSeconds();
  if (time_s == 0) {
    formattedTime = timeClient.getFormattedTime();
    Serial.print("A new minute has started!\nFull time = ");
    Serial.println(formattedTime);
  }
  
  Serial.print("second = ");
  Serial.print(time_s);
  rand_int = random(10, 20);
  cumulative += rand_int;
  Serial.print("\trand = ");
  Serial.print(rand_int);
  Serial.print("\trunning sum = ");
  Serial.println(cumulative);
  if (time_s%10==9) {
    Serial.print("Average  = ");
    Serial.println(cumulative / 10);
    cumulative = 0;
  }
  
  delay(1000);
}

6.6 Home assignment

Go to Assignment 1.