#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* ssid = "";
const char* password = "";
int red = 2;
int yellow = 1;
int green = 3;
int onred = 1;
int off = 0;
String host = "dweet.io";
WiFiClient client;
//Dweet dweet(client);
void connectToWiFi() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print("connect to wifi");
Serial.print("...");
WiFi.begin(ssid, password);
int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 15)) {
retries++;
delay(500);
Serial.print(".");
}
if (retries > 14) {
Serial.println(F("WiFi connection failed "));
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F("wifi conneted"));
Serial.println("IP adress");
Serial.println(WiFi.localIP());
}
Serial.println(F("setup"));
}
void setup() {
Serial.begin(115200);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
connectToWiFi();
}
void loop() {
Serial.println("");
Serial.println("-----");
Serial.println("data run #");
client.setTimeout(10000);
if (!client.connect(host, 80)) {
Serial.println(("connection failed"));
return;
}
client.println(F("GET /get/latest/dweet/for/thomastech HTTP/1.0"));
client.println(F("Host:dweet.io"));
client.println(F("connection closed"));
if (client.println() == 0) {
Serial.println(F("failed to send request"));
return;
}
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("invaild response"));
return;
}
StaticJsonDocument<395> doc;
DeserializationError error = deserializeJson(doc, client);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject with_0 = doc["with"][0];
const char* with_0_thing = with_0["thing"]; // "thomastech"
const char* with_0_created = with_0["created"]; // "2022-11-17T22:43:33.471Z"
int ledstatus = with_0["content"]["led status"]; // 1
Serial.print("ledstatus:");
Serial.println(ledstatus);
if (ledstatus = onred) {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
Serial.println("led is on ");
}
else if (ledstatus = off) {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
Serial.println(" leds are off ");
}
else {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
Serial.println(" leds are off ");
}
delay(5000);
}
= is for assignment
== is comparison
im not sure what to do because the pins on my board don't work as in when there are assigned there is no power to them
First, solve this problem :
if (ledstatus = onred) {
else if (ledstatus = off) {
i have done on my ide and its still not actiaving my pins
Post your updated code in a new reply.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* ssid = "VM2449C8";
const char* password = "scy4eukuHna7";
const int red = 2;
const int yellow = 1;
const int green = 3;
float onred = 1;
float off = 0;
String host = "dweet.io";
WiFiClient client;
//Dweet dweet(client);
void connectToWiFi() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print("connect to wifi");
Serial.print("...");
WiFi.begin(ssid, password);
int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 15)) {
retries++;
delay(500);
Serial.print(".");
}
if (retries > 14) {
Serial.println(F("WiFi connection failed "));
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F("wifi conneted"));
Serial.println("IP adress");
Serial.println(WiFi.localIP());
}
Serial.println(F("setup"));
}
void setup() {
Serial.begin(115200);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
connectToWiFi();
}
void loop() {
Serial.println("");
Serial.println("-----");
Serial.println("data run #");
client.setTimeout(10000);
if (!client.connect(host, 80)) {
Serial.println(("connection failed"));
return;
}
client.println(F("GET /get/latest/dweet/for/thomastech HTTP/1.0"));
client.println(F("Host:dweet.io"));
client.println(F("connection closed"));
if (client.println() == 0) {
Serial.println(F("failed to send request"));
return;
}
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("invaild response"));
return;
}
StaticJsonDocument<395> doc;
DeserializationError error = deserializeJson(doc, client);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject with_0 = doc["with"][0];
const char* with_0_thing = with_0["thing"]; // "thomastech"
const char* with_0_created = with_0["created"]; // "2022-11-17T22:43:33.471Z"
int ledstatus = with_0["content"]["led status"]; // 1
Serial.print("ledstatus:");
Serial.println(ledstatus);
if (ledstatus == onred) {
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
Serial.println("led is on ");
}
else if (ledstatus == off) {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
Serial.println(" leds are off ");
}
else {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
Serial.println(" leds are off ");
}
delay(10000);
}
Why is a 10 second delay necessary here?
wouldnt it just loop continuously
Indeed, that's why it's named as such.
You only want all of part of this to run once?
yeah ideally
All or part?
If part, which part of the loop code only runs once?
You can put all your run once code into your setup function.
You might have to add some print statements to debug the message that you are receiving.
i think it has to be a problem with my board becuase even when i run a simple code the esp8266 pins dont turn on
Show your simple code.
Also provide a wiring diagram.
int red = 3;
void setup() {
// put your setup code here, to run once:
pinMode(red, OUTPUT);
}
void loop() {
digitalWrite(red, HIGH);
}
just came up with an error
What error?
Traceback (most recent call last):
File "C:\Users\thoma\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2/tools/upload.py", line 66, in <module>
esptool.main(cmdline)
File "C:/Users/thoma/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool\esptool.py", line 3551, in main
esp = chip_class(each_port, initial_baud, args.trace)
File "C:/Users/thoma/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool\esptool.py", line 271, in __init__
self._port = serial.serial_for_url(port)
File "C:/Users/thoma/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/pyserial\serial\__init__.py", line 90, in serial_for_url
instance.open()
File "C:/Users/thoma/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/pyserial\serial\serialwin32.py", line 64, in open
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM7': PermissionError(13, 'Access is denied.', None, 5)
Failed uploading: uploading error: exit status 1
Are you sure that you've selected the correct port in the IDE?
Have you uploaded to this ESP8266 before?
With that same USB cable?
Which model of ESP8266 is it?
The led turns on if I put it to the vcc port but not the digital pins so the board works just digital ports. If that’s makes sense