Toon posts:

Arduino code wijzigen - I2C -> 4 wire SPI

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Goedemiddag heren/dames,

probleempje. Ik was, met opvolging van instructies van het web, een cointracker-doosje in elkaar aan 't zetten.

Simpel ESP8266 wifi boardje en een schermpje, wat i2c had moeten zijn.
Ik ontving echter een 4-Wire SPI schermpje.

Nu komt er inderdaad niets op het display met de gebruikte code, ik ben bang dat ik het zal moeten aanpassen, maar hier heb ik dus echt helemaaaallll geen verstand van.

Nu is de vraag... wat dien ik te veranderen om dit aan de gang te krijgen?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
 /*******************************************************************
    A project to display crypto currency prices using an ESP8266

    Main Hardware:
    - NodeMCU Development Board (Any ESP8266 dev board will work)
    - OLED I2C Display (SH1106)

    Written by Brian Lough
    https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
 *******************************************************************/

// ----------------------------
// Standard Libraries - Already Installed if you have ESP8266 set up
// ----------------------------

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Wire.h>

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <CoinMarketCapApi.h>
// For Integrating with the CoinMarketCap.com API
// Available on the library manager (Search for "CoinMarket")
// https://github.com/witnessmenow/arduino-coinmarketcap-api

#include "SH1106.h"
// The driver for the OLED display
// Available on the library manager (Search for "oled ssd1306")
// https://github.com/squix78/esp8266-oled-ssd1306

#include <ArduinoJson.h>
// Required by the CoinMarketCapApi Library for parsing the response
// Available on the library manager (Search for "arduino json")
// https://github.com/squix78/esp8266-oled-ssd1306


// ----------------------------
// Configurations - Update these
// ----------------------------

char ssid[] = "WiFiNetworkName";       // your network SSID (name)
char password[] = "password";  // your network key

// Pins based on your wiring
#define SCL_PIN D5
#define SDA_PIN D3

// CoinMarketCap's limit is "no more than 10 per minute"
// Make sure to factor in if you are requesting more than one coin.
// We'll request a new value just before we change the screen so it's the most up to date
unsigned long screenChangeDelay = 10000; // Every 10 seconds

// Have tested up to 10, can probably do more
#define MAX_HOLDINGS 10

#define CURRENCY "eur" //See CoinMarketCap.com for currency options (usd, gbp etc)
#define CURRENCY_SYMBOL "E" // Euro doesn't seem to work, $ and £ do

// You also need to add your crypto currecnies in the setup function

// ----------------------------
// End of area you need to change
// ----------------------------


WiFiClientSecure client;
CoinMarketCapApi api(client);

SH1106 display(0x3c, SDA_PIN, SCL_PIN);

unsigned long screenChangeDue;

struct Holding {
  String tickerId;
  float amount;
  bool inUse;
  CMCTickerResponse lastResponse;
};

Holding holdings[MAX_HOLDINGS];

int currentIndex = -1;
String ipAddressString;

void addNewHolding(String tickerId, float amount = 0) {
  int index = getNextFreeHoldingIndex();
  if (index > -1) {
    holdings[index].tickerId = tickerId;
    holdings[index].amount = amount;
    holdings[index].inUse = true;
  }
}

void setup() {

  Serial.begin(115200);

  // ----------------------------
  // Holdings - Add your currencies here
  // ----------------------------
  // Go to the currencies coinmarketcap.com page
  // and take the tickerId from the URL (use bitcoin or ethereum as an example)
  
  addNewHolding("bitcoin");
  addNewHolding("dogecoin");
  addNewHolding("ethereum");

  // ----------------------------
  // Everything below can be thinkered with if you want but should work as is!
  // ----------------------------

  // Initialising the display
  display.init();
  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(ArialMT_Plain_16);
  display.drawString(64, 0, F("HODL Display"));
  display.setFont(ArialMT_Plain_10);
  display.drawString(64, 18, F("By Brian Lough"));
  display.display();
  

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
  ipAddressString = ip.toString();
}

int getNextFreeHoldingIndex() {
  for (int i = 0; i < MAX_HOLDINGS; i++) {
    if (!holdings[i].inUse) {
      return i;
    }
  }

  return -1;
}

int getNextIndex() {
  for (int i = currentIndex + 1; i < MAX_HOLDINGS; i++) {
    if (holdings[i].inUse) {
      return i;
    }
  }

  for (int j = 0; j <= currentIndex; j++) {
    if (holdings[j].inUse) {
      return j;
    }
  }

  return -1;
}

void displayHolding(int index) {

  CMCTickerResponse response = holdings[index].lastResponse;

  display.clear();

  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.setFont(ArialMT_Plain_16);
  display.drawString(64, 0, response.symbol);
  display.setFont(ArialMT_Plain_24);
  double price = response.price_currency;
  if (price == 0) {
    price = response.price_usd;
  }
  display.drawString(64, 20, formatCurrency(price));
  display.setFont(ArialMT_Plain_16);
//  display.setTextAlignment(TEXT_ALIGN_CENTER);
//  display.drawString(64, 48, " 1h:" + String(response.percent_change_1h) + "%");
  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.drawString(64, 48, "24h: " + String(response.percent_change_24h) + "%");

  display.display();
}

void displayMessage(String message){
  display.clear();
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawStringMaxWidth(0, 0, 128, message);
  display.display();
}

String formatCurrency(float price) {
  String formattedCurrency = CURRENCY_SYMBOL;
  int pointsAfterDecimal = 6;
  if (price > 100) {
    pointsAfterDecimal = 2;
  } else if (price > 1) {
    pointsAfterDecimal = 4;
  }
  formattedCurrency.concat(String(price, pointsAfterDecimal));
  return formattedCurrency;
}

bool loadDataForHolding(int index) {
  int nextIndex = getNextIndex();
  if (nextIndex > -1 ) {
    holdings[index].lastResponse = api.GetTickerInfo(holdings[index].tickerId, CURRENCY);
    return holdings[index].lastResponse.error == "";
  }

  return false;
}

void loop() {
  unsigned long timeNow = millis();
  if ((timeNow > screenChangeDue))  {
    currentIndex = getNextIndex();
    if (currentIndex > -1) {
      if (loadDataForHolding(currentIndex)) {
        displayHolding(currentIndex);
      } else {
        displayMessage(F("Error loading data."));
      }
    } else {
      displayMessage(F("No funds to display. Edit the setup to add them"));
    }
    screenChangeDue = timeNow + screenChangeDelay;
  }
}

Acties:
  • 0 Henk 'm!

  • EddoH
  • Registratie: Maart 2009
  • Niet online

EddoH

Backpfeifengesicht

https://github.com/squix78/esp8266-oled-ssd1306
Zie voorbeeld voor SPI.

Succes.

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Thanks, maar wetende dat ik 0 ervaring heb hiermee; zou je me nog iets verder kunnen helpen?

Als ik dus toevoeg:

code:
1
#include <SPI.h>


en dan de pins based on wiring verander naar:

code:
1
2
3
4
#define SCLK_PIN D5
#define MOSI_PIN D3
#define CS_PIN D1
#define D/C_PIN D2


Zou het dan moeten werken?

Excuses als mijn vragen super-noob overkomen, dat is enkel omdat ik dat ook ben _/-\o_

Ik doe mijn best om het zelf uit te vogelen, maar meezitten doet het nog niet echt :+

Acties:
  • 0 Henk 'm!

  • EddoH
  • Registratie: Maart 2009
  • Niet online

EddoH

Backpfeifengesicht

Verwijderd schreef op woensdag 24 januari 2018 @ 13:58:
[...]


Thanks, maar wetende dat ik 0 ervaring heb hiermee; zou je me nog iets verder kunnen helpen?

Als ik dus toevoeg:

code:
1
#include <SPI.h>


en dan de pins based on wiring verander naar:

code:
1
2
3
4
#define SCLK_PIN D5
#define MOSI_PIN D3
#define CS_PIN D1
#define D/C_PIN D2


Zou het dan moeten werken?

Excuses als mijn vragen super-noob overkomen, dat is enkel omdat ik dat ook ben _/-\o_

Ik doe mijn best om het zelf uit te vogelen, maar meezitten doet het nog niet echt :+
Niet helemaal. Je moet zorgen dat

code:
1
SH1106Spi display(RES, DC, CS);


Met de correcte pins wordt aangeroepen. De defines die jij doet kunnen daar onderdeel van zijn maar dan moet je die wel correct doorgeven aan de display constructor. Verder mag je het zelf uitzoeken, ik heb geen ervaring met dit platform ;)

Acties:
  • +1 Henk 'm!

Verwijderd

Topicstarter
EddoH schreef op woensdag 24 januari 2018 @ 14:07:
[...]


Niet helemaal. Je moet zorgen dat

code:
1
SH1106Spi display(RES, DC, CS);


Met de correcte pins wordt aangeroepen. De defines die jij doet kunnen daar onderdeel van zijn maar dan moet je die wel correct doorgeven aan de display constructor. Verder mag je het zelf uitzoeken, ik heb geen ervaring met dit platform ;)
Ik denk dat ik een eenvoudiger oplossing voor mijn probleem gevonden heb. Dat ik daar overheen gekeken heb...

Als ik hier BS1 omsoldeer naar 1 ipv 0, dan zou het schermpje op I2C moeten werken en komen we er ook

Afbeeldingslocatie: https://www.galerieallegro.pl/zdjecia/z887/8870936/big_800x600/3.jpg

_/-\o_

Bedankt voor je hulp, ik hoop dat ik er zo uitkom! De code veranderen is in ieder geval so-far geen gigantisch success 8)7