I have recently converted a project to use the nodemcu, it all works apart from the serial monitor isn't showing anything when I Serial.println
I have tried different baud rates, and made no difference.
/*
* Created on: 17.11.2019
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
//ESP Web Server Library to host a web page
#include <ESP8266WebServer.h>
#define NUM_ZONES (2)
// @@@@@@@@@@@@@@@ You only need to midify modify wi-fi and domain info @@@@@@@@@@@@@@@@@@@@
const char* ssid = "BT-WCA2SX"; //enter your ssid/ wi-fi(case sensitive) router name - 2.4 Ghz only
const char* password = "Fp6VCPWKw6bV"; // enter ssid password (case sensitive)
//Static IP address configuration
IPAddress staticIP(192, 168, 1, 90); //ESP static ip
IPAddress gateway(192, 168, 1, 254); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(192, 168, 1, 254); //DNS
WiFiServer server(80);
//light switches
const int zone2[] = { 3,0 };//decking
const int zone3[] = { 2,0 }; //hanging
const int* zones[NUM_ZONES] = { zone2,zone3 };
int buttonState[NUM_ZONES] = { 0,0 };
int previousState[NUM_ZONES] = { 0,0 };
boolean LightLevelRequest = false;
int ldrReading;
//web control lights
const int Decking(16);//(16);
const int Hanging(16);//(11);
void setup()
{
Serial.begin(115200);
Serial.println("");
Serial.println("Pin setup");
//initialize the output pins that will control lights
pinMode(16, OUTPUT);
pinMode(16, OUTPUT);
// initialize the pushbutton pin as an input:
byte i;
//this loop sets all the 1-3pins as inputs
for (i = 1; i < 3; i++) {
pinMode(i, INPUT);
digitalWrite(i, HIGH); // this makes it connect to the internal resistor
}
Serial.println("Wifi set up");
WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
WiFi.hostname("GrdenLghtCtrl"); // DHCP Hostname (useful for finding device for static lease)
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Wi-fi connected");
digitalWrite(16, HIGH); //tmp
server.begin();
}
void loop()
{
//FOR TESTING OF LDR ONLY
//ldrReading = analogRead(0); //need to check pin
//Serial.println(ldrReading);
//delay(1000);
Serial.println("Wifi set up");
//
int arrayPosition;
for (int z = 0; z < NUM_ZONES; ++z)//zero based array needs to be as many as there are zones!!!
{
buttonState[z] = digitalRead(z); // original start posistion +30);
for (arrayPosition = 0; zones[z][arrayPosition]; arrayPosition++)
{
if ((buttonState[z] == HIGH) && (previousState[z] == LOW)) {
// turn LED on:
digitalWrite(zones[z][arrayPosition], HIGH);
}
else if ((buttonState[z] == LOW) && (previousState[z] == HIGH)) {
// turn LED off;
digitalWrite(zones[z][arrayPosition], LOW);
}
}
//each light assigned to the zone has been turned on or off, now set previousstate
previousState[z] = buttonState[z];
}
//HTTP section
//EthernetClient client = server.available();
WiFiClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
if (client.find("GET /")) {
while (client.findUntil("lightMode", "\n\r")) {
String type = client.readString();
String LightMood = type.substring(0, type.indexOf('='));
String stat = type.substring(type.indexOf('=') + 1, type.indexOf('=') + 2);
int TurnOn = stat.toInt();
// this is for test
Serial.println();
Serial.println(LightMood + " before function call");
//lightLevel means return value of LDR else call the function
if (LightMood == "LightLevelRequest")
{
Serial.println("FirstConditon");
LightLevelRequest = true;//test
}
else
{
Serial.println("SecondConditon");//tst
MoodLighting(LightMood, TurnOn);//calls MoodLighting
}
}
}
if (LightLevelRequest == false)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
}
else {
char str[80];
char strConverted[80];
strcpy(str, itoa(ldrReading, strConverted, 10));
strcat(str, " trying to return light level to the browser");
puts(str);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//this was all remmed out
String PostData = strConverted;
client.println("POST /test/index.php HTTP/1.1");
client.println("Host: artiswrong.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
Serial.println(str);
}
break;
}
}
// give the web browser time to receive the data
delay(3);
client.stop();
}
}
//function turns light on or off
void MoodLighting(String mLightMood, int mTurnOn)
{
if (mLightMood == "Decking")
{
digitalWrite(Decking, mTurnOn);
}
if (mLightMood == "Hanging")
{
digitalWrite(Hanging, mTurnOn);
}
//events
if (mLightMood == "WholeHouse")
{
digitalWrite(Decking, mTurnOn);
digitalWrite(Hanging, mTurnOn);
}
}